if(window.location.hash) {
	getPage = location.hash;
	getPage = getPage.replace( '#', '');
	
} else {
	getPage = 'home';
	processPage(getPage);
}


$.address.change(function(event) {  
	if(window.location.hash) {
		getPage = location.hash;
		getPage = getPage.replace( '#', '');
		
	
	} else {
		getPage = 'home';

	}
	$('html, body').animate({scrollTop:0}, 'fast');
	processPage(getPage);
	$('#nav li a').removeClass('active');

	$('li.'+ getPage + ' a', '#nav').addClass('active');

});  


$(document).ready(function() {



}); // end Doc Ready




	
function processPage(name){
	$('#page').animate({opacity: 0 }, 300 , function(){
		$('#page').html('');
		ajaxMe(name);
		$('body').removeClass();
		
		if (name == "home") {
			$('body').addClass('front');	
		} else {
			$('body').addClass('page-' + name);
		}

	}); // end animate

}



function ajaxMe(filename){

	var html = $.ajax({
  	url: "inc/pg_" + filename + ".php",
	  async: false
	 }).responseText;

	$('#page').html(html);

	$('#page').delay(50).animate({ opacity: 1	}, 300, function(){
		if (filename == "work") {
			iniGallery();
		} else if (filename == "home"){
			iniGallery();
			iniHome();
		}	else if (filename =="contact") {
			iniContact();
		}
	});

Cufon.replace('h1', { fontFamily: 'Gotham Light' });
Cufon.replace('h2', { fontFamily: 'Gotham Light' });
Cufon.replace('h3', { fontFamily: 'Gotham Light' });
Cufon.replace('h4', { fontFamily: 'Gotham Light' });

} //end ajaxME



function iniGallery(){
			$("a[rel=gallery]").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over"><span class="fb-image"> Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span><span class="fb-title">'+ title +'</span></span>';
					}

				});

				$('div.thumb', '#gallery').hover(function(){
					$(this).find('div.boxHover').fadeIn(200);
				}, function(){
					$(this).find('div.boxHover').fadeOut(200);
				});
}


function iniHome(){
	// Random Number, make this bigger if you want more slides
	var rndSlide = Math.floor(Math.random() * 3);

	// Ini Slideshow
	$('#calloutList').cycle({ 
		timeout: 0,
		speed: 500,
		pause: 1,
		startingSlide: 0
		//use these settings below for just one callout, not to rotate
		//startingSlide: rndSlide
		//timeout: 6000
	});

	$('div.thumb', '#gallery').hover(function(){
		$(this).find('div.boxHover').fadeIn(275);
	}, function(){
		$(this).find('div.boxHover').fadeOut(225);
	});
				
} // end iniHome



function iniContact(){
	$("label.overlabel").overlabel();

	$("input.formClear").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			$(this).css('color', '#000');
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).css('color', '#999');
		}
	}); 

	$("textarea.formClear").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			$(this).css('color', '#000')
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).css('color', '#999')
		}
	}); 



	// bind to the form's submit event 
	$('#myForm').submit(function() { 
		$('.formClear').removeClass('formError');
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var phoneReg = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

		var nameVal = $("#name").val();
		if(nameVal == '') {
			$('#name').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		}

		var emailFromVal = $("#email").val();
		if(emailFromVal == '') {
			$('#email').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			$('#email').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		}

		var phoneVal = $("#phone").val();
		if(phoneVal == '') {
		
		} else if(!phoneReg.test(phoneVal)) {
			$('#phone').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		}

		var subjectVal = $("#subject").val();
		if(subjectVal == '') {
			$('#subject').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		}

		var msgVal = $("#message").val();
		if(msgVal == '') {
			$('#message').addClass('formError')
				.parent().prev('div.form-error').show(0);
			hasError = true;
		}

		if(hasError == false) {
			$(this).hide();

			// WHEN LIVE PLEASE USE TOP POST LINE 
			$.post("/inc/pg_thankyou.php",
			// INSTEAD OF THIS ONE BELOW (for dev) 
			//$.post("/inc/pg_thankyou.php",
 				{ emailFrom: emailFromVal, name: nameVal, phone: phoneVal, subject: subjectVal, msg: msgVal },

   					function(data){
						$("#contactForm").fadeOut("fast", function() {				   
							
							$("#contactForm").after('<p>Thanks.</p><p>Your email was sent.</p>');											
						});
   					}
				 ); // end post
		} // end if error

		return false;

	}); 

} // end iniContact



