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());
		if(this.useXHR) {
			this.useXHR = (!$('blurb') || !$('blurb').getChildren().length>0);
		}

		this.reloadElements();

		this.request = new Request.HTML({
			IVWClick: true,
			onSuccess: this.updateContainer.bindWithEvent(this)
		});

		this.checkCurrentPhoto();
		this.prepareLinks();
		fbContInit();
	},

	post: function(url, form) {
		this.request.options.url = url;
		this.request.options.data = form.toQueryString() + '&version=1';
		this.request.post();
	},

	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));

		// warum geht das nicht?
		// window.document.removeEvent('keyup', this.cursorPagination.bindWithEvent(this));
		window.document.removeEvents('keyup');
		window.document.addEvent('keyup', this.cursorPagination.bindWithEvent(this));
		stars = $$('.stars')[0];
		if(stars) {
			stars.getElements('a').each(function(star, idx) {
				star.addEvent('click', function() { this.rate(star, idx+1); return false; }.bind(this) );
			}, 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');
		setTimeout( this.createNameplate.bind(this), 1500 );
		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 (typeof(FB) != 'undefined' && FB && FB.XFBML.parse() ) {
            setTimeout( this.reparseFBML.bind(this), 100 );
		}
		fbContInit();
		try {
			initQuickComment();
		}
		catch(e){}

		// wird in anzeigen.page definiert
		if(typeof(showRoar) != 'undefined' && showRoar) {
			rr = new RoarHandler('keyboard_foto', {
				'delay': 100,
				'container': this.container.photo
			});
		}

		if(_gaq) {
			_gaq.push(['_trackPageview', this.request.options.url]);
		}

		initEventStars();
	},

	createNameplate: function() {
		nameplate = new nameplateObject($('photo'), $('nameplate'), $('nameplate-overlay'));
	},

	reparseFBML: function(){
		if($$('.photo-like')[0]) {
			FB.XFBML.parse($$('.photo-like')[0]);
		}
		if($$('.fbcomments')[0]) {
			FB.XFBML.parse($$('.fbcomments')[0]);
		}
	},

	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){ }
		}
	},

	rate: function(el, r) {
		el.setStyle('cursor', 'wait');
		stars = $$('.stars')[0];
		new Request({
			url: el.get('href'),
			onSuccess: function(res) {
				stars.getElements('a').dispose();
				stars.getElements('li').removeClass('active');

				stars.getElements('li').each(function(item, idx) {
					if(idx <= r-1) {
						item.addClass('active');
					}
				});
				stars.highlight('#96D050');
				el.setStyle('cursor', 'default');
				new Element('p', {
					'html': 'Danke für deine Bewertung',
					'class': 'infotext',
					'styles': {
						'margin-left': '-6px',
						'clear': 'left'
					}
				}).inject($$('.ratings')[0]);
				$('load_rating').hide();
				$('rating_count').show();
				$('rating_count_nr').set('html', $('rating_count_nr').get('html').toInt() + 1);
			},
			onFailure: function() {
				stars.highlight('#dd0000');
				el.setStyle('cursor', 'default');
				$('load_rating').hide();
			}
		}).send();
	},

	cursorPagination: function(e){
		var el = null;

		if(this.disabled){
			return true;
		}

		// Bewertungen mit der Tastatur (normale Zahlen und Numpad)
		if(e.code == '49' || e.code == '97') { // 1
			r = 1;
			el = $$('.stars')[0].getElement('li.one').getElement('a');
		}
		else if(e.code == '50' || e.code == '98') { // 2
			r = 2;
			el = $$('.stars')[0].getElement('li.two').getElement('a');
		}
		else if(e.code == '51' || e.code == '99') { // 3
			r = 3;
			el = $$('.stars')[0].getElement('li.three').getElement('a');
		}
		else if(e.code == '52' || e.code == '100') { // 4
			r = 4;
			el = $$('.stars')[0].getElement('li.four').getElement('a');
		}
		else if(e.code == '53' || e.code == '101') { // 5
			r = 5;
			el = $$('.stars')[0].getElement('li.five').getElement('a');
		}
		if(el) {
			$('load_rating').show();
			el.set('href', el.get('href') + '/keyboard');
			this.rate(el, r);
		}

		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.options.data = {'keyboard': true};
					this.request.send();
				}
				else {
					document.location.href = el.get('href');
				}
			}
			catch(e){ }
			return false;
		}
		return;
	}

});
