 /* 
    #########################################
    Author : JACKSON OATES jackson@zeroninelab.com
    Company : ZERO NINE LAB, LLC - www.zeroninelab.com
    Date: 10/22/09 
    Updated: na
    Class: Projekt 
    Version : 0.1
    * 
    #########################################

    INITATE NEW PROJECT
    
    IMPLEMENT:  
     
    UPDATE LOG:
    09/04/09:
    v0.3 cured this.setOptions error when using with MT library version 1.2+
    added support for image dimensions, and a morph from loading image to injected asset,
    timeout added to kill request if asset is not found after x time.
    
*/


var Projekt = new Class({
	Implements: [Options],	
    options:{
		_trigger	 		: null,		
		_target		 		: null,
		_fire				: null,		
		_errorMsg	 		: 'Request could not be completed',
		_timeout		 	: 5000
		
    },
    initialize: function(options){
    	this.setOptions(options);
		this._trigger 			= this.options._trigger;
		this._target 			= this.options._target;
		this._url	 			= this.options._trigger.href;
    	this.timeout			= this.options._timeout;
    	this.initEvent();
    },
	initEvent : function(){
		this._trigger.addEvents({
			'click':this.triggerClick.bindWithEvent(this),
			'mouseenter':this.triggerMouseEnter.bindWithEvent(this),
			'mouseleave':this.triggerMouseLeave.bindWithEvent(this)
		})
	},
	triggerClick : function(event){
		if(event) event.stop();
		this.targetClear();
		var triggers = $(document.body).getElements('.'+this._trigger.className);
		triggers.each(function(el){
			el.removeClass('selected');
		})
		this._trigger.addClass('selected');
		this.initRequest();

	},
	triggerMouseEnter : function(event){
		event.target.fade('.5')
	},
	triggerMouseLeave : function(event){
		event.target.fade('in')
	},
	targetClear : function(){
		var fx = new Fx.Morph(this._target, {
			duration: 250, 
			transition: Fx.Transitions.Quad.easeInOut,
			onComplete:function(){
				this.element.empty();
				this.element.fade('in');
			}
			
		});
	
		fx.start({
			'opacity': 0
		});
	},
	fire : function(){
		this.fireEvent('fire');
	},
	initRequest : function(){
		var request = new Request.HTML({
			url:this._url,
			method: 'post',
			onSuccess:this.requestSuccess.bindWithEvent(this),
			onFail:this.requestFail
		}).send()		
		
	},
	requestFail : function(){
		//console.log('asset not found: trying again ', this._url + this._defaultFileName);
	},
	
	requestSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript){
		this.fireEvent('fire', responseHTML);
	}
	
});
Projekt.implement(new Events, new Options);
