$(function() {
	//$(".disableClick").click(function() {
	//	$(this).attr("disabled", "disabled");
	//});
	$.ajaxSetup ({
	    // Disable caching of AJAX responses and 10 second timeout
	    cache: false, timeout: 10000
	});
	
	// Go through all queue configurations and load them in
	$(".queueConfiguration").each(function()
	{
		ajaxQueue($(this), $(this).metadata().action);
	});
	
   $('.slider').anythingSlider({
   
       startStopped    : true,
       
       onInitialized: function(e, slider) {
           slider.$currentPage.find('img').each(function(){
           var newsrc = $(this).attr('fimage');
           $(this).attr('src', newsrc);
		   });
		   
		   // Do next
		   slider.$currentPage.next().find('img').each(function(){
			   var newsrc = $(this).attr('fimage');
			   $(this).attr('src', newsrc);
		   });
		   
		   // Do last
		   slider.$currentPage.parent().children(':last').find('img').each(function(){
			   var newsrc = $(this).attr('fimage');
			   $(this).attr('src', newsrc);
		   });
       },
       
	   onSlideInit: function(e, slider) {
	       slider.$targetPage.find('img').each(function(){
	           if ( ($(this).attr('src') === 'https://images.jewelry.com/spacer.gif') || ($(this).attr('src') === ''))  {
	           var newsrc = $(this).attr('fimage');
	           $(this).attr('src', newsrc);
	           }
	       });
		   
		   // Do next
		   slider.$targetPage.next().find('img').each(function(){
			   if ( ($(this).attr('src') === 'https://images.jewelry.com/spacer.gif') || ($(this).attr('src') === ''))  {
	           var newsrc = $(this).attr('fimage');
	           $(this).attr('src', newsrc);
	           }
		   });
		   
		   slider.$targetPage.prev().find('img').each(function(){
			   if ( ($(this).attr('src') === 'https://images.jewelry.com/spacer.gif') || ($(this).attr('src') === ''))  {
	           var newsrc = $(this).attr('fimage');
	           $(this).attr('src', newsrc);
	           }
		   });
	   },
       onSlideBegin: function(e, slider) {
    	   //Google Analytics event code
    	   //var index = slider.runTimes;
    	   _gaq.push(['_trackEvent', 'JComArrow', 'Clicked', 'HomePageSlider']);
       }       
   });
	
	$("#promoCode").keypress(function(e)
	{
		if(e.which == 13)
		{
			e.preventDefault();
			$(this).parents('form:first').prepend('<input type="hidden" name="recalculate" value="" />');
			$(this).parents('form:first').submit();

			return false;
        }
    });
	
	$('#shippingSame').click(function() {
		fixShippingSame();
	});
	
	$(".recalculate").click(function() {
		// Lets change the posted action before it continues
		var thisForm = $(this).parents('form:first');
		
		// Change the action
		thisForm.attr("action", "RecalculateShoppingCart.action");
		
		return true;
	});
	
	fixShippingSame();
	
	if ($("#submenu").length > 0) {
		dropdown.init({
			mainmenuid: "submenu", //menu DIV id
			orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
			classname: 'dropdown', //class added to menu's outer DIV
			//customtheme: ["#1c5a80", "#18374a"],
			contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
		});
	}
	
	jQuery('.jcarousel-skin-jcom').jcarousel();

	// random slideshow
	$('.randomSlideShow').slideShow({
		interval : 11,
		start : 'random'
	});
	
	var options = {
		zoomWidth : 370,
		zoomHeight : 320,
		position : 'left',
		yOffset : 0,
		xOffset : 5,
		title : false
	};
	// $(".jqzoom").jqzoom(options);
	
	imagePreview();
	
    $('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',                              //class of the element that will be your tab
        pathToTabImage: 'images/signupTAB.png',          //path to the image for the tab (optionaly can be set using css)
        imageHeight: '120px',                               //height of tab image
        imageWidth: '75px',                               //width of tab image    
        tabLocation: 'left',                               //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                                        //speed of animation
        action: 'click',                                   //options: 'click' or 'hover', action to trigger animation
        topPos: '200px',                                   //position from the top
        fixedPosition: false                               //options: true makes it stick(fixed position) on scroll
    });
	$('.slide-out-div').show();
	
	
});

function imagePreview() {
	/* CONFIG */

	xOffset = 150;
	yOffset = 30;

	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.preview").unbind("hover").hover(
			function(e) {
				this.t = this.title;
				this.title = "";
				var c = (this.t != "") ? "<br/>" + this.t : "";
				$("body").append(
						"<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"
								+ c + "</p>");
				$("#preview").css("position", "absolute").css("top",
						(e.pageY - xOffset) + "px").css("left",
						(e.pageX + yOffset) + "px").fadeIn("fast");
			}, function() {
				this.title = this.t;
				$("#preview").remove();
			});
	$("a.preview").unbind("mousemove").mousemove(
			function(e) {
				$("#preview").css("top", (e.pageY - xOffset) + "px").css(
						"left", (e.pageX + yOffset) + "px");
			});
};

