$(".switch").removeClass('no_js');
jQuery.event.add(window, "load", resizeFrame);
 jQuery.event.add(window, "resize", resizeFrame);
 
function resizeFrame()  /* took a while to figure out, but scrollbars mess up the caluculations, so I temporarily reset the elements making scrollbars to 0 */
{
	//alert("jquery is working");
    $(".bkgd").css('width',0, 'height', 0); /* reset them to 0 so they don't make scroll bars - which messes up the calculations */
	var windowsHeight = $(window).height();
	var pageHeight = $("#page").height();
	var footerHeight = $("#footer").height();
	var contentsHeight = (pageHeight+footerHeight);
	var pageOffset = (windowsHeight-footerHeight)-pageHeight;
	/* Do height stuff first, then width stuff */
    $("#page").css('margin-bottom',(contentsHeight < windowsHeight) ? pageOffset : 0 ); 
    $(".bkgd").css('height', $("#content").height()); /* content height has changed, so grab the current number */
	/* get the widths right */
    var rightWidth = ($(window).width()-$("#content").width())/2;
    var leftWidth = ((($(window).width()-$("#content").width())/2)+232);
    $(".right").css('width', rightWidth); 
    $(".left").css('width',(leftWidth > 230) ? leftWidth : 232); /* 232 is the width of the navigation */
	$(".grid").css('height', $(".left").height()); /* the grid is already  width:100% so it needs height too */
}


/* assign an id to the nav item that matches the current page */
 $(function(){
   var path = location.pathname.substring(1);
  // alert(path);
   if ( path )
     $('li a[href$="' + path + '"]').attr('id', 'selected');
 });
 
 
$(function () {
	$('#slideshow1').anythingSlider({
	
		buildArrows : false,
		delay               : 7000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		animationTime       : 600,       // How long the slideshow transition takes (in milliseconds)
		/*themeDirectory : 'lib/{themeName}.css',*/
		navigationFormatter : function(i, panel){return panel.find('h2.slideTitle').text();}
	
	
	});
});


$(function()	
{  
	if ($.browser.msie && parseInt($.browser.version)< 7) 
	{  
		$("#nav li").hover(
			function(){$(this).addClass("sf");}, /*amend to your class */
			function(){$(this).removeClass("sf");}
		);
	}  
});  

// make elements equal height
function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
	
