﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

function pageLoad() {
	_bindLink();
}

$(document).ready(function() {
	// Any events bound here may not work after partial page postback.
	_bindEvents();
});

function callback_AddItem(data, textStatus) {
	addedToCart();
}

function callback_ViewCart(data, textStatus) {
	var ready = 1;
	var count = 0;
	var insuranceFound = false;
	var $tblShoppingCart_tbody = $("#tblShoppingCart tbody");
	var rowData = "<tbody>";

	$(data).find("data").find("items").find("row").each(function() {

		var id = $(this).attr("Id");
		var categoryName = $(this).attr("Name");
		var title = $(this).attr("Title");
		var quantity = $(this).attr("Quantity");
		var price = $(this).attr("Price");
		var available = $(this).attr("Available");
		var availableImage = "Images/CartItemAvailable.png";

		count++;

		if (available == 0) {
			availableImage = "Images/CartItemNotAvailable.png";
			ready = 0;
			$("#pnlCartRefresh").show();
			$("#pnlCartItemNotAvailable").show();
			$("#pnlCartCheckout").hide();
		}
		rowData += "<tr>";
		rowData += "<td style='width: 24px;'><img src='" + availableImage + "' /></td>"
		rowData += "<td style='white-space: nowrap;'>" + categoryName + " - " + title + "</td>";
		if (categoryName == "Insurance") {
			rowData += "<td style='width: 100px;'>" + quantity + "&nbsp;&nbsp;&middot;&nbsp;&nbsp;<a class='removeCartInsurance' id='" + id + "' href='#'>Remove</a></td>";
			insuranceFound = true;
		}
		else if (categoryName == "Insurance Required") {
			rowData += "<td style='width: 100px;'>" + quantity + "&nbsp;&nbsp;&middot;&nbsp;&nbsp;<a id='insuranceRequired' href='#'>Explain?</a></td>";
			insuranceFound = true;
		}
		else {
			rowData += "<td style='width: 100px;'>" + quantity + "&nbsp;&nbsp;&middot;&nbsp;&nbsp;<a class='removeCartItem' id='" + id + "' href='#'>Remove</a></td>";
		}
		rowData += "<td style='width: 100px;'>$ " + price + "</td>";
		rowData += "</tr>"

	});

	if (insuranceFound) {
		$("#divInsurance").hide();
	}
	else {
		$("#divInsurance").show();
	}

	$("#divSubTotalText").text("(Cart is empty)");
	$(data).find("data").find("subtotal").find("row").each(function() {
		var subTotal = $(this).attr("SubTotal");
		if (subTotal != undefined) {
			$("#divSubTotalText").text("(Subtotal: $" + subTotal + ")");
		}
	});

	rowData += "</tbody>";

	$tblShoppingCart_tbody.replaceWith(rowData);

	if (ready == 1) {
		$("#pnlCartRefresh").hide();
		$("#pnlCartItemNotAvailable").hide();
		$("#pnlCartCheckout").show();
	}

	if (count == 0) {
		$("#pnlCartRefresh").hide();
		$("#pnlCartItemNotAvailable").hide();
		$("#pnlCartCheckout").hide();
	}

	$("#tblShoppingCart tbody tr:odd").css("background-color", "#816f85");
	$("#tblShoppingCart tbody tr:even").css("background-color", "#a397a6");

	$("#tblShoppingCart .removeCartItem").mousedown(function() {
		var id = $(this).attr("id");
		_resetShoppingCart();
		ServiceCart.RemoveItem(id, callback_ViewCart);
	});

	$("#tblShoppingCart .removeCartInsurance").mousedown(function() {
		ServiceInsurance.RemoveInsurance(callback_RemoveInsurance_Success, callback_RemoveInsurance_Failed);

		function callback_RemoveInsurance_Success(result, eventArgs) {
			_resetShoppingCart();
			$("#divInsurance").show();
			ServiceCart.GetCart(callback_ViewCart);
		}

		function callback_RemoveInsurance_Failed(result, eventArgs) {
			alert("Unable to remove insurace.\nPlease contact support at support@shopcraze.com");
		}
	});

	$("#insuranceRequired").mousedown(function() {
		alert("Your account requires insurance to be purchased.\nFor more information please contact customer.service@shopcraze.com");
	});

	$("#pnlLoading").hide();
	$("#pnlCartNavigation").show();
}

