//----------------------------
// Bilderplattform
//----------------------------

document.observe("dom:loaded", function() {
	
	if ($('pictures') && $('pictures').down('.listview_index')) {
		var Slideshow = {
			init: function() {
				$$('.listview_index a').each(function(link) {
					l = this.tagslice(link.href);
					params = {
						url: l.split('?')[0],
						id: l.toQueryParams()["id"],
						action: l.toQueryParams()["action"],
						filter: l.toQueryParams()["filter"],
						tag: this.tag
					}
					link.observe("click", this.linkClicked.bindAsEventListener(this, params));
				}.bind(this));
				
				pl = this.tagslice($('preview').down('a.prev').href);
				this.prev = pl.toQueryParams()["id"] || 0;

				$('preview').down('a.prev').observe("click", this.clickPrev.bindAsEventListener(this, {
					url: pl.split('?')[0],
					action:  pl.toQueryParams()["action"],
					filter:  pl.toQueryParams()["filter"],
					tag:  this.tag
				}));
				
				nl = this.tagslice($('preview').down('a.next').href);
				this.next = nl.toQueryParams()["id"] || 0;

				$('preview').down('a.next').observe("click", this.clickNext.bindAsEventListener(this, {
					url: nl.split('?')[0],
					action:  nl.toQueryParams()["action"],
					filter:  nl.toQueryParams()["filter"],
					tag:  this.tag
				}));

				this.managePaging();
			},
			linkClicked: function(event, params) {
				event.stop();

				if (params.id) {
					new Ajax.Request(params.url, {
						method: "GET",
						parameters: {
							id: params.id,
							ajax: true,
							action: params.action,
							filter: params.filter,
							tag: params.tag
						},
						onSuccess: this.ajaxSuccess.bind(this)
					});
				}
				
			},
			ajaxSuccess: function(request) {
				response = request.responseJSON;
				$('previewbild').src = response.activeimageurl;
				$('previewbild').alt = response.title;
				$('previewbild').title = response.title;
				$('previewbild').up('a').href = response.titleurl;
				
				$('bilduser').update(response.username);
				$('bilduser').href = response.userurl;
				
				$('bildtitle').update(response.title);
				$('bildtitle').href = response.titleurl;
				
				$('pictures').down('.listview_index .active').removeClassName("active");
				
				$$('.listview_index a').each(function(link) {
					l = this.tagslice(link.href);
					if (l.toQueryParams()["id"] == response.activeid) {
						link.up("li").addClassName("active");
					}
				}.bind(this));
				
				this.next = response.nextid;
				this.prev = response.previd;
				
				this.managePaging();
			},
			clickNext: function(event, params) {
				this.linkClicked(event, {
					id: this.next,
					url: params.url,
					action: params.action, 
					filter: params.filter, 
					tag: params.tag 
				});
			},
			clickPrev: function(event, params) {
				this.linkClicked(event, {
					id: this.prev,
					url: params.url,
					action: params.action,
					filter: params.filter, 
					tag: params.tag 
				});
			},
			managePaging: function() {
				if (this.next) {
					$('preview').down('a.next').show();
				} else {
					$('preview').down('a.next').hide();
				}
				
				if (this.prev) {
					$('preview').down('a.prev').show();
				} else {
					$('preview').down('a.prev').hide();
				}
			},
			tagslice: function(l) {
				tagpos = l.indexOf('tag=');
				tag = l.slice(tagpos+4);
				l = l.slice(0,tagpos-1);
				amppos = tag.indexOf('&');
				if (amppos>0){
					l = l.concat(tag.slice(amppos));
					tag = tag.slice(0,amppos);
				}
				this.tag = tag;
				return l;
			}
			
		}
		Slideshow.init();
		
	}
	
});
