﻿$(document).ready(function(){

		//page height
		//pageHeight()
		
		///checkout blackout
		$('#checkout-btn').click(function() {
		var mask = "<div class='blackout'></div>";
		var container = "<div class='processing'></div>";
		var details = "<div class='processing-grad'><h2>Processing Order...</h2><h3 style='color:red;'>Please do not refresh this page.</h3><br/><img src='/images/common/duncan-thinking.gif'/><div class='clear'></div></div>";
		
		$(mask).prependTo('body');
		$(container).appendTo('.blackout');
		$(details).appendTo('.processing');
		
		
		});
		
		///enter key form submit
		$('form').bind('keypress', function(e) {
		
        if(e.keyCode==13)
        { 
        var id = $(this).attr("id");
		var selector = "#" + id;
      	
      	$(""+selector+"").submit();
        }
		});
	
		//product lightbox
		$("a#single_image").fancybox({'overlayColor':'#000000','overlayOpacity':0.6,'transitionIn':'none', 'transitionOut':'none'});
		$("a.group").fancybox({'overlayColor':'#000000','overlayOpacity': 0.6,'transitionIn':'none', 'transitionOut':'none'});	
		$("a#inline").fancybox({'overlayColor':'#000000','overlayOpacity':0.6,'transitionIn':'none', 'transitionOut':'none'});
		
		///Splash and Frontpage Slideshow
		$("#slides").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 7000, true)
		
		$("#slides img").hover(  
		function() { 
		//alert("hover!") 
		$("#slides").tabs("rotate",0,true);  
		},  
		function() {  
		$("#slides").tabs("rotate",7000,true);  
		}  
		); 
	
		///Most Popular Carousel
		$('#most-popular').jcarousel({
		wrap: 'circular',
		slideEasing: "easeOutBounce",
		dispItems: 1
		});
		
		///Most Popular Carousel
		$('#other-products').jcarousel({
		wrap: 'circular',
		slideEasing: "easeOutBounce",
		dispItems: 1
		});
		
		//correct navigation
		$("#secnav #sec a:last").css("border-bottom","none");
		
		//list grid tabs
		$("#listgrid ul.tabs li").click(function() {
		
		$(".caption").hide();
		
		
		var tab = $(this).attr("class");
		var data = 'method=tab&d='+ tab;
		
		$.ajax({  type: "POST",  url: "/index.php/site/tabUpdate",  data: data }); 
	
		var cookieOptions = {expires: 7, path: '/'};
		$.cookie('selected', tab, cookieOptions);
			   
		$("#listgrid ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
		});
		
		
		///Selected Tab
		var selected_tab = "list";
		
		if(!$.cookie('selected'))
		{
		    var selected_tab = "list";
		} else {
			var selected_tab = $.cookie('selected');
		}
		
		var tab = "." +selected_tab;
		
		$(".tab_content").hide(); //Hide all content
		
		$(""+ tab +"").addClass("active"); //Activate first tab
		
		if(selected_tab =="list"){ $(".tab_content:first").show();}
		if(selected_tab =="grid"){ $(".tab_content:last").show();}
		
		
		///Filter sorting
		$("#sortby li a").click(function() {
			
		var sortby = $(this).attr("id");
		var category = $("#main-category").val();
		var subcategory = $("#subcategory").val();
		var offset = $("#offset").val();
		var perpage = $("#numpage").val();
		
		var data = 'sortby='+ sortby +'&c=' + category +"&s="+ subcategory;
		
		$.ajax({  type: "POST",  url: "/index.php/site/sortByUpdate",  data: data, 
		success: function(response)
		{ 
			$("#items").html(response);
			
			var selected_tab = $.cookie('selected');
			var tab = "." +selected_tab;
			
			$(".tab_content").hide();
			
			if(selected_tab =="list"){ $(".tab_content:first").show(); $(".star_rating").stars();}
			if(selected_tab =="grid"){ $(".tab_content:last").show(); $(".star_rating").stars();}
			
		} });
		
		});
		
		///News Tab Box Tabs
		$("#news .tab_content").hide(); //Hide all content
		$("#news ul.tabs li:first").addClass("active").show(); //Activate first tab
		$("#news .tab_content:first").show(); //Show first tab content
		
		//On Click Event
		$("#news ul.tabs").tabs("select","tab2");
		$("#news ul.tabs li").click(function() {
		$("#news ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#news .tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
		});
		
		//Star Rating
		$(".star_rating").stars();
		
		///star rating other/most popular hover
		$('#mostpop li').hover(
		function () {
		$(this).find("div .caption").fadeTo("fast", 1.00);
		},
		function () {
		$(this).find("div .caption").fadeTo("fast", 0.00);
		});
		
		///star rating products hover
		$('#grid-products li').hover(
		function () {
		$(this).find("div .caption").fadeTo("fast", 1.00);
		},
		function () {
		$(this).find("div .caption").fadeTo("fast", 0.00);
		});
		
		///star rating recent hover
		$('#recent-products li').hover(
		function () {
		$(this).find("div .caption").fadeTo("fast", 1.00);
		},
		function () {
		$(this).find("div .caption").fadeTo("fast", 0.00);
		});
		
		///Filter Num Per Page
		$('#numpage').change(function() { $('#ppg').submit(); });
		
		///Clone pagination
		var pagination = $('#pagination').html();
		$('.pageclone').html(pagination);
		
		
		///search click
		$('#search-activate').click(function() {
		$('#login-slide').slideUp("fast");
		$('#search-slide').slideDown("slow");
		});
		
		$('#search-close').click(function() {
		$('#search-slide').slideUp("slow");
		});
		
		///login click
		$('#login-activate').click(function() {
		$('#search-slide').slideUp("fast");
		$('#login-slide').slideDown("slow");
		});
		
		$('#login-close').click(function() {
		$('#login-slide').slideUp("slow");
		});
		

		//Product Thumbnail Switch
		$(".alt-images a").hover(
		function () {
		var largePath = $(this).attr("ret");
		$("#largeImage").attr({ src: largePath });
		
		return false;
		},
		function () {
		var largePath = $(this).attr("ret");
		var ltBoxPath = $(this).attr("href");
		
		//update links to new image path
		$("a.main").attr({ href: ltBoxPath });
		$("#largeImage").attr({ src: largePath });
		return false;
		});
		
		//Crew Thumbnail Switch
		$("li .crew-images a").hover(
		function () {
		var largePath = $(this).attr("ret");
		var source = $(this).attr("id")
		$("#large-"+ source).attr({ src: largePath });
		return false;
		},
		function () {
		var source = $(this).attr("id")
		var largePath = $(this).attr("ret");
		$("#"+source+ " #largeImage").attr({ src: largePath });
		return false;
		});
		
		
		///product tabbox
		$("#product-tabs .tab_content").hide(); //Hide all content
		$("#product-tabs ul.tabs li:first").addClass("active").show(); //Activate first tab
		$("#product-tabs .tab_content:first").show(); //Show first tab content
		
		//On Click Event
		$("#product-tabs ul.tabs").tabs("select","tab1");
		$("#product-tabs ul.tabs li").click(function() {
		$("#product-tabs ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#product-tabs .tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
		});
	
	///////////////////////////////////Cart Jquery//////////////////////////////////////////////////
	
	
		///Hide estimate button
		$("#estimate-button").hide();
		$("#shipping-choices").hide();
	
		///Zipcode ajax to set county
		$('#shipping-zip').blur(function() {
  			
  			var zipcode = $("#shipping-zip").val();
  			var data = "zipcode=" + zipcode; 
  			
  				$.ajax({  
				type: "POST",
				async: false,
				dataType: "html",
				url: "/index.php/site/ajax_county",  
				data: data,  
				success: function(response)
				{	
					if(response.length == 2)
					{
					$("#estimate-button").slideDown("fast");
					} else {
					$("#shipping-counties").html(response);	
					}
				
				}
				});
		});
		
		
		///Estimate Shipping Cost
		$('#estimate_shipping').click(function() {
		  
		  $("#loading").html("<img src='/images/common/duncan-thinking.gif'/>");
		  
		  var country = $("#shipping-country").val();
		  var county = $("#shipping-county").val();
		  var zipcode = $("#shipping-zip").val();
		  var city = $("#shipping-city").val();
		  var state = $("#shipping-state").val();
		  var shippingweight = $("#shipping-weight").val();
		  var data = "country="+ country +"&county="+ county +"&zipcode="+ zipcode +"&city="+ city +"&state="+ state +"&weight="+ shippingweight
			
				$.ajax({  
				type: "POST",  
				url: "/index.php/site/ajax_shipping",  
				data: data,  
				success: function(response)
				{
					$("#loading").html("");
					$("#shipping-choices").html(response);
					$("#shipping-choices").slideDown("fast");
				}
				});
		
		});
		
		///more countries
		$('#more-countries').click(function() {
		 	$('#con-countries').hide();
		 	$('#all-countries').show();
		 	$('#con-countries select').attr('name','con-country-code');
		 	$('#all-countries select').attr('name','country-code');
		 	$('#more').html("<a id='less-countries' href=''>Less</a>");
		});
		
		///less countries
		$('#less-countries').click(function() {
		 	$('#all-countries').hide();
		 	$('#con-countries').show();
		 	$('#con-countries select').attr('name','country-code');
		 	$('#all-countries select').attr('name','all-country-code');
		 	$('#more').html("<a id='more-countries' href=''>More</a>");
		});
		
		///Canada Provinces
		$('#country-code').change(function() {
			
			var country = $(this).val();
		
			if(country == "CA")
			{
			$('#provinces').slideDown('slow');
			} else {
			$('#province-code').val('');
			$('#provinces').slideUp('slow');
			}
			
		});
		
		///Set Shipping Carrier and Cost
		$('#shipping-rates').change(function() {
		$('#estimatorform').attr('action','/index.php/site/updateShipping');
		$('#estimatorform').submit();
		});
	
		///Update Cart Contents
		$('#update-basket').click(function() {
		var parameter_string = '';
		allNodes = $('.process');
		
		for(i = 0; i < allNodes.length; i++) {
		var tempid = allNodes[i].id;
		var temp = new Array;
		temp = tempid.split("_");
		var real_id = temp[2];
		var real_value = allNodes[i].value;
		parameter_string += real_id +':'+real_value+',';
		}
		var data = 'ids='+parameter_string;
		
		$.ajax({  
		type: "POST",  
		url: "/index.php/site/ajax_cart",  
		data: data,  
		success: function(response){  
		location.reload(); 
		}});
			
	});
	
	/////////////////Billing Shipping Jquery////////////////
	
	$('#clone-to-shipping').click(function() {
	
	var bfname = $("#b-fname").val()
	var blname = $("#b-lname").val()
	var bcname = $("#b-cname").val()
	var baddr1 = $("#b-addr1").val()
	var baddr2 = $("#b-addr2").val()
	var bcity = $("#b-city").val()
	var bstate = $("#b-state").val()
	var bzipcode = $("#b-zip").val()
	var bcountry= $("#b-country").val()
	var bphone = $("#b-phone").val()

  	$("#s-fname").val(bfname);
	$("#s-lname").val(blname);
	$("#s-cname").val(bcname);
	$("#s-addr1").val(baddr1);
	$("#s-addr2").val(baddr2);
	$("#s-city").val(bcity);
	$("#s-state").val(bstate)
	$("#s-zip").val(bzipcode);
	$("#s-country").val(bcountry);
	$("#s-phone").val(bphone);
	
});

////slide toggle title field
$('#s-save').click(function() {$('#s-label').slideToggle();});
$('#b-save').click(function() { $('#b-label').slideToggle(); });

////use a saved address
$('#saved-addresses li a').click(function() {

		var type = $(this).attr("class");
        var address = $(this).attr("atr");
        
        var addressArray = address.split(':');
		
		if(type == "bill")
		{
			$("#b-fname").val(addressArray[0]);
			$("#b-lname").val(addressArray[1]);
			$("#b-cname").val(addressArray[2]);
			$("#b-addr1").val(addressArray[3]);
			$("#b-city").val(addressArray[4]);
			$("#b-state").val(addressArray[5])
			$("#b-zip").val(addressArray[6]);
			$("#b-country").val(addressArray[7]);
			$("#b-phone").val(addressArray[8]);

		} else {
			$("#s-fname").val(addressArray[0]);
			$("#s-lname").val(addressArray[1]);
			$("#s-cname").val(addressArray[2]);
			$("#s-addr1").val(addressArray[3]);
			$("#s-city").val(addressArray[4]);
			$("#s-state").val(addressArray[5])
			$("#s-zip").val(addressArray[6]);
			$("#s-country").val(addressArray[7]);
			$("#s-phone").val(addressArray[8]);
		}
		
return false;
});

////paypal notification
$("#card-type").change(function() {

	var cardType = $(this).val();
	
	if(cardType =="Paypal")
	{
		$('#card-no').hide();
		$('#exp-date').hide();
		$('#cv-code').hide();
	} else {
		$('#card-no').show();
		$('#exp-date').show();
		$('#cv-code').show();
	}
	
});

///contact form
$("#contact-reason").change(function() {

	var dropVal = $(this).val();
	
	if((dropVal != 'order') || (dropVal != 'retail'))
	{
		$("#company-name").slideUp('slow');
		$("#order-no").slideUp('slow');
	}
	
	if(dropVal == 'retail')
	{
		$("#order-no").slideUp('slow');
		$("#company-name").slideDown('slow');
	}
	
	if(dropVal == 'order')
	{
		$("#company-name").slideUp('slow');
		$("#order-no").slideDown('slow');
	}
	


});


///delete saved address
$('#test').click(function() {
		question = 'Are you sure you want to delete this address?';
		var link = $(this).attr("href");
		
        var r = confirm(question);
        if (r==true)
          {
          $(location).attr('href',link);
          }
        else
          {
          return false;
          }
    });
    
    
///set promo code
$('#set-promo').click(function() {
		
		var code = $("#code").val();
		
		var data = "code="+ code;
		
		$.ajax({  
		type: "POST",  
		url: "/index.php/site/ajax_promo_code",  
		data: data,  
		success: function(response){  
		location.reload(); 
		}});
		
    });
    
    $('#delete-promo').click(function() {
		
		var code = $("#code").val();
		
		var data = "code="+ code;
		
		$.ajax({  
		type: "POST",  
		url: "/index.php/site/ajax_remove_promo_code",  
		data: data,  
		success: function(response){  
		location.reload(); 
		}});
		
    });
    
	//crew nav
	initMenu();
	
//end document.ready
});

function setCounty(value)
{

		var data = 'county=' + value;
		
		$.ajax({  
		type: "POST",  
		url: "/index.php/site/ajax_setCounty",  
		data: data,  
		success: function(response){  
		
		///reveal estimate button
		$("#estimate-button").slideDown("fast");
		
		}});

}

function pageHeight()
{
		var contentHeight = $('#products').height();
		var wrapperHeight = $('#wrapper').height();
		
		if(contentHeight < 600)
		{
		$('#products').height(wrapperHeight);
		}
	
}

///quick rating
function quickRating()
{
	  var catalog_no = $("#quick-cat-no").val();
	  var rating = $("#quick-rating").val();
	  var data = "c="+ catalog_no +"&r="+ rating;		
		
		
		$.ajax({  
		type: "POST",  
		url: "/index.php/site/quickRating",  
		data: data,  
		success: function(){ location.reload(); }
		});
}

function initMenu() {
$('#menu ul').hide();
var seg = $.url();
var category = seg.segment(4);
var subcategory = seg.segment(5);
 
 if(subcategory)
 {
 $('#menu .'+category).show();
 } else {
 $('#menu .'+category).slideDown();
 
 }

  }