$(document).ready(function()					   
	{
		
			equalHeight($(".fCol"));
			equalHeight($("#customers li ul"));
			//equalHeight($(".footer-col"));


	function isValidEmailAddress(emailAddress) // this function checks that emails are valid. It's bung tho, thinks that 6 chars is okay on the end.
		{ 
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
		}
	
	
	function isValidName(theirName) // this function checks that emails are valid. It's bung tho, thinks that 6 chars is okay on the end.
		{ // checks for numbers, new lines, bad email shit
		var pattern = new RegExp(/(\d+|\n+|\r+|content-type:|to:|cc:|bcc:)/i);
		return pattern.test(theirName); 
		}
	
	
	function isValidSubject(theirSubject) // checks for numbers, new lines, bad email shit
		{ 
		var pattern = new RegExp(/(\n+|\r+|content-type:|to:|cc:|bcc:)/i);
		return pattern.test(theirSubject); 
		}
		


	$(function() // contact form initial refresh
		{ 
		$("form").each(function() 
			{
			this.reset();
			$("input").removeClass('error');
			});
		$('#cf_error .message').html("");
		$("#contact .label").removeAttr("disabled");
		$("#cf_submit").removeAttr("disabled");
		});



	$(function() // put labels inside the contact form elements and moves em all around. The else statement is in case you need stuff to animate upwards
		{ 
		$('.label').focus(function() /* when they click on the input */ 
			{
			if ($(this).is('.slideLeft')) 
				{
				$("label[for=" + $(this).attr("id") + "]").animate({left: "-"+(parseFloat($(this).siblings("label").width()) + 16)}, 200, function() {});
				};
			});
		
//		$('.label').each(function(ele)
//			{
//			if($(this).val().length = 0)
//				{
//				if ($(this).is('.slideLeft'))
//					{
//					$('label[for=' + $(this).attr('id') + ']').animate({ opacity: 1, left: '0',}, 200, function() {});
//					};
//				};
//			// not sure why I need this..?
//			});		
		
		$('.label').blur(function() // check each time they interact with the input
			{ 
			//if there is nothing inside
			if($(this).val().length == 0) 
				{
				if ($(this).is('.slideLeft')) 
					{
					$("label[for=" + $(this).attr("id") + "]").animate({ left: 16}, 200, function() {});
					};
				}
			});
		});
	
	

	
	$('.txt').blur(function() // Validate inputs with class .txt
		{ 
		if (($(this).val() == "")) 
			{ // it's wrong
			$('label[for=' + $(this).attr('id') + ']').siblings('span.validation').css({ "background-position": "0 0" });
			} else { // it's right
			$('label[for=' + $(this).attr('id') + ']').siblings('span.validation').css({ "background-position": "0 -42px" });
				$("#cf_error").fadeOut("fast", "linear");
				//$('#cf_error .message').html("");
				//$('#cf_error').css({"left": -9999});
			}
		});
	
	$('.txt').keyup(function() // gets things happening when you type
		{ 
		if (($(this).val() == "")) 
			{ // it's wrong
			$('label[for=' + $(this).attr('id') + ']').siblings('span.validation').css({ "background-position": "0 0" })
			} else { // it's right
			$('label[for=' + $(this).attr('id') + ']').siblings('span.validation').css({ "background-position": "0 -42px" });
				
				//$('label[for=' + $(this).attr('id') + ']').animate({top: 16, opacity: 1,}, 200, function() {});
				//$('#cf_error').animate({"left": -9999}, 1000, function() {});
				$("#cf_error").fadeOut("fast", "linear");
				//$('#cf_error .message').html("");
				//$('#cf_error').css({"left": -9999});
			}
		});
	
	
	$("#cf_email").blur(function() // validate the email address as you type
		{
		var email = $("#cf_email").val();
		if(email != 0)
			{
			if(isValidEmailAddress(email))
				{ // it's right, show tick
				$(this).siblings("span.validation").css({ "background-position": "0 -42px" });
				$('#cf_error .message').html("");
				$("#cf_error").fadeOut("fast", "linear");

				} else { // it's wrong, show cross
				$(this).siblings("span.validation").css({ "background-position": "0 0" });
				}
			}
		});
	
	
	$("#cf_email").keyup(function() // validate the email address as you type
		{
		var email = $("#email").val();
		if(email != 0) /* only do this if there's something typed */
			{
			if(isValidEmailAddress(email))
				{ // it's right
				$(this).siblings("span.validation").css({ "background-position": "0 -42px" });
				$("#cf_error").fadeOut("fast", "linear");
				$('#cf_error .message').html("");
				//$('#cf_error').css({"left": -9999});
				} else { // it's wrong
				$(this).siblings("span.validation").css({ "background-position": "0 0" });
				}
			}
		});
	
	
	$(function() //submit email w/o page refresh
		{  
		$('.error').hide();
		$("#cf_submit").click(function() // validate and process form. Should prob be .submit() not click. Pushing enter can submit it too
			{ 
			$("#contact-form *").removeClass('error');
			var newLeft = 500;

			// validation stuff that mimics the php validation, cause I don't know how to display the php errors
			// check for bad shit. I think I only need it for email and subject. Do they need separate var names?
			var badshit = $('input#cf_name').val();
			if (isValidName(badshit))
				{ this 
				$("input#cf_name").addClass('error');
				$('#cf_error .message').html("sorry, your name has to be just letters");
				$('#cf_error').css({ "top": $("input#cf_name").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				return false;
				}
			var badshit = $('input#cf_subject').val();
			if (isValidSubject(badshit))
				{ this 
				$("input#cf_subject").addClass('error');
				$('#cf_error .message').html("sorry, the subject can't have things like cc: or bcc: (spammers use those)");
				$('#cf_error').css({ "top": $("input#cf_subject").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				return false;
				}
			//  check the invisible field is empty, spambots will prob fill it in cause they're dumb
			var spambot = $("input#bender").val();
			if (spambot != "") 
				{
				$("input#bender").addClass('error');
				$('#cf_error .message').html("I think you're a spam robot");
				return false;
				}
			// check if they've entered a name
			var cf_name = $("input#cf_name").val();
			if (cf_name == "") 
				{
				$("input#cf_name").addClass('error');
				$('#cf_error .message').html("what do you call yourself?");
				$('#cf_error').css({ "top": $("input#cf_name").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				$("input#cf_name").focus();
				return false;
				}
			// check if they've entered an email
			var cf_email = $("input#cf_email").val();
			if(isValidEmailAddress(cf_email)) 
				{ // if they did, sweet
				} else {
				$("input#cf_email").addClass('error');
				$('#cf_error .message').html("your email appears to be missing something")
				$('#cf_error').css({ "top": $("input#cf_email").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				$("input#cf_email").focus();
				return false;
				}
			// check if they've entered a subject
			var cf_subject = $("input#cf_subject").val();
			if (cf_subject == "") 
				{
				$("input#cf_subject").addClass('error');
				$('#cf_error .message').html("please enter a subject")
				$('#cf_error').css({ "top": $("input#cf_subject").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				$("input#cf_subject").focus();
				return false;
				}
			// check if they've entered a message
			var cf_message = $("textarea#cf_message").val();
			if (cf_message == "") 
				{
				$("textarea#cf_message").addClass('error');
				$('#cf_error .message').html("please enter a message")
				$('#cf_error').css({ "top": $("textarea#cf_message").parent().position().top-($("#cf_error").height()-26), "left": newLeft }).fadeIn("fast", "linear");
				$("textarea#cf_message").focus();
				return false;
				}

		
			// submit via jquery 
			var dataString = "cf_name="+ cf_name + "&cf_email=" + cf_email + "&cf_subject=" + cf_subject + "&cf_message=" + cf_message;
			// usibility stuff.
			// sending message.
			$("#cf_error .message").html("Sending your email.");
			$('#cf_error').fadeIn("fast", "linear").css({ "top": 130, "left": 365 });
			
			// disable the fields so they can't clik clik clik
			$("#contact .label").attr("disabled", "disabled");
			$("#contact #cf_submit").attr("disabled", "disabled");
			  
			$.ajax(
			{
			type: "POST",
			url: "http://www.axiam.co.nz/lib/email.php",
			data: dataString,
			error: function() // for now we'll just pretend nothing happened...
				{
				$("#cf_error .message").html("Uh oh, something went horribly wrong...");
				setTimeout('$("#cf_error .message").html("Try one more time. If it happens again, call us");$("#cf_error").animate({ opacity: 1, top: ($("ul.fields").height()-26), left: 486}, 200, function() {});$("#contact .label").removeAttr("disabled");$("#cf_submit").removeAttr("disabled")', 2000);
				setTimeout('$("#cf_error").fadeOut("fast", "linear")', 6000);
				},
			success: function() // do some nice stuff
				{
				$("#cf_error .message").html("Thanks, your email has been sent");
				$("#cf_error").animate({ opacity: 1, top: ($("ul.fields").height()-26), left: 486}, 200, function() {});
				setTimeout('$("#contact .label").removeAttr("disabled");$("#cf_submit").removeAttr("disabled")', 2000);
				setTimeout('$("#cf_error").fadeOut("fast", "linear")', 6000);
				
				}
			});
			return false;
		});  
	});
	
	$(function() // reset the email form
		{ 
		$("#cf_cancel").click(function()
			{
			$("form").each(function() 
				{
				this.reset();
				$('label').animate({left: '16'}, 200, function() {});
				$("input").removeClass('error');
				});
			
			$('#cf_error .message').html("");
			$('#cf_error').css({ "left": -9999 });
			$("#contact .label").removeAttr("disabled");
			$("#cf_submit").removeAttr("disabled");
			  
			return false;
		});  
	});

})
