var current_asset = "";
var current_assetIndex = 0;
var feebackUrl = "http://www.immi.gov.au/contacts/forms/ibis"

function viewItem(asset) {
	current_asset = asset;
	control_prevnext();
	if ($("viewitem_main").style.display == "none") {
		$("viewitem_main").style.display = "block";
		current_assetIndex = 0;
	}
    new Ajax.Request(asset,{method:'get',  onComplete:function(transport) { loadItem(transport.responseText); }});
}

function control_prevnext() {
	$("prevnext_buttons").style.display = "block";

    // if home, do not display previous / next links
	if (document.location.href.indexOf("/home") != -1) {
		$("prevnext_buttons").style.display = "none";
	}
}

function loadItem(text_content) {
	$("viewitem_content").innerHTML = text_content;
	processLinks();
	updatePrevNext_buttons();
}

function updatePrevNext_buttons() {
	if (current_assetIndex == 0) {
		for (var i=1; i<itemsArray.length; i++) {
			if (itemsArray[i] == current_asset) {
				current_assetIndex = i;
				break;
			}
		}
	}
	
	if (current_assetIndex==1) {
		$("previousButton").className = "inactive_prevnext";
	}
	else {
		$("previousButton").className = "active_prevnext";
	}
	if (current_assetIndex==itemsArray.length-1) {
		$("nextButton").className = "inactive_prevnext";
	}
	else {
		$("nextButton").className = "active_prevnext";
	}

}

function closeItem() {
	$("viewitem_main").style.display = "none";
	$("asset_container").innerHTML = "";
}

function previousItem() {
	if (current_assetIndex > 1) {
		current_assetIndex = current_assetIndex-1;
		viewItem(itemsArray[current_assetIndex]);
	}
}

function nextItem() {
	if (current_assetIndex < itemsArray.length-1) {
		current_assetIndex = current_assetIndex+1;
		viewItem(itemsArray[current_assetIndex]);
	}
}

function showFeedback() {
	var p = window.open(feebackUrl,"popup","height=600, width=800, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, toolbar=no, left=15, top=15");
	p.focus();
	
}

function popup(url) {
	if (screen.height > 600) {
		var height = screen.height * 0.8;
	}
	else {
		var height = 580;
	}
	if (screen.width > 800) {
		var width = screen.width * 0.8;
	}
	else {
		var width = 780;
	}
	var p = window.open(url,"popup","height=" + height + ", width=" + width + ", location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes, left=15, top=15");
	p.focus();
}
function linkAlert(URL){
	var newline = '\n';
	var sMessage = "You are about to leave the Immigration Borders Information Service website.";
	sMessage = sMessage + newline;
	sMessage = sMessage + "If this is your intention please click: "
	sMessage = sMessage + newline;
	sMessage = sMessage + "- OK to proceed or"
	sMessage = sMessage + newline;
	sMessage = sMessage + "- CANCEL to return to the current page"; 
	
	if (confirm(sMessage)) {
		var w = window.open(URL)
		//window.location = URL;	
		return false;
	}else{
		return false;
	}
	
}
function logout() {
	if (BrowserDetect.browser == "Firefox") {
		alert("Please close your web browser to logout of the Immigration Borders Information Service website.");
	}
	else {
		window.close();	
	}
}
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
    	window.onload = function() {
    	oldonload();
   	 	func();
    	}
	}
}
//addLoadEvent(processLinks);
function processLinks() {
	var as = $A(document.getElementById("viewitem_container").document.getElementsByTagName("a"));
	as.each(function(i) {
		if (i.getAttribute("href")) {
			if ((i.getAttribute("href") != "#" && i.getAttribute("href").indexOf(domain) == -1) || (i.getAttribute("href").indexOf("/site/links/") != -1)) {
				var link = i;
				attachLinkEvent(link);
			}
		}
	});
}
function attachLinkEvent(link) {
		var url = link.getAttribute("href");
		link.onclick = function() {
		popup(url);
		return false;
	}
}
function popup(url) {
	window.open(url,"popup");
}
function linkAlert(URL){
	var newline = '\n';
	var sMessage = "You are about to leave the Immigration Borders Information Service website.";
	sMessage = sMessage + newline;
	sMessage = sMessage + "If this is your intention please click: "
	sMessage = sMessage + newline;
	sMessage = sMessage + "- OK to proceed or"
	sMessage = sMessage + newline;
	sMessage = sMessage + "- CANCEL to return to the current page"; 
	if (confirm(sMessage)) {
		window.open(URL);	
		return false;
	}
	else{
		return false;
	}
	
}
function setCurrentLink(elem){
  var a = document.getElementById("left").getElementsByTagName("a");
  for (var i=0; i<a.length; i++){
    a[i].removeAttribute("class");
  }
  elem.setAttribute("class","current");
}
function filterNewsTable(id){ 
  var tr = document.getElementById("news_table").getElementsByTagName("tr");
  for (var i=0;i < tr.length; i++) {
    var t = tr[i];
    if (id == "all-news"){
       t.style.display = "";
    }
    else{
    
      if (tr[i].getAttribute("class")){      
       // alert(tr[i].getAttribute("class") + " " + id);
        if (tr[i].getAttribute("class").indexOf(id) != -1) {       
          t.style.display = "";
        } 
        else {
          t.style.display = "none";
        }
      }   
    }
  }
  return false;
}
