(function($){

cp_currentSection = null;
	
$.cp = {};
$.extend($.cp, {
	
	pageId : null,
	headerId : null,
	mapObject : null,
	headerObject : null,
	
	headerSelector : "#flashheader_wrapper",
	contentSelector : "#cp_content",
	
	contentPath : "/consumerfiles/pageitems/master/categorypages/FlatTV2011_sep2/assets/content/",
	headerPath : "/consumerfiles/pageitems/master/categorypages/FlatTV2011_sep2/assets/headers/",
	
	statsSent : true,
	
	currentJS : [],
	currentCSS : [],
	
	sitemap : [],
	headers : [],
	loading : false,
	
	pageOpenedCallback:null,
	
	okToLoad : function() {
	
		if(this.loading) return false;
		return true;
	},
	
	getMapVO : function(id) {
		for(var i=0;i < this.sitemap.length;i++){
			var item = this.sitemap[i];
			if(item) if(item.id == id) return item;
		}
	},
	
	getHeaderVO : function(id) {
		for(var i=0;i < this.headers.length;i++){
			var item = this.headers[i];
			if(item.id == id) return item;
		}
	},
	
	setHeaderHeight : function(h) {
		$("#cp_headerAnimation").css("height", h + "px");
		$("#cp_header").css("height", h + "px");
		$("#cp_header").css("background-color", "#000000");
	},
	
	setHeader : function(id) {
		if(this.headerId==id) return;
		
		removeSWFObject("cp_headerAnimation");
		
		this.headerObject = this.getHeaderVO(id);
		if(!this.headerObject) {
			$("#cp_header").css("background", "#000");
			this.headerId = null;
			return;
		}
		
		$("#cp_header").css("background", "url("+this.headerPath+this.headerObject.bg+")");
		
		if(this.headerObject.playbutton == undefined) this.headerObject.playbutton = "true";
		if(this.headerObject.replayButton == undefined) this.headerObject.replayButton = "true";
		if(this.headerObject.autoPlay == undefined) this.headerObject.autoPlay = "true";
		
		if(this.headerObject.type == "custom"){
			//create flashheader with SWFWrapper
			var flashvars = {
				theXML:this.headerPath+this.headerObject.config
			};
			var params = {
				menu: "false",
				seamlesstabbing: "false",
				wmode:"transparent",
				allowFullScreen:"true",
				allowScriptAccess :"sameDomain",
				bgColor:"0x000000"
			};
			
			var attributes = {
				id:"cp_headerAnimation"
			};
			
			swfobject.embedSWF(this.headerPath+this.headerObject.swf, "cp_headerAnimation", "960", "275", "9", "/consumerfiles/pageitems/master/categorypages/FlatTV2011_sep2/assets/js/expressInstall.swf", flashvars, params, attributes);
			
		}else{
			//create flashheader with SWFWrapper
			var flashvars = {
				swf:this.headerPath+this.headerObject.swf,
				swfConfig:this.headerPath+this.headerObject.config,
				wrapperConfig:this.SWFWrapperConfig,
				playButton:this.headerObject.playbutton,
				replayButton:this.headerObject.replayButton,
				autoPlay:this.headerObject.autoPlay
			};
			var params = {
				menu: "false",
				seamlesstabbing: "false",
				wmode:"transparent",
				allowFullScreen:"true",
				allowScriptAccess :"sameDomain",
				bgColor:"0x000000"
				
			};
			
			var attributes = {
					id:"cp_headerAnimation"
				};
			
			var wpath = this.SWFWrapperPath;
			if(this.headerObject.wrapperType=="old") wpath = this.SWFWrapperPath2;
				
			swfobject.embedSWF(wpath, "cp_headerAnimation", "960", "275", "9", "/consumerfiles/pageitems/master/categorypages/FlatTV2011_sep2/assets/js/expressInstall.swf", flashvars, params, attributes);
			
		}
		
		this.headerId = this.headerObject.id;
	},
	
	gotoPage : function(id) {
		if(!this.okToLoad()) return false;
		
		//window.location = id;
		
		return false;
	},
	
	openPage : function(id) {
		
		if(!this.okToLoad()) return;
		
		this.pageId = id;
		this.mapObject = this.getMapVO(id);
		if(!this.mapObject) {
			this.pageId = "/overview";
			this.mapObject = this.getMapVO(this.pageId);
			
			if(!this.mapObject) return false;
			
			window.location = "#"+this.pageId;
		}
		
		
		this.loadPage(this.mapObject.id);
		this.sendStatistics(this.mapObject.id);
		this.setHeader(this.mapObject.id);
	},
		
	loadPage : function(id) {
		this.loading = true;
		
		var mapObject = this.getMapVO(id);
		/* If no cache is present, show the gif preloader and run an AJAX request: */
		//$('div#cp_preloader').html('<img src="/consumerfiles/pageitems/master/categorypages/FlatTV2011_sep2/assets/images/preloader.gif" width="64" height="64" class="preloader" />');
		
		$(mapObject.target).load(this.contentPath+mapObject.page,function (msg){
			
			
			//unload previous css and js
			var i = 0;
			/*while(i < $.cp.currentCSS.length){
				var css = $.cp.currentCSS[i];
				//alert(css)
				
				var found = false;
				$("link[type=text/css]").each(function(){
					if(this.href.indexOf(css)>-1){
						$(this).remove();
						found = true;
					}
				})
				
				if(found) $.cp.currentCSS.pop();
				else i++
				
			}*/
			
			if($.cp.currentJS){
				//$.removeScript($.cp.contentPath+this.currentJS);
			}
			
			//load in new css and js
			/*if(mapObject.css) {
				$.getCSS($.cp.contentPath+mapObject.css);
				$.cp.currentCSS.push(mapObject.css);
			}*/
			if(mapObject.js){
				$.getScript($.cp.contentPath+mapObject.js,function (msg){
					$.cp.loading = false;
					$('div#cp_preloader').html('');
				});
				$.cp.currentJS.push(mapObject.js);
			}else{
				$.cp.loading = false;
			}
			
			//if($.cp.pageOpenedCallback) setTimeout($.cp.pageOpenedCallback,100);
			if($.cp.pageOpenedCallback) $.cp.pageOpenedCallback();
		});
		
	},
	
	sendStatistics : function(value){
		if (typeof s != 'object') return;
		
		value = value.replace(/_/gi,"[]");
		value = value.replace(/\\/gi,":");
		value = value.replace(/\//gi,":");
		value = value.replace(/\[\]/gi,"_");
		value = "_"+value.substr(1,value.length);
		
	    if (!this.statsSent) {
		    var PRODUCTSUBCATEGORY = this.getMetrics('PRODUCTSUBCATEGORY');
		    var DIVISION = this.getMetrics('DIVISION');
		    var SECTION = this.getMetrics('SECTION');
		    var CATALOGTYPE = this.getMetrics('CATALOGTYPE');
		    var PRODUCTGROUP = this.getMetrics('PRODUCTGROUP');
		    var PRODUCTCATEGORY = this.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();
	    }
	    this.statsSent = false;
	},
	
	getMetrics : function(metric) {
		var content = $("meta[name='PHILIPS.METRICS."+metric+"']").attr("content");
		if (typeof content == 'undefined' || content == null){
			return '';
		}else{
			return content;
		}
	}
	
});

$.getCSS = function( url, media ) {
    $( document.createElement('link') ).attr({
            href: url,
            media: media || 'screen',
            type: 'text/css',
            rel: 'stylesheet'
    }).appendTo('head');
}; 



$.removeCSS = function( url, media ) {
	if(!media) media = "screen";
	
	return $("link[type=text/css]").each(function(){
		if(this.href.indexOf(url)>-1){
			$(this).remove();
			return true;
		}else{
			return false;
		}
	})
    
}; 



})(jQuery);





//----- Flash 

function isDefined(variable)
{
return (!(!(document.getElementById(variable))))
}

function removeSWFObject(clipName){
	var flashMovie = getFlashMovieObject(clipName);
	if(!flashMovie) return;
	var p = flashMovie.parentNode;
	if(!p) return
	swfobject.removeSWF(clipName);
	var d = document.createElement("div");
	d.id = clipName;
	p.appendChild(d);
	
}

function removeFlashObject(clipName){
	var flashMovie = getFlashMovieObject(clipName);
	if(!flashMovie) return;
	if(flashMovie){
		var p = flashMovie.parentNode;
		if(p){
		p.removeChild(flashMovie);
		}
	}
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
	  return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
	if (document.embeds && document.embeds[movieName])
	  return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
	return document.getElementById(movieName);
  } 	
}


/*
 * Bandwidth detection
 */

(function($){
  $.bandwidth = null;
  $.bandwidthTestEndpoint = 'http://test.philips.com/esi/getbw_js_tpi.esi';
  $.testBandwidth = function(cb){
    if ($.bandwidth) {
      cb($.bandwidth);
    } else {
      $.onBandwidthTested = cb;
      $.getScript($.bandwidthTestEndpoint);
    }
  }
})(jQuery);


