/*
	VeAvisos's SlideShow is essentially based on JonDesign's SmoothGallery, using 
    the Prototype javascript framework instead of Moo Tools, and is based extensively on
    the excellent work of Jonathon Schemoul.
	
	Date: 2009-01-05
*/
var SlideShow = Class.create();
SlideShow.prototype = {
	initialize: function(element, options) {
        
        if (!options) options = {};
		this.setOptions({
			embedLinks: true,
			delayAfterConstruct: 0,			
			delay: 4000,
			preloader: true,
			/* Data retrieval */
            manualData: [],
			destroyAfterPopulate: true,
			elementSelector: "div.imageElement",
			linkSelector: "a.open",
			imageSelector: "img.full",
			
			/* CSS Classes */			
            baseClass: 'jdGallery'
		}, options);
		
		this.waitingForEffect = 0;
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.galleryElement = element;
		this.galleryData = this.options.manualData;
		this.galleryInit = 1;
		this.galleryElements = Array();
		this.galleryElement.addClassName(this.options.baseClass);
        
        this.populateFrom = element;
        this.populateData();
		element.style.display = "block";
		
		if (this.options.embedLinks)
		{
			this.currentLink = new Element('a').addClassName('open');
			this.currentLink.writeAttribute('href', '');
			this.currentLink.writeAttribute('title', '');		
			element.appendChild(this.currentLink);
			this.galleryElement = element = this.currentLink;
			
		}
        
		this.constructElements();
		
		this.loadingElement = new Element('div').addClassName('loadingElement');
        element.insert(this.loadingElement);
		
		if (this.delayAfterConstruct > 0)
			setTimeout(this.startSlideShow.bind(this), this.delayAfterConstruct);
		else
			this.startSlideShow();
	}, 
	
	
	nextItem: function() {
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},

	prevItem: function() {
		this.nextIter = this.currentIter-1;
		if (this.nextIter <= -1)
			this.nextIter = this.maxIter - 1;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},

	goTo: function(num) {
		this.clearTimer();
		if (this.options.embedLinks)
			this.clearLink();		
		setTimeout(this.changeItem.bind(this, num), 500);
		if (this.options.embedLinks)
			this.makeLink(num);
		this.prepareTimer();
        
	},

	changeItem: function(num) {
		this.galleryInit = 0;
		if (this.currentIter != num)
		{
			for(i=0;i<this.maxIter;i++)
			{
				if ((i != this.currentIter)) {
                    this.galleryElements[i].setStyle({'opacity': 0});
                }
			}
                        
            new Effect.Parallel([
            new Effect.Appear(this.galleryElements[num], {sync: true}),
            new Effect.Fade(this.galleryElements[this.currentIter], {sync: true})]);
			this.currentIter = num;
		}
	},
	
	clearTimer: function() {
		clearTimeout(this.timer);
		clearInterval(this.timer);
	},
	
	prepareTimer: function() {		
		this.timer = setInterval(this.nextItem.bind(this), this.options.delay);
	},
	
	makeLink: function(num) {
		this.currentLink.writeAttribute('href', this.galleryData[num].link);
		this.currentLink.writeAttribute('title', this.galleryData[num].linkTitle);
		this.currentLink.setStyle('display', 'block');
	},
	clearLink: function() {
		this.currentLink.writeAttribute('href', '');
		this.currentLink.writeAttribute('title', '');

	},
	
	startSlideShow: function() {
		this.loadingElement.style.display = "none";
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.galleryInit = 0;
		this.galleryElements[parseInt(this.currentIter)].setStyle({'opacity': 1});
		this.prepareTimer();
		if (this.options.embedLinks)
			this.makeLink(this.currentIter);
	},
	
	
	populateData: function() {
		currentArrayPlace = this.galleryData.length;
		options = this.options;
		var data = this.galleryData;
		this.populateFrom.select(options.elementSelector).each(function(el) {
			elementDict = {
				image: el.select(options.imageSelector)[0].readAttribute('src'),
				number: currentArrayPlace				
			};
			
			if (options.embedLinks)
				Object.extend(elementDict, {
					link: el.select(options.linkSelector)[0].readAttribute('href'),
					linkTitle: el.select(options.linkSelector)[0].readAttribute('title'),
					linkTarget: el.select(options.linkSelector)[0].readAttribute('target')
				});
				
			
			data[currentArrayPlace] = elementDict;
			currentArrayPlace++;
			if (this.options.destroyAfterPopulate)
				el.remove();
		});
		this.galleryData = data;
	},

	constructElements: function() {
		el = this.galleryElement;
		this.maxIter = this.galleryData.length;
		var currentImg;
		for(i=0;i<this.galleryData.length;i++)
		{
        
            var currentImg = new Element('div');
            currentImg.addClassName('slideElement');
            currentImg.setStyle({
					'position':'absolute',
					'left':'0px',
					'right':'0px',
					'margin':'0px',
					'padding':'0px',
					'backgroundImage':"url('" + this.galleryData[i].image + "')",
					'backgroundPosition':"center center",
					'opacity':'0'
                    });
            el.insert(currentImg);
			this.galleryElements[parseInt(i)] = currentImg;
		}
	},
	
	// Set default options, and override with specified options if applicable    
    setOptions: function(defaults, options) {
        for (var option in defaults) {
            if (typeof(options[option]) != 'undefined') {
                this[option] = options[option];
            } else {
                this[option] = defaults[option];
            }
        }
        this.options = defaults;
    }
};