// JScript File
$(document).ready(function()
{

    //hide drop down menus on doc ready
	$('ul.submenu').hide();	
	
 /* ------------------------------------------- 
	Navigation Drop Downs
    ------------------------------------------- */	
	
	//add actions for hover actions
	$('#navigation > li').hover(function()
	{
		$(this).find('ul.submenu').show();
	},function()
	{
		$('ul.submenu').hide();
	});	

 /* ------------------------------------------- 
	Show or hide searchbox on home page
    ------------------------------------------- */

    $('div#searchbox-top').click(function() {
		$('div#searchbox-submenu').toggleClass('display-block', 1000);
		return false;
	});
	
 /* ------------------------------------------- 
	Alignment Corrections on orderlines.aspx
    ------------------------------------------- */
    $('td input.addbottle').addClass('btn-orderlines')
	
 /* ------------------------------------------- 
	Gridview paging
    ------------------------------------------- */
    $('table.gvsearchresults').find('tr:first').removeClass('gvpaging-bottom').addClass('gvpaging-top');
		
 /* ------------------------------------------- 
	For search and other input boxes
    Thanks to: http://www.electrictoolbox.com/jquery-change-default-value-on-focus/
    ------------------------------------------- */
		
    $('.default-value').each(function() {
    var default_value = this.value;
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            this.value = default_value;
        }
      });
    }); 
    
 /* ------------------------------------------- 
	Rollovers 
    ------------------------------------------- */
    
    $("input.sl-link").hover(
        function()
        {
            this.src = this.src.replace("_off","_on");
        },
        function()
        {
            this.src = this.src.replace("_on","_off");
        }
    );
    
 /* ------------------------------------------- 
	Product Scroller on Home page
    ------------------------------------------- */
    
        $("img.scrollleft").hover(
        function()
        {
            this.src = this.src.replace("_off","_on");
        },
        function()
        {
            this.src = this.src.replace("_on","_off");
        }
    );
    
        $("img.scrollright").hover(
        function()
        {
            this.src = this.src.replace("_off","_on");
        },
        function()
        {
            this.src = this.src.replace("_on","_off");
        }
    );
    
 /* ------------------------------------------- 
	Fine Wine Search Bar
    ------------------------------------------- */
    $('#searchbox-top:contains(FINE WINE SEARCH)').addClass('finewinesearch');
    $('div.searchbox:contains(FINE WINE SEARCH)').find('input.finewine').attr({src:"images/btn-search-finewine.png"});
  
/* ------------------------------------------- 
	Set the left margin to stop text wrapping on search results
    ------------------------------------------- */
    
    /* Search Results page*/
    $('div.product').each(function() {
      
      var imagewidth; //width of the image
      var product = $(this);
      imagewidth =  product.find('.product-image').width();  //get the width of the input and set the width variable
      //alert(imagewidth);
      
      imagewidth+= 10;
      
      //set the left hand margin of the product details to be that of the imagewidth
      product.find('.product-details').css('margin-left',imagewidth);    
    });
    
    /* Search Results Drilled in page*/
    $('div.chosenproduct').each(function() {
      
      var imagewidth; //width of the image
      var product = $(this);
      imagewidth =  product.find('.product-image').width();  //get the width of the input and set the width variable
      //alert(imagewidth);
      
      imagewidth+= 20;
      
      //set the left hand margin of the product details to be that of the imagewidth
      product.find('.product-details').css('margin-left',imagewidth);    
    });
 

 /* ------------------------------------------- 
	Product Scroller
    ------------------------------------------- */
    var shift = 137; //value to move
		
		var gvWidth = 0; //set the carousel width to 0
		
		// loop and get total width of carousel based on items within
		$("table.gvproductscroller span.ps-title").each(function(i) {
			gvWidth += $(this).outerWidth(true) + 4; //non me lo spiego il 4
			//alert(gvWidth);
		});
		
		//set the with of the carousel to be our new calculated width
		$("table.gvproductscroller").css("width", gvWidth);
		
		//if the right button is clicked
		$("#ps-right").click(function() {
		   //alert('right button clicked');
		    
		    //get the left position of the carousel minus one 'shift' value
		    var leftPos = $("table.gvproductscroller").position().left;
		    //alert('Left Position is: ' + leftPos);
			var newPos = $("table.gvproductscroller").position().left - shift;
			//alert('newPos is:' + newPos);
			
			//if this value + carousel width < carousel container width then do...
			var gvProdScrollWidth = 0;
			gvProdScrollWidth += $("table.gvproductscroller").width();
			//alert('gvTableScroller width is: ' + gvProdScrollWidth);
			
			if ($("table.gvproductscroller").width() + newPos < $("#ps-products").width()) {
			       //alert('we are here now');
			    //set newPos = carousel container width - #carousel width
				newPos = $("#ps-products").width() - $("table.gvproductscroller").width();
			}
			$("table.gvproductscroller").animate({ left:newPos+"px"},{duration: 'slow', easing: 'easeOutBack'});	
		});
		
		$("#ps-left").click(function() {
			var newPos = $("table.gvproductscroller").position().left + shift;
			if (newPos > 0) newPos = 0;
			$("table.gvproductscroller").animate({ left:newPos+"px"},{ duration: 'slow',easing: 'easeOutBack'});			  
		});
   
});

