var galleryPagination = new Class({
	
	request: null,
	disabled: false,
	usexhr: true,
	
	container: {},
	
	initialize: function(paginationContainer, photoContainer, updateContainer){
		this.container.pag_id = paginationContainer;
		this.container.photo_id = photoContainer;
		this.container.update_id = updateContainer;

		this.usexhr = (!$('bigsizebannerfoto') || !$('bigsizebannerfoto').isDisplayed());
		
		this.reloadElements();
		
		this.request = new Request.HTML({
			IVWClick: true,
			onSuccess: this.updateContainer.bindWithEvent(this)
		});
		
		this.checkCurrentPhoto();
		this.prepareLinks();		
	},
	
	prepareLinks: function(){
		
		var lnks = this.container.pag.getElements('a');
		lnks.addEvent('click', this.reloadPhoto.bindWithEvent(this));
		lnks = this.container.pag.getElements('a.prev, a.next');
		lnks.addEvent('click', this.reloadPhoto.bindWithEvent(this));
		
		window.document.addEvent('keyup', this.cursorPagination.bindWithEvent(this));
	},
	
	reloadPhoto: function(event){
		if (this.disabled || !this.usexhr) return true;		
		target = (event.target.tagName == 'A') ? $(event.target) : $(event.target).getParent('a');
		try{
			this.request.options.url = target.get('href');			
			this.request.send();			
		}
		catch(e){ }
		return false;
	},
	
	updateContainer: function(){
		var url = this.request.options.url;	
		var photo_id = url.replace(/.*photo_id,([0-9]+)(#.*)?/g, '$1');
		if(overlay != null){
			overlay.hide();
		}
		
		try{
			document.title = this.request.getHeader('XHTTP-NEW-PAGE-TITLE');
		}
		catch(e){ }
		
		document.location.hash = photo_id;
		new Element('div').set('html', arguments[2]).getFirst().replaces(this.container.update);
		this.reloadElements();
		this.prepareLinks();
		if ( FB.XFBML.parse ) {
            setTimeout( this.reparseFBML.bind(this), 100 );
		}
		nameplate = new nameplateObject($('photo'), $('nameplate'), $('nameplate-overlay'));
		try {
			initQuickComment();
		}
		catch(e){}
	},
	
	reparseFBML: function(){
		FB.XFBML.parse(this.container.photo.getParent('div.hmedia'));
	},
	
	reloadElements: function(){
		this.container.pag = $(this.container.pag_id);
		this.container.photo = $(this.container.photo_id);
		this.container.update = $(this.container.update_id);
	},
	
	checkCurrentPhoto: function(){
		var url = document.location.href;
		var photo_url_id = url.replace(/.*photo_id,([0-9]+)(#.*)?/g, '$1');
		var photo_hash_id = document.location.hash.replace(/#/, '');
		
		if(parseInt(photo_url_id) != parseInt(photo_hash_id) && !isNaN(photo_hash_id) && photo_hash_id.length > 0){
			try{
				this.request.options.url = url.replace(/photo_id,[0-9]+(#.*)?/g,'photo_id,'+ photo_hash_id);
				this.request.send();
			}
			catch(e){ }
		}
	},
	
	cursorPagination: function(e){
		var el = null;
		
		if((e.code != 39 && e.code != 37 && e.code != 35 && e.code != 36) || this.disabled){
			return true;
		}
		
		if(e.code == 39) { // rechts
			el = $('photo-pagination').getElement('a.next');
		}
		else{
			if(e.code == 37){ //links
				el = $('photo-pagination').getElement('a.prev');
			}
			else{
				if(e.code == 35){ // Ende
					return true;
				}
				else{ // 36 Pos 1
					return true;
				}
			}
		}
		if(el != null){
			try{
				if (this.usexhr) {
					this.request.options.url = el.get('href');
					this.request.send();
				}
				else {
					document.location.href = el.get('href');
				}
			}
			catch(e){ }
			return false;
		}
		return;
	}
	
});

var pag, nameplate;
window.addEvent('domready', function() {
	pag = new galleryPagination('photo-pagination', 'photo-canvas', 'photo-container');	
	nameplate = new nameplateObject($('photo'), $('nameplate'), $('nameplate-overlay'));
});
