/*
 * Thickbox 2.1.2 - jQuery plugin for displaying content in a box above the page
 * 
 * Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// on page load call TB_init. All other items must render and load.
$(window).load(TB_init);
var isTbLayerActive=false;
var clickToChatIframeLayerDisplayed=false;
var SWAP_LAYER_IDE="";
// add thickbox to href elements that have a class of .thickbox
function TB_init(){
	log.info("thickbox","****************************\n"+
		"Thickbox (Modified Version) loaded. Version 2.1.2");
	var EnvokedOnload = false;
	$("a.popUp").click(function(event){
		log.info("thickbox","Adding thickbox overlay item. ");
		// stop default behaviour
		event.preventDefault();
		// remove click border
		this.blur();
		// get caption: either title or name attribute
		var caption = this.title || this.name || "";
		// get rel attribute for image groups
		var group = this.rel || false;
		// display the box for the elements href
		TB_show(caption, this.href, group);
	});
	
	$("a.popUp[displayOnLoad=true]").each(
		function(event){
			if(!EnvokedOnload){
				TB_show("", this.href, false);
				EnvokedOnload=true;
				log.info("thickbox:a.popUp[@displayOnLoad=true]","Init: a.popUp[@displayOnLoad=true]");
			}
	});
	$("div.welcome-obj[display-on-load=true]").each(
		function(event){
			if(!EnvokedOnload){
				page_showWelcome(this.id);
				EnvokedOnload=true;
			}
	});
	$("div.cart-obj[display-on-load=true]").each(
		function(event){
			if(!EnvokedOnload){
				page_showCart(this.id);
				EnvokedOnload=true;
			}
	});
  $("#shop_global_nav_click2chat").bind('click', function(event) {
       log.info("thickbox","Adding thickbox overlay item. ");
       event.preventDefault();
       this.blur();
       var caption=this.title||this.name||"";
       var group=this.rel||false;
       TB_show(caption,this.href,group,false);
  });
   $("#ec_hsi_click2chat").bind('click', function(event) {
       log.info("thickbox","Adding thickbox overlay item. ");
       event.preventDefault();
       this.blur();
       var caption=this.title||this.name||"";
       var group=this.rel||false;
       TB_show(caption,this.href,group,false);
  });
  
}


function TB_showId(id){
	//var surl = "#TB_inline?height=400&width=450&inlineId="+id;
	//var url = (document.getElementById(id)) ? document.getElementById(id).href : surl;
	var closeHidden=false;
	if(document.getElementById(id).href.indexOf("hideClose=true")!=-1){
		closeHidden=true;
	}
	TB_show('', document.getElementById(id).href, false,closeHidden);
}
function TB_showOverlay(id, width, height, noClose){
	if(!isTbLayerActive){
		log.info("TB_showOverlay","Calling TB_showOverlay("+id+", "+width+", "+height+", "+noClose+")");
		try{TB_SwapOut();} catch (e){}
		SWAP_LAYER_IDE = id;
		var myWidth = (width)?width:400;
		var myHeight = (height)?height:200;
		var url = "#TB_inline?height="+myHeight+"&width="+myWidth+"&inlineId="+id;
		TB_show('', url, false, noClose);
	} else {
		log.warn("TB_showOverlay()","Cannot call Calling TB_showOverlay("+id+", "+width+", "+height+", "+noClose+")");
	}
}
function TB_SwapOut(){
	if($("#TB_window") && SWAP_LAYER_IDE!="" && !isCustomTbLayerActive){
		log.info("TB_SwapOut()","TB_SwapOut() \n > Copy TB_ajaxContent back into "+SWAP_LAYER_IDE);
		$('#' + SWAP_LAYER_IDE).html($("#TB_ajaxContent").html());
		$("#TB_ajaxContent").html("");
		log.info("TB_SwapOut()Delete TB_ajaxContent"," > Delete TB_ajaxContent content");
		$("#TB_window").html("");
		log.info("TB_SwapOut()Delete TB_window"," > Delete TB_window content");
	}
}
function TB_show(caption, url, rel, noClose) 
{
	if(!isCustomTbLayerActive)
	{
		isTbLayerActive=true;
		if(isIE6) page_hideSelectLists();
		setFlashObjectsVisibility("hidden");
		if ( !$("#TB_HideSelect").length ) 
		{
			$("#footerBase").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		}
		$(window).scroll(TB_position);
		TB_overlaySize();
		$("#footerBase").append("<div id='TB_load'><img src='/qcms/qCmsRepository/Global/Dots/images/loadingAnimation.gif' /></div>");
		TB_load_position();
		
		// check if a query string is involved
		//try{url = (url.indexOf("?")!=-1) url.substring(0,url.indexOf("?")): url;}catch(e){}
		var baseURL = url.match(/(.+)?/)[1] || url;
		try
		{
			var url = (url.indexOf("#")!=-1)? url.substring(url.indexOf("#")): url;
			//log.warn("thickboxUrlE","IE Thickbox.url.trim: "+url);
		} catch(e){log.error("thickboxUrlE","Thickbox.url.trim"+e.message);}
	
		var queryString = url.match(/\?(.+)/)[1];
		var params = TB_parseQuery( queryString );
		
		TB_WIDTH = (params['width']*1) + 16;
		TB_HEIGHT = (params['height']*1) + 40;
    var ajaxContentW = TB_WIDTH - 16,
        ajaxContentH = TB_HEIGHT - 45;
		
		// this was a modification/add-in from /global/dots/js (GLOBAL_MAIN) version of thickbox.js for new shop rebranding 
	if((url.indexOf('TB_iframe') != -1 && url.indexOf('clicktochatma.html') != -1)||(url.indexOf('TB_iframe') != -1 && url.indexOf('clicktochat.html') != -1))
    { 
      // on the phoneServicesShop page there are multiple jquery libs loaded firing the onClick multiple times thus the if logic below
      if(!clickToChatIframeLayerDisplayed)
      {
        if(url.indexOf('setAnchor') != -1 )
        {
          url = url.substring(22);
        }
        urlNoQuery = url.split('TB_');
        parentPage = '';
          $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a onclick='javaScript:void(TB_remove());' href='javaScript:void(TB_remove());' id='TB_closeWindowButton'>Close Window</a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+(ajaxContentW + 16)+"px;height:"+(ajaxContentH + 17)+"px;' onload='TB_showIframe()' allowtransparency='true'" + parentPage + "> </iframe>");
        TB_position();
        clickToChatIframeLayerDisplayed=true;
      }
    }
    else
    {
  		if(!noClose){
  			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='javaScript:void(TB_remove());'  id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
  			$("#TB_closeWindowButton").click(TB_remove);
  		} else {
  			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'>&nbsp;</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
  		}
  		if(url.indexOf('TB_inline') != -1){	
  			SWAP_LAYER_IDE = params['inlineId'];
  			$("#TB_ajaxContent").html($('#' + SWAP_LAYER_IDE).html());
  			$('#' + SWAP_LAYER_IDE).html("");
  			
  			TB_position();
  			$("#TB_load").remove();
  			$("#TB_window").css({display:"block"}); 
  		}
  		else 
  		{
  			$("#TB_ajaxContent").load(url, function(){
  				TB_position();
  				$("#TB_load").remove();
  				$("#TB_window").css({display:"block"}); 
  			});
  		}
  	}
		$(window).resize(TB_position);
		$(window).resize(TB_overlaySize);
	} 
	else 
	{
		alert("Another layer is already open. \nYou are getting this alert because \n" +
		"you should not call TB_showLayer while you have page_showCart \nor page_showWelcome open!");
	}
}

function TB_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function TB_remove() {
	isTbLayerActive=false;
	setFlashObjectsVisibility("visible");
	if(isIE6) page_showSelectLists();
	try{
	 	$("#TB_imageOff").unbind("click");
		//$("#TB_overlay").unbind("click");
		$("#TB_closeWindowButton").unbind("click");
		if(!clickToChatIframeLayerDisplayed)
    {		
		  $('#' + SWAP_LAYER_IDE).html($("#TB_ajaxContent").html());
    }
		$("#TB_ajaxContent").html("");
		$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
		$("#TB_load").remove();
		SWAP_LAYER_IDE="";
		clickToChatIframeLayerDisplayed=false;
	} catch (e)
	{
	 //alert("Exception thrown = " + e);
	}
	return false;
}

function TB_position() {
	var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();
	var style = {width: TB_WIDTH, left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)};
	$("#TB_window").css(style);
}

function TB_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	if(document.getElementById('LoadingPage_finishProfile')){
		yScroll=2300;
		xScroll=2300;
	}
	if(document.getElementById('scopeBundles')){
            if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){  
                yScroll=7000;
            }
        }
	$("#TB_overlay").css({"height": yScroll, "width": xScroll});
	try{
		$("#TB_HideSelect").css({"height": yScroll,"width": xScroll});
	} catch(e){}
}

function TB_load_position() {
	var pagesize = TB_getPageSize();
	var arrayPageScroll = TB_getPageScrollTop();
	$("#TB_load")
		.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2), top: (arrayPageScroll[1] + ((pagesize[1]-100)/2)) })
		.css({display:"block"});
}

function TB_parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};
	
	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}

function TB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}


/*************************************/
/*             NEW CODE              */
/*************************************/
// The following is compatible with LivePerson 
// and is customized for only two layers.
// To override turning off a button use:
// $(cartbtn).hide(); or $(welcbtn).hide();
var ACTIVE_LAYER_ID="welcomeE";
var SWAP_LAYER_ID="";
var ACTIVE_LAYER_WIDTH=650;
var ACTIVE_LAYER_HEIGHT=300;
var cartbtn = "#lp-chat-residential-care-english2";
var welcbtn = "#lp-chat-residential-care-english3";
var isCustomTbLayerActive=false;
function page_showCart(id, w, h){
	if(!isTbLayerActive){
		try{page_swapOut("cartInnerContent",SWAP_LAYER_ID);} catch (e){}
		w=(w)?w:650; h=(h)?h:300;
		if(!isTbLayerActive){
			log.info("thickbox"," > Show Cart: page_showCart("+id+", "+w+", "+h+")");
			SWAP_LAYER_ID = id;
			page_swapOut(id,"cartInnerContent");
			$("#cartInnerContent").css({width: w-20, height: h-45});
			showLayer("cartE", w, h);
		} else {
			alert("Another layer is already open. \nYou are getting this alert because \n" +
			"you should not call page_showCart while you have TB showLayer open!");
		}
	}
}
function page_showWelcome(id, w, h){
	if(!isTbLayerActive){
		try{page_swapOut("welcomeInnerContent",SWAP_LAYER_ID);} catch (e){}
		w=(w)?w:650; h=(h)?h:300;
		if(!isTbLayerActive){
			log.info("thickbox"," > Show Welcome Object: page_showWelcome("+id+", "+w+", "+h+")");
			SWAP_LAYER_ID = id;
			page_swapOut(id,"welcomeInnerContent");
			$("#welcomeInnerContent").css({width: w-20, height: h-45});
			showLayer("welcomeE", w, h);
		} else {
			alert("Another layer is already open. \nYou are getting this alert because \n" +
			"you should not call page_showCart while you have TB showLayer open!");
		}
	}
}
function page_swapOut(id, swapId){
	if(id!="" && swapId!=""){
		if(!isCustomTbLayerActive) {
			var tempHTML = $('#'+id).html(); 
			$('#'+id).html("");
			if(isIE6) page_hideSelectLists();
			setFlashObjectsVisibility("hidden");
			$("#"+swapId).append(tempHTML);
			log.info("thickboxswap"," > > Swap in. Copy '"+id+"' into '"+swapId+"'");
		} else {
			var tempHTML = $('#'+id).html();
			$('#'+id).html("");
			if(isIE6) page_showSelectLists();
			setFlashObjectsVisibility("visible");
			$("#"+swapId).append(tempHTML);
			log.info("thickboxswap"," > > Swap out. Copy '"+id+"' into '"+swapId+"'");
		}
	}
}
function showLayer(id, w, h){
	isCustomTbLayerActive=true;
	try{
		ACTIVE_LAYER_ID=id;
		ACTIVE_LAYER_WIDTH=(w)?w:650;
		ACTIVE_LAYER_HEIGHT=(h)?h:300;
		setPopUpPosition();
		$("#"+ACTIVE_LAYER_ID).fadeIn();
		$(window).scroll(setPopUpPosition);
		$(window).resize(setPopUpPosition);
		if(!obj('TB_overlay')){
			$("#footerBase").append("<div id='TB_overlay' style='z-index:100;'></div>");
		}
		TB_overlaySize();
		$(window).resize(TB_overlaySize);
	} catch (e){
		alert(e);
	}
}	
function page_hideWelcomeLayer(){
	page_swapOut("welcomeInnerContent",SWAP_LAYER_ID);
	page_hideLayer();
}
function page_hideCartLayer(){
	page_swapOut("cartInnerContent",SWAP_LAYER_ID);
	page_hideLayer();
}
function page_hideLayer(hideOverlay){
	isCustomTbLayerActive=false;
	log.info("thickbox"," > Hide Layer: page_hideLayer()\n > > ID: "+ACTIVE_LAYER_ID);
	try{$("#"+ACTIVE_LAYER_ID).css({display:"none"});} catch (e){}
	$("#TB_overlay").remove();
	$(cartbtn).show();
	$(welcbtn).show();
	SWAP_LAYER_ID="";
}
function setPopUpPosition() {
	try{
		var pagesize = TB_getPageSize();	
		var arrayPageScroll = TB_getPageScrollTop();
		var style = {
			width: ACTIVE_LAYER_WIDTH, 
			height: ACTIVE_LAYER_HEIGHT, 
			left: (arrayPageScroll[0] + (pagesize[0] - ACTIVE_LAYER_WIDTH)/2), 
			top: (arrayPageScroll[1] + (pagesize[1]-ACTIVE_LAYER_HEIGHT)/2)};
		$("#"+ACTIVE_LAYER_ID).css(style);
	} catch (e){alert(e.message);}
}
function obj(id){
	return document.getElementById(id);
}

