/*
	scripts.js
			
	Created by Cubed Eye Studios
	27th August, 2011
	Copyright © 2011, Cubed Eye Studios
	
	For use with the jQuery Framework	
*/
$(document).ready(function() {
	if ("onhashchange" in window && !$.browser.msie) {
	    $(window).bind('hashchange', hashChange);
		$('.ajaxLink').each(function(){
			$(this).attr('href', href2hash($(this).attr('href')));
		});	
	}
	if(location.hash)
	{
		hashChange();
	}
	$('.email').html('info@'+'cubedeyestudios.com.au');
	$('.menu').mouseenter(function(){$('.menu nav').fadeIn()});
	$('.menu nav').mouseleave(function(){$('.menu nav').fadeOut()});
	$('.contactForm').submit(function(event){
		event.preventDefault();
	
		function successFunction(data){
			alert(data);
			$('#contactForm')[0].reset();
		}
		
		function errorFunction(){
			alert('There was an error sending email, please try again.');
		}
		
		$.post('/contact.php', $(this).serialize(), successFunction)
		.error(errorFunction);
	});
});

function hashChange()
{
	function successFunction(data){
		$('.page-container').append('<div id="newContent" style="display:none">'+data+'</div>');
		_gaq.push(['_trackPageview','/'+service]);
	}
	
	function completeFunction(){
		$('.page-container .heading').html($('#newContent .heading').html());
		$('.page-container .content').html($('#newContent .content').html());
		$('.page-background').html($('#newContent .page-background').html());
		$('#newContent').remove();
		
		$('.page-background').fadeIn('slow', function(){
			$('.top-bar').show('slide', 'slow', function(){
				$('.content').animate({height:'+=100%', minHeight:'+=100%'},'slow', function(){
					$('.content').css('height', 'auto');
				});
				$('.heading').slideDown('slow');
			});
		});
		
		$('.ajaxLink').each(function(){
			$(this).attr('href', href2hash($(this).attr('href')));
		});
		
		
		if(location.hash == '#social-media')
		{
			gapi.plusone.render("gpajax-button", {'size':'medium','annotation':'bubble'});
			IN.init();
			stButtons.locateElements();
		}
	}
	
	function errorFunction() {
		location.hash = "";	
		location.reload();
	}
	
	if(location.hash){
		service = '/'+location.hash.substring(1);
	}else{
		service = location.pathname;
	}
	
	$('.content').css('height', '100%');
	$('.content').animate({height:'-=100%', minHeight:'-=100%'},'slow', function(){
		$('.top-bar').hide('slide', 'slow', function(){
			$('.page-background').fadeOut('slow', function(){
				$.get(service, successFunction, 'html').complete(completeFunction).error(errorFunction);
			});	
		});
	});
	$('.heading').slideUp('slow');
}

function href2hash(url)
{
	retstr = ""
	if (url.charAt(0) === "/") {
		retstr = "#"+url.substring(1);
	}else if(url.charAt(0) === "#"){
		retstr = url;
	}else{
		retstr = "#"+url;
	}
	
	return retstr;
}
