/**
 * What do you think you're doin!? ;)
 **/

var scrollSpeed = 850;
var panelTogggleSpeed = 250;

PreviewNavItem = function(c,n,i){
	this.init(c,n,i);
}


$.extend(PreviewNavItem.prototype,{
	
	init:function(c,n,i)
	{
		var self = this;
		this.control = c;
		this.navItem = n;
		this.id = i;
		$(n).bind('click',function(e){ self.click(); });
	},
	
	click:function()
	{
		//alert(this.id*250);
		this.control.gotoPreview(this.id*250);
	}
	
});

SitePage = function(c,p,i){
	this.init(c,p,i);
}

$.extend(SitePage.prototype,{

	init:function(c,p,i) 
	{
		var self = this;
		this.control = c;
		this.page = p;
		this.panel = $(p).find('.pagePanel');
		this.preview = $(p).find('.preview');  
		this.imgHolder = $(p).find('.imgHolder');
		this.previewNav = $(this.page).find('.previewNav');
		this.imgs = new Array();
		this.navItems = new Array();
		this.id = i;
		this.xTarget = $(p).offset().left-$('#menu').width()-10;
		
		this.setupPreviewNav();
		
		$(this.preview).bind('click',function(e){ self.click(); });   
		
		$(this.imgHolder).fadeTo(0,0.20);
	},
	
	click:function()
	{
		this.control.gotoPage(this.id);
	},
	
	setupPreviewNav:function()
	{
		var self = this;
		this.activeImg = 0;
		this.imgs = $(this.preview).find('img');
		var n = $(this.previewNav).find('li');
		
		var i = 0;
		var len = n.length;
		for(i;i<len;i++){
			this.navItems.push(new PreviewNavItem(this,$(n[i]),i));
		}
	},
	
	gotoPreview:function(y)
	{
		//pageTracker._trackEvent('portfolio', 'gotoPreview', 'not set', i);
		$(this.preview).animate({scrollTop:y},{duration:panelTogggleSpeed,easing:"easeOutQuad"});
	},
	
	activate:function()
	{   
		$(this.imgHolder).fadeTo(panelTogggleSpeed,1); 
		$(this.panel).animate({top:0},{duration:panelTogggleSpeed,easing:'easeOutQuad'});
	},
	
	deactivate:function()
	{     
		this.gotoPreview(0);   
		$(this.imgHolder).fadeTo(panelTogggleSpeed,0.20);
		$(this.panel).animate({top:-$(this.panel).height()},{duration:panelTogggleSpeed,easing:'easeOutQuad'});
	}
	
});

NavLink = function(c,n,i){
	this.init(c,n,i);
}

$.extend(NavLink.prototype,{

	init:function(c,n,i) 
	{
		this.control = c;
		this.navItem = n;
		this.id = i;
		var self = this;
		$(n).find('a').attr("href","javascript:void(0)");
		$(n).bind('click',function(e){ self.click(); });
	},
	
	click:function()
	{
		this.control.gotoPage(this.id);
	}
	
});

SiteControl = function(){
	this.init();
}

$.extend(SiteControl.prototype,{
	
	init:function() 
	{
		var self = this;
		this.navLinks = new Array();
		this.pages = new Array();
		
		this.setupPages();
		this.setupNav();
	},
	
	setupNav:function()
	{
		var self = this;
		var n = $('#nav').find('.navLink');
		var i = 0;
		var len = n.length;
		for(i;i<len;i++){
			this.navLinks[i] = new NavLink(this,n[i],i);
		}	
	},
	
	setupPages:function()
	{
		var p = $('#pageHolder').find('.page');
		//$('#pageHolder').width($(p[0]).width()*(p.length+$('#pageEnd').width()+50));
		var i = 0;
		var len = p.length;
		for(i;i<len;i++){
			if(i != p.length-1) this.pages.push(new SitePage(this,p[i],i));
		}
		this.activePage = this.pages[0];
	},
	
	gotoPage:function(i)
	{
		//pageTracker._trackEvent('portfolio', 'gotoPage', 'not set', i);
		if(this.activePage){
			var self = this;
			this.activePage.deactivate();
			setTimeout(function(){ self.gotoPageFinal(i); },panelTogggleSpeed/3);
		}else{
			this.gotoPageFinal(i);
		}
	},
	
	gotoPageFinal:function(i)
	{
		var self = this;
		var n = this.pages[i].xTarget;
		this.activePage = this.pages[i];
		$('#content').animate({scrollLeft:n},{duration:scrollSpeed,easing:"easeInOutBack",complete:function(){ self.pages[i].activate(); }});	
	}
	
});