﻿$(document).ready(function()
{
  // hijax all of the purchase button submits
  // let's not hijax purchase button clicks when there is no sidebar item with the shopping cart summary
	if ($("#shoppingCartSidebarItem").length > 0)
	{
		$("form.purchaseForm").livequery("submit", function(event)
		{
			event.preventDefault();

			var actionUrl = $(this).attr("action");
			var data = $(this).serialize();
			// send a POST Ajax request and replace the shopping cart sidebar item with the returned data
			$.post(actionUrl, data, function(result)
			{
				$("#shoppingCartSidebarItem").replaceWith(result);
			});
			$(this).effect("transfer", { to: "#shoppingCartSidebarItem", className: "ui-effects-transfer" }, "slow");
		});
	}

	//hijax wishlist submit form
	if ($(".wishlistLink").length > 0)
	{
		$("form.wishlistForm").livequery("submit", function(event)
		{
			event.preventDefault();
			var actionUrl = $(this).attr("action");
			var data = $(this).serialize();
//			// send a POST Ajax request and replace the shopping cart sidebar item with the returned data
			var form = $(this);
			$.post(actionUrl, data, function(result)
			{
				form.effect("transfer", { to: ".wishlistLink", className: "ui-effects-transfer" }, "slow");
			});
		});
	}
	
//	$(".price").livequery(function()
//	{
//		$(this).tooltip({ delay: 0 });
//	});

	var categoryTree = $(".contentLeft .categoryTree");
	if (categoryTree.length > 0)
	{
		categoryTree.treeview(
		{
			collapsed: "true"
		});
	}
	//hijax questinaire submit button 
	if ($("#questionnaireSidebarItem form.questionnaireForm").length > 0)
	{
		$("#questionnaireSidebarItem form.questionnaireForm").livequery("submit", function(event)
		{
			event.preventDefault();

			var actionUrl = $(this).attr("action");
			var data = $(this).serialize();
			// send a POST Ajax request and replace the questionaire sidebar item with the returned data
			$("#questionnaireSidebarItem div.content").addClass("ajaxLoad");
			$.post(actionUrl, data, function(result)
			{
				$("#questionnaireSidebarItem").replaceWith(result);
			});
		});	}
	//restyle questinaire side bar fo AJAX use
	if ($("#questionnaireSidebarItem form.questionnaireForm").length > 0 && $("#questionnaireSidebarItem INPUT[type=radio]").length > 0 
		&& $("#questionnaireSidebarItem INPUT[type=text]").length == 0)
	{
		$("#questionnaireSidebarItem form.questionnaireForm").addClass("ajaxForm");
		$("#questionnaireSidebarItem form.questionnaireForm div.answer div.text").live("click", function(event)
		{
			if ($("#questionnaireSidebarItem form.questionnaireForm").length > 0)
			{
				$(event.target.parentNode).find("INPUT[type=radio]")[0].checked = true;
				$("#questionnaireSidebarItem form.questionnaireForm").submit();
			}
		});
	}

	//fix target="_blank" xhtml strict problem
	$('a.external').attr('target', '_blank');

	//set autocomplete=off on password
	$('#login_Password').attr('autocomplete', 'Off');
});

