/*
----------------------------------------------
 Plantlife / Custom JS
 Developed by Ollie Bettany / Electric Putty
 Created: 03/02/10
----------------------------------------------
*/

/* Form overlabels */

jQuery.fn.overlabel = function() {
	this.each(function(index) {
		var label = $(this); var field;
		var id = this.htmlFor || label.attr('for');

		if (id && (field = document.getElementById(id))) {
			var control = $(field);
			label.addClass("overlabel-apply");

			if (field.value !== '') {
				label.fadeOut(100);
			}
			control.focus(function () {label.fadeOut(100);}).blur(function () {

			if (this.value === '') {
				label.fadeIn(100);
			}
		});

		label.click(function() {
			var label = $(this); var field;
			var id = this.htmlFor || label.attr('for');

			if (id && (field = document.getElementById(id))) {
				field.focus();
			}
		});
		}
	});
};


/* Function to swap two classes */

$.fn.swapClass = function(c1, c2){
    return this.each(function(){
        var t = $(this);
        (!t.is('.'+c1)) ?
            t.addClass(c1).removeClass(c2) : t.addClass(c2).removeClass(c1);
    });
}

/* Hover state for filter button */

$(document).ready(function(){
	$(".catnav").hover(
		function() {
			// Do on mouseover
			$(this).addClass("show");
		},
			// Undo on mouseout
		function() {
			$(this).removeClass("show");
		}
	);

	// initialize scrollable
	$(".scrollable").scrollable({circular: true});

	// gallery pagination
	$("#gallery-pagination .prev a").click(function() {
		$current = Number($("#g_index").html());
		total = Number($("#g_total").html());
		if($current == 1) {
			$current = total
		} else {
			$current = $current - 1;
		}
		$("#g_index").html($current);
	});
	$("#gallery-pagination .next a").click(function() {
		$current = Number($("#g_index").html());
		total = Number($("#g_total").html());
		if($current == total) {
			$current = 1
		} else {
			$current = $current + 1;
		}
		$("#g_index").html($current);
	});
	
	
	
	$('.cart_update').keyup(function() { updatetotals(); });
	

});

// Shop specific calculator
var total_cards;
var total_cals;
var total_bags;

var cardpp;
var calpp
var bagpp

var sub_total_cost;
var pp_cost;
var donate;
var total;

function updatetotals()
{
	total_cards = 0;
	total_cals = 0;
	total_bags = 0;
	
	cardpp = 0;
	calpp = 0;
	bagpp = 0;
	
	sub_total_cost = 0;
	pp_cost = 0;
	total = 0;

	//console.log("TOTAL BEFORE: " + total_cards);

	// Get cost for each row
	$('.shop-item').each(
		function()
		{
			$fs = $(this);
			$obj = $fs.find('.cart_update');
			$price = $fs.find('.shop-item-description p span').attr('rel');

			$quant = $obj.val();
			if ($quant == '') { $quant = 0 } else { $quant = parseInt($quant); }
		
			sub_total_cost = sub_total_cost + ($quant * $price);
			
			if ($fs.hasClass('item-card'))
			{
				total_cards = total_cards + $quant;
			}
			
			if ($fs.hasClass('item-cardpack'))
			{
				$quant = $quant * 3;
				total_cards = total_cards + $quant;
			}
			if ($fs.hasClass('item-calendar'))
			{
				total_cals = total_cals + $quant;
			}
			if($fs.hasClass('item-bag'))
			{
				total_bags = total_bags + $quant;
			}
		}
	);
	

	
	$('#submit_button').show();
	$('#shop-quant-exceeded').hide();


	// Calculate postage and packing for the cards
	if (total_cards <= 4 && total_cards >= 1)
	{ 
		cardpp = 2.50;
	}

	else if (total_cards >= 5 && total_cards <= 9)
	{ 
		cardpp = 4.00; 
	}
	else if (total_cards > 9) 
	{ 
		cardpp = 0;
		$('#submit_button').hide();
		$('#shop-quant-exceeded').show();
	}
	
	
	// Calculate postage and packing for the calendars
	if (total_cals <= 2 && total_cals >= 1)
	{ 
		calpp = 1.50;
	}

	else if (total_cals >= 3 && total_cals <= 4)
	{ 
		calpp = 2.50; 

	}
	else if (total_cals > 4) 
	{ 
		calpp = 0;
		$('#submit_button').hide();
		$('#shop-quant-exceeded').show();
	}
	
	
	// Calculate postage and packing for the bags
	if (total_bags <= 2 && total_bags >= 1)
	{ 
		bagpp = 1.50;
	}

	else if (total_bags >= 3 && total_bags <= 4)
	{ 
		bagpp = 2.50; 

	}
	else if (total_bags > 4) 
	{ 
		bagpp = 0;
		$('#submit_button').hide();
		$('#shop-quant-exceeded').show();
	}	




	// Get the donation field
	donate = $('#donation_amount').val();
	if (donate == '') { donate = 0 } else { donate = parseFloat(donate); }
	
	// Calculate postage and packing
	pp_cost = bagpp + calpp + cardpp;
	
	// Calculate total	
	total_cost = sub_total_cost + pp_cost + donate;

	// Force to 2 decimal places
	sub_total_cost = sub_total_cost.toFixed(2);
	pp_cost = pp_cost.toFixed(2);
	donate = donate.toFixed(2);
	total_cost = total_cost.toFixed(2);
	
	// Write totals to HTML
	$('#shop-subtotal').html('&pound;' + sub_total_cost);
	$('#shop-pp').html('&pound;' + pp_cost);
	$('#shop-donate').html('&pound;' + donate);
	$('#shop-total').html('&pound;' + total_cost);
	
	// Update form fields
	$('#subtotalamount').val(sub_total_cost);
	$('#shippingcharge').val(pp_cost);
	$('#donationamount').val(donate);
	$('#transactionamount').val(total_cost);
	$('#CC_amount').val(total_cost);

};

// Add classes to road verge radio buttons (for thumbs up / thumbs down )
$(document).ready(function(){
	$('#verge_preference label input').each(function() {
		if ($(this).val() == 'like') {
			$(this).parent('label').attr('id', 'verge_like');
		} else if ($(this).val() == 'dont_like') {
			$(this).parent('label').attr('id', 'verge_dislike');			
		}
	});
	
	
	$("#pl_join_form").validate();
	
	//var verge_radio = $('#verge_preference label input');

	//for (i=0; i<verge_radio.length; i++) {
	///	if (verge_radio.val() == 'like') {
	//		console.log($(this));
	//	} else if (verge_radio.val() == 'dislike') {
	//		console.log('dislike');
	//	}
	//}

});

