(function($, undefined)
{
    $(function()
	{
		//Slideshow
		if ($('#slideshow').length)
        {
			$('#slideshow').nivoSlider({
				effect:'fade', 
				animSpeed:1000,
				pauseTime:4000,
				directionNavHide:true,
				controlNav: true,
				keyboardNav:true,
				captionOpacity:0
			});
		}
		
		//Placeholder text
		$('.numbers').formhint();
		
		//Cufon
		Cufon.replace('.basket b, #shopnav a, #search-form label, .caption, h4, .favourite-price p, .subcategory h1, .subcategory h2, .product-nav h2, .product-main h1, .item-images p, h1, .item-information h2, h2.complete, #user-details h2, .size-categories');
		
		// Sponsors
		$("#brandscroll").simplyScroll({
			autoMode: 'loop'
		});
		
		//Size Guide Tabs
		$(function ()
		{
			var tabContainers = $('div.sizes > div');
			tabContainers.hide().filter(':first').show();
			
			$('div.sizes ul.size-categories a').click(function () {
					tabContainers.hide();
					tabContainers.filter(this.hash).show();
					$('div.sizes ul.size-categories a').removeClass('selected');
					$(this).addClass('selected');
					return false;
			}).filter(':first').click();
		});
		
		//Size Guide Popup
		$("a.size-guide, a.faq-size, a.collection").fancybox({
			'autoScale'				:	true,
			'enableEscapeButton' 	:	true
		});

		
		//Center Screen
		$('.favourite-price p').centerscreen({
			parent : 1,
			horizontal : 0,
			animate : 0,
			onresize : 0
		});
		
		//Range Slider
		$('#slider-bar').slider({
			handle: '#slider-handle',
			min: 0,
			max: 150,
			
			slide: function(e,ui){
				//mypos = $('#slider-handle').position().left + 2;  //grab position of slider dot + 2
				var mypos = $('#slider-bar').slider("value");
				$('#slider-bubble').css('left', mypos).text(ui.value);
				$("#slider-handle").css('left', mypos);
			}
		});
		
		//Checkout Page
		//Placeholder text
		$('#login-email').formhint();
		$('#password').formhint();		
		
		//Hide Delivery address
		if ($("#shipping-address").is(":checked"))
		{
			$("#ship-address").css("display","none");
		}
		else
		{
			$("#ship-address").show();
		}
    
		
		$("#shipping-address").click(function()
		{
			if ($("#shipping-address").is(":checked"))
			{
				$("#ship-address").hide("fast");	
				$("#payment").show("fast");
			}
			else
			{     
				$("#ship-address").show("fast");
				$("#payment").hide("fast");
			}       
		});
		
		//IE6 Hover
		$('#bag-items tr').hover(function() 
		{
			$(this).addClass('table-hover');
		}, 
		function() 
		{
			$(this).removeClass('table-hover');
		});
		
		if ($('#map').length)
		{
			//Google Map
			var stadium = new google.maps.LatLng(50.796505,-1.063941);
			var store = new google.maps.LatLng(50.795764,-1.064771);
			var myOptions = 
			{
				zoom: 15,
				center: store,
				mapTypeId: google.maps.MapTypeId.SATELLITE
			}
			
			var map = new google.maps.Map(document.getElementById("map"), myOptions);
			
			var marker = new google.maps.Marker(
			{
				position: stadium, 
				map: map,
				title:"Portsmouth FC"
			});
			
			var marker = new google.maps.Marker(
			{
				position: store, 
				map: map,
				title:"Portsmouth FC Megastore"
			});
		}
		
		//Drop down widths
		function megaHoverOver()
		{
			$(this).find(".sub").stop().fadeTo('fast', 1).show();
				
			//Calculate width of all sub categories
			(function($) { 
				jQuery.fn.calcSubWidth = function() {
					rowWidth = 0;
					//Calculate row
					$(this).find(".subcategory").each(function() {					
						rowWidth += $(this).width(); 
					});	
				};
			})(jQuery); 
				
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});		
			
		}
		
		function megaHoverOut()
		{ 
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
			  $(this).hide(); 
		  });
		}
	
	
		var config = {    
			 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
			 interval: 1, // number = milliseconds for onMouseOver polling interval    
			 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
			 timeout: 1, // number = milliseconds delay before onMouseOut    
			 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
		};
	
		$("ul#shopnav li .sub").css({'opacity':'0'});
		$("ul#shopnav li").hoverIntent(config);
		
		 //This function deals with shipping addresses in the basket
	   $('#us_bill_states').hide();
     $('#us_ship_states').hide();
     
     if($('#country').val()=='US'){
      $('#us_bill_states').show();
     }
     
     if($('#ship-country').val()=='US'){
      $('#us_ship_states').show();
     }     
     
     $('#country').change(function() {
      if($('#country').val()=='US'){
        $('#us_bill_states').slideDown();
      }
      if(($('#country').val()!='US')&&($('#us_bill_states').is(':visible'))){
        $('#us_bill_states').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();
      }
     });
     
     	
	
  
  });
	
})(jQuery);
