/**===========================================================================================
# mod_megaminislideshow		Mega Mini Slideshow Module For Joomla 1.6.0
#=============================================================================================
# author				OmegaTheme.com
# copyright				Copyright (C) 2011 OmegaTheme.com. All rights reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Website				http://omegatheme.com
# Technical support		Forum - http://omegatheme.com/forum/
#=============================================================================================*/


/**------------------------------------------------------------------------
* file: megamss_moo13.js 1.6.0 00001, Mar 2011 12:00:00Z OmegaTheme:Linh $
* package:	Mega Mini Slideshow module
* description: js  file	 mootools 1.3 native compatible
*------------------------------------------------------------------------*/

var MSS = new Class({
	//default options
	options: {
		showControls: true,
		showDuration: 5000,
		showToc: true,
		tocClass: 'toc',
		tocActiveClass: 'toc_active'
	},

	Implements: [Options,Events],
	initialize: function(container,elements,options) {
		this.container = document.id(container);
		this.elements = document.getElements(elements);
		this.options = Object.merge(this.options, options);
		this.currentIndex = 0;		
		this.interval = '';
		this.toc = new Array();
		
		if(this.options.showToc == true)
		{
			var tocContainer = new Element('div',{'class':'toc_container'}).inject(this.container);
			this.elements.each(function(el,i){
				var toc_elm = new Element('span',{
					text: i+1,
					'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
					events: {
						click: function(e) {
							if(e) e.stop();
							this.mstop();
							this.show(i);
						}.bind(this)
					}
				});
				this.toc.include(toc_elm);
				toc_elm.inject(tocContainer);
				
				if(i > 0) el.set('opacity',0);
			},this);
		}

		if(this.options.showControls == true) {
			this.createControls();
		}

		this.container.addEvents({
			mouseenter: function() { this.mstop(); }.bind(this),
			mouseleave: function() { this.mstart(); }.bind(this)
		});

	},
	show: function(toc_selected) {
		this.elements[this.currentIndex].fade('out');
		if(this.options.showToc == true) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
		this.currentIndex = ((toc_selected != null && toc_selected >= 0 && toc_selected < this.elements.length) ? toc_selected : (this.currentIndex >= 0 && this.currentIndex < (this.elements.length - 1) ? this.currentIndex + 1 : 0));
		this.elements[this.currentIndex].fade('in');
		if(this.options.showToc == true) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
	},

	mstart: function() {
		var toc_selected = null;
		this.interval = this.show.periodical(this.options.showDuration, this, [toc_selected]);
	},

	mstop: function() {
		clearInterval(this.interval);
	},

	/* Create Next and Previous button */
	createControls: function() {
		var next = new Element('span',{
			id: 'next',
			text: ' ',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.mstop(); 
					this.show();
				}.bind(this)
			}
		}).inject(this.container);
		var previous = new Element('span',{
			id: 'previous',
			text: ' ',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.mstop();
					this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
				}.bind(this)
			}
		}).inject(this.container);
	}
});

