var current="cp_tab_1";
function show(A){$("#"+current).css("display","none");$("#"+A).css("display","block");current=A};



function cp_init_sIFR() {
	return false;
}

/* setup other swf files */
function cp_init_swfobject(){

}

// <TABS LOGIC>
var __currentTabIndex;
/*
 * show a tab and hides others
 *
 * @var integer || string that can be parsed into a integer
 */
function cp_setTab(value) {
	if (typeof value == 'undefined')
		value = 0;
	if (!parseInt(value))
		value = 0;

	if (value >= $('#p-rcb #cp_wrap_tab_productdetails li').length)
		value = 0;

	if (__currentTabIndex == value)
		return;

	// hide all
	for (var i = 0; i < $('#p-rcb #cp_wrap_tab_productdetails li').length; i++) {
		if (i == value) {
			$('#cp_tab_'+(i+1)).addClass('cp_active');
			$('#cp_tab_'+(i+1)).removeClass('cp_inactive');
			$('#cp_tab_content_'+(i+1)).show();
		} else {
			$('#cp_tab_'+(i+1)).addClass('cp_inactive');
			$('#cp_tab_'+(i+1)).removeClass('cp_active');
			$('#cp_tab_content_'+(i+1)).hide();
		}
	}

	// remember last set index
	__currentTabIndex = value;
	
	// change the header
	cp_setHeader(value);

	// ping statistics
	cp_sendStatistics(value);
}

var __currentHeaderSRC;
/*
 * changes the header swf
 *
 * @var integer, the index of the availableHeadersSRC global
 */
function cp_setHeader(value) {
	if (typeof availableHeadersSRC[value] == undefined)
		return false;

	var src = availableHeadersSRC[value];
	if (src == __currentHeaderSRC)
		return false;

	__currentHeaderSRC = src;
	var cp_header_animation = new SWFObject(src, "Header Animation", "960", "314", "8");
	cp_header_animation.addParam("wmode","opaque");
	cp_header_animation.write('flash-inner-wrapper'); 
}

/*
 * Send statictics when the user interacts with the tabs.
 */
var __statsHaveNotBeenSendViaTabInteraction = true;
function cp_sendStatistics(value) {
	if (typeof s != 'object')
		return;

	// normalize tab values;
	if (typeof universalTabNames[value] != 'undefined')
		value = universalTabNames[value];
	else
		value = "TAB" + value;
		
	if (!__statsHaveNotBeenSendViaTabInteraction) {
		var PRODUCTSUBCATEGORY = cp_getMetrics('PRODUCTSUBCATEGORY');
		var DIVISION = cp_getMetrics('DIVISION');
		var SECTION = cp_getMetrics('SECTION');
		var CATALOGTYPE = cp_getMetrics('CATALOGTYPE');
		var PRODUCTGROUP = cp_getMetrics('PRODUCTGROUP');
		var PRODUCTCATEGORY = cp_getMetrics('PRODUCTCATEGORY');
		
		if (PRODUCTSUBCATEGORY != "") {
			var TABSECTION = PRODUCTSUBCATEGORY + '_' + value;
			s.pageName = DIVISION + ":" + SECTION + ":" + TABSECTION + ":" + CATALOGTYPE;
		} else if (PRODUCTCATEGORY != "") {
			var TABSECTION = PRODUCTCATEGORY + '_' + value;
			s.pageName = DIVISION + ":" + SECTION + ":" + TABSECTION + ":" + CATALOGTYPE;
		}
		s.t();
	}
	__statsHaveNotBeenSendViaTabInteraction = false;
}

function cp_getMetrics(metric) {
	var content = $("meta[@name='PHILIPS.METRICS."+metric+"']").attr("content");
	if (typeof content == 'undefined' || content == null)
		return '';
	else
		return content;
}

// </TABS LOGIC>

$(document).ready(function() {
	//sIFR needs a timeout for safari or sometimes sIFR won't behave correctly
	setTimeout("cp_init_sIFR()", "1000");
	cp_init_swfobject();
	
});


/**
 * jget plugin
 * @author Alexandre Magno
 * @desc get a query string to be accessible for javascript
 * @version 1.0
 * @example
 *
 * http://www.foo.com/test.php?var1=test1
 * var outputQuery = $.jget['var1'];
 * $.debug(outputQuery);
 * //Will show 'test1'
 *
 *
 * @license free
 * @param bool vertical, bool horizontal
 * @site http://blog.alexandremagno.net
 *
 */

