//global variables
var a_clickable = true;
var lastHash    = null;


$(document).ready(function() {
	
	// browser detection scripts
	if ($.browser.msie) {
		if ($.browser.version < 5) {
			return false;
		}
	}
	if ($.browser.mozilla) {
		if ($.browser.version.substring(0, 3) < 1.9) {
			return false;
		}
	}
	if ($.browser.safari) {
		if ($.browser.version.substring(0, 1) < 5) {
			return false;
		}
	}
	if ($.browser.opera) {
		if ($.browser.version < 9) {
			return false;
		}
	} 
	
	// change the location path to location hash
	var p = window.location.pathname;
	var h = window.location.hash;
	if (p == '/' && h.length < 1) {
		window.location.hash = "#index";
	}
	
	if (p.length > 1) {
		hash = p.substring(1);
		if ($.browser.safari || $.browser.msie) {
			window.location.hash = "#" + hash;
			window.location.pathname = '';
		}
		else if ($.browser.opera) {
			window.location.href = "http://" + window.location.hostname + "#" + hash;
		}
		else {
			window.location.pathname = "#" + hash;			
		}
	}
	
	
	
	
	// delete current page on load
	$('#body').children().remove();	
	
	
	
	
	// bind logo index page ajax loader
	$('#logo').bind('click', function(e) {
		if (a_clickable == false) {
			return false;
		}
		a_clickable = false;
		
		ajaxLoadPage('/index');
		
		return false;
	});
	
	
	
	// bind menu ajax loaders
	$('#menu a').bind('click', function(e) {
		if (a_clickable == false) {
			return false;
		}
		a_clickable = false;
		
		var url = $(this).attr('href');
		ajaxLoadPage(url);
		
		return false;
	});
	
	
	
	// bind live to the project thumbnails
	$('#project_thumbs a').live('click', function(e) {
		if (a_clickable == false) {
			return false;
		}
		a_clickable = false;
		
		var url = $(this).attr('href');
		if ($.browser.msie && $.browser.version < 8) {
			url = url.substring(7);
			var cut = url.indexOf('/')
			url = url.substring(cut);
		}
		ajaxLoadPage(url);
		
		return false;
	});
	
	
	lastHash = window.location.hash.substring(1);
	checkHashUpdate();
	
});


function ajaxLoadPage(url) {
//	console.log('ajax load page: ' + url + " ;; " + lastHash);
	$.get(url, function(data) {
		// update the url hash
		lastHash = '#' + url.substring(1);
		window.location.hash = url.substring(1);
		
		// update the page title
		var possible_title = $('#project_title', data).html();
		if (possible_title != null && possible_title != undefined) {
			updateTitle(possible_title);
		}
		else {
			updateTitle(ucwords(url.replace(/\//g, '')));
		}
		
		
		if ($('#body').children().size() > 0) {
			
			$('#body div').animate({left:-1000}, 600, function() {
				$('#body').html(data);
				$('#body div').css('left', '1000px');
				$('#body div').eq(0).animate({left:0}, 1200, 'easeOutCirc', function() {
					if (window.location.hash.indexOf('/') > 0) {
						nav_init();
					}
					else {
						nav_remove();
					}
					
					a_clickable = true;
				});
			});
		}
		else {
			$('#body').html(data);
			if (window.location.hash.indexOf('/') > 0) {
				nav_init();
			}
			else {
				nav_remove();
			}
			
			a_clickable = true;
		}
	});
	
	return false;
}


function checkHashUpdate() {
	currentHash = window.location.hash.substring(0);
//	console.log(currentHash + " ;; " + lastHash);
	
	if (currentHash != lastHash) {
//		console.log('hash changed: ' + currentHash);
		if (currentHash == '') {
			//ajaxLoadPage(currentHash);
		}
		else {
			ajaxLoadPage('/' + currentHash.substring(1));
		}
	}
	
	lastHash = window.location.hash.substring(0);
	var t = setTimeout('checkHashUpdate()',200);
}



function updateTitle(s) {
	var title = "Illusions Productions » " + s;
	title = ucwords(title);
	document.title = title;
}


function ucwords (str) {
	return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
}
