﻿/*
* lt-tnt-profile-selected-device.js
* 
* Used variables
* mboxName:      mbox name
* mboxParamName: mbox parameter name for device data
* mboxSite:      mbox site parameter
* device:        selected device
*/
// Create cookie for user details
function tntCreateCookie(name, value, days) {
	var date = new Date();
	var expires = "";
	var domain = document.location.href;
	// Define expire date for cookie 
	if(days) { 
		date.setTime(date.getTime()+(days*24*60*60*1000)); 
		expires = date.toGMTString();
	}
	
	// Create cookie (nokia.com specific if possible)
	if(domain.match(/nokia.com/g)) {
		document.cookie = name+"="+value+"; expires="+expires+"; path=/; domain=.nokia.com;";
	}
	else {
		document.cookie = name+"="+value+"; expires="+expires+"; path=/;";
	}
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// Update selected device
function mboxSelectDevice(device) {
	// Required variables for saving device data
	// var mboxParamName2 = "latest_phone";  // global profile parameter
	var requiredVars = new Array(typeof(mboxName), typeof(mboxParamName),/* typeof(mboxParamName2),*/ typeof(mboxSite), typeof(device));
	var reguired = true;
	
	// Check that all variables are set
	for(var i=0; i < requiredVars.length; i++) {
		if(requiredVars[i].toString() == "undefined") {
			reguired = false;
		}
	}
	
	if(reguired == true) {
		// Clean device name (remove + signs)
		device = device.replace(/\+/g, " ");
		// Update user data
		mboxUpdate(mboxName, "profile."+mboxParamName+"="+device,/* "profile."+mboxParamName2+"="+device,*/ mboxSite); //3rd party cookie
		// Create cookie
		tntCreateCookie(mboxParamName, device, 365);
		// tntCreateCookie(mboxParamName2, device, 365); //1st party cookie
	}
}

// Update dynamic mbox
	function mboxDefineUpdate(device){
		mboxDefine("dynamic_mbox_lt_crm_selected_phone", "LT/lt_crm_element", "profile."+mboxParamName+"="+device);
		mboxUpdate("LT/lt_crm_element", "profile."+mboxParamName+"="+device);	
	};
	
// Update accSelectedPhone cookie for accessories link to work
	function updateAccCookie(device){	
	var accDeviceId;
		switch (device) {
						case 'Nokia 5230': accDeviceId = 21786;
			break;
			
			case 'Nokia 5800XM': accDeviceId = 18935;
			break;
			
			case 'Nokia 2730': accDeviceId = 21088;
			break;
			
			case 'Nokia 3720': accDeviceId = 21613;
			break;
			
			case 'Nokia C5-00': accDeviceId = 23026;
			break;
			
			case 'Nokia C6-00': accDeviceId = 23166;
			break;
						
			case 'Nokia C7': accDeviceId = 24199;
			break;
			
			case 'Nokia E52': accDeviceId = 20954;
			break;
			
			case 'Nokia E7': accDeviceId = 24201;
			break;
			
			case 'Nokia E71': accDeviceId = 17942;
			break;
			
			case 'Nokia E72': accDeviceId = 21408;
			break;
			
			case 'Nokia N8': accDeviceId = 23285;
			break;
			
			case 'Nokia N97': accDeviceId = 20915;
			break;
			
			case 'Nokia X3-02': accDeviceId = 23977;
			break;						
			
			case 'Nokia X6': accDeviceId = 23752;
			break;
		}
		setCookie('accSelectedPhone', accDeviceId, 365);
}

// Update dynamic mboxSelection of device from dropdown
	function selectDevice(device){
		$("#deviceDDLinksSelection").slideToggle(400);
		$("#deviceDDContainer").toggleClass("close");
		mboxSelectDevice(device);
		mboxDefineUpdate(device);
		if ( device != "other" ) {
			updateAccCookie(device);
		}
		else {
			deleteCookie( "accSelectedPhone", "/", "www.nokia.lt" );
			deleteCookie( "accSelectedPhone", "/produktai/priedai/", "www.nokia.lt" );
		}
	}


// Set value for accSelectedPhone cookie
function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value +"; path=/produktai/priedai/";
}

	
// Slides the dropdown open
	function openDeviceList(){
		$("#deviceDDLinksSelection").slideToggle(400);
		$("#deviceDDContainer").toggleClass("close");
	};
	
	
// Get cookie for create possible default content
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

// Change device --> update dynamic mbox
	function changeDevice(device){
		mboxDefineUpdate(device);
	}
	
// Fixes production page flash so that it has wmode = transparent
function fixFlash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for(i=0; i<embeds.length; i++)  {
		embed = embeds[i];
		if (embed.id =="fnc_flash") {
			var new_embed;
			// everything but Firefox & Konqueror
			if(embed.outerHTML) {
				var html = embed.outerHTML;
				// replace an existing wmode parameter
				if(html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
					new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i,"wmode='transparent'");
				// add a new wmode parameter
				else
					new_embed = html.replace(/<embed\s/i,"<embed wmode='transparent' ");
				// replace the old embed object with the fixed version
				embed.insertAdjacentHTML('beforeBegin',new_embed);
				embed.parentNode.removeChild(embed);
			} else {
				// cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
				new_embed = embed.cloneNode(true);
				if(!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase()=='window')
					new_embed.setAttribute('wmode','transparent');
				embed.parentNode.replaceChild(new_embed,embed);
			}
		}
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for(i=0; i<objects.length; i++) {
        object = objects[i];
		if (object.id =="fnc_flash") {
			var new_object;
			// object is an IE specific tag so we can use outerHTML here
			if(object.outerHTML) {
				var html = object.outerHTML;
				// replace an existing wmode parameter
				if(html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
					new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i,"<param name='wmode' value='transparent' />");
				// add a new wmode parameter
				else
					new_object = html.replace(/<\/object\>/i,"<param name='wmode' value='transparent' />\n</object>");
				// loop through each of the param tags
				var children = object.childNodes;
				for(j=0; j<children.length; j++) {
					if(children[j].getAttribute('name').match(/flashvars/i)) {
						new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i,"<param name='flashvars' value='"+children[j].getAttribute('value')+"' />");
				}
			}
				// replace the old embed object with the fixed versiony
				object.insertAdjacentHTML('beforeBegin',new_object);
				object.parentNode.removeChild(object);
				return;
			}
		}
    }
}	

$("body").find("div.additional_banner").eq(0).addClass("selectorBanner");
