/*
Script:
	moo.history.js, <http://dev.justinmaier.com/mooHistory>

Based on:
	jquery.history.js <http://www.mikage.to/jquery/jquery_history.html>, by mikage <http://www.mikage.to>

Modified by:
	Justin Maier, <http://justinmaier.com>

License:
	MIT-style license.
*/
var mooHistory = new Class ({
	getOptions: function(){
		return {
			onComplete: function(el){
				
			},
			onLoading: function(){
				
			},
			update: ['content'],
			exclude: [],
			baseTitle: '',
			extension: '/',
			evalScripts: true,
			alwaysStart: false
		};
	},
	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		this.webHost = window.location.host+this.options.extension;
		this.strip = ['http://'+this.webHost,'http://www.'+this.webHost,'#','index.html','index.php']
		if(this.options.alwaysStart != false && location.hash == (''&&'#'))location.hash = '#'+this.options.alwaysStart;
		this.historyCurrentHash = location.hash;
		if(window.ie) {
			// To stop the callback firing twice during initilization if no hash present
			if (this.historyCurrentHash == '') {
				this.historyCurrentHash = '#';
			}
		
			// add hidden iframe for IE
			this.ihistory = new Element('iframe').setStyle('display','none').setProperty('id','ajaxHistory').injectInside(document.body);
			var iframe = this.ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = this.historyCurrentHash;
		}
		else if (window.safari) {
			// etablish back/forward stacks
			this.historyBackStack = [];
			this.historyBackStack.length = history.length;
			this.historyForwardStack = [];
			this.isFirst = true;
		}
		
		this.historyCallback(this.historyCurrentHash.substring(1));//Add to history/get new page
		this.historyCheck.bind(this).periodical(100);//Check hash every 100 millis
		this.hookAnchors();//hook all anchors within your domain
	},
	
	historyCurrentHash: false,
	
	historyCallback: function(hash){
		if(hash) {
			var buffer = new Element('div').setStyle('display','none').injectBefore(document.body.getFirst());
			new Ajax(hash,{method:'get',update: buffer,
				onStart: function(){
					this.fireEvent('onLoading');
				},
				onComplete: function(text,xml){
					if($E('title',buffer))document.title = this.options.baseTitle+' - '+$E('title',buffer).innerHTML;//Grab page title and append it to the base title, doesnt work in IE?
					var newContent = new Hash;
					this.options.update.each(function(el){
						newContent.set(el, $(el).innerHTML);
					});
					buffer.remove();
					newContent.each(function(el){
						$(el).setHTML(newContent.get(el));
						this.fireEvent('onComplete',[$(el)]);
					}.bind(this));
					this.hookAnchors();
					//Eval Scripts
					if(this.options.evalScripts){
						var script, regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
						while ((script = regexp.exec(text))) eval(script[1]);
					}
				}.bind(this)
			}).request();
		}	
	},
	hookAnchors: function(){
		$$('a').each(function(el){
			if(el.href.match(this.webHost)){
				var newHref = el.href;
				if(this.testExclude(el.href)){
					this.strip.each(function(toStrip){
						newHref = newHref.replace(toStrip,'');
					});
					if(newHref != ''){
					el.href = '#'+newHref;
					el.onclick = null;
					el.removeEvents('click');
					if(window.ie)el.addEvent('click',function(){var hash = el.href;hash = hash.substring(1);this.historyLoad(hash);return false;}.bind(this));
					}else{el.setProperty('rel','dead');}
				}
			}
			if(el.rel == 'dead')el.href = '#'+location.hash.substring(1);
		}.bind(this));
	},
	testExclude: function(href){
		var result = true;
		this.options.exclude.each(function(ex){
			if(href.match(ex)){
				result = false;
			}
		}.bind(this));
		return(result);
	},
	historyAddHistory: function(hash) {
		// This makes the looping function do something
		this.historyBackStack.push(hash);
		
		this.historyForwardStack.length = 0; // clear forwardStack (true click occured)
		this.isFirst = true;
	},
	
	historyCheck: function(){
		if(window.ie) {
			// On IE, check for location.hash of iframe
			var ihistory = $('ajaxHistory');
			var iframe = this.ihistory.contentDocument || this.ihistory.contentWindow.document;
			var current_hash = iframe.location.hash;
			if(current_hash != this.historyCurrentHash) {
			
				location.hash = current_hash;
				this.historyCurrentHash = current_hash;
				this.historyCallback(current_hash.substring(1));		
			}
		} else if (window.safari) {
			if (!this.dontCheck) {
				var historyDelta = history.length - this.historyBackStack.length;
				
				if (historyDelta) { // back or forward button has been pushed
					this.isFirst = false;
					if (historyDelta < 0) { // back button has been pushed
						// move items to forward stack
						for (var i = 0; i < Math.abs(historyDelta); i++) this.historyForwardStack.unshift(this.historyBackStack.pop());
					} else { // forward button has been pushed
						// move items to back stack
						for (var i = 0; i < historyDelta; i++) this.historyBackStack.push(this.historyForwardStack.shift());
					}
					var cachedHash = this.historyBackStack[this.historyBackStack.length - 1];
					if (cachedHash != undefined) {
						this.historyCurrentHash = location.hash;
						this.historyCallback(cachedHash);
					}
				} else if (this.historyBackStack[this.historyBackStack.length - 1] == undefined && !this.isFirst) {
					// back button has been pushed to beginning and URL already pointed to hash (e.g. a bookmark)
					// document.URL doesn't change in Safari
					if (document.URL.indexOf('#') >= 0) {
						this.historyCallback(document.URL.split('#')[1]);
					} else {
						var current_hash = location.hash;
						this.historyCallback('');
					}
					this.isFirst = true;
				}
			}
		} else {
			// otherwise, check for location.hash
			var current_hash = location.hash;
			if(current_hash != this.historyCurrentHash) {
				this.historyCurrentHash = current_hash;
				this.historyCallback(current_hash.substring(1));
			}
		}
	},
	historyLoad: function(hash){
		var newhash;
		
		if (window.safari) {
			newhash = hash;
		}
		else {
			newhash = '#' + hash;
			location.hash = newhash;
		}
		this.historyCurrentHash = newhash;
		
		if(window.ie) {
			var ihistory = $('ajaxHistory');
			var iframe = this.ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = newhash;
			this.historyCallback(hash);
		}
		else if (window.safari) {
			this.dontCheck = true;
			// Manually keep track of the history values for Safari
			this.historyAddHistory(hash);
			
			// Wait a while before allowing checking so that Safari has time to update the "history" object
			// correctly (otherwise the check loop would detect a false change in hash).
			var fn = function() {this.dontCheck = false;};
			fn.delay(200);
			this.historyCallback(hash);
			// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
			//      By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
			//      URL in the browser and the "history" object are both updated correctly.
			location.hash = newhash;
		}
		else {
		  this.historyCallback(hash);
		}
	}
});

mooHistory.implement(new Events);
mooHistory.implement(new Options);



