var imageArray = new Array();
var activeTabs = new Array();
var currentLocation = new String(window.location);
var arrExceptions = new Array();

var strFullSites = "de-DE;es-ES;fr-FR;it-IT;en-US;";  
var homeSite = "http://n-gage.com";
var arrLangChangeMsg = new Array();
arrLangChangeMsg["es"] = "En este momento...";

function ShowAlert(lang, address){
	alert(arrLangChangeMsg[lang]);
	window.location.href = address;	
}

function hideUpcoming() {
	var arrHide = new Array("frontpage_games_tabs_tab_titles_cross_0_1",
		"frontpage_games_tabs_tab_1_title",
		"frontpage_games_tabs_tab_1_content");
	if (currentLocation.indexOf("/de-DE/index.html")==-1) {
		arrHide.push("frontpage_info_tabs_tab_titles_cross_0_1");
		arrHide.push("frontpage_info_tabs_tab_1_title");
	}
	var o = null;
	for (x in arrHide) {
		o = document.getElementById(arrHide[x]);
		if (o!=null)
			o.style.display = "none";
//			o.parentNode.removeChild(o);
	}
	o = document.getElementById("frontpage_games_tabs_tab_0_title");
	if (o!=null) o.removeAttribute("href");
	o = document.getElementById("frontpage_games_tabs_tab_titles_end");
	if (o!=null) o.className = "tab_titles_end_on";
	o = document.getElementById("frontpage_info_tabs_tab_titles");
	if (o!=null) o.style.display = "block";
//	o = document.getElementById("frontpage_games_tabs_tab_titles");
//	if (o!=null) o.style.display = "block";
}

//if (currentLocation.indexOf("/en-R1/home") > -1)
//    checkDefaultCountry();

function checkDefaultCountry() {
	var cookieCountry = readCookie("NGAGE_COUNTRY");
	var cookieDomain = readCookie("NGAGE_DOMAIN");
	var browse = readCookie("NGAGE_BROWSE");
	var browsing = false;
		
	if (browse != null && currentLocation.indexOf("/index.html" + browse + "/index.html") > -1)
		browsing = true;

	if(cookieCountry != null && cookieCountry == "/en-R1/home/home.html"){
		createCookie('NGAGE_COUNTRY',"",-1);
		createCookie('NGAGE_COUNTRY',"",100000);
	}
	if(cookieCountry != null && cookieDomain != null && cookieDomain != "full" && cookieCountry != "" && cookieCountry != "/en-R1/home/home.html") {
		createCookie('NGAGE_COUNTRY', cookieCountry, 100000);
		createCookie('NGAGE_DOMAIN',"full", 100000);
	}

	if(!browsing && cookieCountry != null && cookieCountry != "" && cookieCountry != "/en-R1/home/home.html")
		window.location = cookieCountry;
}

function getFirstLevelUrl(channelName, id, state) {
	if (state == 'Published')
	{
		var countryUrl = readCookie("NGAGE_COUNTRY");
		var browse = readCookie("NGAGE_BROWSE");
		var browsing = false;
		var ctryUrlTst = (document.cookie.indexOf("NGAGE_COUNTRY") > -1);
		
		if (browse != null && currentLocation.indexOf("/index.html" + browse + "/index.html") > -1) {
			browsing = true;
		}
		if (countryUrl == null && ctryUrlTst) countryUrl="/en-R1";// set default country if none selected 
		if(!browsing && countryUrl != null && countryUrl.length > 4 && /^\/\w\w\-\w\w/.test(countryUrl))
		{
			var country = countryUrl.substr(1,5);//get country code from cookie
			if (strFullSites.indexOf(country) == -1) return false; //check code in list of countries
			var combined = country + "/index.html" + channelName;
			if (document.getElementById(id) != null)
			{
				if(arrExceptions[combined] != null) {
					if (currentLocation.indexOf("http://index.html") > -1 || currentLocation.indexOf("https://index.html") > -1)
						document.getElementById(id).href = arrExceptions[combined];
					else
						document.getElementById(id).href = homeSite + arrExceptions[combined];
				} else {
					document.getElementById(id).href = homeSite + combined + "/index.html";
				}
			}
		}
	}
}

