$(document).ready(function(){
	
	// Disable Right Click
	$(document).bind("contextmenu",function(e){
        return false;
    });
	

	/* $(".search_form").find("input:submit").click(function(e){
		var string = $(".search_string").val();
		if(string == ''){
			e.preventDefault();
			alert("Please enter a search");
		}
	});*/ 
	
	$(".btn_confirmorder").click(function(e){
		var checked = $("#itermsandcondition").attr("checked");
		if(checked == false){
			e.preventDefault();
			alert("Accept the Terms and Conditions");
		}
	});
	
	$(".openPopup").click(function(e){
		e.preventDefault();
		var url = $(this).attr("href");
		popUp(url);
	});
	
	// fix for target='_blank'
	$('a[href^="http://"], a[href^="https://"], .external').attr({
		target: "_blank"
	});	
	
	//img hover
	$(".hover").hover(function () {
		var filePath = $(this).attr("src");
		var dotPos = filePath.lastIndexOf(".") ;
		var extension = filePath.substr(dotPos,filePath.length);
		var newPath = filePath.replace(extension,"_on"+extension);
		$(this).attr("src",newPath);
	},function () {
		$(this).stop(true,false);
		$(this).attr("src", $(this).attr("src").split("_on.").join("."));
	});

	
	if(lightBox == true) {
	$("body a.lightBox").lightBox();
	}
	
	// Vertically align images inside of a fixed height/width div
	//vAlign(".vAlign");
	
	//hides all tabAreas except first and add class to first tab
	$(".tabArea").find(".tabContent").hide("").end("").find(".tabContent:first").show("");
	$(".tabNav").find("li:first").addClass("selected first");
			
	//gives all items with class tab an id
	$(".tab").attr("id", function (number) {
        return "tab" + number;
    })
	
	//gives all items with class tabContent an id
	$(".tabContent").attr("id", function (number) {
		return "tabContent" + number;		  
	});
					
	//on click of tab hides all tabContents then shows one with same id
	$(".tab").click(function(event){	
		var id = this.id.replace("tab" , 'tabContent');
		$(".tabContent").css({"display" : "none"});
		$("#" + id).css({"display" : "block"});
		$(".tab").removeClass("selected");
		$(this).addClass("selected");
		return false;  
	});
	
	$("input#copyaddress").click(function(){ 
        if ($("input#copyaddress").is(':checked')) 
        { 
            // Checked, copy values 
        	var name = $("input#Firstname").val() + ' ' + $("input#Surname").val();
            $("input#shipping-Name").val(name); 
            $("input#shipping-Telephone").val($("input#DaytimePhone").val()); 
            $("input#shipping-Address1").val($("input#Address1").val()); 
            $("input#shipping-Address2").val($("input#Address2").val()); 
            $("input#shipping-Town").val($("input#Town").val()); 
            $("input#shipping-County").val($("input#County").val()); 
            $("input#shipping-PostCode").val($("input#PostCode").val()); 
            //won't work on drop downs, so get those values
            var country = $("select#Country").val();
            $('select#shipping-Country option[value=' + country + ']').attr('selected', 'selected');
        } 
        else 
        { 
            // Clear on uncheck 
        	$("input#shipping-Name").val(""); 
        	$("input#shipping-Telephone").val(""); 
        	$("input#shipping-Address1").val(""); 
            $("input#shipping-Address2").val(""); 
            $("input#shipping-Town").val(""); 
            $("input#shipping-County").val(""); 
            $("input#shipping-PostCode").val(""); 
            // special for the select
            $('select#shipping-Country option[value=""]').attr('selected', 'selected');
        } 
    }); 

	if(easySlider == true) {
		$(".feature").easySlider({
			auto: true, 
			continuous: true,
			numeric: true,
			pause: 3000,
			speed: 800
		})
	}
	
	if(validate == true) {
		$(".validate").validate({
			rules: {
				'clients.client_password.mirror': {
					minlength: 5,
					equalTo: "#password"
				},
				customerAgreements: "required"
			},
			messages: {
				'clients.client_password.mirror': {
					minlength: "Your password must be at least 5 characters long",
					equalTo: "Your password does not match"
				},
				customerAgreements: "Please accept our Terms and Conditions"
			}
		});
		

	}
	
	//Equal height colums
	pageHeight();
	
	$("body").append("<input class='halfHeight' type='hidden' value='0'/>")
	$("body").append("<input class='maxHeight' type='hidden' value='0'/>")
	
	$(".equal").each(function(){
		var halfHeight = $(this).height();				
		var prevHeight = $(".halfHeight").val();		
		if(halfHeight > prevHeight){
			$(".maxHeight").val(halfHeight);
		}			
		$(".halfHeight").val(halfHeight);
	});
	setTimeout(function(){
		var maxHeight = $(".maxHeight").val() + "px";
		$(".equal").height(maxHeight);
	},1000);
	
	// Click table row
	$('table tr td, table.rowclick tr, .label').click(function(event) {
		if (event.target.type !== 'checkbox') {
			$(':checkbox', this).trigger('click');
		}
	});
	
	// Click table row
	$('table tr td, table.rowclick tr, .scrollbox div.radio, .label').click(function(event) {
		if (event.target.type !== 'radio') {
			$(':radio', this).trigger('click');
		}
	});
	
	$(".testimonals ul").edFade({
		pause:3000
	}); 
	
});


