

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

	Script		: History
	Version		: 1.0
	Authors		: Samuel Birch
	Desc		: Enables browser back/forward buttons when using Javascript or AJAX.
	Licence		: Open Source MIT Licence

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

var History = new Class({
							  
	getOptions: function(){
		return {
			links: '.main'
			,delayClick:50
			,data: ''
			,loadInTo:''
			/* ''
			
			,active_id:''*/
		};
	},
    dmp: function(v){
        this.viewer.set('html',v);
    },
	initialize: function(options){ 
	   return;
        this.anchor = new Element('a').setProperties({
            'name': ''
            ,'id': 'histroyAnchor'
        }).inject(document.body, 'top');
		this.setOptions(this.getOptions(), options);
		this.url = this.getHash();
		this.checkURL.periodical(this.options.delayClick,this);//this.options.delayClick,this);
		d('='+this.url);
		
		this.links = $$(this.options.links+' a');
		this.links.each(function(el,i){			 
			el.addEvent('click',function(e){
				if(e != undefined){
					new Event(e).stop();
				}
				this.set(el);
			}.bind(this));
		}.bind(this));
		
	},
	
	formatHash: function(str){
		str = str.toString();
		var index = str.indexOf('#');
		str = str.substr(index+1);
		return str;		
	},
	
	formatURL: function(str){
		str = str.toString();
		var index = str.indexOf('#');
		if(index > -1){
			str = str.substring(0, index);
		}
		return str;
	},
	getUrl: function(){
        return window.location.href.toString();
    },
    reloadPage: function(){
        window.location.href=this.getUrl();
    },
	getHash: function(){
        var hash = window.location.hash.toString().replace('#', '');
        return hash;
    },
    setHash: function(url){
        window.location = '#'+url;
    },
	set: function(url){
	   this.url=url;
        this.anchor.setProperties({
            'name':url
        });
		this.setHash(url);
	},
	
	checkURL: function(){
		var url = this.getHash();
		if(url != this.url){
		    if(url==''){
                this.reloadPage();
            } else {
                
            }
		    //d(url);
		    /*this.url=url;
		    this.set(url);
			this.setContent();*/
		}
	},
	
	setContent: function(){
		var url = this.url;
		d('set='+url)
	}

});

History.implement(new Events);
History.implement(new Options);


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