ajax_history = new Class({
	Implements: Options,
	options: {
		update_div: 'kon_items'
	},
	getHash: function(){
		if (window.last_hash !=window.location.hash){  
			window.last_hash = window.location.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));
		//console.log(ajax_href);
		this.request(ajax_href);
	},
	request: function(link){
		 //console.log(link);
		//$(this.options.update_div).fade(0);
		history_preloader();
		this.scroll_fx.toElement($(this.options.update_div));
		//this.parentDiv.addClass('preloader_paginationa');
				
		new Request.HTML({
			url: 		link,
			update: 	this.options.update_div,
			evalScripts: true,
			onComplete: (function(){
				history_preloader_disable();
				//this.parentDiv.removeClass('preloader_paginationa');
				//$(this.options.update_div).fade(1);
				this.start_up();
			}).bind(this)
		}).send();
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.parentDiv = new Element('div',{'class':'pagination_parent_div'}).inject($(this.options.update_div),'before');
		//console.log(this.parentDiv);
		this.parentDiv.adopt($(this.options.update_div));
		
		this.timer = this.getHash.periodical(2000,this);		
		
		this.scroll_fx = new Fx.Scroll(window, {
			wait: false,
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		this.start_up();
		this.refresh();
	},
	start_up: function(){
		
		$$('.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;
				break;
			case 'select':
				var ajax_href = $(target.getProperty('id') + 'AjaxLink').value + '&' + $(target.getProperty('id') + 'AjaxLinkGet').value + '=' + target.value;
				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();
		
		var datum = [new Date(), new Date()];
		var current = datum[0].getTime();
		datum[0].setTime(current+3155692597470);
		datum[1].setTime(current-1);
		
		Cookie.set(hash_time, ajax_href.substring(ajax_href.indexOf('?')+1),{expires: datum[0].toUTCString()});
		Set_Cookie(hash_time,ajax_href.substring(ajax_href.indexOf('?')+1),20);
		
		
		window.last_hash = '#' + hash_time;
		window.location = 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);
		}
	}
}); 

window.addEvent('domready', function(e){
	if ($('kon_items')){
		document.ajax_history = new ajax_history({
			update_div: 'kon_items'
		});
	}
	window.last_hash = window.location.hash;
});

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


