/**
 * Copyright 2010 Nicolas Pigelet
 * 
 * @author 		Nicolas Pigelet aka Tanaki
 * @contact		nipigelet@gmail.com
 * @project		Portfolio
 * 
 */

/**
 * @class		Portfolio
 * @methods 	
 * 
 */
var Portfolio = new Class({
	
	initialize : function(){
		this._initMentions();
		this._initSmooth();
		this._initAjax();
		this._initFilters();
		this._initAutoclean();
		this._checkHash();
	},
	
	_initMentions : function(){
		
		$("mentions").hide();
		$("footer").getElement(".mentions").addEvent("click", function(e){
			e.stop();
			$("mentions").toggle();
		});
		$("mentions").getElement(".close").addEvent("click", function(e){
			e.stop();
			$("mentions").toggle();
		});
	},
	
	_initSmooth : function(){
		$$(".smooth").each(function(a){
			a.addEvent("click", function(e){
				e.stop();
				new Fx.Scroll(window).toElement(a.href.split("#")[1]);
			});
		});
	},
	
	_initAjax : function(){
		var _self = this;
		$$(".ajax").each(function(a, i){
			a.addEvent("click", function(e){
				e.stop();
				_self._refresh(a.href, "work_detail", a.title);
			});
		});
		
		$$(".noAjax").each(function(a){
			a.addEvent("click", function(e){ e.stop(); });
		});
	},
	
	_initFilters : function(){
		
		$$(".filter").each(function(a){
			a.addEvent("click", function(e){
				e.stop();
				$$(".article").each(function(article){
					if ( !article.hasClass(a.title) ) article.fade("out").hide();
					else article.fade("in").show();
				});
			});
		});
		
	},
	
	_initAutoclean : function(){
		$$(".autoclean").each(function(el){
			el.addEvents({
				"focus" :function(){
					if(el.value == el.defaultValue) el.value = "";
					el.addClass("focus");
				},
				"blur" : function(){
					if(el.value == "") el.value = el.defaultValue;
					el.removeClass("focus");
				}
			});
		});
	},
	
	_checkHash : function(){
		if ( window.location.hash != "" && window.location.hash != undefined ) {
			var hashTag = window.location.hash.split("#")[1];
			this._refresh( "includes/works/" + hashTag + ".html", "work_detail", hashTag );
		}
	},
	
	_refresh : function(url, target, title){
		var query = new Request({
			url : url,
			onComplete : function(response){
				$(target).set("html", response);
				new Fx.Scroll(window).toTop();
				if( title ) window.location.hash = title;
			}
		}).send();
	}
});

 
// window event
window.addEvent("domready", function(){
	var oPortfolio = new Portfolio();
});