var mooHistory = new Class ({
	getOptions: function(){
		return {
			onComplete: function(el){},
			onLoading: function(){},
			update: ['content_center'],
			exclude: [],
			baseTitle: '',
			dateiname:'ajax_content.php',
			parameters:'',
			idtag:'content_center',
			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(Browser.Engine.trident) {
			// 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(30);//Check hash every 100 millis
		this.hookAnchors();//hook all anchors within your domain
	},
	
	historyCurrentHash: false,
	
	send_me: function(alter_hash){
		if(Browser.Engine.trident) {
				var tpldatei = alter_hash.substring(1);
		}else{
				var tpldatei = alter_hash.substring(2);
		}

	var req = new Request.HTML({  
        	method: 'get',  
        	url: 'ajax_content.php',  
        	data: { 'tpl' : tpldatei},
        	headers:{'Content-type':  'content=text/html; charset=iso-8859-1'},
			evalScripts: true,
			evalResponse:true,
			update:$('content_center'),
        	onSuccess: function(response) {  
        		//$('content_center').set('html',response);
        	}
        });  
	
	
	
	req.setHeader('content-type','content="text/html; charset=iso-8859-1"');
	
	req.send();
	//alert(req.getHeader('Content-type'));
	
	},
	
	
	historyCallback: function(hash){
	
		if(hash) {
					
			var buffer = new Element('div').setStyle('display','none').injectBefore(document.body.getFirst());
		
			new Ajax('ajax_content.php',{
				method:'get',
				data: location.hash.substring(1),
				evalScripts: true,
				update: buffer,
			
				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(Browser.Engine.trident)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(Browser.Engine.trident) {
			// 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));		
		
			this.send_me(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));
			
				if(current_hash != ''){
					this.send_me(current_hash);
				}
			}
		}
	},
	historyLoad: function(hash){
		
		var newhash;
		
		if (Browser.Engine.webkit) {
			newhash = hash;
		}
		else {
			newhash = '#' + hash;
			//location.hash = newhash;
			newhash = location.hash;
		}
		
		//this.historyCurrentHash = newhash;
		
		if(Browser.Engine.trident) {
			var ihistory = $('ajaxHistory');
		
			var iframe = this.ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = newhash;
		}
		else if (Browser.Engine.webkit) {
			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.send_me(hash);
		}
	}
});

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



