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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function resizeLandingPage() {
	var contentPanel = document.getElementById('content');
	var x = contentPanel.offsetHeight;
	
	var mainPanel = document.getElementById('panelLandingMain');
	var y = mainPanel.offsetHeight;

	var leftPanel = document.getElementById('panelLandingLeft');
	var z = leftPanel.offsetHeight;

	var mainHeight = y - z - 22;
	var leftHeight = z - y - 22;

	if (mainHeight > leftHeight)
		document.getElementById('subNav').style.height = document.getElementById('subNav').offsetHeight + mainHeight - 77 + 'px';
	else {
		document.getElementById('panelLandingMain').style.height = document.getElementById('subNav').offsetHeight + 21 + 'px';
	}
	if (document.getElementById('totheTopDiv')) {
		document.getElementById('totheTopDiv').style.display = "inline";
	}
}

function writeFlash(src, id, width, height, alternateSrc, redirect, version, additionalParameters) {
	if ((flashinstalled == 2) && (flashversion >= version)) {
		var additionalParameterString = '';
		if (additionalParameters) {
			for (var i in additionalParameters) {
				additionalParameterString += '<param name="'+i+'" value="'+additionalParameters[i]+'" />';
			}
		}
		document.write('<div class="awards">');
		document.write('<object class="" style="width: ' + width + '; height: ' + height + '" id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0">');
		document.write('<param name="movie" value="' + src + '" />');
		document.write('<param name="allowScriptAccess" value="sameDomain" />');
		document.write('<param name="quality" value="high" />');
		document.write('<param name="wmode" value="transparent" />');
		document.write(additionalParameterString);
		document.write('<embed id="' + id + '_embed" wmode="transparent" swliveconnect="true" style="width: ' + width + '; height: ' + height + '" src="' + src + '" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>');
		document.write('</object>');
		document.write('</div>');
		return true;
	} else {
	 	if (redirect)
			document.write('<a href="http://www.macromedia.com/go/getflashplayer" target="_blank">');
		if (alternateSrc != "")
			document.write('<img src="' + alternateSrc + '" width="' + width + '" height="' + height + '" id="nav" name="nav" alt="" border="0" usemap="#' + id + '_map">');
	 	if (redirect)
			document.write('</a>');
		return false;
	}
}

function popupFlashPlayer(src, name) {
	var left = (screen.width / 2) - 580 / 2;
	var top = (screen.height / 2) - 540 / 2;
	var newPopup = window.open("/flash/flvPlayer.htm?flvSrc=" + src + "&autoPlay=false&skin=green_player3.swf", name, "width=580, height=570, left=" + left + ", top=" + top + ", resizable=no");
	newPopup.focus();
}

var gCountry;
var gCategory;
var gCompany;

function loadXMLDoc(dname) {
	try //Internet Explorer
	  {
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  }
	catch(e)
	  {
	  try //Firefox, Mozilla, Opera, etc.
	    {
	    xmlDoc=document.implementation.createDocument("","",null);
	    }
	  catch(e) {alert(e.message)}
	  }
	try 
	  {
	  xmlDoc.async=false;
	  xmlDoc.load(dname);
	  return(xmlDoc);
	  }
	catch(e) {alert(e.message)}
	return(null);
}

function clearCountry() {
	for (i=0; i < 4; i++) {
		id = 'country' + i;
		document.getElementById(id).style.color = "#323232";
		document.getElementById(id).style.background = "#FFFFFF";
	}
}

function clearCategory(text) {
	for (i=0; i < 4; i++) {
		id = 'category' + i;
		if (text)
			document.getElementById(id).innerHTML = "";
		document.getElementById(id).style.color = "#323232";
		document.getElementById(id).style.background = "#FFFFFF";
		id = 'company' + i;
		document.getElementById(id).innerHTML = "";
		document.getElementById(id).style.color = "#323232";
		document.getElementById(id).style.background = "#FFFFFF";	
	}
}

function clearCompany() {
	for (i=0; i < 4; i++) {
		id = 'company' + i;
		document.getElementById(id).style.color = "#323232";
		document.getElementById(id).style.background = "#FFFFFF";
	}
}

function clearLogo() {
	for (i=0; i < 4; i++) {
		id = 'logo' + i;
		document.getElementById(id).innerHTML = "";
		document.getElementById(id).style.color = "#323232";
		document.getElementById(id).style.background = "#FFFFFF";	
	}
	document.getElementById("companyDescription").innerHTML = "Use table above to get specific information about each brand found in that country.<br />";		
}

function loadCountries(countries) {
	clearCountry();
	clearCategory(1);
	clearLogo();
	for (i=0; i < countries.length; i++) {
		id = 'country' + i;
		document.getElementById(id).innerHTML = '<b>' + countries[i] + '</b>';
	}
}

function loadCountry(category) {
	clearCountry();
	clearCategory(1);
	clearLogo();
	for (i=0; i < category.length; i++) {
		id = 'category' + i;
		document.getElementById(id).innerHTML = category[i];
	}
	document.getElementById("country" + gCountry).style.color = "#FFFFFF";
	document.getElementById("country" + gCountry).style.background = "#8CBD51";
}

function loadCategory(companies) {
	if (document.getElementById("category" + gCategory).innerHTML != "") {
		clearCategory(0);
		clearLogo();		
		for (i=0; i < companies.length; i++) {
			id = companies[i];
			document.getElementById("company" + i).innerHTML = id[0];
			document.getElementById("logo" + i).innerHTML = '<img src="' + id[1] + '" alt="" border="0" />';			
		}
		document.getElementById("country" + gCountry).style.color = "#FFFFFF";
		document.getElementById("country" + gCountry).style.background = "#9AC566";
		document.getElementById("category" + gCategory).style.color = "#FFFFFF";
		document.getElementById("category" + gCategory).style.background = "#8CBD51";
	}
}

function loadCompany(company) {
	if (document.getElementById("company" + gCompany).innerHTML != "") {
		clearCompany();
		id = company[2];
		document.getElementById("companyDescription").innerHTML = id;		
		document.getElementById("category" + gCategory).style.color = "#FFFFFF";
		document.getElementById("category" + gCategory).style.background = "#9AC566";
		document.getElementById("company" + gCompany).style.color = "#FFFFFF";
		document.getElementById("company" + gCompany).style.background = "#8CBD51";
	}
}

function alignButtons() {
	var mainTable = document.getElementById('groupCompanies');
	if (mainTable) {
		for (var x = 0; x < mainTable.tBodies[0].rows.length; x++) {
			var rowHeight = mainTable.rows[x].offsetHeight;
//			alert(rowHeight);
			for (var y = 0; y < mainTable.rows[x].cells.length; y++) {
				//mainTable.rows[x].cells[y].style.height = rowHeight + "px";
				var box = mainTable.rows[x].cells[y].getElementsByTagName("table");
//				alert(box[0].offsetHeight);
				if (box[0].className == "company")
					if (BrowserDetect.browser == "Explorer")
						box[0].style.height = (rowHeight - 200) + "px";
					else
						box[0].style.height = (rowHeight - 5) + "px";
			}
		}
	}
}