jQuery.extend({

	//starting the jget object
	jget: {},
	//get the url
	url: window.location.href.replace(/^[^\?]+\??/,''),
	//get the queryString
	parseQuery: function ( query ) {
	   var Params = {};
	   if ( ! query ) {return Params;}// return empty object
	   var Pairs = query.split(/[;&]/);
	   for ( var i = 0; i < Pairs.length; i++ ) {
		  var KeyVal = Pairs[i].split('=');
		  if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		  var key = unescape( KeyVal[0] );
		  var val = unescape( KeyVal[1] );
		  val = val.replace(/\+/g, ' ');
		  Params[key] = val;
	   }
	   return Params;
	},
	//Make the jget object available to jQuery.extend
	getQueryString: function() {
		this.jget = this.parseQuery(this.url);
	},
	//debugging
	debug: function(message) {

		if(!$.browser.msie) {

			console.info(message);

		} else if($.browser.safari) {

			window.console.log(message);

		} else {

			alert(message);
		}

	}

});

//start the plugin
$.getQueryString();

// DIV POPUP



function showOverlay(element, div, left, top)//, text)
{
	var e = document.getElementById(element);
	var d = document.getElementById(div);
	//var t = document.getElementById('overlay_text');
	
	d.style.display = '';
	//t.innerHTML = text;
	
	var divheight = 0;
	if(d.offsetHeight) { divheight = d.offsetHeight; }
	else if(d.style.pixelHeight) { divheight = d.style.pixelHeight; }

	//left 18 top 50
	d.style.left = (findPosX(e) - (-left)) + "px";
	d.style.top = (findPosY(e) - (-top) - divheight) + "px";
}

function hideMe(div)
{
	var d = document.getElementById(div);
	d.style.display = 'none';
}

function findPosX(obj)
  {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
  }

  function findPosY(obj)
  {
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
  }





