Cufon.replace("h3, #menu li, button, a.button, #button-add, .cufon");
Cufon.replace("h2", { textShadow: "0 3px rgba(0, 0, 0, 0.2)" });
Cufon.replace(".message h3", { textShadow: "0 2px rgba(0, 0, 0, 0.2)" });

function p(v) {
	return parseInt(v, 10);
}

$(function() {

	// switch styles for the fancy version
	$("#section-shop .slidr-window").css({ overflow: "hidden" });
	$("#section-shop .slidr-row").css({ width: "10000px" });

	$("body").removeClass("nojs");

	// adjust the centering of the page (except on the retrieve page)
	if( $("#section-retrieve").length < 1 ) {
		adjustCenterLine();
		$(window).resize(function() {
			adjustCenterLine();
		});
	}

	$("#gallery-list").load("/ajax/gallery/", function() {
		$(this).children("a").lightBox();
		$(this).children("a").click(function() {
			galleryClicked();
		});
		$("#lightbox-secNav-btnClose").live("click", function() {
			galleryClicked();
		});
	});

	if( $(".large-browsr").length < 1 ) {
		slidr.init();
		$(".token-slidr .slidr-row div, .token-browsr .browsr-row a").live("click", function(e) {
			e.preventDefault();
			var sku = $(this).attr("id").split("-");
			slidr.move(sku[1]);
		});
	}

	// open all external links in new window
	$("a[href^=http], .preview a, .order-row .links a").not(".local").click(function(e) {
		e.preventDefault();
		window.open(this.href);
	});

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		var link = $(this).attr("title") || address;
		$(this).html('<a href="mailto:'+address+'">'+link+'</a>');
	});

	// quick login/register
	$("a[rel=login], a[rel=register]").click(function(e) {
		e.preventDefault();
		showHideQuickForm($(this).attr("rel") + "-form-container");
	});

	// add confirm when removing a token or emptying the cart
	$(".remove-token").live("click", function(e) {
		e.preventDefault();
		confirmDelete($(this));
	});
	$("#quick-empty").live("click", function(e) {
		e.preventDefault();
		confirmEmpty($(this));
	});

	// add confirm when switching cart modes
	$(".switch-mode").live("click", function(e) {
		e.preventDefault();
		//confirmDelete($(this));
	});

	// highlight errors better
	$(".form .e").parent().addClass("e");

	$("#form_shipping a, #form_find_address a").click(function(e) {
		e.preventDefault();
		var div = $(this).attr("href").replace("#", "");
		$("form:visible").fadeOut(function() {
			$("form#"+div).fadeIn();
		});
	});

	$("#account-info h3 a").click(function(e) {
		e.preventDefault();
		var div = $(this).attr("href").replace("#", ""),
		vis = $(".account-group:visible");

		if( vis.length > 0 ) {
			vis.slideToggle("fast");
		}
		$("#"+div+":hidden").slideToggle("fast");
	});

	// shipping date datepicker
	var picker = $("#shipping-datepicker");
	if( picker.length ) {
		picker.datepicker({
			showOn: "both",
			buttonImage: "/images/icon_calendar.gif",
			buttonImageOnly: true,
			buttonText: "Choose your date",
			dateFormat: "MM d, yy",
			altField: "#shipping-date",
			altFormat: "yy-mm-dd",
			minDate: 0,
			maxDate: "+12m"
		}).datepicker("setDate", picker.val());
	}

});