function popUp(url) {
	window.open(url,'PopUp','width=1034,height=400,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}

function vAlign(element) {	
		
	if ($(element).is("img")){
		var image = $(element);
	}else{
		var image = $(element).find("img");
	}
	image.hide("");
	setTimeout(function(){
		image.each(function(){
			var imgHeight = $(this).height();
			var topMargin = ($(this).parents("div").height()- imgHeight)/2;
			$(this).css("margin-top",topMargin);
			$(this).fadeIn("slow");
		});
	}, 1000);	
}



$.fn.imageResize = function(options) {  
	var defaults = {  
		maxWidth:300,
		lightBox:true,
		delay:0,
		verticalCenter:false
	};  
	var options = $.extend(defaults, options);   
	return this.each(function() {  		
		if ($(this).is("img")){
			var run = $(this);
		}else{
			var run = $(this).find("img");
		}	
		setTimeout(function () {  
			run.each(function(){
				var image = $(this);
				var maxWidth = options.maxWidth;
				var width = image.width();
				var height = image.height();
				if(width > maxWidth){
					var ratio = height/width;
					var newWidth = maxWidth;
					var newHeight = newWidth*ratio;
					image.attr("width",newWidth);
					image.attr("height",newHeight);
				}else{
					var newWidth = image.width();
					var newHeight = image.height();
				}
				if(options.verticalCenter == true){
					var topMargin = (image.parents("div").height()- newHeight)/2;
					image.css("margin-top",topMargin);
				}
				if(options.lightBox == true){
					var src = image.attr("src");				
					image.wrap("<a class='imageHolder lightBox' href="+ src +"></a>");
					var holderCss = {
						"width": newWidth,
						"height": newHeight,
						"position":"relative",
						"display":"block"
					}
					if(options.verticalCenter == true){
						var holderCss = {
							"width": newWidth,
							"height": newHeight,
							"position":"relative",
							"display":"block",
							"margin-top":topMargin
						}
					}	
					image.parents(".imageHolder").css(holderCss);
					image.before("<div class ='mag'></div>");
					var magCss = {
						"position":"absolute",
						"top":"0px",
						"right":"0px"
					}				
					$(".mag").css(magCss);	
					$("body a.lightBox").lightBox();				
				}
			});	
		}, options.delay);
	});  
};


function pageHeight(){
	var sideHeight = $(".sidebar").height();
	var pageHeight = $("#main").height();
	if(sideHeight <= pageHeight){		
		$(".sidebar").height(pageHeight);
	}	
}
