$(document).ready(function() {
	$('a[rel*=facebox]').facebox();
	$(document).pngFix({ repeatMethod: "crop" }); 
	
	/* Horizontal List */
	$(".listingDesc").live('mouseover',
		      function () {
				  	$(this).find(".panelPop").css('display','block');
					$(this).parent().css('background','#f1f8fe');
			  }
	);
	
	$(".listingDesc").live('mouseout',
					function () {
		        	$(this).find(".panelPop").css('display','none');
					$(this).parent().css('background','#ffffff');
	});	
	
	$(".expand").click(function(e)
	{
		e.preventDefault();
	
	    var url = $(this).attr('href');        
	    var link = $(this); 
	    
	    if(link.find("label").html() != "Close Products")
	    {
	    	tableFootText = link.find("label").html();
	    	link.find("label").append('<img class="loadergif" id="loader" src="/images/loader.gif" />');
	    	
	    	var options = {
		    	url : url,	
		    	dataType : 'text',
		        success : function(data){link.find("label").html(tableFootText); onShowAllSuccess(data, link);},
		        failure : onShowAllFailure
		    };
		
		    $.ajax(options);
	    }
	    else
	    {
	    	onShowAllSuccess(null, link);
	    }
	});	
	
	
	function onShowAllSuccess(data, link)
	{		
		var hiddenListings = link.parent().parent().prev().find("div.hiddenListings");		

		if( data )
		{			
			hiddenListings.replaceWith(data.toString());
			link.parent().parent().parent().find(".hiddenListings").toggle();
			link.find("span").css('backgroundPosition','0px 6px');
			link.find("label").html("Close Products");	
		}
		else
		{
			link.parent().parent().parent().find(".hiddenListings").toggle();
			link.find("span").css('backgroundPosition','0px 12px');
			link.find("label").html(tableFootText);
		}
		return false;
	}
	
  function onShowAllFailure(data)
  {
  	alert('Sorry there has been an error showing all products');
  }	

  /* Horizontal List End */
	
	jQuery(function($){
	   $("#search").Watermark("Search");
	   $("#inkSearch").Watermark("Search by Brand and Model");
	});
	$("#brandFilter").bind("click", function(e){
			$("ul#brandsList").slideToggle('fast');
		});
	
	$(".current").removeAttr("href").css({'text-decoration' : 'none', "color" : "#000"});
	/* Breadcrumb last-child hack */
	$("ul#breadcrumb li:last").css({'font-weight' : '600', 'background' : 'none'});
	$("ul#breadcrumb li:last a").removeAttr("href").css({'text-decoration' : 'none'});
	/* END Breadcrumb last-child hack */
	
	/* Colour Change*/
	var pageColour = $("ul#breadcrumb li").eq(1).find("a").html();
	var $URL = self.location.href;
	$URL = $URL.split("/");
		if($URL[3] != "")
		{	
			var p = 0;
			if($URL[3] == "office_supplies"){
				$("#header").addClass("yellow");
				$("#content").addClass("yellow");
			}
			else{p++;}
			if($URL[3] == "paper_products"){
				$("#header").addClass("orange");
				$("#content").addClass("orange");
			}else {p++;}
			if($URL[3] == "envelopeFinder"){
				$("#header").addClass("orange");
				$("#content").addClass("orange");
			}else {p++;}
			if($URL[3] == "ink_cartridges_toners"){
				$("#header").addClass("red");
				$("#content").addClass("red");
			}else{p++;}
			if($URL[3] == "office_furniture"){
				$("#header").addClass("pink");
				$("#content").addClass("pink");
			}else{p++;}
			if($URL[3] == "technology"){
				$("#header").addClass("purple");
				$("#content").addClass("purple");
			}else{p++;}
			if($URL[3] == "facilities"){
				$("#header").addClass("blue");
				$("#content").addClass("blue");
			}else{p++;}
			if($URL[3] == "specialoffers"){
				$("#header").addClass("green");
				$("#content").addClass("green");
			}else{p++;}
			if (p==8)
			{
				$("#header").addClass("grey");
				$("#content").addClass("grey");
			}
		}
		else
		{
			$("#header").addClass("grey");
			$("#content").addClass("grey");
		}
		/* END Colour Change*/
	
	$(".buttonFade").hover(
      function () {
        $(this).stop().animate({ opacity: 0.7 }, 100);
      }, 
      function () {
        $(this).stop().animate({ opacity: 1 }, 100);
      }
    );

	$(".productList").find(".featuredBox:last").css('border-right', 'none'); /* Remove last border from featured */
	//$(".productList").find(".featuredBox").eq(3).css('border-right-color', '#fefefe'); /* Remove last border from featured */
	$("ul#quickLinks li:last-child").css({'background': 'none', 'paddingLeft' : '0'}); /* Remove last bullet from quicklinks */

	$(".jump").bind("click", function(e){
			var name = "#"+$(this).attr("name");
			$('html, body').animate({scrollTop:$(name).offset().top}, 'slow');
		});
						  
	//$("ul.dropdown li:last").css("border-right","none");							  
								  
								  
	/* Stop  links scrolling to top */
	$("a").bind("click", function(e){
  		//e.preventDefault(); /* prevents default behavior */
	});

	$('#topButton').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
	$('.login').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	/* END Stop  links scrolling to top */
	

	$('.next1').attr('disabled', false);
	$('.prev1').attr('disabled', true);
	$(".prev1").stop().animate({ opacity: 0.2 }, 200);
	//alert($("#categoryBrand > li").size()/5);
	$slideTo = Math.ceil($("#categoryBrand > li").size()/5)-1;
	if ($slideTo==0){
		$('.next1').attr('disabled', true);
		$(".next1").stop().animate({ opacity: 0.2 }, 200);
	}
	var z2 = 0;
	$('.next1').click(function(){
		$(".prev1").stop().animate({ opacity: 1 }, 200);
		z2++;
		$('.prev1').attr('disabled', false);
		var pos = parseInt($('ul#categoryBrand').css('left'));
		$(this).attr('disabled', true);
		$('ul#categoryBrand').animate({ left: pos-730 + "px" }, 500 );
		if(z2 != $slideTo){
			setTimeout(reEnable,500);}
			else{$(".next1").stop().animate({ opacity: 0.2 }, 200);}
		function reEnable()
		{
			$(".next1").stop().animate({ opacity: 1 }, 200);
			$('.next1').attr('disabled', false);
		}
	});
	
	$('.prev1').click(function(){
		$(".next1").stop().animate({ opacity: 1 }, 200);
		z2--;
		$('.next1').attr('disabled', false);
		var pos = parseInt($('ul#categoryBrand').css('left'));
		$(this).attr('disabled', true); 
		$('ul#categoryBrand').animate({ left: parseInt(pos+730) + "px" }, 500 );
		if(z2 > 0){
			setTimeout(reEnable2,500);}
			else{$(".prev1").stop().animate({ opacity: 0.2 }, 200);}
		function reEnable2()
		{
			$(".prev1").stop().animate({ opacity: 1 }, 200);
			$('.prev1').attr('disabled', false);
		}
	});
	 
	/* Product Page rollover */
	var ID; /* This must be the proouct ID */
	var originalSrc;
	var src;

	$(".rollover").mouseover(function() {
			originalSrc = $(".changeMe").attr("src").match(/[^\.]+/) + ".gif";
			src = $(this).attr("src").match(/[^\.]+/) + "-image-"+ID+".gif";
			$(".changeMe").attr("src", src);
		})
		.mouseout(function() {
			$(".changeMe").attr("src", originalSrc);
	});
		
	/* END Product Page rollover */
	
	/* Product Page Quantity Boxes */
	$(".addArea").find(".add").click(function()
	{
		var currentVal = parseInt($(this).parent().find(".qty").val());
		if (currentVal != NaN)
		{
			$(this).parent().find(".qty").val(currentVal + 1);
		}
		else
		{
			$(this).parent().find(".qty").val(1);
		}
	});

	$(".addArea").find(".minus").click(function()
	{
		var currentVal = parseInt($(this).parent().find(".qty").val());
		if (currentVal != NaN && currentVal > 1)
		{
			$(this).parent().find(".qty").val(currentVal - 1);
		}
	});
	/* END Product Page Quantity Boxes */
	
	$('.tabs').tabs(); /* Initialise Tabs */
	
	/* Slide Login Panel */
	/*$(".login").click(function()
	{
		$("#loginContainer").slideToggle('slow');
		$("#loginPanel").slideToggle('slow');
	});*/
	/* END Login Panel */
	
	/* Homepage Fades */
	$(".fade").each(function() {
			$(this).find(".fadeMe").stop().animate({ opacity: 0.5 }, 200);
            $(this).hover(function() {
                $(this).find(".fadeMe").stop().animate({ opacity: 1.0 }, 200);
            },
           function() {
               $(this).find(".fadeMe").stop().animate({ opacity: 0.5 }, 200);
           });
    });
	/* END Homepage Fades */
	
	/* Homepage Featured Items Panel Slider */
	function formatText(index, panel) {
	  return index + "";
	}
	
	/* Initialise slider panel IF it exists on the page */
	if ( $(".panelSlider").length > 0 ) {
		$('body').append("<script type='text/javascript' src='scripts/jquery.anythingslider.js'></script>" + "<script type='text/javascript' src='scripts/jquery.easing.1.2.js'></script>");
		$(function () {
			$('.panelSlider').anythingSlider({
				easing: "swing",        		// Anything other than "linear" or "swing" requires the easing plugin
				autoPlay: true,                 		// This turns off the entire FUNCTIONALY, not just if it starts running or not.
				delay: 7000,                    		// How long between slide transitions in AutoPlay mode
				startStopped: false,            		// If autoPlay is on, this can force it to start stopped
				animationTime: 900,             		// How long the slide transition takes
				hashTags: true,                 		// Should links change the hashtag in the URL?
				buildNavigation: true,          		// If true, builds and list of anchor links to link to each slide
				pauseOnHover: true,             		// If true, and autoPlay is enabled, the show will pause on hover
				startText: "Go",             			// Start text
				stopText: "Stop",               		// Stop text
				navigationFormatter: formatText      	// Details at the top of the file on this use (advanced use)
			});
			$("#slide-jump").click(function(){
				$('.panelSlider').anythingSlider(6);
			});
		});
	};
	/* END Homepage Featured Items Panel Slider */
	
	/* Product list sorting options */
	$('#productListForm select').bind('change', onSubmitProductList); 	
    
    function onSubmitProductList(e)
    {
		e.preventDefault();
		jQuery('#productListForm').submit();     
    }

	
	
	/* END Product list sorting options */
	
});