// jQuery page load function
$(document).ready(function() {

    $.preloadCssImages();

    if ($(".signinInfoCheckout").length) {
        $(".signinInfoCheckout").corner();
    }
    
    $(".searchBox input").focus(function() {
        $(this).val("");
    });

    $(".prodImgThumb").click(
      function() {
          $(".productMainImg img").hide();
          $(".productMainImg a").hide();
          var $bigImg = $(".productMainImg img[alt='" + $(this).attr("alt") + "']");
          $bigImg.css("display", "block");
          var $bigZoom = $(".productMainImg a[alt='" + $(this).attr("alt") + "']");
          $bigZoom.show();
      }
    );

    if ($("#rotatingImages").length) {
        $("#rotatingImages").pwRotatingImages({
            interval: 3500,
            autoCalcHeight: false
        });
    }

    if ($(".toggleBillingInfo span input").length) {
        $(".toggleBillingInfo span input").click(
        function() {
            var $fields = $(".billinginfofields");
            if ($fields.length == 0)
                $fields = $(".billinginfofieldsHide");
            $fields.toggleClass("billinginfofields");
            $fields.toggleClass("billinginfofieldsHide");
        }
      );
    }

    if( $(".aAddCart").length > 0 ) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndAddToCartChangeRequestHandler);
    }

    if( $(".cartTotal").length > 0 ) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndCartPageChangeRequestHandler);
    }

    if( $(".hasHover").length > 0 )
    {
    	$(".hasHover").hover(function(){
	    var currentImg = $(this).attr("src");
	    //currentImg = currentImg.replace(/\.png/,"_hover.png");
	    $(this).attr("src",addHover(currentImg));
    	},function(){
	    var currentImg = $(this).attr("src");
	    //currentImg = currentImg.replace(/_hover\.png/,".png");
	    $(this).attr("src",removeHover(currentImg));
    	});
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndPageChangeRequestHandler);
    }

    if( $("#howToPaySelection").size() > 0 ) {
	$("#howToPaySelection").hide();
    }

  if( $("#upselldialog").children().length > 0 )
  {
    $("#upselldialog").dialog({ 
        autoOpen: false, 
        modal: true, 
        show: 'blind', 
        width: 400,
        buttons: { "View Cart": function() { window.location = '/cart.aspx'; },
                   "No Thanks": function() { $(this).dialog("close"); }                   
                 }
      });
  }
  
});

function EndAddToCartChangeRequestHandler(sender, args) 
{
    var cartButton = $(".aAddCart");
    var cartBagIconImg = $("#cartBagIconImg");

  $(cartButton).effect("transfer", { to: $(cartBagIconImg) }, 1000, function(){
    if( $("#upselldialog").children().length > 0 )
    {
      $("#upselldialog").dialog('open');
    }    
  });

}

function EndCartPageChangeRequestHandler(sender, args) 
{
	$(".cartTotal").effect("highlight", {}, 2000);
}

function EndPageChangeRequestHandler(sender, args) 
{
    	$(".hasHover").hover(function(){
	    var currentImg = $(this).attr("src");
	    //currentImg = currentImg.replace(/\.png/,"_hover.png");
	    $(this).attr("src",addHover(currentImg));
    	},function(){
	    var currentImg = $(this).attr("src");
	    //currentImg = currentImg.replace(/_hover\.png/,".png");
	    $(this).attr("src",removeHover(currentImg));
    	});
}

function addHover(imgSrc)
{
	var newImageSrc = imgSrc;
	if( !/_hover/.test(newImageSrc) )
	{
	    newImageSrc = newImageSrc.replace(/\.png/,"_hover.png");
	}

	return newImageSrc;
}

function removeHover(imgSrc)
{
	var newImageSrc = imgSrc;
	newImageSrc = newImageSrc.replace(/_hover\.png/,".png");

	return newImageSrc;
}
