$(document).ready( function() { //When the document is ready
	

  /* ------------------------- Open external links in new windows - */
  
  var external = $('a[rel="external"]'); //Get all links with rel="external" and place them into the 'external'
	
  external.attr("title", external.attr("title") + " - Link opens in a new window"); //Take the links current title and append it with the external link message
  
  external.click( function() { //When an external link is clicked
  
    window.open( $(this).attr('href') ); //Open its href in a new window
  
    return false; //Cancel the links default behaviour	
  
  }); //End function
  
  
  /* --------------- Sliding Filter - */

	$('#accordionFilter li').children('ul').hide();
	$('#accordionFilter li li').children('ul').show();
	$('#accordionFilter').children('li').addClass('closed').children('h3').toggle(
			function(){
				$(this).parent('li').removeClass('closed');
				$(this).parent('li').children('ul').slideDown();
			},
			function(){
				$(this).parent('li').addClass('closed');
				$(this).parent('li').children('ul').slideUp();
			}			
		);




  /* ------------------------- sIFR (replace text elements on screen with Flash equivalents) - */
  
  $(function () {
	
    $.sifr({path:'/contents/flash/fonts'}); //Where swf are saved, path is relative to the page's location
		
	$('#terrificToolsNav h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
    $('.expandableBoxContainer h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
  
    $('.preFooterItemInner h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
		$('.alertPanel h3').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
		$('.alertPanel p.pointsTotal strong').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
		$('#internalIntro h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
		$('.sifr').sifr({font:'HouschkaMedium'}); //Replace text with flash
		
	$('.eStoresSpotlight .spotlightItemInner h2').sifr({font:'HouschkaMedium'}); //Replace text with flash	
		
    //$('caption').sifr({font:'HouschkaMedium'}); //Replace text with flash
    
    // These are headings within our custom boxes
    $('body:not(.search) one_column h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
    $('.two_column_first h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
    $('.two_column_last h2').sifr({font:'HouschkaMedium'}); //Replace text with flash
 
  }); //End function

  
  /* ------------------------- Toggle expandable boxes - */

  //$(".toggle h2").append("<img src=\"/contents/images/site/icnToggleArrow.png\" alt=\"\" />"); //Append arrow to heading
  
  //$(".toggle caption span").append("<img src=\"/contents/images/site/icnToggleArrow.png\" alt=\"\" />"); //Append arrow to heading
  
  //$(".toggle h2 img").mousedown(function () {

    //if($(this).parents('h2').siblings('.expandableBox').is(":hidden")){ //If box is hidden
     
      //$(this).parents('h2').siblings('.expandableBox').slideDown("fast"); //Slide down
  
      //} else { //Box is visible
    
      //$(this).parents('h2').siblings('.expandableBox').slideUp("fast"); //Slide up
    
    //}
  
  //}); //End function
  
  //$(".toggle caption span img").mousedown(function () {

    //if($(this).parents('caption').siblings('tbody').is(":hidden")){ //If box is hidden
     
      //$(this).parents('caption').siblings('tbody').slideDown("fast"); //Slide down
  
      //} else { //Box is visible
    
      //$(this).parents('caption').siblings('tbody').slideUp("fast"); //Slide up
    
    //}
  
  //}); //End function  



	// If Pretty Photo is included, attempt to instantiate lightbox effect
	if('function' == typeof $.fn.prettyPhoto){
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal',
			padding: 40,
			opacity: 0.35,
			showTitle: false,
			allowresize: true,
			theme: 'light_square'
		});
	}
	
	
	
	
	
	
	
	

// -- Accordion function for 'By category listing page' --


// -- Initialise --

	$('.headingExpansion').text("Shop online categories list (click to expand)");
	$('.headingExpansionRetailers').text("Shop online retailers list (click to expand)");

	$("ul.accordion li h4:odd").addClass('goldStripe');


	$('ul.innerAccordionList').hide();
	$('ul.accordion li h4 a').css({'background-image': 'url("/contents/images/site/accordion/accordionExpand.gif")'});




// -- Actual Accordion movement function --

	$('ul.accordion li h4 a').click( function(){
		
		
		if($(this).parents('h4').siblings('ul').is(":hidden")){
			
			$('ul.innerAccordionList').slideUp('fast');
			$('ul.accordion li h4').removeClass('activeItem');
			$('ul.accordion li h4 a').css({'background-image': 'url("/contents/images/site/accordion/accordionExpand.gif")'})
			
			$(this).parent('h4').addClass('activeItem');
			$(this).css({'background-image': 'url("/contents/images/site/accordion/accordionCollapse.gif")'})
			$(this).parents('h4').siblings('ul').slideDown('slow');
		
		} else {

			$(this).parent('h4').removeClass('activeItem');			
			$(this).css({'background-image': 'url("/contents/images/site/accordion/accordionExpand.gif")'})
			$(this).parents('h4').siblings('ul').slideUp('fast');					
			
		}
		
				
		return false;
		
	})

/* ---------------View search results in Gallery view or List view - */

	$('.viewGallery').click( function(){
		$('#resultsview').removeClass('list');
		$('#resultsview').addClass('gallery');		
		return false;
	});
	
	$('.viewList').click( function(){
		$('#resultsview').removeClass('gallery');
		$('#resultsview').addClass('list');		
		return false;
		
	});
	
	
	$('.thickbox').attr('href','offersReminder.html?&height=487&width=715');	
	$('.thickbox').addClass('offset');
	$('.thickbox').click();
	
	
	// tooltip init
	
	$('.help').click( function(){
		return false;
	});
	
	
	$('.help').show();
	$('.tip').hide();
	tooltip();
	
	
	
	
	
	
	
	
	

	
	
	
}); //End Document Ready


/* -- General Tooltip device (give any a tag a class of 'tooltip' and put a 'div class="tip"' containing the tooltip content next to the a tag) - */

		var xOffset = 10;
		var yOffset = 20;
		var pageWidth;
		var pageX;
		var pageY;
		var e;

this.tooltip = function(){
		
		
	$("a.tooltip").hover(function(e){
		
		pageWidth = $('body').width();
		yOffset = 20;
		if((e.pageX + 300) >= pageWidth){
			yOffset = yOffset - 300;
		}
		
		$(this).attr('title','');
		var tipContent = $(this).siblings('.tip').html();										  								  
		$("body").append("<div id='tooltip'>"+ tipContent +"</div>");
		
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
 
    },	function(){	
		
		$("#tooltip").remove();
		
    });	
	
	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	
				
};

		
	//Check for Safari - (Call isSaf() and check if true/false wherever needed)
    function isSaf() {
        var isSaf = (document.childNodes && !document.all && !navigator.taintEnabled && !navigator.accentColorName) ? true : false;
        return isSaf;
    }



// }); //End document.ready