/* 
**********************************************
*******       Locale selector layer functions      ******
**********************************************
*/
_page.arrLS =[];
_page.ls = function(type, params) {
	params = params || {};
		this.options = {
			keepActive:(typeof(params.keepActive)!="undefined"?params.keepActive:false),
			localeFlag:(params.localeFlag?params.localeFlag:_page.altLocaleFlag || _page.locale),
			localeText: (params.localeText?params.localeText:_page.altLocaleText),
			collapseDirection: (params.collapseDirection?params.collapseDirection:'auto'),
			showRows: (params.showRows?params.showRows:9), 										// Default rows 9
			listHeight: (params.listHeight?params.listHeight:275), 								// Default height defined in styles, may be overruled for faillover
			data: (params.data?params.data:_page.getLocalesArray),								// Requires an array of locales, input can be array or function
			remoteUrl:(params.remoteUrl?params.remoteUrl:""),
			remoteOptions: params.remoteOptions || {},
			removeLocaleList: params.removeLocaleList || []
		}
	this.type = type;	
	// Override remote options, set default output and oncomplete function when not defined by parameters			
	this.options.remoteOptions.onComplete = (this.options.remoteOptions.onComplete?this.options.remoteOptions.onComplete:this._remoteComplete.bindArgs(this));
	// Other declarations
	this.arrIndex = _page.arrLS.length;
	this.id = "ls_"+ this.arrIndex;
	this.timer = "";
	this.rows = this.options.showRows;
	this.move = this.options.collapseDirection;
	this.switchOverflow = (_page.browser.isMac && _page.browser.isGecko?true:false);
	this.localeArray = [];
	this.dataLoaded = false;
	this.elList = null;
	this.listActive = false;
	_page.arrLS[this.arrIndex] = this;	// Add to LS array
	this._init();
}
_page.ls.prototype = {
	_init:function() {
		this._build();
	},
	_load:function(renderImages) {
		this._getData();
		if(this.dataLoaded == true) {
			this._update();
			this._visible(true);
			if(renderImages)this._renderImages();
			this._resize();
			this._position();
			this._hidden(true);
		}
	},
	_remoteComplete:function() {
		// This method is based at the current www.crsc.philips.com/crsc/locales/locale_section url 
		this.localeArray = processLocales();	
		if(this._localesTmp)
			_page.locales = this._localesTmp;
		this.dataLoaded = true;
		this._show();			
	},
	_getData:function() {
		var id;
		if(this.dataLoaded == false) {
			if(this.options.remoteUrl!="") {
				// Create local backup of current loaded _page.locales and reset _page.loaces
				this._localesTmp = {};	
				for(id in _page.locales)
					this._localesTmp[id] = _page.locales[id];
				_page.locales = {};
				_page.loadJSFile(this.options.remoteUrl,function(){var filled=false;for(id in _page.locales){filled=true;break;}return filled}, this.options.remoteOptions.onComplete);
			} else {
				this.localeArray = typeof(this.options.data)=="function"?this.options.data():this.options.data;
				this.dataLoaded = true;
			}
		}
	},
	_mClick:function() {
		clearTimeout(this.timer);
		if(!this.options.keepActive) this._show();
	},
	_mOver:function() {
		clearTimeout(this.timer);
		if(!this.options.keepActive) this.timer=setTimeout('_page.arrLS['+this.arrIndex+']._show()','1000');
	},
	_mClose:function() {
		clearTimeout(this.timer);
		if(!this.options.keepActive) this.timer=setTimeout('_page.arrLS['+this.arrIndex+']._hide()','500');
	},
	_show:function() {
		var objLSBody, objFlag, objIframe;
		if(this.listActive == false)
			this._load(true);
		if(this.elList) {						
			objLSBody = gE('p-ls-body-'+this.arrIndex);
			if(objLSBody && this.switchOverflow) { objLSBody.style.overflow = "auto"; }
			if(this.type == '1') {
				objFlag = gE('p-flag-'+this.arrIndex);
				if(objFlag) applyStyle(objFlag,'p-flag-on');
			}
			if(useIframe) {
				objIframe = gE('p-lsIF-'+this.arrIndex);
				if(objIframe) sE(objIframe);
			}
			this._visible();
			this.listActive = true;
		}
	},
	_hide:function() {
		var objLSBody, objFlag, objIframe;
		if(this.elList) {
			objLSBody = gE('p-ls-body-'+this.arrIndex);
			if(objLSBody && this.switchOverflow) objLSBody.style.overflow = "hidden";
			if(this.type == '1') {
				objFlag = gE('p-flag-'+this.arrIndex);
				if(objFlag) applyStyle(objFlag,'p-flag');
			}
			if(useIframe) {
				objIframe = gE('p-lsIF-'+this.arrIndex);
				if(objIframe) hE(objIframe);
			}
			this._hidden();
			this.listActive = false;
		}
	},
	_build:function() {
		var strFlagLocale, strCountry, strLanguage, strFlagSource, strLocaleText, strHTML, crsc, strArrowName, classClick;
		// Get alternative locale flag
		strFlagLocale = this.options.localeFlag;
		strCountry = strFlagLocale.split("_")[0];
		strLanguage = strFlagLocale.split("_")[1] || "en";
		strFlagSource = "flag_"+strCountry+".gif";
		if(this.options.localeText=="") 			
			this.options.localeText = (_page.countries[strCountry] || "Global") +" / "+ _page.languages[strLanguage];
		strLocaleText = this.options.localeText;
		strHTML = "";
		strHTML += "<div class=\"p-ls-wrapper-"+this.type+"\" id=\"p-ls-"+this.arrIndex+"\" >";
		crsc = _page.crsc_server;
		// Add onclick / mouse over HTML
		strArrowName = (_page.direction == 'ltr'?"arrow_bottom_right.gif":"arrow_bottom_left.gif");
		classClick = (this.options.keepActive!=true?"p-click":"");
		if(this.type == "1") {
			strHTML+="<div class=\"p-ls-hover-container-1 "+classClick+"\" id=\"p-ls-hover-"+this.arrIndex+"\" onmouseover=\"_page.arrLS["+this.arrIndex+"]._mOver()\" onmouseout=\"_page.arrLS["+this.arrIndex+"]._mClose()\" onclick=\"_page.arrLS["+this.arrIndex+"]._mClick()\"><nobr><img alt=\""+strLocaleText+"\" src=\""+crsc+"/crsc/images/" + strFlagSource + "\" id=\"p-flag-"+this.arrIndex+"\" class=\"p-flag\" />&nbsp;<img alt=\"Show locale list\" src=\""+crsc+"/crsc/images/"+strArrowName+"\" id=\"p-arrow-bottom-right-"+this.arrIndex+"\" class=\"p-arrow-bottom-right\" /></nobr></div>\n";
		} else if(this.type == "2") {
			strHTML+="<div class=\"p-ls-hover-container-2 "+classClick+"\" onmouseover=\"_page.arrLS["+this.arrIndex+"]._mOver()\" onmouseout=\"_page.arrLS["+this.arrIndex+"]._mClose()\" onclick=\"_page.arrLS["+this.arrIndex+"]._mClick()\"><table class=\"p-ls-hover\" id=\"p-ls-hover-"+this.arrIndex+"\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td class=\"p-ls-hover-left\"><img alt=\""+strLocaleText+"\" src=\""+crsc+"/crsc/images/" + strFlagSource + "\" id=\"p-flag-"+this.arrIndex+"\" class=\"p-flag\" /></td><td class=\"p-ls-hover-center\"><span id=\"p-ls-header-text-"+this.arrIndex+"\">"+strLocaleText+"</span></td><td class=\"p-ls-hover-right\"><img alt=\"Show locale list\" src=\""+crsc+"/crsc/images/"+strArrowName+"\" id=\"p-arrow-bottom-right-"+this.arrIndex+"\"/></td></tr></table></div>\n";
		}
		strHTML+="</div>";
		this.html = strHTML;
	},
	_renderImages:function() {
		if(this.imagesRendered) return;
		var arrLocales, strLocale, strCurCountry, objLink, strNewClass, i;
		arrLocales = this.localeArray;
		for(i=0;i < arrLocales.length;i++) {
			strLocale = arrLocales[i][0];
			strCurCountry = (strLocale.indexOf("_")==-1?strLocale:strLocale.substring(0,2));
			objLink = gE("p-ls-list-"+this.arrIndex+"-flag-" + strCurCountry);
			strNewClass = "p-flag-" + strCurCountry;	
			if(objLink)
				applyStyle(objLink,strNewClass);
		}
		this.imagesRendered = false;	
	},
	_removeLocales:function() {
		// Remove locale element from array if needs to be removed
		var arrLocales, strLocale, i;
		if(this.options.removeLocaleList.length == 0) return;
		arrLocales = this.localeArray;
		for(i=0;i < arrLocales.length;i++) {
			strLocale = arrLocales[i][0];
			if(hasElement(this.options.removeLocaleList,strLocale)) arrLocales.splice(i,1);
		}	
	},
	_update:function() {	
		if(this.updated) return;
		var arrLocales, crsc, strCurCountry = '', strLastCountry = '', strLocale = '', strHTMLBody = '', overFlow, arrLocalesMax, objFlagDimensions, elContainer, strHTMLHeader, i;
		this._setAutoMovement();
		this._removeLocales();
		arrLocales = this.localeArray;
		// Build body HTML and locales from array
		crsc = _page.crsc_server;
		overFlow = (this.switchOverflow?"style=\"overflow:hidden\"":"");
		strHTMLBody += "<div id=\"p-ls-body-"+this.arrIndex+"\" class=\"p-ls-body\" "+overFlow+">\n";
		strHTMLBody += "<table class=\"p-ls-list\" cellspacing=\"0\" border=\"0\">\n";
		arrLocalesMax = arrLocales.length;
		for( i=0;i < arrLocalesMax;i++) {
			strLocale = arrLocales[i][0];
			strCurCountry = (strLocale.indexOf("_")==-1?strLocale:strLocale.substring(0,2));
			if (strCurCountry != strLastCountry) {
				// Set flag dimensions;
				objFlagDimensions = {width:'17',height:'13'};
				if(strCurCountry=="global" || strCurCountry=="me_en" || strCurCountry=="ce_es"|| strCurCountry=="others")
					objFlagDimensions = {width:'14',height:'14'};
				strHTMLBody +=
				(i!=0?"</ul></td></tr>\n":"")+
				"<tr><td class=\"p-ls-list-left\"><a id=\"p-ls-list-"+this.arrIndex+"-flag-"+strCurCountry+"\" href=\""+arrLocales[i][3]+"\" onclick=\"_page.changelocale('"+strLocale+"');return false;\" /></td><td  class=\"p-ls-list-right\"><ul class=\"p-ls-localelist\">\n";
			}
			// Set country / language
			strHTMLBody += "<li><a href=\""+arrLocales[i][3]+"\" onclick=\"_page.changelocale('"+strLocale+"','"+arrLocales[i][3]+"');return false;\">"+arrLocales[i][4]+"</a></li>";
			strLastCountry = strCurCountry;	
		}
		strHTMLBody += "</ul></td></tr>\n";
		strHTMLBody += "</table>\n";
		strHTMLBody += "</div>\n";
		// Add Dropdown HTML, cannot initiated earlier because of the movement detection
		strHTML = "";
		strHTML+="					<div id=\"p-ls-container-"+this.arrIndex+"\" class=\"p-ls-container-"+this.move+"\" onmouseover=\"_page.arrLS["+this.arrIndex+"]._mOver()\" onmouseout=\"_page.arrLS["+this.arrIndex+"]._mClose()\">\n";
		if(this.type == "1") {
			strHTMLHeader = "<div id=\"p-ls-header-"+this.arrIndex+"\" class=\"p-ls-header\"><table cellspacing=\"0\" class=\"p-ls-header-table\"><tr><td><span id=\"p-ls-header-text-"+this.arrIndex+"\">"+this.options.localeText+"</span></td></tr></table></div>\n";
			strHTML+= strHTMLHeader + strHTMLBody;	// Header used which overlaps the topmenu
		} else if(this.type == "2") {
			strHTML+= strHTMLBody;
		}
		// Add IFRAME html
		if(useIframe) {
			strHTML+="		<iframe frameborder=0 id=\"p-lsIF-"+this.arrIndex+"\" src=\""+crsc + "/crsc/images/t.gif\" scroll=none style=\"FILTER:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);visibility:hidden;height:0;position:absolute;width:0px;top:0px;z-index:0;\"></iframe>";
		}
		strHTML+="</div>";
		elContainer = gE('p-ls-'+this.arrIndex);
		if(elContainer) elContainer.innerHTML += strHTML;
		this.elList = gE("p-ls-container-"+this.arrIndex);
		this.updated = true;
	},
	_resize:function() {
		if(this.resized) return;
		var intMaxRows, objDD, arrRows, intHeight=0, intVisibleRows=0, objCellLeft, objCellRight, objList, objLS, objIframe, i;
		// Set dynamich height of dropdown by defined max rows;
		intMaxRows = this.rows;
		objDD = document.getElementById('p-ls-body-'+this.arrIndex);
		arrRows = objDD.getElementsByTagName('tr');
		for(i=0; i < arrRows.length; i++) {
			objCellLeft = arrRows[i].childNodes[0];
			objCellRight = arrRows[i].childNodes[1];
			if(i < intMaxRows) {	// Count visible height
				intHeight += objCellRight.offsetHeight;
				intVisibleRows++;
			}
			if(!arrRows[i+1]) {	// Remove bottom border
				applyStyle(objCellLeft,getStyle(objCellLeft)+" p-nobottomborder");
				applyStyle(objCellRight,getStyle(objCellRight)+" p-nobottomborder");
			}
		}
		intHeight = (intHeight == 0? this.options.listHeight:intHeight );  // Set failover height, if it can't be determined due to display:none in parent elements
		if(_page.browser.isSafari) intHeight = intHeight + intVisibleRows; // Safari doesn't include the borders in the height
		// Overflow is needed
		if(arrRows.length > intMaxRows) {
			if(!_page.browser.isIE5x) intHeight--;  // Decrease 1px for border, except for border box model applicable browsers
			if(_page.browser.isSafari) intHeight = intHeight - 2;
		// Extend the width if there's no overflow / scrollbar, not applicable for Safari	
		} else {
			if(!_page.browser.isSafari){
				objList = objDD.getElementsByTagName("table")[0];
				sW(objList,objDD.offsetWidth-2); // - 2  due to border
			}
			if(_page.browser.isIE5x) intHeight++; // increase 1px for border only for border box model applicable browsers
		} 
		sH(objDD,intHeight); 
		if (useIframe) {
			objLS = this.elList;
			objIframe = gE('p-lsIF-'+this.arrIndex);
			sH(objIframe,(objLS.offsetHeight));
			sW(objIframe,(objLS.offsetWidth));
		}
		this.resized = true;
	},
	_setAutoMovement:function() {
		if(this.options.collapseDirection != "auto") return;
		var scrollPosTop, screenHeight, scrollPosBottom, objRef, objLS, bottomPosContainer, topPosContainer;
		scrollPosTop = getScrollPosTop();  				// Positon top in scrolled browser window
		screenHeight = getScreenHeight();				// Available height browser window
		scrollPosBottom = scrollPosTop + screenHeight;	// Positon bottom in scrolled browser window	
		objRef = gE('p-ls-hover-'+this.arrIndex);
		objLS = this.elList;
		bottomPosContainer = findPosY(objRef) + (objLS?objLS.offsetHeight:this.options.listHeight);
		topPosContainer = findPosY(objRef) - (objLS?objLS.offsetHeight:this.options.listHeight);
		this.move = (bottomPosContainer > scrollPosBottom && topPosContainer > scrollPosTop?"up":"down");
	},
	_position:function () {
		this._setAutoMovement();
		var elLS, elHover, elContainer, elHeader, elBody
		elLS = this.elList;
		elHover = gE('p-ls-hover-'+this.arrIndex);
		elContainer = gE('p-ls-container-'+this.arrIndex);
		elHeader = gE('p-ls-header-'+this.arrIndex);
		elBody = gE('p-ls-body-'+this.arrIndex);
		if(this.move == "up") {
			if(this.type == "1") {
				if(elHeader && elBody) elHeader.parentNode.insertBefore(elBody,elHeader);
			}
			applyStyle(elLS,"p-ls-container-up");
		} else if(this.move == "down") {
			if(this.type == "1") {
				if(elHeader && elBody) elHeader.parentNode.insertBefore(elHeader,elBody);
				if(elHover && elContainer) elHover.parentNode.insertBefore(elContainer,elHover);
			}		
			applyStyle(elLS,"p-ls-container-down");
		}
	},
	_visible: function(visibility) {
		if(this.elList) {
			if(visibility) hE(this.elList);
			this.elList.style.display = "block";
		}	
	},
	_hidden: function(visibility) {
		if(this.elList) {
			if(visibility) sE(this.elList);
			this.elList.style.display = "none";
		}	
	}
}

