
/* ======== Constants ======== */

var PAGE_TRANSITIONS = false;
var PAGE_TRANSITION_TIME_A = 600;
var PAGE_TRANSITION_TIME_B = 800;
var PAGE_TRANSITION_SELECTOR = "div.page-contents,xdiv.navigation-bottom,xdiv.navigation-bottom-pages";
var FADING_ELEMENT_SPEED = 600;
var FADING_ELEMENT_DELAY = 9000;



/* ======== Variables ======== */
var fadingElements = null;
var currentFadingElement = -1;



/* ======== JQuery Addons ======== */
(function($) {
	$.fn.customFadeTo = function(speed, opacity, callback) {
		$(this).fadeTo(speed, opacity, function() {
			if(opacity == 1 && jQuery.browser.msie) $(this).get(0).style.removeAttribute("filter");
			if(callback != undefined) callback();
		});
	};
})(jQuery);



/* ======== Functions ======== */

function navigateWithTransition(page) {
	// Hide the layout and then redirect
	$(PAGE_TRANSITION_SELECTOR).customFadeTo(PAGE_TRANSITION_TIME_A,0.0,function(){
		window.location.href = page;
	});
}

function fadeNextElement() {
	// Init
	var speed = FADING_ELEMENT_SPEED;
	if(currentFadingElement == -1) {
		currentFadingElement = 0;
		speed = 0;
	}
	
	// Get next element
	var nextFadingElement = currentFadingElement + 1;
	if(nextFadingElement >= fadingElements.length) nextFadingElement = 0;
	
	// Fade out
	fadingElements.eq(currentFadingElement).stop().customFadeTo(speed,0.0,function(){
		
		// Fade in
		fadingElements.eq(nextFadingElement).stop().customFadeTo(speed,1.0);
		
		// Register
		currentFadingElement = nextFadingElement;
		
	});
	
	
}




/* ======== DOM Ready ======== */

$(document).ready(function(){

	// Fading elements
	fadingElements = $("div.fading-quote");
	fadingElements.customFadeTo(0,0.0);
	fadingElements.css("visibility","visible");
	fadeNextElement();
	if(fadingElements.length > 0) {
		setInterval("fadeNextElement()", FADING_ELEMENT_DELAY);
	}

	// Image hovers...
	$("img[hover]").hover(function(){
		//$(this).customFadeTo(200,0.5);
		$(this).attr("normal",$(this).attr("src"));
		$(this).attr("src", $(this).attr("hover"));
		var linked = $("#"+$(this).attr("linked-hover"));
		if(linked.length > 0) {
			linked.attr("normal",linked.attr("src"));
			linked.attr("src", linked.attr("hover"));
		}
		//$(this).parent().parent().find("img").not($(this)).stop().customFadeTo(200,0.5);
	},function(){
		//$(this).customFadeTo(200,1.0);
		$(this).attr("src", $(this).attr("normal"));
		var linked = $("#"+$(this).attr("linked-hover"));
		if(linked.length > 0) {
			linked.attr("src", linked.attr("normal"));
		}
		//$(this).parent().parent().find("img").not($(this)).stop().customFadeTo(200,1.0);
	});
	
	
	if(PAGE_TRANSITIONS) {
	
		// Hide paper and contents
		$(PAGE_TRANSITION_SELECTOR).customFadeTo(0,0.0);
		
		// Hook all clicks for transition...
		$("a").click(function() {
			$(this).addClass("clicked");
			//alert($(this).attr("class"));
			navigateWithTransition($(this).attr("href"));
			return false;
		});
		
	}
	
	
	// Nerves credit
	if(!jQuery.browser.msie) {
		$("div.designbynerves").customFadeTo(0, 0.3);
		$("div.designbynerves").hover(function() {
			$(this).stop().customFadeTo(400, 1.0);
		},function() {
			$(this).stop().customFadeTo(400, 0.3);
		});
	}
});





/* ======== All Resources Loaded ======== */

$(window).load(function() {

	if(PAGE_TRANSITIONS) {
		// Fade in the paper and then the contents
		$(PAGE_TRANSITION_SELECTOR).customFadeTo(PAGE_TRANSITION_TIME_A,1.0);
	}
	
});


