/**
 * @author The Graphic Design School - 2009
 */

jQuery( function() {
	
	function log(str_var) { if(console) if(console.log) console.log(str_var); }

	// distribute menu items
	
	var menuWidth = $("#menuController").width();
	var menuItemString = "#menuController span";
	var menuNb = $(menuItemString).size() - 1;
	var spaceX = menuWidth;
	
	$(menuItemString).each(function() {
		spaceX -= $(this).width();
	});
	
	spaceX = Math.floor(spaceX/menuNb);
	spaceX = Math.floor(spaceX/2);
	
	$(menuItemString).each(function() {
		$(this).css('padding-left',spaceX);
		$(this).css('padding-right',spaceX);
	});
	
	$(menuItemString+":first").css('padding-left',0);
	$(menuItemString+":last").css('padding-right','2px');

	// jFlow Stuff
	
	$("#menuController").jFlow({
		slides 			: "#menuSlides",
		controller 		: ".controlTab", 				// must be class, use . sign
		easing 			: "swing",
		duration 		: 320,
		height 			: "100%",
		width 			: "100%",
		prev 			: ".controlPrev", 				// must be class, use . sign
		next 			: ".controlNext" 				// must be class, use . sign
	});
	
	// keypress
	
	$(document).bind("keyup",function(e) {
		var key = e.originalEvent.keyCode;
		if( key == 37 || key == 40 ) { $(".controlPrev").trigger("click"); }
		if( key == 38 || key == 39 ) { $(".controlNext").trigger("click"); }
		return false;
	});
	
	
	// anchors
	
	var hash = window.location.hash;
	if( hash.length > 2 ) {  
		var l = window.location; 
		var href = "http://" + l.hostname + l.pathname; 
		if( hash.search(/^#\//) != -1 ) {
			var htag = hash.substr(2); console.log(htag);
			var c = $("#menuController > span#"+htag);
			if( c.size() == 1 ) {
				c.trigger("click");
			}
		} else {
			window.location = href;
		}
	}
});
