/**
 * A object that can store global variables for the duration of a page
 **/
var nm = { };
nm.colors = new Array('blue','orange','lime','lightgrey','darkgrey');
nm.transitionDuration = 0.3;
nm.moving = false;

var Page = Class.create({
	initialize: function(params) {
		nm.moving = true;
		if(params.element != undefined)
			this.selected(params.element);
		
		new Effect.Fade('content',{ duration:nm.transitionDuration, afterFinish: function(){
			new Ajax.Request(params.url,{
						onSuccess: function(transport) {
							
							$('content').update(transport.responseText);
							try {
								// new Lightbox();
							} catch (e) {
								
							}
							if(params.color != undefined)
								$('page').addClassName(params.color);
							new Effect.Appear('content',{ duration:nm.transitionDuration, afterFinish : function() {nm.moving = false;} });
							window.location.hash = params.url;
						},
						on404 : function() {
							$('content').update('<h1 style="margin: 50px; text-align: center;">404 Not Found</h1>');
							new Effect.Appear('content',{ duration:nm.transitionDuration, afterFinish : function() {nm.moving = false;} });
						},
						parameters: { },
						method : 'get'
					});
			}
		});
		
		return false;
	},
	
	selected: function(element){
		$$('#navigation li a').each(function(link){
			$(link).removeClassName('active');
		});
	
		$(element).addClassName('active');
	}
});

function nextProject(){	
	if (nm.moving)
		return false;
	nm.moving = true;
	
	var leftPosition = parseFloat($('project-slider').getStyle('left').sub('px','')-622);
	if(leftPosition != -(getProjectWidth()))
		new Effect.Move('project-slider', { 
			x: -622, y: 0, mode: 'relative',
			afterFinish : function () { nm.moving = false; }
		});
	else
		nm.moving = false;
	
	return false;
}

function prevProject(){
	if (nm.moving)
		return false;
	nm.moving = true;
	
	var leftPosition = parseFloat($('project-slider').getStyle('left').sub('px',''));
	if(leftPosition != 0)
		new Effect.Move('project-slider', { 
			x: 622, y: 0, mode: 'relative',
			afterFinish : function () { nm.moving = false; }
		});
	else
		nm.moving = false;
	
	return false;
}

function getProjectWidth(){

	var width = 0;
	$$('#project-slider div.project').each(function(project){
		width += 622;
	});
	
	return width;
}


// XXX..
function selectProject(anchor) {
	anchor = $(anchor);
	anchor.siblings().each(function(a) { a.removeClassName('active'); })
	anchor.addClassName('active');
	
	var container = $('project-container-container');
	var next = $$('.next-project')[0];
	var prev = $$('.prev-project')[0];
	next.remove();
	prev.remove();
	var link = anchor.href;
	
	var query = link.substring(link.indexOf('?'));
	container.fade({
		duration: nm.transitionDuration, 
		afterFinish : function() {
			new Ajax.Request(anchor.href, {
				onSuccess : function (t) {
					container.replace(t.responseText);
					var newContainer = $('project-container-container');
					var c = newContainer.remove();
					$$('.page-box')[1].remove();
					$('page').insert(c);
					// c.appear({duration: nm.transitionDuration});
					
				}
			});
			}
		}
	);
	return false;
}

Event.observe(window, 'load', function() {
	if (window.location.hash && window.location.hash.substring(1) != '#/') {
		new Page({ url : window.location.hash.substring(1) });
	}
	
	var hash = window.location.hash;
	new PeriodicalExecuter(function(p) {
		if (hash != window.location.hash && window.location.hash && !nm.moving)
			new Page({ url : window.location.hash.substring(1) });
		hash = window.location.hash;
	}, .2)
});
