/* Initialize

------------------------------------------------*/

var __currentHeaderSRC;

var userRequestedTab;





/* DOM Loaded

------------------------------------------------*/



$(document).ready(function() {

	

	// GET QUERYSTRING

	$.getQueryString();

	userRequestedTab = $.jget['tab'];

  for (var i = 0; i < universalTabNames.length; i++) { // find index from universal tab names

    if (universalTabNames[i] == userRequestedTab)

      userRequestedTab = i;

  }

  if (userRequestedTab == $.jget['tab'])

    userRequestedTab = 0;

	//sIFR needs a timeout for safari or sometimes sIFR won't behave correctly

	setTimeout("cp_setTab(" + userRequestedTab + ")", "10");

	

	// CHANGE TABS

	$("#cp_tab_productdetails a").click( function() {
		
		var tab = $('#cp_tab_productdetails li').index( $(this).parent() );
		
		cp_activateTab(tab);
		
		return false;
		
	});
	
	//Make the notepad ready
	cp_changeNotepad('epilation');

});


/* Functions

------------------------------------------------*/

function cp_init_sIFR(currentTab){ // Applies sIFR to elements in current tab if not already applied
	
	if(typeof sIFR != 'undefined'){
	sIFR.replace(header, {
		selector: 'h2.grijs'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#555553;font-size:47px;}'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: 'h2.blue'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#3b5e77;font-size:47px;}'
		,wmode: 'transparent'
	});
	
	// tmp style for 'pink'
	sIFR.replace(header, {
		selector: 'h2.roze'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#ee59e8;font-size:47px;}'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h3.blue'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#3b5e77;font-size:24px;}'
		,wmode: 'transparent'
	});
	
	// tmp style for 'pink'
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h3.roze'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#555553;font-size:30px;}'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h3'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#7b95a7;font-size:30px;}'
		,wmode: 'transparent'
	});
	
	// tmp style for 'pink'
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h4.roze'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#ee59e8;font-size:24px;}'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h4'
		,css: '.sIFR-root {background-color:#FFFFFF;color:#3b5e77;font-size:24px;}'
		,wmode: 'transparent'
	});
	
	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h5.current'
		,css: ['.sIFR-root {background-color:#FFFFFF;color:#555553;font-size:18px;}',
		       'a {text-decoration: none; color: #555553;}',
			   'a:hover {text-decoration: none; color: #ee59e8;}']
		,wmode: 'transparent'
	});

	sIFR.replace(header, {
		selector: '#p-rcb ' + currentTab + ' h5'
		,css: ['.sIFR-root {background-color:#FFFFFF;color:#474747;font-size:15px;}',
		       'a {text-decoration: none; color: #ee59e8;}',
			   'a:hover {text-decoration: none; color: #555553;}',
			   'a.current {color: #555553;}']
		,wmode: 'transparent'
	});

	}
}

/* 

 * Wrapper function for activating a tab.

 */





/* 

 * Wrapper function for activating a tab.

 */

