function pageLoad() {

	$('#product-list li:nth-child(4n+1)').addClass("first");
	
	$('input[type="text"]').addClass("text");
	$('input[type="radio"]').addClass("radio");
	$('input[type="checkbox"]').addClass("checkbox");
	$('input[type="image"]').addClass("image");
	$('#quick-quote input[type="text"]').labelify();
	$('input[type="text"], textarea').labelify();


	$.fn.vAlign = function() {
		return this.each(function(i) {
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	};
}

ddaccordion.init({
	headerclass: "expandable",
	contentclass: "categoryitems",
	revealtype: "click",
	mouseoverdelay: 200,
	collapseprev: true,
	defaultexpanded: [],
	onemustopen: false,
	animatedefault: false,
	persiststate: true,
	toggleclass: ["", "openheader"],
	togglehtml: ["prefix", "", ""],
	animatespeed: "fast",
	oninit: function(headers, expandedindices) {
	},
	onopenclose: function(header, index, state, isuseractivated) {
	}
})

function topsearchvalidate(keyword) {

	$error = false;
	$msg = "Error : ";

	if ($("input[id$='" + keyword + "']").val() == "" || $("input[id$='" + keyword + "']").val() == "Enter keyword") {
		$("input[id$='" + keyword + "']").focus();
		$msg += "\n - Please enter your search keyword!!!";
		$error = true;
	}
	if ($error == true) {
		alert($msg);

		return false;
	}
	else return true;
}


function leftsearchvalidate(txtSearch, ddThemedIdeas, ddProductCategory) {


	$iskeywordempty = false;
	$isthemeempty = false;
	$iscatempty = false;
	if ($("input[id$='" + txtSearch + "']").val() == "" || $("input[id$='" + txtSearch + "']").val() == "Enter keyword") {
		$iskeywordempty = true;
	}
	if ($("select[id$='" + ddThemedIdeas + "']").val() == "") {
		$isthemeempty = true;
	}
	if ($("select[id$='" + ddProductCategory + "']").val() == "") {
		$iscatempty = true;
	}
	
	if ($iskeywordempty == false || $isthemeempty == false || $iscatempty == false) {
		return true;
	} else {
		$msg = "Please enter keyword or select Themed ideas or product category and resubmit.";
		alert($msg);
		return false;
	}
	
}


function validateqform(txtFullName,txtCompany,txtEmail,txtPhone, txtQty, ddlQColours, txtMinQty) {

	// reset error to false
	$error = false;
	$msg = "Please amend the following fields on the form and re-submit.";


	if ($("input[id$='" + txtFullName + "']").val() == "" || $("input[id$='" + txtFullName + "']").val() == "Full Name*") {
		$msg += "\n - Your Name";
		$error = true;
	}
	if ($("input[id$='" + txtCompany + "']").val() == "" || $("input[id$='" + txtCompany + "']").val() == "Company*") {
		$msg += "\n - Your Company Name";
		$error = true;
	}

	if ($("input[id$='" + txtEmail + "']").val() == "" || echeck($("input[id$='" + txtEmail + "']").val()) == false) {
		if (echeck($("input[id$='" + txtFullName + "']").val()) == false) {
			$msg += "\n - The email address you have typed is not valid, please verify and retype.";
		}
		$error = true;
	}
	if ($("input[id$='" + txtPhone + "']").val() == "" || $("input[id$='" + txtPhone + "']").val() == "Tel Number*") {
		$msg += "\n - Telephone Number";
		$error = true;
	}

	
	if ($("select[id$='" + ddlQColours + "']").val() == "Print Colours" || $("select[id$='" + ddlQColours + "']").val() == "") {
		$msg += "\n - Select No. Print Colours";
		$error = true;
	}

	var intQty = parseInt($("input[id$='" + txtQty + "']").val());
	var intMQty = parseInt($("input[id$='" + txtMinQty + "']").val());

	if ($("input[id$='" + txtQty + "']").val() == "") {
		$("input[id$='" + txtQty + "']").focus();
		$msg += "\n - Please Enter Quantity";
		$error = true;
	} else if (!isNumeric($("input[id$='" + txtQty + "']").val())) {
		$msg += "\n - Quantity should be numeric";
		$error = true;
	} else if (intQty < intMQty) {
		$msg += "\n - Quantity shouldbe greater than product minimum qty : " + intMQty;
		$error = true;
	}
	


	if ($error == true) {
		alert($msg);
		return false;
	}
	else return true;
}



function echeck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	var msg = "";
	if (str.indexOf(at) == -1) {
		return false
	}
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
		return false
	}
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		return false
	}
	if (str.indexOf(at, (lat + 1)) != -1) {
		return false
	}
	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
		return false
	}

	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false
	}
	if (str.indexOf(" ") != -1) {
		return false
	}
	return true
}

