$(window).load(function () {
	$("#product-detail .product-img:first").fadeIn(800,function(){
		$("#product-detail .product-images").cycle({
			next:   '.more-images',
			speed: 800,
			timeout: 0
		}).cycle('pause');
	});
});

$(function() {
	//select all the a tag with name equal to modal
	var loc = '#';
	/*var show_modal = $.cookie('show_modal');
	$('a[data-modal], button[data-modal]').click(function(e) {

		if(show_modal != "true") {
			//Cancel the link behavior
			e.preventDefault();
			
			$('body').append('<div id="modal"/>');
			$('body').append('<div id="mask"/>');

			//Get the A tag
			loc = $(this).attr('href');
			var id = "#modal";

			$(id).html('<p>'+$(this).attr('data-modal') + '</p><p><a href="'+loc+'" class="continue">Continue</a></p>');
		 
			//Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
		 
			//Set height and width to mask to fill up the whole screen
			$('#mask').css({'width':maskWidth,'height':maskHeight});
			 
			//transition effect     
			$('#mask').show();  
		 
			//Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();
				   
			//Set the popup window to center
			$(id).css('top',  winH/2-$(id).height()/2);
			$(id).css('left', winW/2-$(id).width()/2);
		 
			//transition effect
			$(id).show();
		}
	});*/

	$('#your-information button[data-modal-canada]').click(function(e) {

		if($('#shipping-country').val() == "CAN") {
			//Cancel the link behavior
			e.preventDefault();
			
			$('body').append('<div id="modal"/>');
			$('body').append('<div id="mask"/>');

			var id = "#modal";

			$(id).html('<p>'+$(this).attr('data-modal-canada') + '</p><p><a href="#" class="continue">Continue</a></p>');
		 
			//Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
		 
			//Set height and width to mask to fill up the whole screen
			$('#mask').css({'width':maskWidth,'height':maskHeight});
			 
			//transition effect     
			$('#mask').show();  
		 
			//Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();
				   
			//Set the popup window to center
			$(id).css('top',  winH/2-$(id).height()/2);
			$(id).css('left', winW/2-$(id).width()/2);
		 
			//transition effect
			$(id).show();
		}
	});
	 
	//if close button is clicked
	if($('body').attr('id') != 'shipping-info') {
		$('#modal .continue').live("click",function (e) {
			//Cancel the link behavior
			e.preventDefault();
			$('#mask, #modal').hide();

				$.cookie('show_modal', 'true', {expires: 1, path: '/', domain: 'unclegoose.com'});

			if($('.buy-form').length) {
				$('.buy-form').submit();
			} else {
				window.location = loc;
			}
		});
	} else {
		$('#shipping-info #modal .continue').live("click",function (e) {
			//Cancel the link behavior
			e.preventDefault();
			$('#mask, #modal').hide();

			if($('#shipping-info #your-information').length) {
				$('#shipping-info #your-information').submit();
			} else {
				window.location = loc;
			}
		});
	}
	 
	//if mask is clicked
	$('#mask').live("click",function () {
		$(this).hide();
		$('#modal').hide();
	});         

	setTimeout("$('#coming-soon').animate({'bottom': '0', 'right': '0'}, 300);", 700);
	
	$('#home #slideshow').cycle({
		delay:    -3000, 
		speed:    1000, 
		timeout:  6000,
		prev:     '#prev',
		next:     '#next'
	});

	$('#products #slideshow').cycle({
		speed:    1000, 
		timeout:  5000,
		pager:    '#products-nav',
		pagerAnchorBuilder: function() {return false;},
		updateActivePagerLink: function(pager, activeIndex) { 
			$(pager).find('li:eq('+activeIndex+')').addClass('selected').siblings().removeClass('selected'); 
		}
	});
	
	if($('#products #slideshow').length) {
	$('#products #products-nav li a').hover(function(){
		var idx = $(this).parent().index();
		$('#products #slideshow').cycle({speed: 0});
		$('#products #slideshow').cycle(idx);
		$('#products #slideshow').cycle('destroy');
	
		$('#products #products-nav li.selected').removeClass('selected');
	},function(){
		var idx = $(this).parent().index();

		$('#products #slideshow').cycle({
			startingSlide: idx,
			speed: 1000,
			pager:    '#products-nav',
			pagerAnchorBuilder: function() {return false;},
			updateActivePagerLink: function(pager, activeIndex) { 
				$(pager).find('li:eq('+activeIndex+')').addClass('selected').siblings().removeClass('selected'); 
			}
		});
		
	});
	}
	
	var w = 0;
	
	$('#more-products .products .product').each(function(){
		w += $(this).outerWidth(true);
	});
	if(w > 0) {
		$('#more-products .products .wrapper').width(w);
		$('#more-products .back a').hide();
	}
	
	if(w < 785) {
		$('#more-products .forward a').hide();
	}

	var int;
	var delay = Math.floor(1000 / 24);
	
	$('#more-products .forward a').hover(function(){
		$('#more-products .back a').show();
		int = setInterval(moveForward,delay);
	},function(){
		clearInterval(int);
	});
	
	$('#more-products .back a').hover(function(){
		$('#more-products .forward a').show();
		int = setInterval(moveBack,delay);
		
	},function(){
		clearInterval(int);
	});
	
	$('#more-products .show-hide').click(function(e){
		e.preventDefault();
		
		$parent = $(this).parent();
		
		if(!$parent.hasClass('open')) {
			$parent.addClass('open');
			$parent.find('.products').animate({'height': '145px'},300);
		} else {
			
			$parent.find('.products').animate({'height': '0px'},300,function(){
				$parent.removeClass('open');
			});
		}
		
	});
	
	// CREATE A NAME
	if($("#block-type").val() == "Lowercase") {
		$("#create-a-name-input").css({"text-transform": "lowercase"});
	} else if($("#block-type").val() == "Uppercase") {
		$("#create-a-name-input").css({"text-transform": "uppercase"});
	}
	
	$("#create-a-name-input").keyup(function(){
		var length = $(this).val().replace(/ /g,'').length;
		var blockPrice = $("#block-price").val();
		var blockWeight = $("#block-weight").val();
		
		$("#create-a-name-price").val(length * blockPrice);
		$("#create-a-name-weight").val(length * blockWeight);
	});
	
	$("#block-type").live("change",function() {
		var type = $(this).val();

		if(type == "Lowercase") {
			$("#create-a-name-input").css({"text-transform": "lowercase"});
		} else if(type == "Uppercase") {
			$("#create-a-name-input").css({"text-transform": "uppercase"});
		}
	});
	
	$("#create-a-name-form").submit(function(){
		var type = $("#block-type").val();
		var val = $("#create-a-name-input").val();
		
		if(type == "Lowercase") {
			$("#create-a-name-input").val(val.toLowerCase());
		} else if(type == "Uppercase") {
			$("#create-a-name-input").val(val.toUpperCase());
		}
		
		var length = $("#create-a-name-input").val().replace(/ /g,'').length;
		var blockPrice = $("#block-price").val();
		var blockWeight = $("#block-weight").val();
		
		$("#create-a-name-price").val(length * blockPrice);
		$("#create-a-name-weight").val(length * blockWeight);
	});
	
	$("form.styled").jqTransform();
	$("#choose-shipping").jqTransform();
	$("#store-locator-form").jqTransform();
	
	$("#store-locator #previous-page").live("click",function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		
		$('#search-results').hide().load(href,function(){
			var latitude = $("#search-results #query-lat").val(),
				longitude = $("#search-results #query-lng").val();

			var pins = new Array();
			$("ol li",this).each(function(){
				var lng = $("a",this).attr("data-longitude");
				var lat = $("a",this).attr("data-latitude");
				var href = $("a",this).attr("href");
				
				pins.push({longitude: lng,latitude: lat, href: href});
			});
			
			if( $.isFunction($.fn.gMap) ){
			$("#map").gMap({
				zoom: 12,
				controls: ['GSmallZoomControl3D'],
				markers: pins,
				icon: { 
					image: 'http://www.unclegoose.com/assets/i/map-pin.png',
					shadow: 'http://www.unclegoose.com/assets/i/map-pin-shadow.png',
					iconsize: [25, 59],
					shadowsize: [41, 59],
					iconanchor: [12, 39]
				}
			});
			}
				
			$(this).slideDown(300);
		});
	});
	
	$("#store-locator #next-page").live("click",function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		
		$('#search-results').hide().load(href,function(){
			var latitude = $("#search-results #query-lat").val(),
				longitude = $("#search-results #query-lng").val();
			
			var pins = new Array();
			$("ol li",this).each(function(){
				var lng = $("a",this).attr("data-longitude");
				var lat = $("a",this).attr("data-latitude");
				var href = $("a",this).attr("href");
				
				pins.push({longitude: lng,latitude: lat, href: href});
			});
			
			if( $.isFunction($.fn.gMap) ){
			$("#map").gMap({
				zoom: 12,
				controls: ['GSmallZoomControl3D'],
				markers: pins,
				icon: { 
					image: 'http://www.unclegoose.com/assets/i/map-pin.png',
					shadow: 'http://www.unclegoose.com/assets/i/map-pin-shadow.png',
					iconsize: [25, 59],
					shadowsize: [41, 59],
					iconanchor: [12, 39]
				}
			});
			}
			
			$(this).slideDown(300);
			
			if($("#store-locator ol li").size() < 6) {
				$("#store-locator #next-page").hide();
			}
			
		});
	});
	
	if( $.isFunction($.fn.gMap) ){
	$("#map").gMap({
		latitude:  37.09024,
		longitude: -95.712891,
		zoom: 3,
		controls: ['GSmallZoomControl3D']
	});
	}

	$("#results ol li a").live("click",function(e){
		
		e.preventDefault();
		
		$('#store-info #address .loading').show();
		var href = $(this).attr('href');
		
		$(this).parent().parent().find('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		
		$("#store-info #address").empty().hide().load(href,function(){
			$(this).slideDown(300);
		});
		
		var longitude = $(this).attr('data-longitude'),
			latitude = $(this).attr('data-latitude');
		
		$("#map").gMap({
			latitude:  latitude,
			longitude: longitude,
			zoom: 12,
			controls: ['GSmallZoomControl3D'],
			markers: [{latitude:  latitude, longitude: longitude}],
			icon: { 
				image: 'http://www.unclegoose.com/assets/i/map-pin.png',
				shadow: 'http://www.unclegoose.com/assets/i/map-pin-shadow.png',
				iconsize: [25, 59],
				shadowsize: [41, 59],
				iconanchor: [12, 39]
			}
		});
	});
	
	$('#address a, #footer .facebook, #retailers-outside li .vcard a').live("click",function(){
		window.open(this.href);
		return false;
	});
	
	$("#retailers-outside li > a").click(function(e){
		e.preventDefault();
		var $parent = $(this).parent().parent(),
			$vcard = $(this).parent().find('.vcard');
		
		if($vcard.is(':visible')) return false;
		$parent.find('.current').removeClass('current');
		
		$(this).addClass("current");
		
		$parent.find('.vcard:visible').slideToggle(300);
		
		$vcard.slideToggle(300);
	});
	
	$("#order-history a.order").click(function(e){
		e.preventDefault();
		var $this = $(this);
		
		if($this.hasClass('open')) {
			$this.find('.order-data').slideUp(250,function(){
				$(this).parent().removeClass("open");
			});
		} else {
			$("#order-history a.order.open .order-data").slideUp(250,function(){
				$(this).parent().removeClass("open");
			});
			
			$this.delay(250).addClass("open");
			$this.find('.order-data').delay(250).slideDown(350);

			$this.find('.order-data').load($this.attr('href'));
		}

	});

});

function moveForward() {
	var w = -($('#more-products .products .wrapper').width()) + 855;
	var l = parseInt($('#more-products .products .wrapper').css('left')) - 6;

	if(l > w) {
		$('#more-products .products .wrapper').css({'left': l+'px'});
	} else {
		$('#more-products .forward a').hide();
	}
	
}
function moveBack() {
	var l = parseInt($('#more-products .products .wrapper').css('left')) + 6;

	if(l <= 50) {
		$('#more-products .products .wrapper').css({'left': l+'px'});
	} else {
		$('#more-products .back a').hide();
	}
}
