try {
	document.execCommand('BackgroundImageCache', false, true);
}
catch (e) { }

$(document).ready(function () {
	slideDiv();
	limitChars();
	load_tool_tip();
	ButtonLinks();
});

// this script is used to show/hide the panels on the checkout page
// when the "delivery details" panel is expanded we hide the payment panel to force the user to click continue and refresh price details
// when "customer details" panel is expanded we hide the delivery and payment panels to force the user to click continue and save the customer details
// Also used on the transfer pages for similar reasons, ie when a preceeding panel is expanded the subsequent ones are hidden
function slideDiv() {
	$("h3 a.item_title").click(function (e) {
		var div = $(this).parents("h3").siblings(".view_item");
		if (div.is(":hidden")) {
			$(this).addClass("active");
			div.slideDown("slow");
			// checkout
			if (div.attr("id").indexOf("divDelivery") > -1) {
				$("div[id$=pnlPayment]").hide();
			}
			if (div.attr("id").indexOf("divCustomer") > -1) {
				$("div[id$=pnlPayment]").hide();
				$("div[id$=pnlDeliveryDetails]").hide();
			}
			//transfer wine
			if (div.attr("id").indexOf("divDelivery") > -1) {
				$("div[id$=pnlFinalConfirmation]").hide();
			}
			if (div.attr("id").indexOf("divSelectWine") > -1) {
				$("div[id$=pnlDispatchDetails]").hide();
				$("div[id$=pnlFinalConfirmation]").hide();
			}
		}
		else {
			$(this).removeClass("active");
			div.slideUp("slow");
		}
		e.preventDefault();
	});
}

// function limits the number of characters a user can type in a textbox
// class "limit-nn" where nn is the max characters. "limit-nn" must be the last class if more than one
function limitChars() {
	$("textarea[class*=limit-]").keyup(function(e) {
		el = $(this);
		var limit = el.attr("class").split("limit-")[1];
		var text = el.val();
		var textlength = text.length;
		if (textlength > limit) {
			el.val(text.substr(0, limit));
			return false;
		}
	});
}

function load_tool_tip() {
	$(".tooltip").filter(function() {
		return !$(this).attr("title");
	}).tooltip({
		bodyHandler: function() {
			return $($(this).siblings(".tooltip_tt")).html();
		},
		fade: 200,
		track: true,
		showURL: false
	});
	$(".tooltip[title]").tooltip({
		fade: 200,
		track: true,
		showURL: false
	});
	$("a.tooltip[href^=#tt_]").tooltip({
		bodyHandler: function() {
			return $($(this).attr("href")).html();
		},
		fade: 200,
		track: true,
		showURL: false
	});
}

function page_init() {
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	load_tool_tip();
	limitChars();
}

function popUp(_url, _height, _width, _name) {
	var options = "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=" + _width + ",height=" + _height;
	var name = "";
	if (_name != null)
		name = _name;
	window.open(_url, name, options);
	return true;
}

function ButtonLinks() {
	$('.button_link > a').each(function() {
		var w = $(this).width()
		$(this).parent().css({ 'margin-left': 'auto', 'margin-right': 'auto', 'width': w });
	}); ;

}