function isNumeric(input) {
	var number = /^\-{0,1}(?:[0-9]+){0,1}(?:\.[0-9]+){0,1}$/i;
	var regex = RegExp(number);
	return regex.test(input) && input.length > 0;
}

// DOM READY
$(function() {


	// Basket modal
	$modal = $("#mdlPopup");
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	$modal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");
	$overlay = $("#overlay").css({ height: $(document).height(), opacity: "0.6" });

	// Info modal
	$infomodal = $("#infomdlPopup");
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	$infomodal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");
	//alert($modal);


	$.models = function() {


		$modal = $("#mdlPopup");
		modalH = $modal.outerHeight();
		modalW = $modal.outerWidth();
		windowH = $(window).height();
		windowW = $(window).width();
		$modal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");
		$overlay = $("#overlay").css({ height: $(document).height(), opacity: "0.6" });

		// Info modal
		$infomodal = $("#infomdlPopup");
		infomodalH = $infomodal.outerHeight();
		infomodalW = $infomodal.outerWidth();
		windowH = $(window).height();
		windowW = $(window).width();
		$infomodal.before("<div id=\"overlay\"><!--[if lte IE 6]><iframe></iframe><![endif]--></div>");
		//alert($modal);

	};

});

// Basket modal
basketModal = function(text) {
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	if (text) $modal.find("p").html(text);
	var left = (windowW / 2) - (modalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (modalH / 2) - 22;
	$overlay.fadeIn(500);
	$("#overlay iframe").css({ top: top, left: left, width: $modal.outerWidth(), height: $modal.outerHeight() })

	$modal
	.css({ top: top, left: left })
	.fadeIn(500)
	.find("img[class='modal-close'], img[class='continue']")
	.click(function(e) {
		e.preventDefault();
		$modal.hide();
		$overlay.fadeOut(750);
	});
}

$(window).resize(function() {
	modalH = $modal.outerHeight();
	modalW = $modal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	var left = (windowW / 2) - (modalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (modalH / 2) - 22;
	if ($modal.is(":visible")) {
		$modal.animate({ top: top, left: left }, 500);
	}
});

// Info modal
infoModal = function(text) {
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	if (text) $infomodal.find("p").html(text);
	var left = (windowW / 2) - (modalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (infomodalH / 2) - 22;
	$overlay.fadeIn(500);
	$("#overlay iframe").css({ top: top, left: left, width: $infomodal.outerWidth(), height: $infomodal.outerHeight() })

	$infomodal
	.css({ top: top, left: left })
	.fadeIn(500)
	.find("img[class='modal-close'], img[class='continue']")
	.click(function(e) {
		e.preventDefault();
		$infomodal.hide();
		$overlay.fadeOut(750);
	});
}
$(window).resize(function() {
	infomodalH = $infomodal.outerHeight();
	infomodalW = $infomodal.outerWidth();
	windowH = $(window).height();
	windowW = $(window).width();
	var left = (windowW / 2) - (infomodalW / 2);
	var top = ((windowH / 2) + $(window).scrollTop()) - (infomodalH / 2) - 22;
	if ($infomodal.is(":visible")) {
		$infomodal.animate({ top: top, left: left }, 500);
	}
});

//Image preview
this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
});
