var cp_currentFlashHeader;
var cp_currentFlashHeaderId;

function cp_start() {
	var cp_tabs = [
   		{id:'cp_tab1',name:'overview'},
   		{id:'cp_tab2',name:'pacifiers'},
   		{id:'cp_tab3',name:'teethers'}
   	];
	TabManager.init('#cp_tabmenu',cp_tabs);
}


/*
 * this function will be called right after a tab has changed
 */
function cp_tabChanged(value) {
	// change the flashheader
	cp_setFlashHeader("tabAnimation"+(value+1));
	cp_setBgOnNavBar(value);
	
	// re-initialise sIFR
	setTimeout("cp_init_sIFR('#" + TabManager.data[value].id + "')", 1);
    //cp_init_sIFR("#"+tabMan.ids[value]);
}

/*
 * Changes the navbar bg
 */

// set bg on navbar
function cp_setBgOnNavBar(value){
	var val = value +1;
	var bg = "url(/consumerfiles/pageitems/master/categorypages/AVENT_pacifiers_teethers_2010/assets/css/tabs/tab"+val+"_menubg.jpg)";
	$("#cp_tab_productdetails").css("background-image",bg);

}


/*
 * Changes the header swf
 * @var integer, the index of the availableHeadersSRC global
 */
function cp_setFlashHeader(value) {
	
	//if(!$("#flashheader_wrapper").length) return false;
	
	var flashObject = cp_flashheaders[value];
	if (flashObject == undefined) return false;
	
	var src = flashObject.swf;
	if (src == cp_currentFlashHeader) return false;
		
	// Change placeholder image
	var placeholderSrc = "url("+flashObject.bg+") no-repeat";
	$("#flashheader_wrapper").css("background", placeholderSrc);
	
	if(cp_currentFlashHeaderId) removeSWFObject(cp_currentFlashHeaderId);

	$("#"+value).css("display", "block");
	
	cp_currentFlashHeader = src;
	
	if(flashObject.playbutton == undefined) flashObject.playbutton = "true";
	if(flashObject.replayButton == undefined) flashObject.replayButton = "true";
	if(flashObject.autoPlay == undefined) flashObject.autoPlay = "true";
	
	//create flashheader with SWFWrapper
	var flashvars = {
		swf:src,
		swfConfig:flashObject.config,
		wrapperConfig:cp_SWFWrapperConfig,
		playButton:flashObject.playbutton,
		replayButton:flashObject.replayButton,
		autoPlay:flashObject.autoPlay
	};
	var params = {
		menu: "false",
		seamlesstabbing: "false",
		wmode:"transparent",
		allowFullScreen:"true",
		allowScriptAccess :"sameDomain",
		bgColor:"0x000000"
		
	};
	tabId = "tabAnimation";
	
	var attributes = {
		id:tabId
	};
	
	swfobject.embedSWF(cp_SWFWrapperPath,tabId, "960", "275", "9", "/consumerfiles/pageitems/master/categorypages/flattv/assets/js/expressInstall.swf", flashvars, params, attributes);
	prevTab = value;
	
	cp_currentFlashHeaderId = tabId;
}

//----- sIFR
function cp_init_sIFR(tab){
	if(!tab) tab = "";
	if(typeof sIFR !='undefined') {
		  sIFR.replace(header, {
	  		selector: '#p-rcb ' + tab + ' h2'
	  		,css: '.sIFR-root {color:#0B5ED7;font-size:32px;}'
	  		,wmode: 'transparent'	
	  	});
		  sIFR.replace(header, {
	  		selector: '#p-rcb ' + tab + ' h3'
	  		,css: '.sIFR-root {color:#0B5ED7;font-size:18px; background-color:none;}'
	  		,wmode: 'transparent'	
	  	});
	
		  sIFR.replace(header, {
	  		selector: '#p-rcb ' + tab + ' h4'
	  		,css: '.sIFR-root {color:#0B5ED7;font-size:26px;}'
	  		,wmode: 'transparent'	
	  	});
		  sIFR.replace(header, {
	  		selector: '#p-rcb ' + tab + ' h5'
	  		,css: '.sIFR-root {color:#0B5ED7;font-size:20px;}'
	  		,wmode: 'transparent'	
	  	});
	}
}


//----- CUSTOM FUNCTIONS

function removeSWFObject(clipName){
	var flashMovie = getFlashMovieObject(clipName);
	
	var p = flashMovie.parentNode;
	
	swfobject.removeSWF(clipName);
	var d = document.createElement("div");
	d.id = clipName;
	p.appendChild(d);
	
}

function removeFlashObject(clipName){
	var flashMovie = getFlashMovieObject(clipName);
	
	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);
