
var base_url = "/";
$(window).bind('scroll resize', function(e){
	$window=jQuery(window);
	$control = $('#left-menu');
	this.$control.css({top:($window.scrollTop()+ 192) +'px'});
});
var side = 'billing';
function getStatesOptions(_country,sides){
	side = sides;
	$.ajax({
		  type: "POST",
		  url:"ajax/country",
		  data: {country:_country},
		  success: populate_states});
}

function populate_states (result) {
	
	var states = eval ('('+result+')');
	var target = "#"+side+"-state"; 
	$(target).empty();
	
	var setoptions = "";
	for (var i = 0; i < states.length; i++) 
		setoptions += '<option value="' + states[i].oV + '">' + states[i].oT + '</option>';
	
	$(target).html(setoptions);
	if (!$.browser.msie)
		$("option:first", target).attr("selected","selected");
	$(target).show();
}

function copySelectOptions(source,dest) {
	$(dest).empty();
	$(dest).html($(source).html()).change(); 
	$(dest).selectOptions($(source).val());
}


$(document).ready(function (){
	
	$('#billing-country').change(function(){
		getStatesOptions(this.options[this.options.selectedIndex].value,'billing');
	});
	$('#shipping-country').change(function(){
		getStatesOptions(this.options[this.options.selectedIndex].value,'shipping');
	});
	 //when the same as billing is checked or unchecked
    $('#same_billing').change(function() {
    	var isChecked = ($("#same_billing").is(":checked"))? true: false;
    	$('input[name*=billing]').each( function() {
	         var $this = $(this);
	         var shippingName = $this.attr('name').replace(/billing/,'shipping');
	         $('input[name$=' + shippingName + ']').val( isChecked?$this.val():"" );
	    });
    	$("#shipping-country").selectOptions($("#billing-country").val());               
    	copySelectOptions("#billing-state","#shipping-state");
    });
    
	$('#what-is-this').tooltip(
		{bodyHandler: function() {
		return $('#tool-tip1').html();
		},
		showURL: false});
	
	$("input[rel^='remove']").click(
			function () {
			var target = $(this).parent().attr('href');
			showDialog('Confirm','<br/>Are you sure you want to remove this item from your cart? It\'s so cool!<br /><br /><br /><center><input name="yes" type="button" value="yes" onclick="window.location=\''+target+'\'"> <input name="no" type="button" value="no" onclick="hideDialog(); return false;"> </center>','error');
			return false;
			});
	$('#cupon-code').click (function () {
		var html ="";
		html += "<div id='error-message' class='error_message' style='margin:0;padding:0;display: none;'><img src='"+base_url+"images/ajax-loader-circlethickbox.gif' /></div>";
		html += '<div id="add-code">Enter gift card or discount coupon<br /><br /><input type="text" name="user-cupon" id="user-cupon" value="" size="14" maxlength="20" /> <input name="no" type="button" value="Add" onclick="get_input_code(); return false;">';
		showDialog('Gift or Coupon Code', html, 'error');	
	});	
});

function add_to_cart()
{
	
}
function handleResponse(response)
{
	response = eval('('+response+')');
	if (!response)
	{
		showDialog('Error','Sorry, but this size is temporarily sold out. Check back soon!','error',5);
		return false;
	}
	else
	{
		document.getElementById('add_cart').submit();
	}	
}

function check_size() {
	var sizesel = document.getElementById('size');
	size = sizesel.options[sizesel.selectedIndex].value;
	if (size==0){
		showDialog('Error','Please "Select a Size" for your item.','error',3);
		return false;
	} 

	var qty = document.getElementById('product-qty').value;
	if (qty &&  parseInt(qty)<=0 && qty!='')
	{
		showDialog('Error','Please select a numeric quantity for your item.','error',2);
		return false;
	}
	$.ajax({
		  type: "POST",
		  url: base_url+"ajax/product_available/",
		  data: {product_id: $('input')[0].value,
				 product_size: size	
				},
		  success: handleResponse});
}

function get_input_code()
{
$("#error-message").show();
$("#error-message").html("<img src='"+base_url+"images/ajax-loader-circlethickbox.gif' />");
$("#add-code").hide();
var _code = $("#user-cupon").attr('value');
	$.ajax({
		  type: "POST",
		  url: base_url+"ajax/get_cupon_code",
		  data: {code:_code},
		  success: update_code});
} 
function update_code (response) {
	response = eval ('('+response+')');

	if (response == false){
		
		$("#error-message").html('No such promo code!');
		$("#add-code").show('slow');
		$("#error-message").hide(3000);
		return false;
	}		
	var cupon = $("#user-cupon").attr('value');
	$("#coupon_code").val(cupon);
	inline_discount (response);
}
function inline_discount (response,cupon)
{
	if (!parseFloat(response.discount))
		return false;
	$("#error-message").hide();
	$("#discount").html('$'+ response.discount);
	$('#total').html('$'+response.total);
	hideDialog();
}

function submit_cart (cart_finish)
{
	cart_finish = eval(cart_finish);
	
	if (cart_finish) $('#finish_order').val('true');
	else $('#finish_order').val('false');
	
	$('#update_cart').submit();	
}

function place_order() {
	showDialog('', 'Please wait while your order is being processed. <br />'+ "<img src='"+base_url+"images/ajax-loader-circlethickbox.gif' />", 'error');
	setTimeout("page_ready()",800);
	$(window).unload( function () {hideDialog();} );
}
function page_ready() {
	$('#place_order').submit();
}
