$(document).ready(function(){
	check_country('billing');
	check_country('shipping');
	check_ship_to();
	check_payment_method();
	check_shipping_method();
	
	
	/*
		buttons
	*/
	if ($('#add_to_cart')) {
		//set up the ids of all the buttons			
		var buttons = new Array(
			'add_to_cart',
			'update_cart',
			'check_out',
			'keep_shopping',
			'add_to_wishlist',
			'update_wishlist',
			'send_wishlist'
		);
		
		//create image containers and set src for each button
		for (var i = 0; i < buttons.length; i++) {
			eval('var ' + buttons[i] + '_up = new Image();');
			eval('var ' + buttons[i] + '_down = new Image();');
			
			eval(buttons[i] + '_up.src = \'/images/content/' + buttons[i] + '.jpg\';');
			eval(buttons[i] + '_down.src = \'/images/content/' + buttons[i] + '_down.jpg\';');
		}
		
		//set the actions for all of the buttons
		var ids = '#' + buttons.join(',#');
		
		$(ids).mouseup(function(){
			eval('var src = ' + $(this).attr('id') + '_up.src;');
			$(this).attr('src',src);
		});
		$(ids).mouseout(function(){
			eval('var src = ' + $(this).attr('id') + '_up.src;');
			$(this).attr('src',src);
		});
		$(ids).mousedown(function(){
			eval('var src = ' + $(this).attr('id') + '_down.src;');
			$(this).attr('src',src);
		});
	}
	
	
	
	/*
		IE fix for image inputs (thanks for nothing, bill gates!)
	*/
	$('input[@name=submit]').click(function(){
		$('#real_submit').val($(this).val());
		return true;
	});
	
	
	
	
	/*
		jquery impromptu alert
	*/
	$('a[@rel=alert]').click(function(){
		$.prompt($(this).attr('title'),{ show:'slideDown' });
		return false;
	});
	/*
	*/
	
	
	
	/*
		mailing list validation
	*/
	$('div#mailing_list form').submit(function(){
		if ($('input[@name=fields_email]').val() == '') {
			alert('The email field is required.')
			return false;
		}
		
		return true;
	});
	/*
	*/
	
	
	
	$('a#recommend').click(function(){
		window.open('','page','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=410,left=50,top=50,titlebar=yes');
	});
	
	
	$('input[@name=fields_email]').focus(function(){
		if ($(this).val() == 'Sign Up Here!') {
			$(this).val('');
		}
	});
	$('input[@name=fields_email]').blur(function(){
		if ($(this).val() == '') {
			$(this).val('Sign Up Here!');
		}
	});
	
	

	$('select[@name=billing_country]').change(function(){
		check_country('billing');
	});
	$('select[@name=shipping_country]').change(function(){
		check_country('shipping');
	});
	
	
	$('input[@name=ship_to_billing]').click(function(){
		check_ship_to();
	});
	$('input[@name=ship_to_billing]').change(function(){
		check_ship_to();
	});
	
	$('select[@name=payment_method]').click(function(){
		check_payment_method();
	});
	$('select[@name=payment_method]').change(function(){
		check_payment_method();
	});
});



function check_country(address,skip_else) {
	var country = $('select[@name=' + address + '_country]').val();
	var country_province =  $('#' + address + '_province_' + country)[0]
	
	//province
	$('.' + address + '_province').hide();
	$('.' + address + '_province').find('input,select').attr('disabled','disabled');
	//state
	$('#' + address + '_state').find('input').attr('disabled','disabled');
	$('#' + address + '_state').find('label').addClass('disabled');
	
	if (country_province) {
		//province
		$(country_province).show();
		$(country_province).find('input,select').removeAttr('disabled');
	} else if (country != 'US') {
		//province
		$('#' + address + '_province').show();
		$('#' + address + '_province').find('input,select').removeAttr('disabled');
	} else {
		//state
		$('#' + address + '_state').find('input').removeAttr('disabled');
		$('#' + address + '_state').find('label').removeClass('disabled');
	}
}



function check_ship_to() {
	if ($('input[@name=ship_to_billing]').attr('disabled') == true) {
		return;
	}
	
	if ($('input[@name=ship_to_billing]').attr('checked')) {
		$('#shipping label').addClass('disabled');
		$('#shipping input,#shipping select').attr('disabled','disabled');
	} else {
		$('#shipping label').removeClass('disabled');
		$('#shipping input,#shipping select').removeAttr('disabled');
		check_country('shipping',true);
	}
}



function check_payment_method() {
	if ($('select[@name=payment_method]').attr('disabled') == true) {
		return;
	}
	
	if ($('select[@name=payment_method]').val() == 'PayPal') {
		$('select[@name=payment_method]').parent().siblings().children('label').addClass('disabled');
		$('select[@name=payment_method]').parent().siblings().children('input,select').attr('disabled','disabled');
	} else {
		$('select[@name=payment_method]').parent().siblings().children('label').removeClass('disabled');
		$('select[@name=payment_method]').parent().siblings().children('input,select').removeAttr('disabled');
	}
}



function check_shipping_method(obj) {
	if ($('select[@name=shipping]').length) {
		var total = parseFloat(total_before_shipping) + parseFloat(shipping_prices[$('select[@name=shipping]').val()]);
		$($('#total').text('$' + total.toFixed(2)));
	}
}



