/* Author: 
Minx Design

Contents:

Contact validation
Consultation validation
homepage AJAXy bits
*/
//Contact validation
//if the contact form exists validate it
function validateContactForm() {
	$("#contactform").validate({
		messages: {
			frm_name: "Your name is required",
			frm_message: "A message is required", 
			frm_email: {
				required: "Your email address is required",
				email: "Your email address must be in the format of name@domain.com"
			}
		}
	});
};
if ($("#contactform").length) {
	validateContactForm();
};
//Consultation validation
//if the consultation form exists validate it
function validateConsultationForm() {
	$("#consultationform").validate({
		messages: {
			frm_name: "Your name is required",
			frm_phone: "A phone number is required", 
			frm_email: {
				required: "Your email address is required",
				email: "Your email address must be in the format of name@domain.com"
			}
		}
	});
};
if ($("#consultationform").length) {
	validateConsultationForm();
};
//homepage AJAXy bits
//note this uses innershiv/loadshiv to get over IE's problems with AJAX and html5 elements, see loadshiv.js for more detials
if ($("#home").length) {
	//the home section swooshes slideshow
	$('#swooshes ul').cycle({
		fx: 'scrollLeft',
		speed: 800,
		timeout: 6500, 
		delay: 3500
	});
	//append the article tags
	$('#home').append($(innerShiv('<div id="product"></div><article id="products"></article><div id="service"></div><article id="services"></article><article id="work"></article><article id="about"></article><article id="thinking"></article><article id="people"></article><article id="contact"></article><article id="blog" class="sidebar"></article>', false)));
	//add spinners
	$('#products').spinner();
	$('#services').spinner();
	$('#work').spinner();
	$('#about').spinner();
	$('#thinking').spinner();
	$('#people').spinner();
	$('#contact').spinner();
	$('#blog').spinner();
	//load the pages via AJAX
	$('#products').loadShiv('/product/ .maincontent', function() {
    	$slideshow.init("#products");
		$('#products').spinner('remove');
	});
	$('#services').loadShiv('/service/ .maincontent', function() {
    	$slideshow.init("#services");
		$('#services').spinner('remove');
	});
	$('#work').loadShiv('/work/ .toplevel', function() {
		$("#work #post-14 header").append($(innerShiv('<a href="/contact/" class="contactbutton">Contact</a>', false)));
		$('#work').spinner('remove');
	});
	$('#about').loadShiv('/about/ .toplevel', function() {
		$('#about').spinner('remove');
	});
	$('#thinking').loadShiv('/thinking/ .toplevel', function() {
		$('#thinking').spinner('remove');
	});
	$('#people').loadShiv('/people/ .toplevel', function() {
		$("#people #post-25 header").append($(innerShiv('<a href="/contact/" class="contactbutton">Contact</a>', false)));
		$('#people').spinner('remove');
	});
	$('#contact').loadShiv('/contact/ .maincontent', function() {
		validateContactForm();
		$('#contact').spinner('remove');
	});
	$('#blog').loadShiv('/blog #content', function() {
		//add the back to top
		$('footer').append($(innerShiv('<a href="/" class="btt conscroll"></a>', false)));
		$('#blog').spinner('remove');
		//add the hash links
		hashLinks();
		//if there's a hash in the url scroll to it when everything's loaded
		//-home is added to stop it scrolling so remove that
		if(window.location.hash) {
			historyHash = window.location.hash.replace('-home', '');;
			$.scrollTo( historyHash, 1500, { offset:{ top:-103 } });
		}
	});
	//hash the top links
	function hashThisLink(theItem) {
		//create the anchor
		anchorLink = "#" + theItem.pathname.replace(/\//g,"");
		//if it's the home link "#" go to container
		if (anchorLink=="#") {
			anchorLink = "#container";
		};
		//change the href
		$el = $(theItem);
		$el.attr("href", anchorLink)
		.attr("rel", "internal");
		//add the scrollTo
		$(theItem).click(function(){
			$.scrollTo( theItem.hash, 1500, { 
				offset:{ top:-103 }, 
				onAfter:function(){
					historyHash = theItem.hash+"-home";
					window.location.hash = historyHash;
				}
			});
			return false;
		});
	}
    function hashLinks() {
        siteURL = "http://" + top.location.host.toString();
		//nav links
		$("nav a[href^='"+siteURL+"']").each(function() {
			hashThisLink(this);
		});
		//the contactbutton links
		$("a.contactbutton").each(function() {
			hashThisLink(this);
		});
		//the jump tp contact link
		$("a.jump").each(function() {
			hashThisLink(this);
		});
		//any other contact scroll link
		$("a.conscroll").each(function() {
			hashThisLink(this);
		});
	};
};
