var cp_currentFlashHeader;
var cp_currentFlashHeaderId;

function cp_start() {
	var cp_tabs = [
   		{id:'cp_tab1',name:'overview'},
   		{id:'cp_tab2',name:'unbelievable_sound_quality'},
   		{id:'cp_tab3',name:'superior_design'},
   		{id:'cp_tab4',name:'product_range'}
   	];
	TabManager.init('#cp_tabmenu',cp_tabs);
}


/* Phones
------------------------------------------------*/
var activePhone = 1;
$("ul.phones a.phone, a.greenLink").each(function(){
	$(this).attr("href","javascript:;");
});
$("ul.phones li.inactive a.phone").fadeTo("fast", 0.5);

var phoneSliding = false;
$("ul.phones a.phone").hover(
	function(){
		if($(this).attr('rel').replace("item","")!=activePhone){
			$(this).fadeTo("fast", 1);
		}
		var selectedPhone = $(this).attr('rel').replace("item","");
		if(selectedPhone!=activePhone && !phoneSliding){
			phoneSliding = true;
			var maxWidth = $(this).parent().width()+280;
			var minWidth = $("ul.phones li.item"+activePhone).width()-280;
			$("ul.phones li.item"+activePhone+" a.phone").fadeTo("fast", 0.5);
			$("ul.phones li.item"+activePhone+" .description").hide();
			$("ul.phones li.item"+activePhone).animate({width: minWidth+"px", duration:400},"easeout",function(){phoneSliding = false;});
			$("ul.phones li.item"+selectedPhone).animate({width: maxWidth+"px", duration:400 },"easeout");
			$("ul.phones li.item"+selectedPhone+" a.phone").fadeTo("fast", 1);
			$("ul.phones li.item"+selectedPhone+" .description").show();
			activePhone = selectedPhone;
		}
	},
	function(){
		if($(this).attr('rel').replace("item","")!=activePhone){
			$(this).fadeTo("fast", 0.5);
		}
	}
);

$("ul.phones  a.phone").click(function(){
	
});

$(document).ready(function() {
	/* Saving Energy 
	------------------------------------------------*/
	var greenActive = false;
	if(typeof savingEnergyButtonText == "undefined"){ /* Default Green read more text */
	    savingEnergyButtonText = new Array(
		    'Learn more',
		    'Close ECO mode'
		);
	}
	
	$("a.greenLink").click(function(){
		if(!greenActive){
			$(".tab1_green").slideDown("slow");
						if(typeof sIFR!='undefined'){ 
			sIFR.replace(header, {
		  		selector: '#p-rcb .tab1_green h2'
		  		,css: '.sIFR-root {color:#005c1f;font-size:36px}'
		  		,wmode: 'transparent'
		  	});
			}
		  	$(this).html(savingEnergyButtonText[1]);
			greenActive = true;
		}else{
			$(".tab1_green").slideUp("slow");
			$(this).html(savingEnergyButtonText[0]);
			greenActive = false;
		}
	});
	
	//sIFR.prefetch(header);
	if(typeof sIFR !='undefined')  sIFR.activate(sifr_gillsans);
	cp_init_sIFR();
	setTimeout("cp_start()", 500);

	cp_coverflow();
	/*(loadVideosXML();*/
	
	$("#phones_productrange").productRange();
});

/*
 * 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);
}

/*
 * Changes the navbar bg
 */

// set bg on navbar
function cp_setBgOnNavBar(value){
	var val = value +1;
	
	var bg = "url(/consumerfiles/pageitems/master/categorypages/landlinephones/assets_update_v2/images/tab"+val+"_bg.jpg)";
	$("#ul_tabsmenu").css("background",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", "314", "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(sifr_gillsans, {
		  		selector: '#p-rcb ' + tab + ' h2.green'
			,css: '.sIFR-root {color:#005c1f;font-size:36px}'
			,wmode: 'transparent'
		});
		sIFR.replace(sifr_gillsans, {
			selector: '#p-rcb ' + tab + ' h2'
			,css: '.sIFR-root {color:#ffffff;font-size:36px}'
			,wmode: 'transparent'
		});
		
		sIFR.replace(sifr_gillsans, {
			selector: '#p-rcb ' + tab + ' h3.green'
			,css: '.sIFR-root {background-color:#FFFFFF;color:#005c1f;font-size:24px;}'
			,wmode: 'transparent'
		});
		sIFR.replace(sifr_gillsans, {
			selector: '#p-rcb ' + tab + ' h3'
			,css: '.sIFR-root {background-color:#FFFFFF;color:#fff;font-size:24px;}'
			,wmode: 'transparent'
		});
		
		
		sIFR.replace(sifr_gillsans, {
			selector: '#p-rcb ' + tab + ' .title'
			,css: '.sIFR-root {background-color:#FFFFFF;color:#ffffff;font-size:30px}'
			,wmode: 'transparent'
			,filters: {
			Glow: {
			knockout:false
			,strength: 1.5
			,quality: 3
			,color: '#138796'
			,distance: 4
			}
			}
		});
	}
}


//----- 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);
} 	
}

function cp_coverflow(target){ 
    if (target == undefined) target = 0;
    $("#cp_tab_content_6.flash .block").hide();
    $("#cp_tab_content_6.flash .block:eq(" + target + ")").show();
    cp_init_sIFR("#cp_tab_content_6");
    return false;
}

/*
 * 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);


function cp_closeOverlayHeader(){
	$("#tabAnimation").html("");
	$("#tabAnimation").append("<div id=\"flash-overlay\"></div>");
	$("#tabAnimation").hide();
}




/** DIV POPUP **/
//function showOverlay(element, div, left, top, text)

function showOverlay(element, div, left, top)
{
	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;
}



