/**
 * Project part
 * @author berge
 */

/**
 * Class Project
 * @class Project
 */
NZO.Project = Class.extend(
{
	/**
	 * Current page
	 * @type int
	 */
	page: 0,
	
	/**
	 * Column height
	 * @type int
	 */
	colHeight : 396,
	
	/**
	 * Project data
	 * @type object
	 */
	projectData: null,
	
	/**
	 * Current Project
	 * @type int
	 */
	currentProject : 0,
	
	/**
	 * Constructor
	 * @param {string} projectData Project data
	 */
	init : function(projectData)
	{
		this.projectData = $.parseJSON(projectData);
		
		$("#scroll-buttons .down").click($.proxy(function(){this.scrollDown(); return false;}, this));
		$("#scroll-buttons .up").click($.proxy(function(){this.scrollUp(); return false;}, this));
		$("#project-categories a").click($.proxy(function(e){this.onClickCategory(e); return false;}, this));
		
		$("#project-categories a").eq(0).click();
		
		this.updateButtons();
		
		for(var i=0, il=$("#project-list li a").length; i < il; i++)
		{
			$("#project-list li a").eq(i).bind("click", {id: i}, $.proxy(this.loadProject, this));
		}
		
		this.loadFirst();
	},
	
	/**
	 * On click on a category
	 * @event
	 * @param {object} e Event
	 */
	onClickCategory : function(e)
	{
		$("#project-categories li").removeClass("active");
		
		var category = $(e.currentTarget).attr('href');
		category = category.substr(1, category.length);
		
		$("#project-categories li." + category).addClass("active");
		
		$("#project-list li").hide();
		$("#project-list li[class*=" + category + "]").show();
		
		this.updateButtons();
		this.loadFirst();
	},
	
	/**
	 * Load first project
	 */
	loadFirst : function()
	{
		$("#project-list li:visible a").eq(0).click();
	},
	
	/**
	 * Load a project
	 * @event
	 * @param {object} e Event
	 */
	loadProject : function(e)
	{
		var id = e.data.id;
		
		if(id != this.currentProject)
		{
			$("#project-list li").removeClass("active");
			$("#project-list li").eq(id).addClass("active");
			
			$("#project-detail .picture").append('<img src="' + this.projectData[id].picture_big + '" alt="' + this.projectData[id].name + '" style="display: none;"/>');
			$("#project-detail .picture img").eq(0).fadeOut(200, function(){ $("#project-detail .picture img").eq(0).remove(); $("#project-detail .picture img").fadeIn(200); });
			
			$("#project-detail .date").html(this.projectData[id].year);
			$("#project-detail h3").html(this.projectData[id].name);
			$("#project-detail p").html(this.projectData[id].description);
			
			this.currentProject = id;
			
			return false;
		}
	},
	
	/**
	 * Scroll down
	 */
	scrollDown : function()
	{
		if($("#project-list ul").height() > (this.page + 1) * this.colHeight)
		{
			this.page ++;
			
			$("#project-list ul").animate(
			{
				'margin-top': '-' + (this.colHeight * this.page)
			}, 500);
			
			this.updateButtons();
		}
	},
	
	/**
	 * Scroll up
	 */
	scrollUp : function()
	{
		if(this.page > 0)
		{
			this.page --;
			
			$("#project-list ul").animate(
			{
				'margin-top': '-' + (this.colHeight * this.page)
			}, 500);
			
			this.updateButtons();
		}
	},
	
	/**
	 * Mise ˆ jour des boutons
	 */
	updateButtons : function()
	{
		if($("#project-list ul").height() <= (this.page + 1) * this.colHeight)
		{
			$("#scroll-buttons .down").animate({'opacity': 0.2}, 500);
		}
		else
		{
			$("#scroll-buttons .down").animate({'opacity': 1}, 500);
		}
		
		if(this.page == 0)
		{
			$("#scroll-buttons .up").animate({'opacity': 0.2}, 500);
		}
		else
		{
			$("#scroll-buttons .up").animate({'opacity': 1}, 500);
		}
	}
});
