jQuery(document).ready(function() {	
	// Interface : equalising
	if($(".main").length>0 && $(".sidepanel").length>0) {
		mainHeight = $(".main")[0].offsetHeight;
		sidepanelHeight = $(".sidepanel")[0].offsetHeight;
		offsetAmount = (mainHeight < sidepanelHeight ? 30 : 16);
		offset = (mainHeight - sidepanelHeight) + offsetAmount;
		if(offset<offsetAmount) offset=offsetAmount;
		$(".sidepanel .group:last").addClass("last").css("paddingBottom", offset);
		
		if(document.getElementById('default') && document.getElementById('AdPanel') && mainHeight < sidepanelHeight) {
			offset = (sidepanelHeight - mainHeight) + 15;
			$("#AdPanel")[0].style.marginTop = offset+"px";
		}
	}
	
	// New windows
	$(".external-window, .featured-businesses a").click( function(e) {
		// Abort if a modifier key is pressed
		if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
			return true;
		} else {
			// Change "_blank" to something like "newWindow" to load all links in the same new window
		    var newWindow = window.open(this.getAttribute('href'), '_blank');
			if (newWindow) {
				if (newWindow.focus) {
					newWindow.focus();
				}
				return false;
			}
			return true;
		}
		
	});
	$(".external-window").attr("title", "Link opens in an external window");
	
	// Button : Signup
	$("#btnSignUp").click(
		function() {
			window.location = "signup.php";
		}
	);	
	
	// Button : Search
	$("input[value=Search]").click(
		function() {
			//searchFrmRef = $(this).parents("form:first");
			if($("#txtSearchFor").val()=="Business Name, Keywords eg. isuggest, restaurant, cinema" || $("#txtSearchFor").val()=="Search for...") $("#txtSearchFor").val("");
			if($("#txtLocation").val()=="Location eg. Brisbane, Melbourne, Sydney" || $("#txtLocation").val()=="Location (Suburb)") $("#txtLocation").val("");
		}
	);
	
	// Form Validation	
	if(document.getElementById('frmContactUs')) {
		formRef = $("#frmContactUs");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmRateBusiness')) {
		formRef = $("#frmRateBusiness");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmSuggestABusiness')) {
		formRef = $("#frmSuggestABusiness");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmRegisterIndividual')) {
		formRef = $("#frmRegisterIndividual");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmRegisterBusiness')) { 
		formRef = $("#frmRegisterBusiness");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmInvite')) { 
		formRef = $("#frmInvite");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if(document.getElementById('frmEditIndividual')) { 
		formRef = $("#frmEditIndividual");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
	if($("form.validate").length>0) { 
		formRef = $("form.validate");
		formRef.prepend('<div id="validationErrorContainer"><ol style="display: none;"></ol></div>');
		containerRef = $("#validationErrorContainer");
		formRef.validate({
			errorLabelContainer: containerRef,
			errorLabelContainer: $("ol", containerRef),
			wrapper: 'li'
		});
	}
});

$(window).bind('load', function() {
	$("img.canvased").each(
		function() {
			console.log(this.height + "  " + $(this).attr("cheight"));
			
			if(this.height < $(this).attr("cheight")) {
				heightDiff = $(this).attr("cheight") - this.height;
				$(this).css("padding-top", (heightDiff/2)).css("padding-bottom", (heightDiff/2));
			}
			if(this.width < $(this).attr("cwidth")) {
				widthDiff = $(this).attr("cwidth") - this.width;
				$(this).css("padding-left", (widthDiff/2)).css("padding-right", (widthDiff/2));
			}
		}
	);
});

function printPage() {
	if (window.print) {
		window.print() ;
	} else {
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = "";
	}
}