// PNG Fix
$(document).ready(function() {
	// Fix all inline PNG images and the element #container with the custom sizingMethod of "scale"
	$("img[src@='.png'], .btn a, .bar, h1 a, .new, .paper-top, .paper-bottom").pngfix();
});



$(document).ready(function(){
	FillCostLabels(); // do when loading page
	$("li.hassubtotal .jNiceSelectWrapper ul li a").click(FillCostLabels); // do on click as well
	//$('div.cost span').livequery(function(){FillCostLabels();});
});

function FillCostLabels() {
	// keep track of and populate total cost down the bottom of the form
	var updateTotal = true;
	
	var total_cost = 0;
	$('li.hassubtotal').each(function() {
		var text = $(this).find('.jNiceSelectWrapper ul li a.selected').html();
		var costlabel = $(this).find('.cost');
		var oldCostLabelText = $(costlabel).html();
		var textarray = text.split('$');
		if (textarray.length > 1) {
			var moneyarray = textarray[1].split('.');
			if (moneyarray[1] == '00') {
				$(costlabel).html('$' + moneyarray[0]);
				//if ($(costlabel).html() != oldCostLabelText) {
					total_cost += parseFloat(moneyarray[0]);
				//	updateTotal = true;
				//}
			} else {
				$(costlabel).html('$' + textarray[1]);
				//if ($(costlabel).html() != oldCostLabelText) {
					total_cost += parseFloat(textarray[1]);
				//	updateTotal = true;
				//}
			}
		} else {
			$(costlabel).html('');
		}
	});
	
	// handle the coupon effects
	/*
	var effectPanel = $('div.coupon div.cost');	
	var couponEffect = jQuery.trim(effectPanel.html());
	var effectArray = couponEffect.split('|');

	for (var i = 0; i < effectArray.length; i++) {
		var curEffect = effectArray[i];
		var effectType = curEffect.charAt(0);
		var effectAmount = parseFloat(curEffect.slice(1));
		switch (effectType) {
			case '%':  // percentage off
				total_cost *= ((100.0 - effectAmount) / 100.0);
				effectPanel.html(effectAmount + '% off');
				updateTotal = true;
				break;
			case 'P': // fixed product price
				// total_cost -= ???
				effectPanel.html('Fixed cost: $' + effectAmount);
				updateTotal = true;
				break;
			case 'S': // fixed shipping price
				// total_cost -= ???
				effectPanel.html('Shipping: $' + effectAmount);
				updateTotal = true;
				break;
			case 'X': // other message
				effectPanel.html(curEffect.slice(1));
				updateTotal = true;
				break;
		}
	}
	*/
	
	if (updateTotal) {
		// populate cost label
		$('p.cost span em').html('$' + total_cost.toFixed(2));
	}
}





// 'FancyBox' calls. (Pretty new lightbox)
$(document).ready(function(){
	$("a.zoom").fancybox();
	$("a.tastingnote").fancybox({
		frameWidth: 600, frameHeight: 200
	});
});
