var current="cp_tab_1";
var akt_seite = '';
var firstTabRandomHeaderRange = new Array(10,10);

function show(A){$("#"+current).css("display","none");$("#"+A).css("display","block");current=A};



function cp_init_sIFR(){
	if(sIFR){
/*
	sIFR.replace(header, {
		selector: '#p-rcb h2'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#DF2823;font-size:18px; }'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: '#p-rcb h3'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#DF2823;font-size:30px}'
		,wmode: 'transparent'
	});
*/

	}
	
	// get variable from location
	
    var userRequestedTab = $.jget['tab'];

    // find index from universal tab names
    for (var i = 0; i < universalTabNames.length; i++) {
        if (universalTabNames[i] == userRequestedTab)
            userRequestedTab = i;
    }

	// needs to wait to make sure sifr has kicked in.
	setTimeout("cp_setTab('"+userRequestedTab+"')", "50");
}
/* 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) {
	
	/* @2009-04-10 added variables for getting country + language, used to call placeholder image*/
	var __COUNTRYFLASH = cp_getMetrics('COUNTRY');
	if(!__COUNTRYFLASH) __COUNTRYFLASH = "gb";
	
	var __LANGUAGEFLASH = cp_getMetrics('LANGUAGE');
	if(!__LANGUAGEFLASH) __LANGUAGEFLASH = "en";
		
	var __COUNTRY = __COUNTRYFLASH.toUpperCase();
	/* end of variables */
	
	if (typeof availableHeadersSRC[value] == 'undefined')
		return false;
	
	var src = availableHeadersSRC[value];
	if (src == __currentHeaderSRC)
		return false;
	__currentHeaderSRC = src;
	
	var banner;
	
    // Random header on the first tab
	if ((value + 1) == 1) {
	    var requestedBanner;
    	requestedBanner = $.jget['banner']; // Check querystring for specific banner request
    	if (requestedBanner == undefined) {
		    if (typeof(firstTabRandomHeaderRange) == "undefined") {
				random = 1;
			} else {
				var minVal = firstTabRandomHeaderRange[0];
				var maxVal = firstTabRandomHeaderRange[1];
				var random = Math.round(minVal + (Math.random() * (maxVal-minVal)));
			}
    		banner = random;
    	} else {
    	    banner = requestedBanner;
    	}
	} else { // Not the first tab
        banner = value + 1;
		$("#flashcontent_header a").remove();
    }

    var placeholderSrc = "url(/consumerfiles/pageitems/locales/" +__LANGUAGEFLASH+"_"+__COUNTRY+"/CONSUMER/categorypages/hair/assets/images/tabAnimation_" + banner +".jpg) no-repeat -2px 0px";
    src = src.replace("&flashvar_tab=1", "&flashvar_tab=" + banner);
    
	// Change placeholder image
	$("#flashcontent_header").css("background", placeholderSrc);
			
	// SWFObject 2
	$.testBandwidth(function(status){
							 $("#flashcontent_header a").remove();
							 
							
							 
	    if (status == "medium" || status == "high" || status =="vhigh") {
    		var flashvars = {
    		    bandwidth : status
    		};
    		var params = {
    			menu : "false",
    			wmode : "transparent"
    		};
    		var attributes = {};
    		swfobject.embedSWF(src, "flash-inner-wrapper", "956", "275", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    	}
		if ((status == "low" || status == "medium") && (value + 1) == 1) {createSmallbandBanner(availableHeadersSRC[value], banner);
		}
    });
}


/*
 * 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;
}


function cp_hideSubDivs() {
	document.getElementById('gracekelly').style.display = "none";
	document.getElementById('baroqueandroll').style.display = "none";
	document.getElementById('thesideeffect').style.display = "none";
	document.getElementById('vavavoom').style.display = "none";
	document.getElementById('seventieslovestory').style.display = "none";
	document.getElementById('img_hair_1').src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/1_n.jpg";
	document.getElementById('img_hair_2').src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/2_n.jpg";
	document.getElementById('img_hair_3').src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/3_n.jpg";
	document.getElementById('img_hair_4').src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/4_n.jpg";
	document.getElementById('img_hair_6').src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/6_n.jpg";
}



var akt_seite = '';
function cp_showHairDiv(name) {
	if (akt_seite != name) {
		if (name=='gracekelly') __bild = 1;
		if (name=='baroqueandroll') __bild = 2;
		if (name=='thesideeffect') __bild = 3;
		if (name=='vavavoom') __bild = 4;
		if (name=='seventieslovestory') __bild = 6;
		akt_seite = name;
		// alle ausblenden:
		cp_hideSubDivs();
		// gewähltes Div einblenden:
		document.getElementById(name).style.display = "block";
		document.getElementById('img_hair_'+__bild).src = "/consumerfiles/pageitems/master/categorypages/hair/assets/images/"+__bild+"_o.jpg";
		// Stats an Content Metrics senden:
		cp_sendHairDivStats(name);
	}
}


function cp_sendHairDivStats(value) {
	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 + '_overview_' + value;
		s.pageName = DIVISION + ":" + SECTION + ":" + TABSECTION + ":" + CATALOGTYPE;
	} else if (PRODUCTCATEGORY != "") {
		var TABSECTION = PRODUCTCATEGORY + '_overview_' + value;
		s.pageName = DIVISION + ":" + SECTION + ":" + TABSECTION + ":" + CATALOGTYPE;
	}
	s.t();
}

// </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();
	
});


/*
 * 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);

/**
 * 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;
  }
  
  function createSmallbandBanner(src, id){var regex = new RegExp("flashvar_xml\=.+\.xml");
var xml_url = regex.exec(src);
if (xml_url != null) {
xml_url = xml_url[0].replace("flashvar_xml=","");
$.ajax({
type: "GET",
url: xml_url,
dataType: "xml",
success: function(xml) {
if ($(xml).find('tab[id=' + id + ']').find("URLpath").length> 0) { // Check if there's an url
link_url = $(xml).find('tab[id=' + id + ']').find("URLpath").text();
$("#flashcontent_header").css("position","relative");
$("#flashcontent_header").append('<a href="' + link_url + '" style="position: absolute; top: 0; left: 0; display: block; width: 800px; height: 275px; z-index: 9999; cursor: pointer;"></a>')
}
}
});
} else { // If the path to the xml isn't found, die silent
return false;
}
}



