// JavaScript Document
$(document).ready(function() {
    $('#productmenu ul li a').each(function() {
		$(this).css({'width': ($("#productmenu").width() / $('#productmenu ul li a').length)+'px'});											   
	});
						   												  
    $('#productmenu ul li a div').each(function() {
		$(this).css({'top': ((($('#productmenu ul li a').height()/2) - ($(this).height())/2)) -4 +'px'});											   
	});
	
	$('.container_12').each(function() {
		$(this).append('<div class="clear"></div>');
	});
	
	$('.content').each(function() {
		var container1 = $(this).parent();
		var content = $(this).html();
		$(this).replaceWith('<div class="flex-box"><div class="top-edge"><div class="right-edge"><div class="bottom-edge"><div class="left-edge"><div class="top-left"><div class="top-right"><div class="bottom-left"><div class="bottom-right"><div class="content">' + content + '</div></div></div></div></div></div></div></div></div></div>');
	});
	
	$('.blue-content').each(function() {
		var container1 = $(this).parent();
		var content = $(this).html();
		$(this).replaceWith('<div class="blue-flex-box"><div class="top-edge"><div class="right-edge"><div class="bottom-edge"><div class="left-edge"><div class="top-left"><div class="top-right"><div class="bottom-left"><div class="bottom-right"><div class="blue-content">' + content + '</div></div></div></div></div></div></div></div></div></div>');
	});
	
	$('.container_12 .container_12').each(function() {
		if ($(this).find('.content') != null) {
			var newHeight = $(this).height() - 30;
			//$(this).find('.content').css({'height': newHeight+'px'});
		}
	});
	
	$("#SameAsBilling").toggle( 
	function () {
		$("#SameAsBilling").attr("checked", "checked");
		$("#DeliveryName1").attr("disabled", true); 
		$("#DeliveryName2").attr("disabled", true); 
		$("#DeliveryCompany").attr("disabled", true); 
		$("#DeliveryStreetAddress").attr("disabled", true); 
		$("#DeliveryCity").attr("disabled", true); 
		$("#DeliveryState").attr("disabled", true); 
		$("#DeliveryPostalCode").attr("disabled", true); 
		$("#DeliveryCountry").attr("disabled", true); 
	}, 
	function () { 
		$("#SameAsBilling").removeAttr("checked");
		$("#DeliveryName1").removeAttr("disabled"); 
		$("#DeliveryName2").removeAttr("disabled"); 
		$("#DeliveryCompany").removeAttr("disabled"); 
		$("#DeliveryStreetAddress").removeAttr("disabled"); 
		$("#DeliveryCity").removeAttr("disabled"); 
		$("#DeliveryState").removeAttr("disabled"); 
		$("#DeliveryPostalCode").removeAttr("disabled"); 
		$("#DeliveryCountry").removeAttr("disabled"); 
	});
	
	$("#check_all").change(function() {
		if ($(this).attr("checked")) {
			$('form input[type=checkbox]').each(function() {
				$(this).attr("checked", "checked");
			});
		} else {
			$('form input[type=checkbox]').each(function() {
				$(this).removeAttr("checked");
			});
		}
	});
	
	$("a[rel='tab-items']").click(function() {
		$(this).addClass('active');
		$("a[rel='tab-details']").removeClass('active');
		$(".tab-items").css("display","block");
		$(".tab-details").css("display","none");
		return false;
	});
	$("a[rel='tab-details']").click(function() {
		$(this).addClass('active');
		$("a[rel='tab-items']").removeClass('active');
		$(".tab-items").css("display","none");
		$(".tab-details").css("display","block");
		return false;
	});
	
	$('#Vancouver').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'van_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#van_details').css('display') == 'block') {
			$('#van_details').hide('slow');
		} else {
			$('#van_details').show('slow');
		}
		return false;
	});


	$('#Edmonton').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'edm_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#edm_details').css('display') == 'block') {
			$('#edm_details').hide('slow');
		} else {
			$('#edm_details').show('slow');
		}
		return false;
	});


	$('#Calgary').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'cal_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#cal_details').css('display') == 'block') {
			$('#cal_details').hide('slow');
		} else {
			$('#cal_details').show('slow');
		}
		return false;
	});


	$('#Winnipeg').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'winn_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#winn_details').css('display') == 'block') {
			$('#winn_details').hide('slow');
		} else {
			$('#winn_details').show('slow');
		}
		return false;
	});


	$('#Toronto').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'tor_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#tor_details').css('display') == 'block') {
			$('#tor_details').hide('slow');
		} else {
			$('#tor_details').show('slow');
		}
		return false;
	});


	$('#Windsor').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'wind_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#wind_details').css('display') == 'block') {
			$('#wind_details').hide('slow');
		} else {
			$('#wind_details').show('slow');
		}
		return false;
	});


	$('#Montreal').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'mon_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#mon_details').css('display') == 'block') {
			$('#mon_details').hide('slow');
		} else {
			$('#mon_details').show('slow');
		}
		return false;
	});


	$('#Dartmouth').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'dar_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#dar_details').css('display') == 'block') {
			$('#dar_details').hide('slow');
		} else {
			$('#dar_details').show('slow');
		}
		return false;
	});


	$('#StJohns').click(function() {
		$('.city_details').each(function() {
			if ($(this).css('display') == "block" && $(this).attr('id') != 'stj_details') {
				$(this).hide('slow');
			}
		});
		
		if ($('#stj_details').css('display') == 'block') {
			$('#stj_details').hide('slow');
		} else {
			$('#stj_details').show('slow');
		}
		return false;
	});

});

function updateCart(itemID, itemQty, itemStock) {	
	if (itemQty < 1) {
		itemQty = 1;
		$("#qty_" + itemID).val(itemQty);
	}

	$.ajax({ 
		type: "GET",
		url: '/emplastics/20100218/site/lib/ajax/cart.php', 
		data: ({
			update : 'update',
			product : itemID,
			qty : itemQty
		}),
		success: function(data, textStatus, XMLHttpRequest){
			$("#item_total_" + itemID).html("$" + data);
			var total = 0;
			
			$(".item_total").each(function() {
				item_amount =  $(this).html().replace("$", "")-0
				total = total + item_amount;
			});
			
			$("#order_total").html("$" + total.toFixed(2));
		}
	});	
	
	if (itemQty > itemStock) {
		window.location.reload();
	} else {
		$("#substitute_" + itemID).css("display", "none");
	}
}
