var shipAction = "";
var billAction = "";
var updateAction = "";

$(document).ready(function() {

	$("#cvv2-open").click(function(e) {
		showCvv();
		e.preventDefault();
	});

	$("#cvv2-window").click(function(e) {
		$("#cvv2-window").slideUp();
		e.preventDefault();
	});

	$("#add-new-shipping, #add-new-card").click(function(e) {
		var shippingUrl = $(this).attr("href");
		shipAction = "add";
		billAction = "add";
		openPopup(shippingUrl, e);
		e.preventDefault();
	});

	$("#edit-shipping-info, #edit-card-info").live("click",function(e) {
		var shippingUrl = $(this).attr("href");
		shipAction = "edit";
		billAction = "edit";
		openPopup(shippingUrl, e);
		e.preventDefault();
	});
	
	$("#account-cards").change(function() {
		$("#changedPaymentProfile").attr("disabled",false);
		$("#bill-keyvalue").val($(this).val());
		$("#bill-acct-form").submit();
	});
	
	$("#account-shipping").change(function() {
		$("#changedAddress").attr("disabled",false);
		$("#ship-keyvalue").val($(this).val());
		$("#ship-acct-form").submit();
	});

	$("#bill-acct-form").submit(function() {
		var billUrl = "/account/bill_info_display_ajax.jsp";
		var billParams = "";
		$("#bill-acct-form input").each(function() {
		  if(($(this).attr("name") != "") && ($(this).attr("disabled") == false)) {
	      	if($(this).attr("type") != "hidden") {
	      	  $(this).val($.trim($(this).val()));
	            encodedVal = encodeURIComponent($(this).val());
	            billParams += $(this).attr("name") + "=" + encodedVal + "&";
	      	} else {
	      		billParams += $(this).attr("name") + "=" + $(this).val() + "&";
	      	}
		  }    	      
		});
		//alert(billParams);
		$.post(billUrl,billParams,function(data) {
			$("#billing-info-content").html(data);
			setBar();
		});
		return false;
	});
	
	$("#ship-acct-form").submit(function() {
		var shipUrl = "/account/ship_info_display_ajax.jsp";
		var shipParams = "";
		$("#ship-acct-form input").each(function() {
		  if(($(this).attr("name") != "") && ($(this).attr("disabled") == false)) {
	      	if($(this).attr("type") != "hidden") {
	      	  $(this).val($.trim($(this).val()));
	            encodedVal = encodeURIComponent($(this).val());
	            shipParams += $(this).attr("name") + "=" + encodedVal + "&";
	      	} else {
	      		shipParams += $(this).attr("name") + "=" + $(this).val() + "&";
	      	}
		  }    	      
		});
		//alert(shipParams);
		$.post(shipUrl,shipParams,function(data) {
			$("#shipping-info-content").html(data);
		});
		return false;
	});

	if ( ($(".my-account-page #account-cards option").length > 0) && ($(".my-account-page #account-cards option:first").val().length != 0) ) {
		$("#changedPaymentProfile").attr("disabled",false);
		$("#bill-keyvalue").val($("#account-cards option:first").val());
		//alert($("#bill-keyvalue").val());
		$("#bill-acct-form").submit();
	}
	
	if ( ($(".my-account-page #account-shipping option").length > 0) && ($(".my-account-page #account-shipping option:first").val().length != 0) ) {
		$("#changedAddress").attr("disabled",false);
		$("#ship-keyvalue").val($("#account-shipping option:first").val());
		//alert($("#ship-keyvalue").val());
		$("#ship-acct-form").submit();
	}
	
	$(".track-order-link").click(function(e) {
		var trackPage = "/account/trackingInfo.jsp";
		var trackParams = "ci=" + $(this).attr("id");
		var clicky = e.pageY;
		$.get(trackPage, trackParams, function(data) {
			lightBox();
			$("#tracking-window").html(data);
			$("#tracking-window-wrapper").css({"top":clicky-150,"left":$(document).width()/2-200});
			$("#tracking-window-wrapper").show();
		});
		e.preventDefault();
	});
	
	$("#enlarge-window").click(function(e) {
      $(this).fadeOut();
      e.preventDefault();
    });

    $(".view-invoice-link").click(function(e) {
      var orderid = $(this).attr("href").split("#")[1];
      var invoiceUrl = "/account/view-invoice.jsp?orderId="+orderid;
      $("#invoice-frame").attr("src",invoiceUrl);
    
        lightBox();
		$("#invoice-display-win").css({"top":$(document).scrollTop()+50,"left":$(document).width()/2-340}).show();

      e.preventDefault();
	});

});

function openPopup(billingUrl, e) {
	lightBox();
	$("#acct-ae-window").css({
				"left" : ($(document).width() / 2 - ($("#acct-ae-window")
						.width() / 2))
						+ "px",
				"top" : "50px"
			});
	$("#acct-ae-frame").attr("src", billingUrl + "?action=" + billAction);
	$("#acct-ae-window").show();
}

function addShipEvents(e) {
	$("#cancel-add-shipping").click(function(e) {
		cancelShipping();
		e.preventDefault();
	});
	$("#add-shipping-form").submit(function(e) {
		var shipUrl = "/checkout/shipping_add_edit.jsp";
		var shipParam = "action=" + shipAction;
		if (updateAction.length != 0) {
			shipParam += "&key=" + encodeURIComponent(updateAction);
		}
	});
	$("#choose-shipping-select").change(function() {
		shipAction = "edit";
		updateAction = $("#choose-shipping-select").val();
		$("#add-shipping-form").submit();
		updateAction = "";
	});
}

function addBillEvents(e) {
	$("#cancel-add-card").bind("click", function(e) {
		cancelBilling();
		e.preventDefault();
	});
	$("#add-card-form").submit(function(e) {
		var billUrl = "/checkout/billing_add_edit.jsp";
		var billParam = "action=" + billAction;
		if (updateAction.length != 0) {
			billParam += "&key=" + encodeURIComponent(updateAction);
		}
	});
	$("#choose-card-select").change(function() {
		billAction = "edit";
		updateAction = $("#choose-card-select").val();
		$("#add-card-form").submit();
		updateAction = "";
	});
}

function cancelShipping() {
	closeLightBox();
	$("#acct-ae-frame").attr("src","/sfd/plain.html");
	$("#tracking-window-wrapper").hide();
	$("#acct-ae-window").hide();
}

function cancelBilling() {
	closeLightBox();
	$("#acct-ae-frame").attr("src","/sfd/plain.html");
	$("#tracking-window-wrapper").hide();
	$("#acct-ae-window").hide();
}

function showCvv() {
	var divWidth = $("#cvv2-window").width();
	var browserWidth = $(document).width();
	// calculate the center of the page using the browser and element dimensions
	var x = (browserWidth - divWidth) / 2;
	$("#cvv2-window").css( {
		"left" : x + "px",
		"top" : $(document).scrollTop() + 150,
		"z-index" : "1050"
	});
	$("#cvv2-window").slideDown();
}

function changedValues(val) {
	document.orderform.changedAddress.disabled = false;
	document.orderform.keyValue.value = val.value;
	document.orderform.submit();
}

function changedPaymentGroupValue(v) {
	document.orderform.changedPaymentGroup.disabled = false;
	document.orderform.keyValue.value = v.value;
	document.orderform.submit();
}

function shippingMethod() {

	document.orderform.submit();
}
