$(document).ready(function () {

	// Template scripts for navmat.co.uk

	// Select all links that contains lightbox in the attribute rel and convert to lightbox
	$('a[rel*=lightbox]').lightBox();

	// Clear search field on focus
	$("#search-input").data("value",$("#search-input").attr("value"));
	$("#search-input").focus(function () {
		$(this).attr("value", "");
	});
	$("#search-input").blur(function () {
		$(this).attr("value", $(this).data("value"));
	});

	// Remove left column if empty
	if ($("#content-column").children().size() == '0') {
		$("#content-column").remove();
		$("#content-inner").attr("class", "grid-12");
	}

	// Breakout products from shop homepage
	if ($("#home_region2").size() > '') {
		$("#home_region2").remove().insertAfter("div#top-nav");
		$("#home_region2 h2.boxheader").remove();
		$("#home_region2 div.products div.product").each(function () {
			$(this).addClass("product-item grid-3").removeClass("product");
			$(this).find("em.strapline").remove().insertAfter($(this).find("div.minprice"));
			$(this).find("a img").parent().addClass("product-image");
		});
		$("#content-inner").remove();
	}

	// Relocate product information to top
	if ($("#versions").size() > '') {
		$("#versions").remove().appendTo("div.description");
		$("em.strapline").remove().prependTo("div.description");
		$("#pages-content h1").remove().prependTo("div.description");
		$("div.addtobasket input.submit").attr("value", "");
	}

	// Create lightbox thumb previews
	if ($("div#productimage").size() > '') {
		$("div.product div.main").children("a").wrapAll("<div class=\"product-thumbs\"></div>");
		$("div.product div.product-thumbs").children("a").each(function () {
			// Replace Width
			// $(this).attr("href",$(this).attr("href").replace(/width=[0-9]*/, "width=500"));
			// Calculate thumbnail attributes
			var thumbnail = {
				width: 420 / parseInt($("div.product div.product-thumbs").children("a").size()) - 10,
				height: "100",
				css: "product-thumb",
				src: $(this).attr("href").replace(/\/cms\/imgscale\.aspx\?width=[0-9]*.*\&img=/, "")
			}
			// Create Image
			$(this).append("<img src=\"" + thumbnail.src + "?width=" + thumbnail.width + "&amp;height=" + thumbnail.height + "\" class=\"" + thumbnail.css + "\" />");
		});
		// Move container
		$("div.product-thumbs").appendTo($("div#productimage"));
	}

	// Add logo for ePDQ page
	/*
	if ($("form[action*=checkout.asp]").size() > '') {
	$("form[action*=checkout.asp] div.inputform").prepend("<input type=\"hidden\" name=\"cpi_logo\" value=\"https://www.navmat.co.uk/images/logo-epdq.png\">");
	console.log("added ePDQ logo");7
	}
	*/

	// Run slideshow on homepage
	$("#home-slideshow-slides").objectSlideshow({
		ReloadResources: true,
		Debug: true,
		Controls: {
			Enabled: true,
			Arrow: true,
			Numeric: true
		}
	});

});