jQuery(window).load(function() { //jQuery(window).load() must be used instead of jQuery(document).ready() because of Webkit compatibility				
	// Slideshow > Standard
	jQuery(".homepage_slideshow").sliderkit({
		autospeed : 5000,
		circular : true,
		fastchange : false
	});
});

function ajaxQueue(div, url)
{
	var jqxhr = $.post(url, function(data) 
	{
    	div.html(data);
    })
    .error(function() { ajaxQueue(div, url) });
}

function fixShippingSame()
{
	if($('#shippingSame:checked').val() != null)
	{
		// Move objects over from billing to shipping
		$('#shippingAddress\\.address1').val($('#billingAddress\\.address1').val());
		$('#shippingAddress\\.address2').val($('#billingAddress\\.address2').val());
		$('#shippingAddress\\.city').val($('#billingAddress\\.city').val());
		$('#shippingAddress\\.state').val($('#billingAddress\\.state').val());
		$('#shippingAddress\\.zip').val($('#billingAddress\\.zip').val());
		
		$('#shippingAddress\\.address1').attr('readonly', 'true');
		$('#shippingAddress\\.address1').css("background-color", "#eee");
		$('#shippingAddress\\.address1').css("color", "#777");
		$('#shippingAddress\\.address2').attr('readonly', 'true');
		$('#shippingAddress\\.address2').css("background-color", "#eee");
		$('#shippingAddress\\.address2').css("color", "#777");
		$('#shippingAddress\\.city').attr('readonly', 'true');
		$('#shippingAddress\\.city').css("background-color", "#eee");
		$('#shippingAddress\\.city').css("color", "#777");
		$('#shippingAddress\\.state').attr('readonly', 'true');
		$('#shippingAddress\\.state').css("background-color", "#eee");
		$('#shippingAddress\\.state').css("color", "#777");
		$('#shippingAddress\\.zip').attr('readonly', 'true');
		$('#shippingAddress\\.zip').css("background-color", "#eee");
		$('#shippingAddress\\.zip').css("color", "#777");
		
		$('#billingAddress\\.address1').keyup(function() {
			$('#shippingAddress\\.address1').removeAttr('readonly');
			$('#shippingAddress\\.address1').val($('#billingAddress\\.address1').val());
			$('#shippingAddress\\.address1').attr('readonly', 'true');
		});
		
		$('#billingAddress\\.address2').keyup(function() {
			$('#shippingAddress\\.address2').removeAttr('readonly');
			$('#shippingAddress\\.address2').val($('#billingAddress\\.address2').val());
			$('#shippingAddress\\.address2').attr('readonly', 'true');
		});
		
		$('#billingAddress\\.city').keyup(function() {
			$('#shippingAddress\\.city').removeAttr('readonly');
			$('#shippingAddress\\.city').val($('#billingAddress\\.city').val());
			$('#shippingAddress\\.city').attr('readonly', 'true');
		});
		
		$('#billingAddress\\.state').change(function() {
			$('#shippingAddress\\.state').removeAttr('readonly');
			$('#shippingAddress\\.state').val($('#billingAddress\\.state').val());
			$('#shippingAddress\\.state').attr('readonly', 'true');
		});
		
		$('#billingAddress\\.zip').keyup(function() {
			$('#shippingAddress\\.zip').removeAttr('readonly');
			$('#shippingAddress\\.zip').val($('#billingAddress\\.zip').val());
			$('#shippingAddress\\.zip').attr('readonly', 'true');
		});
	}
	else
	{
		$('#shippingAddress\\.address1').removeAttr('readonly');
		$('#shippingAddress\\.address2').removeAttr('readonly');
		$('#shippingAddress\\.city').removeAttr('readonly');
		$('#shippingAddress\\.state').removeAttr('readonly');
		$('#shippingAddress\\.zip').removeAttr('readonly');
		
		$('#shippingAddress\\.address1').removeAttr('style');
		$('#shippingAddress\\.address2').removeAttr('style');
		$('#shippingAddress\\.city').removeAttr('style');
		$('#shippingAddress\\.state').removeAttr('style');
		$('#shippingAddress\\.zip').removeAttr('style');
		
		$('#billingAddress\\.address1').unbind("keyup");
		$('#billingAddress\\.address2').unbind("keyup");
		$('#billingAddress\\.city').unbind("keyup");
		$('#billingAddress\\.state').unbind("change");
		$('#billingAddress\\.zip').unbind("keyup");
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = name+"="+value+expires+"; path=/";
	} else {
		var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	
	return null;
}

function deleteCookie(name) {
	document.cookie = name+"=null;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
}
