$(document).ready(function(){

    
    /* This is for the lightboxes on the deal info page */
    $('.lightboxesNoJS').removeClass('lightboxesNoJS'); //remove class so lightboxes are hidden    
    $('#dealInfoLightboxes').appendTo('body'); //move lightbox elements to directly inside body so that they overlay correctly
    
    //Add to basket lightbox fix
    $('.lightbox4').appendTo('body'); //move lightbox elements to directly inside body so that they overlay correctly
    //lightbox error fix
    $('.lightboxError').appendTo('body'); //move lightbox elements to directly inside body so that they overlay correctly
    //basket update fix
    $('.lightboxUpdatingBasket').appendTo('body'); //move lightbox elements to directly inside body so that they overlay correctly
    
    //addClass to change css on rollovers
	$(".buttonContainer").addClass('js');
	
    $('.buttonContainer a').each(function(){    
        var linkHasFocus = false;

        $(this).hover(function(){
			var $buttonElements = $(this).closest(".buttonContainer").children();			
			jQuery.each($buttonElements, function() {
				if($.browser.msie)
				{
					$(this).css("background-position-y","-28px");
				}
				else
				{
					var position = $(this).css("background-position").replace("px 0px","px -28px");
					$(this).css("background-position",position);
				}
			});
        }, 
        function() {
            if (!linkHasFocus) {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -28px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
            }
        });

	    $(this).focus(function()
		    {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","-56px");
				    }
				    else
				    {
				        var position = $(this).css("background-position").replace("px 0px","px -56px");
					    position = position.replace("px -28px","px -56px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = true;
			    return false;
		    }
	    );
    	
	    $(this).blur(function()
		    {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -56px","px 0px");
					    position = position.replace("px -28px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = false;
		    }
	    );
	    
	    
	    $(this).click(function()
	        {
		        var $buttonElements = $(this).closest(".buttonContainer").children();			
		        jQuery.each($buttonElements, function() {
			        if($.browser.msie)
			        {
				        $(this).css("background-position-y","-56px");
			        }
			        else
			        {
				        var position = $(this).css("background-position").replace("px -28px","px -56px");
				        $(this).css("background-position",position);
			        }
		        });
		        $(this).blur();
	        }
	    );

	
    });
	




	$('#Local').click(function()
	{
		//$('#q').removeClass('fullSearchBar').addClass('halfSearchBarLeft').attr('value','What for');
		//$('#near').removeClass('access').addClass('halfSearchBar').attr('value','Where');
		$('#q').removeClass('fullSearchBar').addClass('halfSearchBarLeft');
		$('#near').removeClass('access').addClass('halfSearchBar');
	});
	
	$('#Web, #UK').click(function()
	{
		//$('#q').addClass('fullSearchBar').removeClass('halfSearchBarLeft').attr('value','');
		//$('#near').addClass('access').removeClass('halfSearchBar').attr('value','');
		$('#q').addClass('fullSearchBar').removeClass('halfSearchBarLeft');
		$('#near').addClass('access').removeClass('halfSearchBar');
	});
	
	$('.promptText').focus(function()
	{
		if( ($(this).val()) == ($(this).attr("title")))
		{
			$(this).css("color","#555555");
			$(this).attr('value','');
		}
	});
	$('.promptText').blur(function()
	{
		if( ($(this).attr('value'))=='')
		{
			$(this).css("color","#cccccc");
			$(this).attr('value',$(this).attr("title"));
		}
	});
	
	
	/*$('#q').focus(function()
	{
		if( ($(this).attr('value'))=='What for')
		{
			$(this).css("color","#555555");
			$(this).attr('value','');
		}
	});
	$('#q').blur(function()
	{
		if( ($(this).attr('value'))=='')
		{
			$(this).css("color","#cccccc");
			$(this).attr('value','What for');
		}
	});
	$('#near').focus(function()
	{
		if( ($(this).attr('value'))=='Where')
		{
			$(this).css("color","#555555");
			$(this).attr('value','');
		}
	});
	$('#near').blur(function()
	{
		if( ($(this).attr('value'))=='')
		{
			$(this).css("color","#cccccc");
			$(this).attr('value','Where');
		}
	});*/
	
	$(".bigbuttonContainer").addClass('js');
	
    $('.bigbuttonContainer a').each(function(){    
        var linkHasFocus = false;

        $(this).hover(function(){
			var $buttonElements = $(this).closest(".bigbuttonContainer").children();			
			jQuery.each($buttonElements, function() {
				if($.browser.msie)
				{
					$(this).css("background-position-y","-50px");
				}
				else
				{
					var position = $(this).css("background-position").replace("px 0px","px -50px");
					$(this).css("background-position",position);
				}
			});
        }, 
        function() {
            if (!linkHasFocus) {
			    var $buttonElements = $(this).closest(".bigbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -50px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
            }
        });

	    $(this).focus(function()
		    {
			    var $buttonElements = $(this).closest(".bigbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","-100px");
				    }
				    else
				    {					    
					    var position = $(this).css("background-position").replace("px 0px","px -100px");
					    position = position.replace("px -50px","px -100px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = true;
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    var $buttonElements = $(this).closest(".bigbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -100px","px 0px");
					    position = position.replace("px -50px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = false;
		    }
	    );
	    
	    
	    $(this).click(function()
	        {
			    var $buttonElements = $(this).closest(".bigbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","-100px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -50px","px -100px");
					    $(this).css("background-position",position);
				    }
			    });
	        }
	    );	
    });
	
	$("input.rollover").hover(
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
			$(this).attr("src",$imageName.replace(".jpg","_roll.jpg"));
		},
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
			$(this).attr("src",$imageName.replace("_roll",""));
		}
	);
	$("input.rollover").focus(
		function(){
			var $imageName = $(this).attr("src");
			$imageName = $imageName.replace("_roll","");
			$(this).attr("src",$imageName.replace(".jpg","_click.jpg"));
			$(this).blur();
		}
	);
	// for Safari only
	$("input.rollover").click(
		function(){
		    if($.browser.safari)
		    {
			var $imageName = $(this).attr("src");
			$imageName = $imageName.replace("_roll","");
			$(this).attr("src",$imageName.replace(".jpg","_click.jpg"));
			}
		}
	);
	
	$("input.rollover").blur(
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
		}
	);
	
    //toggle the display of the header basket
	ToggleHeaderBasket();
	
	$('.lightbox .closeLightbox a').click(function()
	{
	    $('.lightbox').hide();
	    $('.lightbox .lightboxContent .middle').height('auto');
	    $('#overlay').hide();
	    return false;
	});	
	
    $('.addToBasket').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('addToBasketClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('addToBasketClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('addToBasketClick');
	        }
	    );	
    });
    
    $('.checkout_btn').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('checkout_btnClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('checkout_btnClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('checkout_btnClick');
	        }
	    );	
    });
    
    $('.addBtn').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('addBtnClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('addBtnClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('addBtnClick');
	        }
	    );	
    });
    
    $('.checkOutBtn').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('checkOutBtnClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('checkOutBtnClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('checkOutBtnClick');
	        }
	    );	
    });
    
    $('.updateBasket_btn').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('updateBasket_btnClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('updateBasket_btnClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('updateBasket_btnClick');
	        }
	    );	
    });
    
    
    $('.simList').each(function(){
        var $biggerSim = 0;
        $('.sim',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.sim',this).height($biggerSim);
    });
    
    $('.simrow').each(function(){
        var $biggerSim = 0;
        $('.simInfo',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.simInfo',this).height($biggerSim);
    });
    
    $('.dealsRow').each(function(){
        var $biggerSim = 0;
        $('.info',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.info',this).height($biggerSim);
    });
    
    $('.largeTabbedContent').each(function(){
        var $biggerSim = 0;
        $('.info',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.info',this).height($biggerSim);
    });
    
    // Item Detail page lightboxes
	$('.handsetPanel .buttonContainer a').attr("href","javascript:void(0);");
	$('.handsetPanel,.handsetPanel .buttonContainer a').click(function()
    {
       showLightBox('lightbox1',469);
       return false;
    });
	$('.tariffPanel .buttonContainer a').attr("href","javascript:void(0);");
    $('.tariffPanel,.tariffPanel .buttonContainer a').click(function()
    {
       showLightBox('lightbox2',469);
       return false;
    });
	$('.giftPanel .buttonContainer a').attr("href","javascript:void(0);");
    $('.giftPanel,.giftPanel .buttonContainer a').click(function()
    {
       if ($(".giftPanel .nogift").length < 1) {
           showLightBox('lightbox3',469);
           return false;
       }
    });
	
	
	/* calling card scripting */
	
	
	
	
	
	$('#CChomepageCarousel').before('<div class="CCheaderContainer"></div>');	
	
	$('#CChomepageCarousel .header').each(function(){
			$(this).appendTo('.CCheaderContainer');
		});
		
    $('.callchargecalculator').addClass('calculatordisplay');
    
    function DisplayCallRates(selectedValue) 
	    {
	        var landlineRateSelector = '#'+selectedValue+"-L";
	        var mobileRateSelector = '#'+selectedValue+"-M";
	        var ukTextSelector = '#'+selectedValue;
	        
	        $('.landlinerate p:visible').hide();
	        $('.mobilerate p:visible').hide();
	        $('.uktext p:visible').hide();
	        
	        $(landlineRateSelector).show();
	        $(mobileRateSelector).show();
	        $(ukTextSelector).show();
	    }

    $('.landlinerate').children('p').each(
        function(){
            $(this).hide();
        }
    );
    
    $('.mobilerate').children('p').each(
        function(){
            $(this).hide();
        }
    );
    
    $('.uktext').children('p').each(
        function(){
            $(this).hide();
        }
    );
    
    $('.countryselector').change(function() {
        DisplayCallRates($('select.countryselector option:selected').val());
    });
    
    DisplayCallRates($('select.countryselector option:selected').val());

	$('#CChomepageCarousel #homepageCarouselFrames').cycle({ 
		timeout: 0,
		speed: 800,
		
		pause:true,
		fx: 'scrollHorz',
		next:'#homepageCarouselNext',
		prev:'#homepageCarouselPrev',
		easing:'easeInOutExpo',
		pager: '.CCheaderContainer',
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '.CCheaderContainer h2:eq(' + idx + ') a'; 
		}

	});	
	$('.noJS').removeClass('noJS');
	$('#CCtoppanel').cycle({ 
		timeout: 5000,
		speed: 800,
		slideExpr: '.frame'
	});	
	
	$('#CCminiCarousel').addClass('JS');
	
	
	
	$('.seealldestinations').addClass('donotseealldestinations');
	
	
	
});

	
 /**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
	    yScroll = self.pageYOffset;
	    xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
	    yScroll = document.documentElement.scrollTop;
	    xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
	    yScroll = document.body.scrollTop;
	    xScroll = document.body.scrollLeft;	
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
};

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};	

//args - class of lightbox you want to display, width of content in pixels
function showLightBox(lightBoxID,lightboxWidth)
{
        //get height of page and how far down user has scrolled
        lightBoxID = '.'+lightBoxID;
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		$('#overlay').show();
		// Calculate top offset for the lightbox and center it	
		$(lightBoxID).css({
			top:	    arrPageScroll[1] + (arrPageSizes[3] / 10),
			width:      (46 + lightboxWidth),
			marginLeft: -(46 + lightboxWidth)/2
		}).show();
		//set the content width
		$(lightBoxID+' .middle').css({
			width:	lightboxWidth
		});
		//calculate height of content and set borders to this size
		var lightboxHeight = $(lightBoxID+' .lightboxContent .middle').height();
		$(lightBoxID+' .lightboxContent .left').height(lightboxHeight);
		$(lightBoxID+' .lightboxContent .middle').height(lightboxHeight);
		$(lightBoxID+' .lightboxContent .right').height(lightboxHeight);	
}

function hideLightBox(lightBoxID)
{
    var bIsSomeoneElseUsingOverlay = false;
    var currentClass = "";
    
    //hide lightbox
    lightBoxID = '.'+lightBoxID;
    $(lightBoxID).hide();
    
    //PK check to see if another lightbox is using our overlay
    $('.lightbox').each(
        function(e)
        {
            if($(this).is(':visible') && ($(this).attr("class") != $(lightBoxID).attr("class")))
            {
                bIsSomeoneElseUsingOverlay = true;        
                return;
            }
        }
    )
    
    //only hide overlay if no other lightbox is using it
    if(!bIsSomeoneElseUsingOverlay)
    {
	    $('#overlay').hide();
	}
}

function ToggleHeaderBasket()
{
    $('#basketToggle').click(function()
    {
        if($('#basketFooter').hasClass('open'))
        {
            $(this).text('View basket');
        }
        else
        {
            $(this).text('Hide basket');
        }
        $('#basketContent').slideToggle('normal');
        $('#basketFooter').toggleClass('open');
        return false;          
    });
}

function ResetForm()
{
    var formClientID = $('form').attr('id');
    
    document.getElementById(formClientID).reset();
}
	
