$(document).ready(function() {
	// Loading Functions
	$("li:last-child").addClass('last'); // last items in a list get a border removed
	$('#sidebar table tr:odd').addClass('alt');
	$('#shipping_table td:odd').addClass('cost');
	
	// Navigation Roll Over			
	$(".nav_item").css({backgroundPosition: "0 0"}).hover(function() {
		$(this).stop().animate({backgroundPositionY:"-48px"}, {duration:400});
	}, function () {
		$(this).stop().animate({backgroundPositionY:"0px"}, {duration:400});
	});
	
	// Navigation Drop Down
	$("#navigation li.nav_item").children("ul").hide();
	$("#navigation li.nav_item").hover(function() {
		$(this).children("ul").css('opacity', 0).show().stop().animate({opacity:1}, {duration:400});
	}, function () {
		$(this).children("ul").stop().animate({opacity:0}, {duration:300}).fadeOut(5);
	});	
	
	// Logo Roll Over
	$("#logo").hover(function() {
		$(this).stop().animate({opacity:0}, {duration:400});
	}, function () {
		$(this).stop().animate({opacity:1}, {duration:400});
	});
	
	// Image Swap
	$(".swap").hover(function() {
		$(this).attr("src", $(this).attr("src").replace(".gif", "_on.gif"));
	}, function () {
		$(this).attr("src", $(this).attr("src").replace("_on.gif", ".gif"));
	});
	
	// Preload Image Swaps
	(function($) {
		var cache = [];
		$.preLoadImages = function() {
			var args_len = arguments.length;

			for (var i = args_len; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	})(jQuery)
	
	jQuery.preLoadImages();
	
	// Detail Page Graphics
	$("#how_it_works li:contains(Grape)").parent().parent().removeClass().addClass('grapes');
	$("#how_it_works li:contains(grape)").parent().parent().removeClass().addClass('grapes');
	$("#how_it_works li:contains(Grapefruit)").parent().parent().removeClass().addClass('grapefruit');
	$("#how_it_works li:contains(grapefruit)").parent().parent().removeClass().addClass('grapefruit');
	
	// Input Clear/Fill
	function autoFill(id, v){
		$(id).css({ color: "#368104" }).attr({ value: v }).focus(function(){
			if($(this).val()==v){
				$(this).val("").css({ color: "#606c09" });
			}
		}).blur(function(){
			if($(this).val()==""){
				$(this).css({ color: "#368104" }).val(v);
			}
		});
	};
	
	autoFill($("#search_bar"), "");
	autoFill($("#quantity"), "1");
	autoFill($("#mailing_list_bar"), "Enter email address");
	
	// Input Field Digits Only
	$("#quantity").keypress(function (e) {
		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
			$('.error').animate({backgroundPosition: '65px 0px'}, {duration: 500});
			$('.error_message').html("Numbers only");
			$('.error_message').hide();
			$('.error_message').fadeIn(400);
			return false;
		} else {
			$('.error').animate({backgroundPosition: '180px 0px'}, {duration: 500});
			$('.error_message').fadeOut(400);
		}
	});
	
	$("#quantity").focusout(function() {
		$('.error').animate({backgroundPosition: '180px 0px'}, {duration: 500});
		$('.error_message').fadeOut(400);
	});
	
	// JQuery Uniform
	$("select, input:checkbox, input:radio, input:file").uniform();
	
	// Reviews Stars
	$(".review_message div.radio").hover(function() {
		$(this).prev().css({backgroundPosition: '-235px -279px'})
	}, function () {
		$(this).prev().css({backgroundPosition: '-181px -279px'})
	});
	
	// Detail Drop Down Select
	/*$("select").change(function () {
		var str = "";
		$("select option:selected").each(function () {
			str += $(this).text() + " ";
		});
		
		$(".price .data").text(str);
	}).change();*/
	
	// Product Detail Description Inserts
	$('#details .philosophy').before('<h2>Product Philosophy</h2><p>').after('</p><hr/>');
	$('#details .directions').before('<h2>Directions</h2><p>').after('</p><hr/>');
	$('#details .ingredients').before('<h2>Ingredients</h2><p>').after('</p>');
	$('#details .specials').before('<h2>Specials</h2><p>').after('</p><hr/>');
	
	// Shopping Cart Update Cart
	$("#update input").hide();
	
	$(".quantity").focus(function() {
		$("#update input").fadeIn();
	});
	
	$(".cart_delete div span input").change(function() {
		$("#update input").fadeIn();
	});
	
	// Twitter
    $(".tweet").tweet({
        username: "RealPurity",
        join_text: "auto",
        avatar_size: 0,
        count: 5,
        auto_join_text_default: "", 
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "Loading tweets"
    });
});