ajax_history = new Class({
	Implements: Options,
	options: {
		update_div: 'items',
		parent_pagination_class: 'pagination_parent_div'
	},
	initialize: function(options){
		this.setOptions(options);
		
		/*
		 * periodicke volani fnc pro zjistovani
		 * zda se zmenil v link, respektive #neco
		 */ 
		this.timer = this.getHash.periodical(2000,this);
		
		
		this.original_update_div = $(this.options.update_div);
		if(this.original_update_div){
			this.scrollY = this.original_update_div.getPosition().y;
			// vytvoreni prvni urovne divka
			this.parent_div_pagination = new Element('div',{'class':this.options.parent_pagination_class}).inject(this.original_update_div,'before').setStyles({'overflow-x':'hidden'});
			this.parent_div_pagination.adopt($(this.options.update_div));
					
			/*
			 * vytvoreni druhe urovne divek, pro Fx
			 */
			
					
			this.fx_div = new Element('div').inject(this.original_update_div,'before')
				.setStyles({
					'width'	: this.original_update_div.getSize().x.toInt(),
					'height': this.original_update_div.getSize().y.toInt()
				})
				.adopt(this.original_update_div);
			/*
			 * Fx pro move left x right
			 */
			this.moveFx = new Fx.Tween(this.fx_div,'margin-left',{
				duration: 700,
				transition: Fx.Transitions.Quad.easeInOut,
				onComplete: (function(){
					this.fx_div.setStyle('margin-left',0);
					if($('temp_div'))
						$('temp_div').dispose();
				}).bind(this)
			});
			
			// sirka bloku		
			this.original_width = this.original_update_div.getSize().x.toInt();
			
			// zavolani funkci
			this.start_up();
			this.refresh();
		}
	},
	
	getHash: function(){
		if (window.last_hash !=window.location.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));
		this.request(ajax_href);
	},
	
	request: function(link){
		if ($(this.options.update_div)){
			this.moveFx.start(this.move == 'left'?((-1) * this.original_width):this.original_width);
			$(this.options.update_div).fade('hide');
			new Request.HTML({
				url: 		link,
				update: 	this.options.update_div,
				evalScripts: true,
				onComplete: (function(){
					this.start_up();
					$(this.options.update_div).fade(1);
				}).bind(this)
			}).send();
		}
	},
	
	start_up: function(){
		if ($(this.options.update_div).getElement('.current_page'))
		this.current_page = $(this.options.update_div).getElement('.current_page').getHTML();
		$$('.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;
		
		var ajax_href = target.getProperty('href');
		
		var goto_page_num = target.getHTML();
		/*
		 * Pokud jdu se strankovanim nahoru
		 */
		if (goto_page_num == 'Předchozí strana' || goto_page_num < this.current_page){
			this.move = 'right';
		}else if (goto_page_num == 'Další strana' || goto_page_num > this.current_page){
			this.move = 'left';
		}
		
		var orig = $(this.options.update_div);
		orig.addClass('sloupce').setProperty('id','temp_div').setStyle('width',this.original_width);
		
		var newdiv = new Element('div',{id:this.options.update_div,'class':'sloupce'})
			.inject(orig, this.move == 'left'?'after':'before')
			.setStyle('width',this.original_width);
			
		this.fx_div.setStyles({
			'width': 2 * this.original_width,
			'margin-left':(this.move == 'left')?0:0//(-1)*this.original_width
		});
			
		/*
		 * to uz je klasika
		 */
		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;
		window.location = href_basic + '#' + hash_time;
		
		// move scroll
		window.scroll(0,this.scrollY);
		
		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){
	document.ajax_history = new ajax_history({
		update_div: 'items'
	});
	window.last_hash = window.location.hash;
});