function cp_setTab(value) {

  if (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;

  $('#cp_wrap_tab_productdetails li:eq('+ (value) +') a').click();

  return false;

}



// <TABS LOGIC>

var __currentTabIndex;

/*

 * show a tab and hides others

 *

 * @var integer or string, the index of the availableTabs global or a string which must be present in availableTabs

 */

function cp_activateTab(value) {
	
	$('#p-rcb #cp_wrap_tab_productdetails li').each( function(key) {
		
		// use 'reference number' here instead of 'tab index' to be able to remove item from tabs/content without 'renumbering'
		var tab_number = this.id.substr(this.id.length - 1);
		
		var content_tab = '#cp_tab_content_'+ tab_number;
		
		// campaign links may have no #cp_tab_content
		if(!$(content_tab).length) 
			return true;
		
		if (key == value) {
			
			$('#cp_tab_'+ tab_number).addClass('cp_active');
			
            $('#cp_tab_'+ tab_number).removeClass('cp_inactive');
			
            $(content_tab).show();
			
            // initialise the sIFR within this tab
			
            cp_init_sIFR(content_tab);
			
		}
		else {
			
			$('#cp_tab_'+ tab_number).addClass('cp_inactive');
			
            $('#cp_tab_'+ tab_number).removeClass('cp_active');
			
            $(content_tab).hide();
			
		}
		
	} );
	
	
	
    // remember last set index

    __currentTabIndex = value;



    // Change the header

    cp_setHeader(value);



    // ping statistics

    cp_sendStatistics(value);



}



/*

 * 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;



	// Change placeholder image

	var placeholderSrc = "";

	$("#flashcontent_header").css("background", placeholderSrc);



	// SWFObject 2

	$.testBandwidth(function(status){

    if (status == "medium" || status == "high" || status == "vhigh") {

      var flashvars = {

        bandwidth: status

      };

    	var params = {

        menu: "false",

        wmode: "opaque"

      };

      var attributes = {};
    	swfobject.embedSWF(src, "flash-inner-wrapper", "960", "237", "9.0.0","expressInstall.swf", flashvars, params, attributes);

    }

  });

}





/*

 * DIV POPUP

 */



//function showOverlay(element, div, left, top, text)

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(id){
	centerPopup(id);
	//loads popup only if it is disabled
	if(popupStatus==0){
		/*$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");*/
		$(id).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(id){
	//disables popup only if it is enabled
	//if(popupStatus==1){
		//$("#backgroundPopup").fadeOut("slow");
		$(id).fadeOut("slow");
		popupStatus = 0;
	//}
}

//centering popup
function centerPopup(id){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(id).height();
	var popupWidth = $(id).width();
	//centering
	$(id).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2 + $(window).scrollTop(),
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	/*$("#backgroundPopup").css({
		"height": windowHeight
	});*/
	
}


function showCenteredOverlay(div){
	
	var d = document.getElementById(div);
	
	var divheight = 0;
	var divwidth = 0;
	if(d.offsetHeight) { divheight = d.offsetHeight; }
	else if(d.style.pixelHeight) { divheight = d.style.pixelHeight; }
	
	if(d.offsetWidth) { divwidth = d.offsetWidth; }
	else if(d.style.pixelWidth) { divheight = d.style.pixelWidth; }
	
	
	
	d.style.left = ($(document).width()/2) -  (divwidth/2);
	d.style.top = ($(document).height()/2) -  (divheight/2);
}


//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";
	
	d.style.left = "18px";
	d.style.top = (findPosY(e) - (-top) - divheight-100) + "px"; 
	
	return false;
}



function hideMe(div)

{
	
	var d = document.getElementById(div);

	//d.style.display = 'none';
	disablePopup(d);
}



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;

}



/*

 * 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);

	}



});





/*

 * 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;



}


//
// REPLACE NOTEPAD CONTENT
//

var __currentNotepadIndex;

function cp_changeNotepad(value) {
	
	// Hide all
    for (var i = 0; i < $('#p-rcb #tipsNotepad .notepadText').length; i++) {
	
		var element = $("#p-rcb #tipsNotepad .notepadText:eq(" + i +")");
        var content_link = "#" + element.attr('id');

        var value_link = '#tipsNotepad_content_'+value;

        if (content_link == value_link) {
	
            $(content_link).show();

            cp_init_sIFR(content_link);

        } else {
	
            $(content_link).hide();

        }

    }


    for (var j = 0; j < $('#p-rcb #tipsContentMenu h6').size(); j++) {

		var menu_element = $("#p-rcb #tipsContentMenu h6:eq(" + j +")");
        var menu_id = menu_element.attr('id');
		
		if(value.indexOf(menu_id) >= 0) {
			menu_element.addClass('current');
		} else {
			menu_element.removeClass('current');
		}

	}


    // remember last set index

    __currentNotepadIndex = value;

}