function _bindEvents() {

	_bindItems();
	_bindShoppingCart();
	_bindPhotoGallery();
	_bindInsurance();
	_bindHighlight();
	_bindSale();

}

function _bindSale() {

	var $pnlSale = $(".pnlSale");
	var $pnlSaleClose = $("#pnlSaleClose");

	$pnlSaleClose.click(function() {
		$pnlSale.hide();
	});
}

function _bindReviewOrder() {

	var $divReviewOrder = $("#divReviewOrder");
	var $divToolTip = $("#divToolTip");

	var pageY = 0;
	var pageX = 0;

	$divReviewOrder.unbind();
	$divToolTip.unbind();

	$divReviewOrder.hover(function() {
		$(this).css("cursor", "pointer");
		$(this).toggleClass("linkPinkHover");
	}, function() {
		$(this).css("cursor", "default");
		$(this).toggleClass("linkPinkHover");
	});

	$divReviewOrder.mousedown(function(e) {
		pageX = e.pageX;
		pageY = e.pageY;
		ServiceCart.ReviewOrder(callback_ReviewOrder_Success);
	}).mouseleave(function() {
		$divToolTip.hide();
	}).mousemove(function(e) {
		$divToolTip.css({ 'top': e.pageY + 30, 'left': e.pageX - 220 });
	});

	function callback_ReviewOrder_Success(result, eventArgs) {

		if (result) {
			window.location.href = "PageCheckout.aspx";
		}
		else {
			var tipContent = "<div class='tip-text'><div class='pad5'>Your cart is empty.</div></div>";
			$divToolTip.html(tipContent);
			$divToolTip.css({ 'top': pageY + 30, 'left': pageX - 220 });
			$divToolTip.show();
		}
	}
}

function _bindInsurance() {

	$("#divAddInsurance").mousedown(function() {
		ServiceInsurance.AddInsurance(callback_AddInsurance_Success, callback_AddInsurance_Failed);
	});

	function callback_AddInsurance_Success(result, eventArgs) {
		_resetShoppingCart();
		$("#divInsurance").hide();
		ServiceCart.GetCart(callback_ViewCart);
	}

	function callback_AddInsurance_Failed(result, eventArgs) {
		alert("Unable to purchase insurace.\nPlease contact support at support@shopcraze.com");
	}
}

function _bindItems() {
	$(".itemPanel").find(".addToCart").unbind();
	$(".itemPanel").find(".notifyInStock").unbind();
	$(".itemPanel").unbind();

	$(".itemPanel").find(".addToCart").mousedown(function() {
		var id = $(this).find(".txtAddToCart").val();
		ServiceCart.AddItem(id, callback_AddItem);
		showNotificatonCart();
	});

	$(".itemPanel").find(".addToCart").mouseenter(function() {
		$(this).css("background-color", "#6a5a6e");
	});

	$(".itemPanel").find(".addToCart").mouseleave(function() {
		$(this).css("background-color", "#9c8f9f");
	});

	$(".itemPanel").find(".moreInformation").mouseenter(function() {
		$(this).css({ "background-color": "#6a5a6e", "cursor": "pointer" });
	});

	$(".itemPanel").find(".moreInformation").mousedown(function() {
		$(this).parent().find(".itemDescription").show();
	});

	$(".itemPanel").find(".moreInformation").mouseleave(function() {
		$(this).css({ "background-color": "#9c8f9f", "cursor": "default" });
		$(".itemPanel").find(".itemDescription").hide();
	});

	$(".itemPanel").find(".notifyInStock").mouseenter(function() {
		$(this).css("background-color", "#6a5a6e");
	});

	$(".itemPanel").find(".notifyInStock").mouseleave(function() {
		$(this).css("background-color", "#9c8f9f");
	});

	$(".itemPanel").mouseenter(function() {

		$(this).css({ 'border': 'solid 2px #523f56' });
	});

	$(".itemPanel").mouseleave(function() {
		$(this).css({ 'border': 'solid 2px #9c8f9f' });
	});
}