/* Overloaded method to show some message on title bar - Intake-377*/

function TB_showOverlay_cpni(id, width, height, noClose,msg,mRight,onClickCode,cpni_bgImage){
	if(!isTbLayerActive){
		log.info("TB_showOverlay_withTitle","Calling TB_showOverlay("+id+", "+width+", "+height+", "+noClose+")");
		try{TB_SwapOut();} catch (e){}
		SWAP_LAYER_IDE = id;
		var myWidth = (width)?width:400;
		var myHeight = (height)?height:200;
		var url = "#TB_inline?height="+myHeight+"&width="+myWidth+"&inlineId="+id;
		TB_show_cpni('', url, false, noClose,msg,mRight,onClickCode,cpni_bgImage);
	} else {
		log.warn("TB_showOverlay()","Cannot call Calling TB_showOverlay("+id+", "+width+", "+height+", "+noClose+")");
	}
}

function TB_show_cpni(caption, url, rel, noClose,msg,mRight,onClickCode,cpni_bgImage) {
	if(!isCustomTbLayerActive){
		isTbLayerActive=true;
		if(isIE6) page_hideSelectLists();
		setFlashObjectsVisibility("hidden");
		if ( !$("#TB_HideSelect").length ) {
			$("#footerBase").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		}
		$(window).scroll(TB_position);
		TB_overlaySize_cpni();
		$("#footerBase").append("<div id='TB_load'><img src='/qcms/qCmsRepository/Global/Dots/images/loadingAnimation.gif' /></div>");
		TB_load_position();
		
		// check if a query string is involved
		//try{url = (url.indexOf("?")!=-1) url.substring(0,url.indexOf("?")): url;}catch(e){}
		var baseURL = url.match(/(.+)?/)[1] || url;
		try{
			var url = (url.indexOf("#")!=-1)? url.substring(url.indexOf("#")): url;
			//log.warn("thickboxUrlE","IE Thickbox.url.trim: "+url);
		} catch(e){log.error("thickboxUrlE","Thickbox.url.trim"+e.message);}
	
		var queryString = url.match(/\?(.+)/)[1];
		var params = TB_parseQuery( queryString );
		
		TB_WIDTH = (params['width']*1) + 16;
		TB_HEIGHT = (params['height']*1) + 40;
	
		var ajaxContentW = TB_WIDTH - 16,
			ajaxContentH = TB_HEIGHT - 45;
		if(!noClose){
			$("#TB_window").append("<div id='TB_title'><label style='font-size:1.5em;font-weight:bold;margin-right:"+mRight+"px'>"+msg+"</label><label onmouseover='style.cursor=\"pointer\"' onclick='"+onClickCode+"' id='TB_closeWindowButton' style='font-size:.9em;color:#FFFFFF;margin-right:15px'><u>close</u></label></div><div id='TB_ajaxContent' style='background-image: url("+cpni_bgImage+");width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
			$("#TB_closeWindowButton").click(TB_remove);
		} else {
			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'>&nbsp;</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
		}
		if(url.indexOf('TB_inline') != -1){	
			SWAP_LAYER_IDE = params['inlineId'];
			$("#TB_ajaxContent").html($('#' + SWAP_LAYER_IDE).html());
			$('#' + SWAP_LAYER_IDE).html("");
			
			TB_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
		} else {
			$("#TB_ajaxContent").load(url, function(){
				TB_position();
				$("#TB_load").remove();
				$("#TB_window").css({display:"block"}); 
			});
		}
		$(window).resize(TB_position);
		$(window).resize(TB_overlaySize);
	} else {
		alert("Another layer is already open. \nYou are getting this alert because \n" +
		"you should not call TB_showLayer while you have page_showCart \nor page_showWelcome open!");
	}
}

function TB_overlaySize_cpni(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	yScroll += 400;
	xScroll += 300;
	$("#TB_overlay").css({"height": yScroll, "width": xScroll});
	try{
		$("#TB_HideSelect").css({"height": yScroll,"width": xScroll});
	} catch(e){}
}