var slidr = {

	rowHeight: 0,
	tokenWidth: 0,
	currSeries: 0,
	currToken: 0,
	currSKU: 0,
	url: [],
	urlSep: "#/token-",

	init: function() {
		$(".browsr-row:first a:first").addClass("here");
		var row = $(".token-slidr .slidr-row:eq(0)"),
		token = $(".token-slidr .slidr-row div:eq(0)");
		this.rowHeight = row.outerHeight();
		this.tokenWidth = token.outerWidth(true);
		this.url = location.href.split(this.urlSep);
		// move to specified token from URL
		if( this.url[1] ) {
			if( this.url[1].indexOf("-") > 0 ) {
				var nid = $(".browsr-row a[rel='"+this.url[1]+"']");
				if( nid.length > 0 ) {
					this.move(nid.attr("id").replace("preview-", ""));
				} else {
					this.move("0-0");
				}
			} else {
				if( !isNaN(+(this.url[1])) ) {
					this.move(this.url[1]);
				} else {
					this.move("0-0");
				}
			}
		}
	},

	move: function(sku) {
		var selectedToken = $("#token-"+sku);
		if( selectedToken.length > 0) {
			var rel = selectedToken.attr("rel").split("-"),
			series = rel[0],
			token = rel[1],
			tokenName = selectedToken.children("strong").text(),
			tokenDesc = selectedToken.children("p").html();
			if( typeof(selectedToken.children("img").attr("src")) !== "undefined" ) {
				if( sku !== this.currSKU ) {
					var top = -(series * this.rowHeight),
					left = -(token * this.tokenWidth);
					$(".browsr-row a.here").removeClass("here");
					$("#preview-"+sku).addClass("here");
					$(".slidr-wrapper").animate({ top: top+"px", left: left+"px" }, 250, null, function() {
						$(".token-info h3").text(tokenName);
						$(".token-info p").html(tokenDesc);
						Cufon.replace(".token-info h3");
					});
					this.currSeries = series;
					this.currToken = token;
					this.currSKU = sku;
				}
			}
		}
		this.updateCartSKUs();
	},

	updateCartSKUs : function() {
		if( this.currSKU > 0 ) {
			// update the deep link
			location.href = this.url[0] + this.urlSep + this.currSKU;
			// update SKU in hidden input
			$("#cart-token-sku").attr("value", this.currSKU);
		}
		return true;
	}

};

function confirmEmpty(el) {
	answer = confirm("Are you sure you want to empty your basket?\nAny information you entered into the shipping, message or billing forms will be lost!");
	if( answer === true ) {
		window.location = el.attr("href") || "#";
	} else {
		alert("Your basket will not be emptied.");
	}
}

function confirmDelete(tokenEl) {
	var name = '"'+tokenEl.children("img").attr("alt")+'"' || "this token",
	answer = confirm("Are you sure you want to remove "+name+" from your basket?\n\nAny information you entered into the shipping or\nmessage forms for this token will be lost!");
	if( answer === true ) {
		window.location = tokenEl.attr("href") || "#";
	} else {
		alert(name+" will not be removed.");
	}
}

function adjustCenterLine() {
	var winWidth = $(window).width(),
	minWidth = p($("body").css("minWidth"));

	if( winWidth <= (minWidth + 170) ) {
		var newWidth = ((winWidth - minWidth) > 20) ? winWidth - minWidth : 20;
		$("#content-wrapper").css({ marginLeft: newWidth });
	} else {
		$("#content-wrapper").css({ margin: "0 auto" });
	}
}

// shows/hides quick login/register
var currDiv = null;
function showHideQuickForm(id) {
	$(".quick-form-container").animate({ width: 0 }, 250, null, function() {
		$(".quick-form").hide();
		if( currDiv !== id ) {
			$("#"+id).show();
			$(".quick-form-container").animate({ width: "165px" }, 250);
			currDiv = id;
		} else {
			currDiv = null;
		}
	});
	return false;
}

function galleryClicked() {
	var mcName = "starburst",
	mc = document[mcName];
	if( navigator.appName.indexOf("Microsoft") != -1 ) {
		mc = window[mcName];
	}
	mc.galleryClicked();
}

// used on retrieve page
function resizeDiv(h, d) {
	$("#section-retrieve #content").animate({ height: h }, { queue: false, duration: d });
}
