$(document).ready(function () {
	
	// Menu slow fade
	// Don't run in IE 6 and lower.
	if ($.browser.msie && $.browser.version < 7)
	{
		return
	}
	
	$("#menu .items ul li").removeClass("highlight").find("a").append('<span class="hover" />').each(function () {
		var a = $("> span.hover", this).css("opacity", 0);
		$(this).hover(function () {
			a.stop().fadeTo(500, 1);
		}, function () {
			a.stop().fadeTo(500, 0);
		})
	});
	
	// Home page slides
	if($('#slides').length)
	{
		$('#slides').cycle({
			fx: 'fade, fadeZoom, scrollDown, scrollUp, blindX, zoom, wipe',
			randomizeEffects: 1,
			timeout: 6000
		});
	}
	
	// Submenu sliding
	if($('#submenu').length)
	{
		var menu = '#submenu';
		var y_loc = null;
		y_loc = parseInt($(menu).css('top').substring(0, $(menu).css('top').indexOf('px')));

		$(window).scroll(function(){
			var offset = y_loc + ($(document).scrollTop() - 200) + "px";
			if ($(document).scrollTop() > 215)
			{
	        	$(menu).animate({top:offset}, {duration:500, queue:false});
			}
			else
			{
				$(menu).animate({top:y_loc}, {duration:500, queue:false});
			}
		});
	}
	
	// Contact form processing
	if($('#contact-frm').length)
	{
		var use_ajax = true;

		$('#contact-frm').validationEngine({
			inlineValidation: true,
			promptPosition: 'topRight',
			success: function(){ use_ajax = true },
			failure: function(){ use_ajax = false }
		});

		$('#contact-frm').submit(function(e){
			if(use_ajax)
			{
				$('.success').css('display', 'none');
				$('.processing').css('display', 'block');
				$.post('/php/contact.php', $(this).serialize() + '&ajax=1', function(data){
					if(parseInt(data) != -1)
					{
						$('#contact-frm').hide('slow');
						$('.ajax-success').fadeIn('slow');
					}
				});
			}

			e.preventDefault(); /* stop the default form submit */
		});
	}
	
	// Products page processing
	if ($('#prod-frm').length)
	{
		// Show the action buttons, upgrades total row and checkboxes
		$('h4 span, #actions, .upg-total, .select').show();
		
		$('#sf-btn').click(function(e){
			$('#request-more').slideUp();
			$('#send-friend').slideToggle();
			$('html,body').animate({scrollTop: $('#send-friend').offset().top},'slow');
			$('#txt-sf-name').focus();
			e.preventDefault();
		});
		
		$('#rm-btn').click(function(e){
			$('#send-friend').slideUp();
			$('#request-more').slideToggle();
			$('html,body').animate({scrollTop: $('#request-more').offset().top},'slow');
			$('#txt-rm-name').focus();
			e.preventDefault();
		});
		
		// Turn cursor into pointer on upgrade rows
		$('#prod-frm td').not('.upg-total').css('cursor', 'pointer');
		
		// Prepend "Base" and "Total" to appropriate totals rows
		$('.total').not('.upg-total').prepend('Base ');
		$('.upg-total').prepend('Total ');
		
		// Enable clicking entire row to enable checkbox
		$("#prod-upg tr").click(function(a) {
	        if (a.target.type !== "checkbox") {
	            $(":checkbox", this).trigger("click");
				updateTotal();
	        }
	    });
	
		// Enable clicking checkbox to enable checkbox
		$("input[type='checkbox']").click(function(a) {
			updateTotal();
	    });
	}
	
	// Send to a Friend form processing
	if($('#friend-frm').length)
	{
		var use_ajax = true;

		$('#friend-frm').validationEngine({
			inlineValidation: false,
			promptPosition: 'topRight',
			success: function(){ use_ajax = true },
			failure: function(){ use_ajax = false }
		});

		$('#friend-frm').submit(function(e){
			if(use_ajax)
			{
				$('.processing').css('display', 'block');
				$.post('/php/send-friend.php', $(this).serialize() + '&ajax=1', function(data){
					if(parseInt(data) == 1)
					{
						$('#friend-frm').hide('slow');
						$('#send-friend .ajax-success').fadeIn('slow');
					}
				});
			}
			e.preventDefault(); /* stop the default form submit */
		});
	}
	
	// Send to a Friend form processing
	if($('#request-frm').length)
	{
		var use_ajax = true;

		$('#request-frm').validationEngine({
			inlineValidation: false,
			promptPosition: 'topRight',
			success: function(){ use_ajax = true },
			failure: function(){ use_ajax = false }
		});

		$('#request-frm').submit(function(e){
			if(use_ajax)
			{
				$('.processing').css('display', 'block');
				$.post('/php/request-more.php', $(this).serialize() + '&ajax=1', function(data){
					if(parseInt(data) == 1)
					{
						$('#request-frm').hide('slow');
						$('#request-more .ajax-success').fadeIn('slow');
					}
				});
			}
			e.preventDefault(); /* stop the default form submit */
		});
	}
	
});

function updateTotal() {
    var total = parseFloat($('.base-price').text());

	$("input[type='checkbox']:checked").each(function (b) {
		total += parseFloat($(this).val());
		$("." + $(this).attr("class")).css("background", "#ccc");
	});

	$("input[type='checkbox']:not(:checked)").each(function (b) {
		var row = "." + $(this).attr("class");
		$(this).css("background", "none");
		$(row + ".alt2").css("background", "#eee");
		$(row + ".alt1").css("background", "none");
	});
	$(".total-price").text(total)
}
