/* 
 * wayfinder.js 
 * version: 1.0
 * function: shows tabbed country selection, 
 * if javascript is enabled in users browser
 * 
 * v 1.1:
 * added cookie functionality
 * 
 * v 1.2:
 * changed the js funtionality to support 
 * Content Management system
 *
 */

/* INIT ----------------------------------------------------- */

	/* let's add some dynamics, if javascript is enabled */
	document.getElementById("wf_container").className = "wf_containerWithJS";


/* get elements by tag name --------------------------------- */

document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
}; 


/* show & hide tab bgs -------------------------------------- */ 

function showCountries(targetElement){
    target = targetElement.id;
    var tabs = document.getElementsByClassName('wf_tabi');
    for (i=0;i<tabs.length;i++){
        /* first we reset all, then we show the target tab and country-div */
        document.getElementById(tabs[i].id).style.background = 'none';
        document.getElementById(tabs[i].id).firstChild.style.background = 'none';
        document.getElementById(tabs[i].id).firstChild.style.color = "#FFFFFF";
        document.getElementById(tabs[i].id+"Tbl").style.display = 'none';
        if (tabs[i].id == target){
            document.getElementById(tabs[i].id).style.background = "#FFF url('/NOKIA_COM_1/Microsites/wayfinder/images/tab_1.gif') top left no-repeat";
            document.getElementById(tabs[i].id).firstChild.style.background = "url('/NOKIA_COM_1/Microsites/wayfinder/images/tab_2.gif') top right no-repeat";
            document.getElementById(tabs[i].id).firstChild.style.color = "#115522";
            document.getElementById(tabs[i].id+"Tbl").style.display = 'block';
        }
    }
}


/* picture carousel ----------------------------------------- */

function DrawPictureCarousel(){
	document.getElementById("wf_main_graphics").style.background = "url('/NOKIA_COM_1/Microsites/wayfinder/images/carousel/image_" + Math.floor(Math.random()*11+1) + ".jpg')";
}


/* redirect by using cookies -------------------------------- */

function cookieReDirect(){
	if (readCookie("WayfinderCheck") == "true" && readCookie("WayfinderUrl") != null && readCookie("WayfinderName") != null ){
		location.href= "/A41460102"; // cookie-less country list page
	}
}

function cookieReDirectBack(){
	if (readCookie("WayfinderUrl") == null || readCookie("WayfinderName") == null ){
		location.href= "/A4138120";  // wayfinder landing page
	}
}

/**
 * @param store boolean, set when the online store is accessed
 */
function ApplyCookie(target, store){
	createCookie("WayfinderCheck", document.getElementById("rememb").checked, 365);
	createCookie("WayfinderUrl", target, 365);
	var countryName = target.innerHTML;
  if(store) countryName += ' Online Store';
	createCookie("WayfinderName", countryName, 365);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	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) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

