

/**************************************************************

	Script		: Page Loader
	Version		: 1.0
	Authors		: Samuel Birch
	Desc		: load pages via AJAX.
	Licence		: Open Source MIT Licence

**************************************************************/

var pageLoader = new Class({
	Implements: [Chain, Events, Options],			  
	getOptions: function(){
		return {
			links: '.main',
			loadInTo: 'content_fix',
			loadFrom: 'content_fix',
			data: 'a',
			active_id: 'sub',
			loaderHtml: '',
			onStart: $empty,
			onComplete: Class.empty,
			evalScript: true
		};
	},

	initialize: function(options){
	
		this.setOptions(this.getOptions(), options);
		
		this.links = $$(this.options.links+' a');
		this.links.each(function(el,i){
		  //d(el);
		  //el.setProperty('href', el.getProperty('href')+'?'+this.options.data);
			el.addEvent('click',function(e){
				if(e != undefined){
					new Event(e).stop();
				}
				this.start(el);
			}.bind(this));
		}.bind(this));
	},
	
	setContent: function(text){
		//var temp = new Element('div').setProperties({id:'temp'}).setStyles({display:'none'}).injectInside(document.body);
		//d(text);
		$$(this.options.loadInTo).set('html', text);
		//var newEl = $('temp').getElement('#'+this.options.loadFrom);
		//$(this.options.loadInTo).replaces($('temp'));
		//newEl.replaces($(this.options.loadInTo));
		//newEl.setProperties({id:this.options.loadInTo});
		//temp.dispose();
		//this.options.onComplete();
		this.success();
		//contentFunctions();
	},
	success: function(){
        this.onSuccess();
    },
    onSuccess: function(){
		this.fireEvent('success', arguments).callChain();
	},
	start: function(el){
		//this.options.onStart();
		//el.href=el.href+'?a';
		if(this.options.loaderHtml!=''){
		  $$(this.options.loadInTo).set('html', this.options.loaderHtml);
		  //this.setContent(this.options.loaderHtml);
		}
		if(this.options.active_id!=''){
    		if(el.getParent().getProperty('id')==this.options.active_id){
                //return true;
            }
    		$$(this.options.links+"[id="+this.options.active_id+"]").removeProperty('id');
    		el.getParent().setProperty('id', this.options.active_id);
         }
		 var req = new Request({
                                url: el.href,
								method: 'get'
                                ,data: this.options.data
                                ,evalScripts: this.options.evalScript
                                //,evalResponse: true
                                /*,
								onComplete: this.complete.bind(this),
								autoCancel: true*/
								,onSuccess: this.complete.bind(this)
							}).send();
        /*req.onSuccess=function(t){
            //d(t);
        }*/
        //this.content
        /*this.content={
            response:{
                text:this.content.get()
            }
        };*/
        //this.complete();
        //this.content.response.text=this.content.get();
	},
	
	complete: function(txt){
		this.setContent(txt);
		//this.options.onComplete();
	}

});
pageLoader.implement(new Events);
pageLoader.implement(new Options);
window.addEvent('domready', function(){
	//new pageLoader({links:'#sub-navigation li', loadInTo:'#c-text', data:'a-sub', active_id:'sub1'});
	//new History();
});


/*************************************************************/
