$(document).ready(function() {
	// MENU -------------------/
	$("#menu .header").toggle(
		function () {
			$(this).siblings("ul.sub_menu").show();
		},
		function () {
			$(this).siblings("ul.sub_menu").hide();
		}
	);
	$("#menu .sub").hover(function() {
		$(this).children("ul.fly_out").attr('style', 'left: 256px');
	}, function() {
		$(this).children("ul.fly_out").attr('style', 'left: -9999px');
	});
	
	$("a.fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'overlayOpacity':	0.7,
		'speedIn'		:	300, 
		'speedOut'		:	300, 
		'titlePosition'	:	'over'
	});
	
	$('#image_rotation').innerfade({
		speed: 'slow',
		timeout: 7000,
		type: 'sequence',
		containerheight: '241px'
	});
	
	$(".button_order_info").click(function(){
		location.href = "/order.php";
	});
	
	//ACCOUNT SIGNIN -----------/
	$("#form_account_sign_in #password").keyup(function(){
		$("#form_account_sign_in #customer_returning").attr("checked", "checked");
	});
	
	// PRODUCT OPTIONS ---------- /
	$("#form_choose_options .product_option").change(function(){
		var i = 0;
		var postData = {};

		$("#form_choose_options .product_option option:selected").each(function(){
			postData["option_" + i] = $(this).val();
			i++;
		});
		postData["product_id"]	= $("#form_choose_options #id").val();
		postData["qty"]			= $("#form_choose_options #qty").val();
		$.ajax({
			type:		"POST",
			url:		"/ajax/get_option_info.php",
			data:		(postData),
			async:		false,
			success:	function(xml){
							$(xml).find("element").each(function(){
								var dataId = $(this).find("id").text();
								var dataPriceChange = $(this).find("price_change").text();
								var dataOptionsTotal = $(this).find("options_total").text();
								var dataItemsTotal = $(this).find("items_total").text();
								var dataGrandTotal = $(this).find("grand_total").text();
								$("#form_choose_options #option_cost_" + dataId).html(dataPriceChange);
								$("#form_choose_options #options_total").html(dataOptionsTotal);
								$("#form_choose_options #items_total").html(dataItemsTotal);
								$("#form_choose_options #grand_total").html(dataGrandTotal);
							});
						}
		});
		return false;
	});
	
	// PRODUCT OPTIONS FOR DETAILS ---------- /
	$(".update_options .product_option").change(function(){
		var i = 0;
		var postData = {};
		var oldValues = {};
		var theID = $(".product_id").val();
		
		$("#prod_cost").html("Calculating...");

		$(".product_option").find("option:selected").each(function(){
			postData["option_" + i] = $(this).val();
			var catId = $(this).parent().attr("id");
			catId = catId.replace("option_", "");
			oldValues[catId] = $(this).val();
			i++;
		});
		var catId = $(this).attr("id");
		catId = catId.replace("option_", "");
		oldValues[catId] = $(this).val();
		postData["product_id"]	= theID;
		postData["email"]		= $("#email").val();
		postData["qty"]			= "1";
		var qString = "";
		$.each(postData, function(key, value) {
			qString = qString + "&" + key + "=" + value;
		});		hingeTypeOptions = "";
		//alert(qString);
		$.ajax({
			type:		"POST",
			url:		"/ajax/get_option_info.php",
			data:		(postData),
			async:		false,
			success:	function(xml){
							$("#prod_cost").html("$0.00");
							$(xml).find("element").each(function(){
								var dataId = $(this).find("id").text();
								var dataPriceChange = $(this).find("price_change").text();
								var dataOptionsTotal = $(this).find("options_total").text();
								var dataItemsTotal = $(this).find("items_total").text();
								var dataGrandTotal = $(this).find("grand_total").text();
								$("#prod_cost").html(dataGrandTotal);
							});
						},
			error:	function(xml){
							$("#prod_cost").html("Sorry, could not calculate price");
						}
		});
		return false;
	});
	
	// VIEW CART ---------- /
	$(".view_cart_update").click(function(){
		$("#action").val("update");
		$("#form_cart").submit();
	});
	$(".checkout_button").click(function(){
		$("#action").val("checkout");
		$("#form_cart").submit();
	});
	$(".continue_shopping_button").click(function(){
		$("#action").val("continue_shopping");
		$("#form_cart").submit();
	});
	$(".removeItem").click(function(){
		return confirm("Are you sure you want to remove this product from your cart?");
	});
	$("#view_cart_calculate_shipping").click(function(){
		location.href = "view_cart.php?country=" + $("#country").val() + "&zip=" + $("#zip").val() + "";
	});
	
	// CHECKOUT ---------- /
	$(".update_shipping_method").change(function(){
		location.href = "/checkout.php?shipping_method_id=" + this.value;
	});
	$("#checkout_place_order_button").click(function(e){
		if(!$("#checkout_agree").is(':checked')) {
			alert("You must agree to the terms before placing your order");
			e.preventDefault;
			return false;
		}
		
		$("#checkout_place_order").hide("fast");
		$("#checkout_processing").show("fast");
		$("#form_checkout").submit();
		return false;
	});
	$("#apply_promo_code").click(function(){
		$("#form_promo_code").submit();
		return false;
	});	
	
	//MISC ---------------/
	function number_format(a, b, c, d) {
		a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
		e = a + '';
		f = e.split('.');
		if (!f[0]) {
			f[0] = '0';
		}
		if (!f[1]) {
			f[1] = '';
		}
		if (f[1].length < b) {
			g = f[1];
			for (i=f[1].length + 1; i <= b; i++) {
				g += '0';
			}
			f[1] = g;
		}
		if(d != '' && f[0].length > 3) {
			h = f[0];
			f[0] = '';
			for(j = 3; j < h.length; j+=3) {
				i = h.slice(h.length - j, h.length - j + 3);
				f[0] = d + i +  f[0] + '';
			}
			j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
			f[0] = j + f[0];
		}
		c = (b <= 0) ? '' : c;
		return f[0] + c + f[1];
	}
	
	$(".select_rel").each(function(){
		$(this).val($(this).attr("rel"));
	});
	
	$(".focus_me").focus();
});

$.fn.preloadImages = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
