$().ready(function() {

	handle_background()

	$(window).resize( function() {
		handle_background()
	});
		
	$("#search").click( function() {
		if( $("#search").val() == 'I\'m looking for...' ) { $("#search").val(''); }
	});
	
	$("#search").autocomplete( "ajax/autocomplete-search.php", 
														{
															width: 390
														});
	
	$("#search-btn").click( function() {
		$("#search-form").submit();																	 
	});
	
	// -------- TWITTER TICKER
	if( $("#twitter-ticker").length > 0 ) {
		$('#twitter-ticker').list_ticker({
			speed:5000,
			effect:'fade'
		});
	}
	
	// -------- HOME SLIDESHOW
	if( $(".slideshow").length > 0 ) {
		$('.slideshow').cycle({
			fx: 'fade',
			timeout: '6000'
		});
	}
	
	// -------- OUTER CONTAINER
	//$('#outer-container').height( $(document).height() );
	
	// -------- PRODUCT PAGE
	if( $("#product-image").length > 0 ) {
		$('#production-image-zoom').jqzoom( { zoomType: 'innerzoom', zoomWidth: 556, zoomHeight: 304 } );
		//$('.product-right').height( $('.product-left').height() - 43 );
	}
	
	
	if( $("#package-form").length > 0 ) {
		$('#package-form').submit( function()  {
			var filled_in = 0;
			var additional = '';
			
			if( $('#pacakage-gender').val() ) {
				filled_in++;
				additional += 'Gender: ' + $('#pacakage-gender').val();
			}
			
			if( $('#pacakage-height').val() ) {
				filled_in++;
				additional += '<br />Height: ' + $('#pacakage-height').val();
			}
			
			if( $('#pacakage-weight').val() ) {
				filled_in++;
				additional += '<br />Weight: ' + $('#pacakage-weight').val();
			}
			
			if( $('#pacakage-chest').val() ) {
				filled_in++;
				additional += '<br />Chest: ' + $('#pacakage-chest').val();
			}
			
			if( $('#pacakage-inside-leg').val() ) {
				filled_in++;
				additional += '<br />Inside Leg: ' + $('#pacakage-inside-leg').val();
			}
			
			if( $('#pacakage-shoe-size').val() ) {
				filled_in++;
				additional += '<br />Show Size: ' + $('#pacakage-shoe-size').val();
			}
			
			if( $('#pacakage-head-circumference').val() ) {
				filled_in++;
				additional += '<br />Head Circumference: ' + $('#pacakage-head-circumference').val();
			}
			
			if( filled_in < 7 ) {	
				alert( 'Please Enter your specifications to proceed.' );
				scrollToElement( '.product-right' );
				return( false );
			} else {
				$('#additional').val( additional );
				return( true );
			}
		});
	}
		
	// -------- CART
	if( $("#swap-delivery-information").length > 0 ) {
		$('#swap-delivery-information').change( function() {
				if( this.checked ) {
					$('.sin').attr('disabled', 'disabled').val('');
					$('#delivery-information-container').slideUp();
				} else {
					$('.sin').removeAttr('disabled');
					$('#delivery-information-container').slideDown();
				}
		});
	}
	
	// -------- THROWOUTS	
	$('#top-nav a').hover(
		function() {
			$('.throw-out').hide();
			var throw_out = '#' + $(this).attr("class") + '-throw-out';
			$(throw_out).slideDown();
		},
		function() {
			//var throw_out = '#' + $(this).attr("class") + '-throw-out';
			//$(throw_out).slideUp();
		}
	);
	
	$('.throw-out').hover(
			function() {
				$(this).slideDown();
			},
			function() {
				$(this).slideUp();
			}
	);
	
	// -------- SEARCH FILTER */
	if( $(".search-filter").length > 0 ) {
		
		$(".search-filter input").click( function() {
			//$('.search-results').fadeOut( 'slow', function() {											
				var qs = $("#filter-form").serialize();																						
			
				$.ajax({
					type: "GET",
					url: "product-results.php",
					data: qs
				}).done(function( msg ) {	
					//$("#search-results").html( msg );
					document.getElementById('search-results').innerHTML = msg;
					$("#search-results").hide().fadeIn( 'slow' );
					$("#search-results").show();
				});
			//});
			
		});
		
	}
	
	// -------- Drop Downs */
	$('.ddmenu').singleDropMenu({parentMO: 'ddmenu-hover', childMO: 'ddchildhover', timer: 500, show: 'mySpecialFadeIn', hide: 'mySpecialFadeOut'});
	
	// Fade In
	$.fn.mySpecialFadeIn = function(){
		return $(this).stop().css('opacity', 0).show().fadeTo('normal', 1);
	};

	// Fade Out
	$.fn.mySpecialFadeOut = function(){
		return $(this).stop().fadeTo('normal', 0, function(){
			$(this).hide();
		});
	};
	
});

function handle_background() {
		
	var document_width = $(document).width();
	var document_height = $(document).height();
	var header_height = $('#header').height();
	var outer_container_width = $('#outer-container').width();
	
	var bg_half = (document_width - outer_container_width) / 2;
	var bg_x = bg_half + outer_container_width;
	
	//if( document_width < 1330 ) {
	//	var bg_x_difference = document_width - 1330;
	//	bg_x_difference = -bg_x_difference;
	//	bg_x -= bg_x_difference;
	//}
	
	$('#advert-container').height(document_height - header_height);
		
	$('.body').css('backgroundImage', 'url(media/common/background-stripes.gif)' );
	$('.body').css('backgroundPosition', bg_x + 'px 0px' );
	$('.body').css('backgroundRepeat', 'repeat-y' );

	$('#advert-label').css('left', bg_x + 115 );
	
}

function scrollToElement( e ) {
	$('html, body').animate({
    scrollTop: $(e).offset().top
	}, "fast");
}

function switch_tab( show, hide1, hide2, hide3 ) {
	$(show).fadeIn();
	$(hide1).hide();
	$(hide2).hide();
	$(hide3).hide();
}

// Twitter Ticker
(function($){
  $.fn.list_ticker = function(options){
    
    var defaults = {
      speed:4000,
	  effect:'slide'
    };
    
    var options = $.extend(defaults, options);
    
    return this.each(function(){
      
      var obj = $(this);
      var list = obj.children();
      list.not(':first').hide();
      
      setInterval(function(){
        
        list = obj.children();
        list.not(':first').hide();
        
        var first_li = list.eq(0)
        var second_li = list.eq(1)
		
		if(options.effect == 'slide'){
			first_li.slideUp();
			second_li.slideDown(function(){
				first_li.remove().appendTo(obj);
			});
		} else if(options.effect == 'fade'){
			first_li.fadeOut(function(){
				second_li.fadeIn();
				first_li.remove().appendTo(obj);
			});
		}
      }, options.speed)
    });
  };
})(jQuery);
