 /** 
    #########################################
    Author : JACKSON OATES jackson@summitprojects
    Company : Summit Projects http://summitprojects.com
    Date: 04/02/2010 
    Updated: NA 
    * 
    #########################################

    GENERATE 
     
    IMPLEMENT:  
    var wall = new Productwall(id,{  // the element
    	target:'overlayContainer',   // id for your container element
    	option:true					 // NA
   	})
    UPDATE LOG:
    NA
    
*/


var Productwall = new Class({
	Implements: [Options],	
    options:{
    	// passed when creating new OverlayImg
    	target	: null,
		history	: false
		
    },
    initialize: function(id, options){
    	this.setOptions(options);
    	this.id			= $(id); 
    	this.h			= this.options.history;
		this.loc		= this.setlocation();
    	this.target		= $(this.options.target);
		this.init();
	},
	init : function(){
		if(this.id.href == '' || this.target == ''){return false}
		this.id.addEvent('click',this.start.bindWithEvent(this))
	},
	setlocation : function(){
		// TODO: merge into regex
		if(this.id.href.contains('products/')){
			try
			{
				return this.id.href.split('products/')[1];
			}
			catch(err){
				console.log(err);
			}
		}
		if(this.id.href.contains('request/')){
			try
			{
				return this.id.href.split('request/')[1];
			}
			catch(err){
				console.log(err);
			}
		}
	},
	historyController : function(h){
		this.h.addState(this.loc);
	},
	start : function(event){
		event.preventDefault();
		
		$$('#catNav a').each(function(el){
			el.removeClass('active')
		});
		event.target.toggleClass('active');
		
		this.historyController();
		this.url = this.id.href.replace("products", "request");
		this.target.empty();
		
		
		
		this.loader = new Asset.image('/resources/images/embed/wall-loader.gif').inject(this.target);
		
		this.request = new Request.HTML({
			url:this.url,
			evalScripts:false,
			evalResponse:false,
			onSuccess:this.buildwall.bind(this) // should go though history manger now
		}).send();
	},

	// BUILD WALL WITH CONTENT //
	buildwall : function(responseTree, responseElements, responseHTML, responseJavaScript){
		this.target.empty();
		this.target.set('html',responseHTML)
		$exec(responseJavaScript);
		
		// not needed, this will now be handled by the history manager
		/*
		if(this.loc){
			window.location.hash = '#/' + this.loc;
		}
		*/
	}
	
});
Productwall.implement(new Events, new Options);
