$(document).ready(function() {
	
//  Menu javascript ----------------------------------------------
	$(".headlink").each(function() {
		$(this).children("ul").addClass('headlink_ul_hide');
		$(this).children("ul").children("li").addClass('headlink_ul_hide');
	});
	
    //MSIE has a weird quick where it will add spaces after the <li> if the display is BLOCK.
    //Add in the display=block to submenu links in any non-MSIE browser
	jQuery.each(jQuery.browser, function(i, val) {
		if(i!="msie")
		{
			$(".headlink a").css("display","block");
		}
	});	
	
	$(".headlink").hover(function() {
		$(this).children("ul").removeClass('headlink_ul_hide');
		$(this).children("ul").children("li").removeClass('headlink_ul_hide');
		$(this).children("ul").addClass('headlink_hover_ul');
	    $(this).children("ul").children("li").addClass('headlink_hover_ul_li');
    	var pos = $(this).offset();
    	var posLeft = pos.left + 5;
    	var postTop = pos.top + 15;
	    $(this).children("ul").css('left', posLeft);
	    $(this).children("ul").css('top', postTop);
	    
	}, function() {
	    $(this).children("ul").removeClass('headlink_hover_ul');
	    $(this).children("ul").children("li").removeClass('headlink_hover_ul_li');
		$(this).children("ul").addClass('headlink_ul_hide');
		$(this).children("ul").children("li").addClass('headlink_ul_hide');	    
	});
// End Menu  -------------------------------------------------------


//Popup alerts
	
	if ($("#jsMessage").length)
	{
		alert($("#jsMessage").html());
	}	
	
	$(".highlight").animate( {
		opacity : ".1"
	}, 1);
	$(".highlight").animate( {
		opacity : "1"
	}, 2000);	
	
//Input button hover functionality (for IE)
	$(".inputButton").hover(function() {
	    $(this).addClass('inputButton_hover');
	}, function() {
	    $(this).removeClass('inputButton_hover');
	});
	
//Message bar 
	$(".message").hide();
	$(".message").slideDown("slow");

//Browse 'List' View 
	$(".detailAcc").hide(); //to begin, hide all details

	$(".headAcc").click(function(){
		$(this).next(".detailAcc").slideToggle("slow");
		$(this).next(".detailAcc").toggleClass("active");
		$(this).parent(".item").toggleClass("boxed");
	});
	
	$(".headAcc").hover(function() {
	    $(this).addClass('headAcc_hover');
	}, function() {
	    $(this).removeClass('headAcc_hover');
	});
	
	$(".highlighter").hover(function() {
	    $(this).addClass('highlighter_hover');
	}, function() {
	    $(this).removeClass('highlighter_hover');
	});	
	
	$(function() {
		$(".inputTextBoxDatepicker").datepicker({ dateFormat: 'yy-mm-dd' });
	});
	
//Large calendar page
	$(".calendar").animate( {
		opacity : ".1"
	}, 1);
	$(".calendar").animate( {
		opacity : "1"
	}, 2000);	
	
//Shop page
	$(".addToBasket").click(function(){
		//First reset any previously clicked add links
		$(".addQty").hide();
		$(".addToBasket").show();
		$(".headerFav").show();
		//Now show the qty entry box
		$(this).hide();
		$(this).next('.addQty').show();
		$(this).next('.addQty').next('.headerFav').hide();
	});
/*
 * don't need, submitting the form will reset everything
	$("#submitAdd").click(function(){
		$(this).parent('#addQty').hide();
		$(this).prev("a[rel='addToBasket']").show();
		$(this).next("a[rel='headerFav']").show();
	});
*/
	
//Producer - Product Detail variable weight
	if ($('#varwgt').attr('checked'))
	{
		$('#nonVarWgtFields').hide();
	}
	else
	{
		$('#varWgtFields').hide();
	}
	
	
	$("#varwgt").click(function(){
		if ($('#varwgt').attr('checked'))
		{
			$('#varWgtFields').show();
			$('#nonVarWgtFields').hide();
		}
		else
		{
			$('#varWgtFields').hide();
			$('#nonVarWgtFields').show();
		}
	});
	
	//Track qty
	if (!$('#track_qty').attr('checked'))
	{
		$('#limitQtyFields').hide();
	}
	
	
	$("#track_qty").click(function(){
		if ($('#track_qty').attr('checked'))
		{
			$('#limitQtyFields').show();
		}
		else
		{
			$('#limitQtyFields').hide();
		}
	});	
	
	//Edit member basket 'Enter' button handling
	$(":input[rel='mmbField']").keydown(function (e) {
		//alert('Handler for .keydown() called.');
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
        {
        	e.preventDefault();
        	$("#updateBasket").click();
            return false;
        } 
        else 
        {
            return true;
        }
    });

	
	
//Cashier checkout
	if ($('#payment_method').val() != 'mixed') {
		if ($('#payment_method').val() != 'cash') {
			$("#cash_amt").hide();
		}
		if ($('#payment_method').val() != 'credit') {
			$("#credit_amt").hide();
		}
		if ($('#payment_method').val() != 'accts receivable') {
			$("#acct_recv_amt").hide();
		}		
		if ($('#payment_method').val() != 'check') {
			$("#check_amt").hide();
		}
	}
	if ($('#payment_method').val() == '') {
		$("#completeCheckout").hide();
	}
	
	$("#payment_method").change(function() {
		$("#cash_amt").hide();
		$("#credit_amt").hide();
		$("#check_amt").hide();
		$("#acct_recv_amt").hide();
		$("#completeCheckout").hide();
		if ($('#payment_method').val() == 'cash') {
			$('#cash_amt').show();
		}
		if ($('#payment_method').val() == 'credit') {
			$('#credit_amt').show();
		}
		if ($('#payment_method').val() == 'accts receivable') {
			$('#acct_recv_amt').show();
		}		
		if ($('#payment_method').val() == 'check') {
			$('#check_amt').show();
		}
		if ($('#payment_method').val() == 'mixed') {
			$('#cash_amt').show();
			$('#credit_amt').show();
			$('#acct_recv_amt').show();			
			$('#check_amt').show();
		}
		if ($('#payment_method').val() != '') {
			$('#completeCheckout').show();
		}
	});
	
	/*Cash tendered countback
	$("#cashamt2").change(function() {
		if ($('#cash_amt2').val() != '' && $('#cash_tendered').val()!='') {
			var output = parseFloat($('#cash_tendered').val()) - parseFloat($('#cash_amt2').val());
			output = output.toFixed(2) + "";
			$('#change').html('Change: $' + output);
		}
	});*/
	
	$("#cashtendered").keyup(function() {
		if ($('#cashamt2').val() != '' && $('#cashtendered').val()!='') {
			//var output = parseFloat($('#cash_tendered').val()) - parseFloat($('#cash_amt2').val());
			var output = parseFloat($('#cashtendered').val()) - parseFloat($('#cashamt2').val());
			output = output.toFixed(2) + "";
			$('#change').html('Change: $' + output);
		}
	});	
	
	
	$("#cpa_producer").change(function() {
	    $('#checkoutForm').submit();
	});
	
	$("#cpa_product").change(function() {
	    $('#checkoutForm').submit();
	});
	
	$("#adj_type").change(function() {
	    $('#checkoutForm').submit();
	});		

	$("#adj_producer").change(function() {
	    $('#checkoutForm').submit();
	});		
	

	//Colorbox calls		
	//Picture gallery
	$("a[rel='pictures']").colorbox();

	//In-line HTML
	$("a[rel='html']").colorbox({width:"50%", inline:true});
	
});


//Browse - add product functions
function cashierAdd()
{
	window.location.href=window.location.href + '//add';
}

function memberAdd()
{
	window.location.href=window.location.href + '//add';
}	

