jQuery(document).ready(function () {
	
	//update the price sheet on page load and if variation form fields are changed
	update_price_sheet();
	jQuery(".update_price_sheet").live('change', function() {
		update_price_sheet();
	});

	jQuery(".alert_min_quantity_24").live('change', function() {
		if (jQuery('#57').attr('checked') == 'checked' && jQuery('#wpsc_quantity_update_176').val() < 24) {
			alert('Billboard Speed is only available in quantities of 24 or more');
		}
	});

	// update the subtotal price when the variations OR quantity are altered.
	update_subtotal();
	jQuery(".wpsc_select_variation_commsol").live('change', function(){ update_subtotal(); } );


	// move more/less on upload page
	if (jQuery('#fileTypesInfoHolder').is('*')) {
		jQuery('#fileTypesInfoHolder').html(jQuery('#fileTypesInfoStaging').html());
	}

	// show more/less info on the proof page if desired
	jQuery('#expandContract').click( function() {
		var imgsrc = jQuery('#expandContract img').attr('src');
		if ( jQuery('#more:visible').length === 0) { //hidden
			jQuery('#more').show('slow');
			jQuery('#expandContract img').attr('src', imgsrc.replace('expand.gif', 'contract.gif'));
		} else { // visible
			jQuery('#more').hide('slow');
			jQuery('#expandContract img').attr('src', imgsrc.replace('contract.gif', 'expand.gif'));
		}
		return false;
	});

	// home page hovering
	jQuery('#banners, #lawnSigns, #magnets').mouseover( function() {
		jQuery(this).children('a').addClass('hover');
	});
	jQuery('#banners, #lawnSigns, #magnets').mouseleave( function() {
		jQuery(this).children('a').removeClass('hover');
	});
});

function update_subtotal() {
		var priceData = jQuery("input[name='priceSheet']").val();
		eval(priceData);
		var quantity = jQuery("input[name='wpsc_quantity_update']").val();
		var product_id = jQuery("input[name='product_id']").val();
		var unitPrice = 0;
		if (false == isNaN(quantity) && typeof prices !== 'undefined') {
			for (var i=0; i<prices.length; i++) {
				if (quantity >= prices[i].qty)
					unitPrice = prices[i].price;
				else
					break;
			}
			target_id = "product_price_"+product_id;
			price_target_selector = "#" + target_id + ".pricedisplay, ." + product_id + " .currentprice";
			jQuery(price_target_selector).html('$'+(unitPrice * quantity).toFixed(2));
		}	
		
		// disallow navigation to proof page if lawn sign with billboard quality and less than 24 quantity
		// see wpsc_submit_checkout in ajax.functions.php for server side code
		var button = "product_176_submit_button";
		if (jQuery("#"+button).is('*')) {
			var imgsrc = jQuery("#"+button).attr('src');
			if ((jQuery("#57").attr("checked") == "checked") && (quantity < 24)) {
						jQuery("#"+button).attr('disabled',true).attr('src', imgsrc.replace('btnNext.png', 'btnNext-disabled.png'));
			} else {
						jQuery("#"+button).removeAttr('disabled').attr('src', imgsrc.replace('btnNext-disabled.png', 'btnNext.png'));
			}
		}



		/*jQuery('option[value="0"]', this).attr('disabled', 'disabled');
		parent_form = jQuery(this).parents("form.product_form");
		if ( parent_form.length == 0 )
			return;		
		form_values =jQuery("input[name='product_id'], .wpsc_select_variation_commsol",parent_form).serialize( );
		jQuery.post( 'index.php?update_product_price_commsol=true', form_values, function(returned_data) {
			variation_msg = '';
			eval(returned_data);
			if( product_id != null ) {
				if( variation_msg != '' ){
					if(variation_status){
						jQuery("div#stock_display_"+product_id).removeClass('out_of_stock');	
						jQuery("div#stock_display_"+product_id).addClass('in_stock');	
					}else{
						jQuery("div#stock_display_"+product_id).removeClass('in_stock');	
						jQuery("div#stock_display_"+product_id).addClass('out_of_stock');	
					}
					
					jQuery("div#stock_display_"+product_id).html(variation_msg);
				
				}
				if( typeof(price) !== 'undefined' && typeof(old_price) !== 'undefined' && typeof(you_save) !== 'undefined' && typeof(numeric_price) !== 'undefined' ) {
					target_id = "product_price_"+product_id;
					price_target_selector = "#" + target_id + ".pricedisplay, ." + product_id + " .currentprice";
					second_target_id = "donation_price_"+product_id;
					third_target_id = "old_product_price_"+product_id;
					yousave_target_id = "yousave_"+product_id;
					buynow_id = "BB_BuyButtonForm"+product_id;
					if(jQuery("input#"+target_id).attr('type') == 'text') {
						jQuery("input#"+target_id).val(numeric_price);
					} else {
						jQuery(price_target_selector).html(price);
						jQuery("#"+third_target_id).html(old_price);
						jQuery("#"+yousave_target_id).html(you_save);
					}
					jQuery("input#"+second_target_id).val(numeric_price);
				}
			}
		});*/
	}

function update_price_sheet() {
	if (jQuery("#product_price_sheet").is('*')) {
		jQuery('#tbl_commsol_price').fadeOut(700);
		jQuery('#imgLoading').show();
		jQuery('option[value="0"]', this).attr('disabled', 'disabled');
		jQuery.ajaxSetup({async:false});
		get_prices();
		jQuery.ajaxSetup({async:true});
		var priceData = jQuery("input[name='priceSheet']").val();
		eval(priceData);
		jQuery('#tbl_commsol_price').find('tr:gt(0)').remove();
		for (var i=0; i<prices.length; i++) {
			jQuery('#tbl_commsol_price > tbody:last').append('<tr><td class="tdData">'+prices[i].qty+'</td><td class="tdData">$'+prices[i].price.toFixed(2)+'</td></tr>');
		}
		jQuery('#imgLoading').hide();
		jQuery('#tbl_commsol_price').fadeIn(700);
	}
};


function get_prices() {
	parent_form = jQuery("form.product_form");
	if ( parent_form.length == 0 )
		return;
	form_values =jQuery("input[name='product_id'], .wpsc_select_variation_commsol",parent_form).serialize( );
	jQuery.post( 'index.php?get_price_sheet_commsol=true', form_values, function(returned_data) {
		jQuery("input[name='priceSheet']").val(returned_data);
	});
};