_page.localesProcessed = false;
_page.localesArray = [];	
// Build locale sorted array, used for i.e. locale selector list
_page.getLocalesArray = function() {
	if(_page.localesProcessed == false) {
		_page.localesArray = processLocales();
		if(_page.localesArray.length > 0)
			_page.localesProcessed = true;
	}	
	return _page.localesArray;
}

function processLocales() {
	var arrLocales = [], i, anItem, strCountry, strLanguage, sortCol, sortOrder, lengthLocales;
	// Build array
	if(_page.locales["others"]) delete _page.locales["others"];			// Delete current others var if exists, as it will be appended to the bottom
	i = 1;
	for(anItem in _page.locales){
		if(anItem != "global") {
			strCountry = _page.countries[anItem.substring(0,2)];
			strLanguage = _page.languages[anItem.substring(3,5)];
			arrLocales[i] = [];
			arrLocales[i][0] = anItem;									// Store locale code
			arrLocales[i][1] = strCountry;								// Store country
			arrLocales[i][2] = strLanguage; 							// Store language
			arrLocales[i][3] = _page.locales[anItem]; 					// Store link
			arrLocales[i][4] = strCountry+" / "+strLanguage; 			// Store combi country/language
			i++;
		}
	}
	if(i > 1) {
		arrLocales[0] = []; 	// Create first container for locale global.
		arrLocales[0][4] = "/"	// Enter empty value, inorder to make sure the element is on top of the list
		// Sort array
		function sortArray(a,b) {
			if (a[sortCol]<b[sortCol]) return (sortOrder=="asc"?-1:1);
			if (a[sortCol]>b[sortCol]) return (sortOrder=="asc"?1:-1);
			return 0;
		}
		sortCol=4;
		sortOrder="asc";
		arrLocales.sort(sortArray);
		// Insert global locale as first element
		arrLocales[0][0] = "gb_en";
		arrLocales[0][1] = "United Kingdom";
		arrLocales[0][2] = "English";
		arrLocales[0][3] = '?ls=gb_en';
		arrLocales[0][4] =  arrLocales[0][1]+" / "+arrLocales[0][2];
		// Add others option at the last position in the list / Only applicable for normal internet site
		if(_page.headerType =="internet") {
			lengthLocales = arrLocales.length;
			_page.locales["others"] = "http://www.philips.com/countries";
			arrLocales[lengthLocales] = [];
			arrLocales[lengthLocales][0] = "others";
			arrLocales[lengthLocales][1] = "Others";
			arrLocales[lengthLocales][2] = "";
			arrLocales[lengthLocales][3] = _page.locales["others"];
			arrLocales[lengthLocales][4] = "Others";
		}
	}	
	return arrLocales;
}