function _bindShoppingCart() {

	var $divViewCart = $("#divViewCart");
	$divViewCart.unbind();

	$divViewCart.hover(function() {
		$(this).css("cursor", "pointer");
		$(this).toggleClass("linkPinkHover");
	},
	function() {
		$(this).css("cursor", "default");
		$(this).toggleClass("linkPinkHover");
	});

	$divViewCart.mousedown(function() {
		_resetShoppingCart();
		ServiceCart.GetCart(callback_ViewCart);
	});

	$(".shoppingCart_Close").unbind();
	$("#divShoppingCart .shoppingCart_Close").mousedown(function() {
		$("#divShoppingCart").hide();
	});

	$("#pnlCartRefresh a").mousedown(function() {
		_resetShoppingCart();
		ServiceCart.RefreshCart(callback_ViewCart);
	});
}

function _bindPhotoGallery() {
	var $thumbnailImage = $(".thumbnailImage");
	var $imgPhotoGallery_ImageMain = $("#imgPhotoGallery_ImageMain");

	$thumbnailImage.mouseenter(function() {
		$(this).css({ "cursor": "pointer" });
	});

	$thumbnailImage.mouseleave(function() {
		$(this).css({ "cursor": "default" });
	});

	$thumbnailImage.mousedown(function() {
		var id = $(this).siblings(".txtPhotoGallery").val();
		$.post("Service/PhotoGallery.aspx", 'cmd=1&Id=' + id, callback_ViewPhotoGallery, "xml");
	});

	$("#photoGallery_Close").mousedown(function() {
		$imgPhotoGallery_ImageMain.attr("src", "Images/Loading.gif");
		$("#divPhotoGallery").hide();
	});

	function callback_ViewPhotoGallery(data, textStatus) {
		var count = 0;
		var rowData = "<tbody>";
		var imageMainURL = "";

		$(data).find("Pictures").find("Picture").each(function() {

			count++;

			var itemTitle = $(this).attr("ItemTitle");
			var pictureURL = $(this).attr("PictureURL");

			if (count == 1) {
				imageMainURL = pictureURL;
			}

			$("#divPhotoGallery_Header").text(itemTitle);

			rowData += "<tr>";
			rowData += "<td><img style='width: 100px; border: solid 2px #262626;' src='" + pictureURL + "' /></td>"
			rowData += "</tr>"

		});

		rowData += "</tbody>";

		var $tblPhotoGallery_Thumbs = $("#tblPhotoGallery_Thumbs");
		$tblPhotoGallery_Thumbs.find("tbody").replaceWith(rowData);
		$("#imgPhotoGallery_ImageMain").attr("src", imageMainURL);

		$tblPhotoGallery_Thumbs.find("img:first").css({ "border": "solid 2px #ab65a7" });

		$tblPhotoGallery_Thumbs.find("img").mouseenter(function() {
			$(this).css({ "cursor": "pointer" });
		});

		$tblPhotoGallery_Thumbs.find("img").mouseleave(function() {
			$(this).css({ "cursor": "default" });
		});

		$tblPhotoGallery_Thumbs.find("img").mousedown(function() {
			$tblPhotoGallery_Thumbs.find("img").css({ "border": "solid 2px #262626" });
			$(this).css({ "border": "solid 3px #ab65a7" });
			$imgPhotoGallery_ImageMain.attr("src", $(this).attr("src"));
		});

		if (count > 0) {
			$("#divPhotoGallery").show();
		}
	}
}

function _resetShoppingCart() {
	$("#tblShoppingCart tbody tr").remove();
	$("#divShoppingCart").show();
	$("#pnlCartNavigation").hide();
	$("#pnlCartRefresh").hide();
	$("#pnlCartItemNotAvailable").hide();
	$("#pnlCartCheckout").hide();
	$("#pnlLoading").show();
}

function _bindLink() {

	var $link = $(".link");

	$link.mouseenter(function() {
		$(this).css({ "cursor": "pointer" });
	});

	$link.mouseleave(function() {
		$(this).css({ "cursor": "default" });
	});

}

function _bindHighlight() {

	var $highlight = $(".highlight");

	$highlight.mouseenter(function() {
		$(this).css({ "background-color": "#a196a4" });
	});

	$highlight.mouseleave(function() {
		$(this).css({ "background-color": "#8e8191" });
	});
}
