function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}
function update_product_cost (p_id) {
	var price = $("#" + p_id + "_price");
	var qty = $("#" + p_id + "_qty").val();

	$.getJSON("modules/store/ajax/update_price.php", { id: p_id, qty: qty }, 
		function(json){
	  	$("#" + p_id + "_cost .cost").text(json.price);
		}
	);
}

function update_total(ele) {
	var id = ele.id;
	var p_id = id.replace('_qty', '');
	update_product_cost(p_id);
	update_cart_total();
}

function update_cart_total () {
	$('#cart_total_field').val(0);
	$('.product_price').each(
		function(i) {
			var price = $("#" + this.id).val();
			var p_id = this.id.replace('_price', '');
			var qty = $("#" + p_id + "_qty").val();
			var total = $('#cart_total_field').val();
			console.log(price);
			$('#cart_total_field').val( parseFloat(total) + (parseFloat(price) * parseFloat(qty)));
		}
	);
	$('#cart_total').text(formatCurrency($('#cart_total_field').val()));
	$('#header_cart_total').text(formatCurrency($('#cart_total_field').val()));
	// $("#cart_total").animate({fontColor: "#FFF600"}, 1000 );
}
function updateMainPhoto( selected ) {
	var id = selected.value;
	var photo_url = $('#product_option_photo_' + id).val();
	if (photo_url == 'false') {
		photo_url = $('#default_photo').val();
	}
	$('#product_photo').attr('src', photo_url);
}
$(document).ready(function() {
	$('#search_content').click(
		function() {
			var default_content = $('#search_content_default').val();
			var ele = $(this);
			if (ele.val() == default_content) {
				ele.val('');
			};
		}
		);
		$('#newsletter_email').click(
			function() {
				var default_content = $('#newsletter_email_default').val();
				var ele = $(this);
				if (ele.val() == default_content) {
					ele.val('');
				};
			}
			);
	$('.numbers').attr("maxLength", "3");
	$('.numbers').numeric();
	$('.empty_cart').click( function() {
			return (confirm('Are you sure you want to empty your cart?'));
	});
	$('.up_qty').click(function () {
		var p_id = this.id.replace('_up_qty', '');
		var ele = $("#" + p_id + "_qty");
		var val = ele.val();		
		val = parseInt(val);

		ele.val(val + 1);
		update_product_cost(p_id);
		update_cart_total();
		return false;
   });

	$('.down_qty').click(function () {
		var p_id = this.id.replace('_down_qty', '');
		var ele = $("#" + p_id + "_qty");
		var val = ele.val();		
		val = parseInt(val);
		if (val != 1) {
			ele.val(val - 1);
			update_product_cost(p_id);
			update_cart_total();
		};
			return false;
   });

	$('.qty').change(function () {
			update_total(this);
   });

	$('.qty').keydown(function(event){
		var ele = $(this);
	  switch (event.keyCode) {
			case 38:
				var val = ele.val();
				ele.val(parseInt(val)+1);
				update_total(this);
				break;
			case 40:
				var val = ele.val();
				var	val = parseInt(val);
				if (val != 1) {
					ele.val(val - 1);
					update_total(this);
				};
				break;
	  }
	});
	
	
});