/**

	Author: 		James Florentino
	website: 	www.jamesflorentino.com

**/
function Main()
{
	var main = this;
	this.actions	= 0;
	this.active;
	this.active_feature_thumb;
	this.active_feature_count;
	this.projects		= new Object();
	this.feature_height	= $("#feature").height();
	this.feature_title	= $("#feature_title").text();
	this.item_template	= $("#item_template");
	this.thumb_template	= $("#thumb_template");
	
	this._t0;
	this._t1;
	this._t2;
	
	this.init = function()
	{
		// empty the item containers
		$("#projects .container").html('');
		$("#featured_thumbs").html('');
		var em	= 	"j@";
		em			+=	"ja"; em			+=	"mes"; em			+=	"flo"; em			+=	"ren"; em			+=	"ti"; 	em			+=	"no.com";
		setTimeout(function(){
			$(".em_field").attr("href", "mailto:" + em);	
		}, 1000);
		
		$("#active_media .next").click(function(){
			if(main.active_feature_count + 1 < parseInt(main.projects[main.active]['media'].length)){				
				main.active_feature_count++;
				$.address.value("/projects/" + main.projects[main.active]['shortname'] + "/" + main.active_feature_count);
			}
		})
		
		$("#active_media .prev").click(function(){
			if(main.active_feature_count - 1 > -1){				
				main.active_feature_count--;
				$.address.value("/projects/" + main.projects[main.active]['shortname'] + "/" + main.active_feature_count);
			}
		})
		
		if(window.location.hash.match("/projects/") == null){
			$("#feature").slideUp(0);
		}
		
		
		this.generateProjects();
	}
	
	this.openProject = function(id)
	{
		var project = this.projects[this.active];
		main.typeText("#project_title", project.title, 1, 0);
		main.typeText("#project_role", project.tags, 1, 0);
		main.typeText("#project_description", project.description, 1, 0);
		main.typeText("#project_date", project.year, 1, 0);
		main.typeText("#project_agency", project.agency, 1, 0);
		main.typeText("#feature_title", project.title, 1, 0);
		
		$("a#project_agency").attr("href", project.agencylink);
		$("a#project_agency").attr("target", "_blank");
		$("#feature a.close").show();
		$("#project_link ").text("Launch Project");
		$("#project_link").attr("href", project.link);
		$("#project_link").attr("target", "_blank");
		$("#profile").slideUp(1000,'easeInOutCubic');
		$("#feature").slideDown( 1000, 'easeInOutCubic', this.populateFeatureProject );
		$("#featured_image").hide();
		$("#featured_thumbs").text('');
		if(project['media']){
			if(project['media'].length > 0){
				this.generateGallery();
			}
		}
	}
	
	this.generateGallery = function()
	{
		$("#featured_thumbs").text('');
		var media 	= this.projects[this.active]['media'];
		var path		= "#/projects/" + this.projects[this.active]['shortname'] + "/" ;
		for(var i = 0 ; i < media.length; i++ ){
			var item	= media[i];
			var tpl	= this.thumb_template.clone();
			var id	= "thumb_" + i;
			var div	= "#" + id;
			
			$(tpl).attr('id', id);
			$("#featured_thumbs").append(tpl);
			$(div).fadeOut(0);
			$(div).delay(i * 100).fadeIn(500);
			
			$(div + " img").fadeOut(0);
			$(div + " img").attr('src', item['thumb']);
			$(div + " img").load(function(){
				$(this).fadeIn(1000);
			});
			$(div + " a").attr('href', path + i);
			$(div + " .selected").hide();
		}
		
		this.openProjectPreview(0);
		
		if(media.length > 0 && !this.__t2) null;
			//this.openProjectPreview(0);
	}
	
	this.openProjectPreview = function(id){
		if(this.active_feature_thumb){
			$(this.active_feature_thumb + " .selected").hide();
		}
		
		this.active_feature_count = parseInt(id);
		
		this.active_feature_thumb = "#thumb_" + id;
		$(this.active_feature_thumb + " .selected").show();
		
		var url	= this.projects[this.active]['media'][parseInt(id)]['url'];
		$("#featured_image").stop(true, true);
		$("#featured_image").fadeOut(0);
		$("#featured_image").attr('src', url);
		$("#featured_image").load(function(){
			$(this).fadeIn(1000);
		})
	}
	
	
	this.typeText = function(id, text, time, delay){
		Tween.to({ 'id': $(id), 'text': text}, { to: 1, from: 0, time:time, delay:delay  }, function(event){
			event.target.id.text(event.target.text.substr(0, (event.target.text).length * event.now));
		});
	}
	
	this.generateProjects = function(){
		var list	= Projects;
		for(var i=0; i<list.length; i++){
			var project	= list[i];
			var tpl		= this.item_template.clone();
			var id		= 'project_' + i;
			var __cb		= this.animateTextCallback; 
			
			main.projects[project.shortname] = project;
			$(tpl).attr('id', id);
			$(tpl).fadeTo(0,0);
			$("#projects .container").append(tpl);
			
			var div = "#" + id;
			$(div).delay(400 + (i * 200)).fadeTo(1000,1);
			$(div + " .title").attr('href', '#/projects/' + project.shortname);
			$(div + " .description").attr('href', '#/projects/' + project.shortname);
			$(div + " .cta").attr('href', '#/projects/' + project.shortname);
			$(div + " .image img").fadeOut(0);
			$(div + " .image img").attr("src", project.coverimage);
			$(div + " .image img").load(function(){
				$(this).fadeIn(1000);
			})
			
			Tween.to({'div': $(div + " .title"), 'prop': project}, { to:1, from:0, time:0.5, delay: 1 + (0.2 * i) }, function(event){
				(event.target.div).text((event.target.prop.title).substr(0, (event.target.prop.title).length * event.now));
			});
			
			Tween.to({'div': $(div + " .description"), 'prop': project}, {to:1, from:0, time:0.5, delay: 1 + (0.2 * i) }, function(event){
				__cb(event.target.div, event.target.prop.tags, event.now);
			});
			//$(div + " .title").text(project.title);
			//$(div + " .description").text(project.tags);
		}
	}
	
	this.animateTextCallback	=  function(target, message, now){
		var strlength	= (message).length * now;
		var str			= (message).substr(0, strlength);
		(target).text(str);
	}
	
	this.closeProject = function()
	{
		var txt		= this.projects[this.active].title ;
		Tween.to($("#feature_title"), { to: 0, from: 1, time:.5  }, function(event){
			event.target.text(txt.substr(0, txt.length * event.now));
		});
		$("#feature a.close").hide();
		$("#feature").delay(400).slideUp(1000, 'easeInOutCubic');
		$("#profile").delay(400).slideDown(1000,'easeInOutCubic');
		
		this.active = null;
	}

	this.deeplink = function(event)
	{
		var addr		= event.value;
		var __arr	= addr.split('/');
		
		this.__t0		= __arr[1];
		this.__t1		= __arr[2];
		this.__t2		= __arr[3];
		
		if(this.__t0 == "projects"){
			if(this.__t1){
				if(this.active != this.__t1){
					this.active = this.__t1;
					this.openProject();
				}
				if(this.__t2){
					this.openProjectPreview(this.__t2);
				}
			}
		}else{
			if(this.active){	
				this.closeProject();
				this.active = null;
			}
		}
	}
	
	this.init();
};
/**/
