/*
 * Photoshow Presenter-Script
 * 2008 Copyright: Felix Pfeiffer : Neue Medien info@felixpfeiffer.com
 * Lizenz: GPL
 * params: el:		ID of the List, taht holds the thumbnails and urls of the fullsize image
 *         options: {
 *				hide: Do you want the thumbnails to be hidden?
 *				nextButtonGraf: Grafic for the next-button
 *				prevButtonGraf: Grafic for the previous-button
 */

var PhotoShow = new Class({
    options: {
        hide: true,
        nextButtonGraf: 'system/modules/photoshow/html/gallery_next.gif',
        prevButtonGraf: 'system/modules/photoshow/html/gallery_prev.gif',
        width: null,
        height: null
    },
	initialize: function(el,options)
	{
		this.setOptions(options);
		this.el = el;
		this.ap = 0;
		this.op = 0;
		this.ad = $(el).getElements('div.thumb');
		this.al = $(el).getElements('a');
		this.ai = $(el).getElements('img');
		if(this.options.hide) $(el).setStyle('display', 'none');
		this.le = this.al.length;
		this.create();
		this.changeImage();
		
	},
	changeImage : function()
	{
		$(this.ici).setProperty('src',this.al[this.ap].getProperty('href'));
		$(this.icc).innerHTML = this.al[this.ap].getProperty('title');
		$(this.ici).setProperty('alt',this.ai[this.ap].getProperty('alt'));
		$(this.ad[this.op]).removeClass('active');
		$(this.ad[this.ap]).addClass('active');		
	},
	next: function()
	{
		this.op = this.ap;
		this.ap = (this.ap == this.le - 1) ? 0 : this.ap+1;
		this.changeImage();
		return false;	
	},
	prev: function()
	{
		this.op = this.ap;
		this.ap = (this.ap == 0) ? this.le - 1 : this.ap-1;
		this.changeImage();
		return false;
	},
	showImage: function(element)
	{			
		this.op = this.ap;
		var newp = this.al.indexOf($(element));
		
		if(this.ap != newp)
		{
			this.ap = newp;
			this.changeImage();
		}
	},
	create: function()
	{
		var gc = new Element('div',{'class':'photoShowImage'});
		
		var nb = new Element('div',{'class':'nextButton'}).injectInside(gc);
		var nba = new Element('a',{'class': 'nextButtonLink','href':'#'}).injectInside(nb);
		var nbi = new Element('img',{'class':'', 'src':this.options.nextButtonGraf}).injectInside(nba);
		nba.onclick = this.next.bind(this);
		
		var pb = new Element('div',{'class':'prevButton'}).injectInside(gc);
		var pba = new Element('a',{'class': 'prevButtonLink','href':'#'}).injectInside(pb);
		var pbi = new Element('img',{'src':this.options.prevButtonGraf}).injectInside(pba);
		pba.onclick = this.prev.bind(this);
		
		this.ic = new Element('div',{'class':'imgBig'}).injectInside(gc);
		this.icl = new Element('a',{'href':'#','class':'imgBigLinkNext'}).injectInside(this.ic);
		this.ici = new Element('img',{'src':this.ai[0],'class':'imgBigSRC'}).injectInside(this.icl);
		this.icc = new Element('div',{'class':'imgBigCaption'}).injectAfter(this.ic);
		this.icl.onclick = this.next.bind(this);
		
		$(gc).inject(this.el, 'before');		
		
	}
});

PhotoShow.implement(new Options);