function init() {
	// arena login links	
	var arena_id = readCookie("NGAGE_EXT");
	// sdk document.getElementById('arena_register_link').innerHTML = arena_register_str;
	// sdk document.getElementById('arena_login_link').innerHTML = arena_logout_str;
	
	createCookie();
	fixFooter();
	
	// set control
	controlObj = document.getElementById('control');
	adjustObj = document.getElementById('consoleAdjust');
	
	if(controlObj) {
		if(controlObj.innerHTML.length>200) {
			if (adjustObj)
				controlObj.style.left=adjustObj.innerText;
			else
				controlObj.style.left='800px';
		}
	}

	// set controlerror
	controlObj = document.getElementById('controlerror');
	
	if(controlObj) {
		if(controlObj.innerHTML.length>200) {
			if (adjustObj)
				controlObj.style.left=adjustObj.innerText;
			else
				controlObj.style.left='800px';
		}
	}
	if (/\/\w\w\-\w\w\/home\//.test(currentLocation)) hideUpcoming();
	//if there is a country selected, with the current location then hideUpcoming links
}

function setFooter() {
	// redundant, functionality moved to fixfooter()
}

function fixFooter() {
	var totalHeight = getTotalHeight();

	document.getElementById('main').style.height = (totalHeight+60)+'px';
	
	document.getElementById('footer').style.top = (totalHeight+60)+'px';
	document.getElementById('footer').style.visibility = 'visible';
}

function getNewSrc(imgObj) {
	return (imgObj.src.indexOf('_over.')!=-1)?imgObj.src.replace('_over.','_off.'):imgObj.src.replace('_off.','_over.');
}

function getNewName(src) {
	return src.substr(src.lastIndexOf('/')+1, src.length).replace('.jpg','').replace('.gif','');
}

function imageToggle(obj) {
	for( a=0; a<obj.childNodes.length; a++) {
		if(obj.childNodes[a].src) {
			obj.childNodes[a].src = getNewSrc(obj.childNodes[a]);	
		}
	}
}

function getLeft(obj) {
    x = obj.offsetLeft;
    temp_obj = obj.offsetParent;
    while (temp_obj!=null) {
        x += temp_obj.offsetLeft;
        temp_obj = temp_obj.offsetParent;
    }
    return x;
}

function getTop(obj) {
    y = obj.offsetTop;
    temp_obj = obj.offsetParent;
    while (temp_obj!=null) {
        y += temp_obj.offsetTop;
        temp_obj = temp_obj.offsetParent;
    }
    return y;
}

function getTotalHeight() {
	var objs = document.getElementsByTagName('div');
	var height = 0;
	
	for(a=0; a<objs.length; a++) {
		//if((objs[a].className.indexOf('tab') == -1 && objs[a].className.indexOf('news') == -1) || objs[a].className == 'tabs_box') {	

		if(objs[a].id) {
			objT = getTop(objs[a]);
			objH = objs[a].offsetHeight;
			objTotal = objT+objH;
		
			if(objTotal > height) {
				// onepager & frontpage fix, dont adjust footer for games listings
				if (!(objs[a].id.indexOf('frontpage_games_tabs_') > -1))
				{
					height = objTotal;
				}
			}
		}
	}

	return height;
}

function tabs_set(id, status) {
	var status_array = status.split(',');
	var active = '';

	for( a in status_array ) {
		var target_title = eval('document.getElementById("'+id+'_tabs_tab_'+a+'_title");');
		var target_content = eval('document.getElementById("'+id+'_tabs_tab_'+a+'_content");');
		
		if(status_array[a]=='on') {
			target_title.className = 'tab_title_on';
			target_content.className = 'tab_content_on';
		}
		else {
			target_title.className = 'tab_title_off';
			target_content.className = 'tab_content_off';
		}

		// set active
		if(status_array[a]=='on') {
			active = a;
		}

	}
	
	// clear cross sections
	active_old = activeTabs[id];
	
	if(active_old>0) {
		var target_cross_prev_id = id+'_tabs_tab_titles_cross_'+(Number(active_old)-1)+'_'+active_old;
		var target_cross_prev = document.getElementById(target_cross_prev_id);
		
		target_cross_prev.className = 'tab_titles_cross_off_off';
	}

	if(active_old && active_old!=(status_array.length-1)) {
		var target_cross_next_id = id+'_tabs_tab_titles_cross_'+active_old+'_'+(Number(active_old)+1);
		var target_cross_next = document.getElementById(target_cross_next_id);
		
		target_cross_next.className = 'tab_titles_cross_off_off';
	}

	// activate cross sections
	if(active>0) {
		var target_cross_prev_id = id+'_tabs_tab_titles_cross_'+(Number(active)-1)+'_'+active;
		var target_cross_prev = document.getElementById(target_cross_prev_id);
		
		target_cross_prev.className = 'tab_titles_cross_off_on';
	}

	if(active!=(status_array.length-1)) {
		var target_cross_next_id = id+'_tabs_tab_titles_cross_'+active+'_'+(Number(active)+1);
		var target_cross_next = document.getElementById(target_cross_next_id);
		
		target_cross_next.className = 'tab_titles_cross_on_off';
		
		var target_end_id = id+'_tabs_tab_titles_end';
		var target_end = document.getElementById(target_end_id);
		
		target_end.className = 'tab_titles_end_off';
	}
	else if(active) {
		var target_end_id = id+'_tabs_tab_titles_end';
		var target_end = document.getElementById(target_end_id);
		
		target_end.className = 'tab_titles_end_on';
	}	
	
	activeTabs[id] = active;
	
	// save active tab status to cookie
	if(getCookie('ngage_tabs_'+id)) {
		eraseCookie('ngage_tabs_'+id);
	}
	
	setCookie('ngage_tabs_'+id,status,356);
}
	
//Added for now to enable image swapping/JRE 12/02/2004
//NOTE: Will be replaced
function swapImage(imgName,newImg) { 
	eval('document.' + imgName + '.src = "' + newImg + '"'); 
}

//Popup scripts
function popupWindow(theURL, winName, features) {
	window.open(theURL, winName, features);
	return false;
}

function defaultPopupWindow(theURL, theWidth, theHeight) {
	return popupWindow(theURL, "NGagePop", "width=" + theWidth + ",height=" + theHeight + ",scrollbars=no,top=50,left=50");
}

function setCookie(name,value,days) {
	var host = "";
	var expires = null;
	var domain = null;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*60*1000)); //*24*60*60*1000
		expires = "; expires="+date.toGMTString();
	}
	else {
		expires = "";
		domain = "";
	}
	
	document.cookie = name+"="+value+expires+";";
}

function getCookie(name) {
	var mod = "";
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0; i<ca.length; i++) {
		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) {
				mod = c.substring(nameEQ.length,c.length);
			}
		}

	if (mod == "") {
		return null;
	}
	else {
		return mod.substring(mod.indexOf("=")+1);
	}
}
