/**************************************************************


        Script          : Page Loader
        Version         : 1.1
        Authors         : Samuel Birch, GURU (http://www.designguru.sumy.ua)
        Desc            : load pages via AJAX.
        Licence         : Open Source MIT Licence


**************************************************************/


var pageLoader = new Class({
                                                          
        getOptions: function(){
                return {
                        links: '.loadMe',
                        loadInTo: 'content',
                        loadFrom: 'content',
                        _onStart: $empty,
                        _onComplete: $empty
                };
        },


        initialize: function(options){
        
                this.setOptions(this.getOptions(), options);
                
                this.links = $$(this.options.links);
                this.links.each(function(el,i){
                        el.addEvent('click',function(e){
                                if(e != undefined){
                                        new Event(e).stop();
                                }
                                this.start(el);
                        }.bind(this));
                }.bind(this));
        },
        
        setContent: function(html){
                var temp = new Element('div').set({
                        'id': 'temp',
                        'styles': {
                                'display': 'none'
                        },
                        'html': html
                }).inject(document.body);
                
                var newEl = $('temp').getElement('#'+this.options.loadFrom);
                newEl.replaces($(this.options.loadInTo));
                newEl.set('id', this.options.loadInTo);
                temp.destroy();
                
        },
        
        start: function(el){
                this.options._onStart();
                this.content = new Request.HTML({
                                                                method: 'get',
			                                                    evalScripts: false,
																onRequest: function(){
																	Popup.preloader('basicContent', 'on');
																},
                                                                onSuccess: this.complete.bind(this),
																onFailure: function(){
																	$('basicContent').set('html', '<div class="left"><h2>Error</h2><p>The page you are trying to access does not exist.</p></div>');
																	Popup.preloader('basicContent', 'off');
																},
                                                                autoCancel: true
                                                        }).get(el.href);
        },
        
        complete: function(tree, els, html, js){
				Popup.preloader('basicContent', 'off');
                this.setContent(html);
                this.options._onComplete();
        }


});
pageLoader.implement(new Events);
pageLoader.implement(new Options);


//Init basic content links
window.addEvent('domready', function(){
			var basicLoader = new pageLoader({
                        links: $$('.basic'),
                        loadInTo: 'basicContent',
                        loadFrom: 'content'
                });
        });
/*************************************************************/