_page.changelocale = function (locale, url) {
	var strCheckLocale, newLink, blnRedirect;
	// Check if new country selected
	strCheckLocale = (_page.selectedLocale!=""?_page.selectedLocale:_page.locale);
	// Redirect to site with new Locale	
	newLink = url || getLocaleURL(locale);
	if(locale == "") {
		// No Locale selected
		alert(_page.text["alert2"]);
	} else {
		blnRedirect = false;
		if(locale!="others") {
			// Check if the user want to save this Locale
			// The function will also redirect the user
			if(confirm(_page.text["confirmation1"])) {
				// Set locale
				_page.switchHandler("Javascript:_page.setlocale('"+locale+"','"+newLink+"');", "");
			} else {
				blnRedirect = true;
			}
		} else {
			blnRedirect = true;
		}
		if(blnRedirect) {
			// Redirect to site using the switchHandler function
			_page.switchHandler(newLink, "");
		}
	}
};





//////////////////////
/// STINK FUNCTIONS
//////////////////////

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 '';
}


function cveTrackOmniture(tag, type) {
	return null;
}


/// document on ready


$(document).ready(function () {


setTimeout(function () {

	//try getting locale from cookie
	var userLoc = readCookie('Philips=userlocale');

	//check for existence of la
	if (!$.jget['ls'] && userLoc.length == 0) {

		$("#flashcontent").remove();
		$("#selector #label").html('LANGUAGE SELECTOR')
		$("#header, #selector, #p-footer").show();

		//build the language selector
		//current draws the locales from ... assets/js/homepages_cinemas.js
		headerLS = new _page.ls("2");
		headerLS.area	   = "CONSUMER";
		_page.area = headerLS.area.toLowerCase();

		var lsCode = headerLS.html;

		//append the selector to the HTML
		$('#languages').empty().append(lsCode);

	} else {
	
		$("body, html").css({
			width: "100%",
			minWidth: "960px", 
			height: "100%", 
			minHeight: "680px"
		});
		
		//gather cookie first, and follow with URL
		userLoc = !$.jget['ls'] ? userLoc : $.jget['ls'];
		
		if (userLoc == '_') {
			userLoc = 'gb_en';
		}

		//remove unwanted html
		$("#flashwrapper, #flashcontent").show();

		//gather data and assign to vars
		var useDynamicData = false;
		var language = userLoc.slice(3, 5);
		var country = userLoc.slice(0, 2);
		var locale = language + '-' +country;
		
		//check to see for rubbish local and details		
		if (typeof _page.locales[country + '_' +language] === 'undefined') {
			language = 'en';
			country = 'gb';
			locale = 'en-gb';
		}

		//omniture
		$("meta[name='PHILIPS.METRICS.COUNTRY']").attr({"content": country});
		$("meta[name='PHILIPS.METRICS.LANGUAGE']").attr({"content": language});

		//check to see if omniture object exists
		if (typeof s == 'object' && typeof _page.languages[language] == 'string') {
			var strLanguage = _page.languages[language].toLowerCase();
			s.tl(true,"0",'CP:cinema:select_language:' + strLanguage);
		}


		//fetch and read XML
		$.ajax({
			type: "GET",
			url: "flash-assets/xml/copy/locales/copy_" + locale + '.xml',
			dataType: "xml",
			success: function(xml) {
				$(xml).find('item').each(function(){
					if ($(this).attr('id') == 'footer.share.siteTitleLabel') {
						document.title = $(this).text();
						$("meta[name='title']").attr({"content":$(this).text()});
					}
					if ($(this).attr('id') == 'footer.menu.title') {
						$("meta[name='description']").attr({"content":$(this).text()});
					}
				});
			}
		});
		

		//initiate the flash
        var so = new SWFObject('Main.swf', 'website', '100%', '100%', '9.0.0', '#000000');
        so.useExpressInstall('html-assets/js/swfobject/expressinstall.swf');
		so.addParam('menu', 'false');
		so.addParam('allowFullScreen', 'true');
		so.addParam('allowScriptAccess', 'always');
		so.addParam('wmode','opaque');
		so.addVariable("locale", locale);
		so.addVariable("s_division","CP");
		so.addVariable("s_section","CP:cinema");
		so.addVariable("s_country",country);
		so.addVariable("s_language",language);
		so.addVariable("showSystemMonitors", true);
		so.write('flashcontent');

	}

},1000);


});
