var historyB = new Class({
	Implements: Options,
	options: {
		update_div: 'items'
	},
	getHash: function(){
		if (window.last_hash !=window.location.hash){  
			window.last_hash = window.location.hash;
			if (window.last_hash != '')
				this.change(window.location.hash);
		} 
	},
	change: function(hash){
		if (document.location.href.indexOf('#') == -1) url  = document.location.href;
		else url  = document.location.href.substring(0,document.location.href.indexOf('#'))
	
		var ajax_href = url + '?'+Cookie.get(hash.substring(1));
		$(this.options.update_div).empty();
		this.request(ajax_href);
	},
	request: function(link){
		$('preloader').setStyle('display','block');
		new Request.HTML({
			url: 		link,
			update: 	this.options.update_div,
			evalScripts: true,
			onComplete: function(){$('preloader').setStyle('display','none')}
		}).send();
	},
	initialize: function(options){
		this.setOptions(options);
		this.timer = this.getHash.periodical(2000,this);		
		$$('.pagination_link').addEvent('click', this.eventFunction.bindWithEvent(this));
		$$('.pagination_link_select').addEvent('change', this.eventFunction.bindWithEvent(this));
	},
	eventFunction: function(e){
		var event = new Event(e);
		target = event.target;
		switch(target.get('tag')){
			case 'a':
				var ajax_href = target.getProperty('href');
				break;
			case 'input':
				var ajax_href = $(target.getProperty('id') + 'AjaxLink').value;
				var window_location = true;
				break;
			case 'select':
				var ajax_href = $(target.getProperty('id') + 'AjaxLink').value + '&' + $(target.getProperty('id') + 'AjaxLinkGet').value + '=' + target.value;
				var window_location = true;
				break;
				
		}
		
		if (document.location.href.indexOf('#') == -1){
			href_basic = document.location.href;
		} else {
			href_basic = document.location.href.substring(0,document.location.href.indexOf('#'))
		}
		
		this.url = href_basic;
		
		hash_time = $time();
		Cookie.set(hash_time, ajax_href.substring(ajax_href.indexOf('?')+1));

		window.last_hash = '#' + hash_time;
		if (window_location){
			window.location = href_basic + '#' + hash_time;
		} else {
			target.setProperty('href',href_basic + '#' + hash_time) ;
		}
		this.request(ajax_href);
	},
	refresh: function(){
		if (window.location.hash != ''){
			if (document.location.href.indexOf('#') == -1){
				this.url  = document.location.href;
			} else {
				this.url  = document.location.href.substring(0,document.location.href.indexOf('#'))
			}
			this.change(window.location.hash);
		}
	}
});
