(function($, undefined)
{
    $(function()
	{
		
		//All JS functions to be included in here, not the template files!
		
		//Slideshow
		if ($('#featured').length && $('#featured img').size() > 1)
        {
			var n = $('#featured img').length;
			var j = 1;
			
			function change_text(j, that){
				var lastSlide = $('#featured img').last().attr('title');
				
				var firstSlide = $('#featured img').first().attr('title');
				
				if(j == 1){
					$('#prev-caption').html(lastSlide);
				}else{
					$('#prev-caption').html(that.prev().attr('title'));
				}
			
				if(j == n){
					$('#next-caption').html(firstSlide);
					j = 1;
				}else{
					$('#next-caption').html(that.next().attr('title'));
					j = j+1;
				}
				
				return j;
			}
			
			$('#featured') 
			.after('<div id="featured-nav">') 
			.cycle(
			{ 
				pager:  '#featured-nav',
				pagerAnchorBuilder: function(i, slideEl) { 
				
				prefix_num = '';
				if((i+1)<10){
					prefix_num = 0;
				}
				
				return '<a href="#">'+prefix_num+(i+1)+'</a>';
				},
				fx:     'fade', 
				speed:  '1400', 
				next:   '#next', 
				prev:   '#prev',
				before: function(){
					$('#prev-caption').fadeOut();
					$('#next-caption').fadeOut();
				},				
				after: function()
				{  
					if($('#next-caption:hidden')){
						j = change_text(j, $(this));			
					}
					
					$('#prev-caption').fadeIn();
					$('#next-caption').fadeIn();
				
				} 
			});
		}else{
			$('#featured-navigation').hide();
		}
		
		//Placeholder text
		if (!Modernizr.input.placeholder)
		{
			$('#search').formhint();
			$('#login-email').formhint();
			$('#password').formhint();
			$('#newsletter').formhint();
		}
		
		//Item Tabs
/*
		$(function () 
		{
			var tabContainers = $('div.tabs > div');
			
			$('div.tabs ul.item-info a').click(function () 
			{
				tabContainers.hide().filter(this.hash).show();
				
				$('div.tabs ul.item-info a').removeClass('selected');
				$(this).addClass('selected');
				
				return false;
			}).filter(':first').click();
			
		});
*/
		$(document).ready(function(){
		
		$("#specification").siblings(".block").hide(); 
        
            $("a.tab").click(function (event) { 
            	
          		event.preventDefault();
          		$(".tab_wrapper").stop();
          		
          		var content_show = $(this).attr("href");
          		var next = $(this).attr("href");
          		var h = $(next).height(); 
          
                $(".active").removeClass("active");  
          
                $(this).addClass("active");  
           
                $(".block").hide(); 
                
                $(content_show).show();
                
                $(content_show).jScrollPane({showArrows: true});                   
                
                //$(".tab_wrapper").animate({"height": ""+h+"px"},"slow");  
            });
            
            
            $(".jump").click(function(event){
                   event.preventDefault();
                    $('html,body').stop(true, true);
                   var to = $(this).attr("href");
                   var targetOffset = $(to).offset();
                   var topx = targetOffset.top;
                   
                   if($(".faqh").length>0){
                   if($(".faqh").hasClass("fixed")){
                    	var topx = targetOffset.top-$(".faqh").height()-20;
                   }
                   else {
                   		var topx = targetOffset.top-$(".faqh").height()-50;
                   }
                   }
                   
                   $('html,body').animate({scrollTop: topx}, 1000);
            	});
            
         });  
		
		//Checkout Page
		
		//Hide Delivery address
		$("#ship-address").css("display","none");
		$("#ship-payment").css("display","none");
		
		
		$("#shipping-address").click(function()
		{
			if ($("#shipping-address").is(":checked"))
			{
				$("#ship-address").slideUp();
/*
				$("#ship-payment").hide("fast");
				$(".continue_btn").hide("fast");
				$("#payment").show("fast");
				$(".continue_btn").show("fast");
*/
			}
			else
			{   
				$("#ship-address").slideDown(); 
/*
				$(".continue_btn").hide("fast");
				$(".continue_btn.new").show("fast"); 
				
				$("#ship-payment").show("fast");
				$("#payment").hide("fast");
*/
			}       
		});
		
		//IE6 Hover
/*
		if(!$('#bag-items').hasClass("overview")){
		$('#bag-items tr').hover(function() 
		{
			$(this).addClass('table-hover');
		}, 
		function() 
		{
			$(this).removeClass('table-hover');
		});
		}
*/
		
		//US States
		$('#us_bill_states').hide();
		$('#us_ship_states').hide();
		
		if($('#country').val()=='US')
		{
			$('#us_bill_states').slideDown();
		}
		
		if($('#country').val()=='GB')
		{
			$('.uk-county').slideDown();
		}
		
		if($('#ship-country').val()=='US')
		{
			$('#us_ship_states').slideDown();
		}     
		
		$('#country').change(function()
		{
			if($('#country').val()=='US')
			{
				$('#us_bill_states').slideDown();
			}
			if(($('#country').val()!='US')&&($('#us_bill_states').is(':visible')))
			{
				$('#us_bill_states').slideUp();
			}
			
			//UK Counties
			if($('#country').val()=='GB')
			{
				$('.uk-county').slideDown();
			}
			if(($('#country').val()!='GB')&&($('.uk-county').is(':visible')))
			{
				$('.uk-county').slideUp();
			}
		});
		
		$('#ship-country').change(function()
		{
			if($('#ship-country').val()=='US')
			{
				$('#us_ship_states').slideDown();
			}
			
			if(($('#ship-country').val()!='US')&&($('#us_ship_states').is(':visible')))
			{
				$('#us_ship_states').slideUp();
			}
			
			//UK Counties
			if($('#ship-country').val()=='GB')
			{
				$('#ship-address .uk-county').slideDown();
			}
			
			if(($('#ship-country').val()!='GB')&&($('#ship-address .uk-county').is(':visible')))
			{
				$('#ship-address .uk-county').slideUp();
			}
		});
		
		//Google Map
		if ($('#map').length)
        {
			var myLatlng = new google.maps.LatLng(55.883914,-4.21205);
			var myOptions = 
			{
				zoom: 11,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			
			var map = new google.maps.Map(document.getElementById("map"), myOptions);
			
			var marker = new google.maps.Marker(
			{
				position: myLatlng, 
				map: map,
				title:"Addict Clothing"
			});
		}
			
		
		if($(".faq").length>0){
			var $comment = $('.faq, .faqh');
			eltop = $comment.offset().top - parseFloat($comment.css('marginTop').replace(/auto/, 0));
			$window = $(window);
			
			$window.scroll(function (event) {
			// what the y position of the scroll is
			var y = $window.scrollTop();
			// whether that's below the form
			if (y >= eltop) {
			// if so, ad the fixed class
			$comment.addClass('fixed');
			} else {
			// otherwise remove it
			$comment.removeClass('fixed');
			}
			});
		}
		
		$("h6.change-currency").hover(
  function () {
    $(this).children(".currency-body").stop(true, true).delay(800).fadeIn();
  }, 
  function () {
  	$(this).children(".currency-body").stop(true, true).fadeOut();
  }
);

	$(".close, .intro_link").click(function(event){
	  		event.preventDefault();
	  		$(".feature").slideToggle("slow");

	  		if($(".category-navigation .intro_link").text()=="Hide Intro"){
	  			$(".category-navigation .intro_link, .secondary-nav .intro_link").text("Show Intro");
	  		}
	  		else {
	  			$(".intro_link, .secondary-nav .intro_link").text("Hide Intro");
	  		}
	  		
	  });



//folder over hover

	$(".hoverx .item").hover(
		  function () {
		  if($(this).find(".hover").length>0){				   	
	   		 $(this).find(".hover").stop(true, true).fadeIn(600);
	   		 $(this).find(".first").stop(true, true).fadeOut();
	   		}
		  }, 
		  function () {	
	  		 $(this).find(".first").stop(true, true).fadeIn(600);
	  		 $(this).find(".hover").stop(true, true).fadeOut();

	});


$(".region").click(function () {
	$(this).find(".rl").stop(true, true).fadeIn();
	$(this).find(".change").css({"color":"#fff"});
}).mouseleave(function(){

$(this).find(".rl").stop(true, true).fadeOut();
$(this).find(".change").removeAttr("style");
});

//basic form validation
if($(".js_validate").length>0){
	$('.js_validate').submit(function() {
	
		//clear other form validations
		$('.js_req').removeClass("error");		
		
		var errors = new Array();
		var el = $(this).attr("id");

		var required = $(this).find(".js_req");

		required.each(function(i){
		
			if($(this).val()==""){
			console.log($(this).val());
				errors[i]= $(this).attr("name");
				$("#"+el+"").find("input[name='"+$(this).attr("name")+"']").addClass("error");
			}
			
		});
		
		if(errors.length>0){
			return false;
			alert(errors.length);
		}
		else {
			return true;
		}
	
	});
}


	});	
	
})(jQuery);
