/*

Slideshow

*/

(function($)
{
	var Slideshow = function(element, options)
	{
		var container = $(element);
		var obj = this;
		var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'startIndex':       1,
			'autoStart':        false, 
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'msfade',
            'children':         null,
			'callback': 		null
        };
		var elements;
		var current;
		var timer;
		var state;
		var z_count = 100;
		
		settings = $.extend( settings, options || {});
		
		elements = $(container).children();
		
		this.init = function()
		{
			if (elements.length > 1) 
			{
	        	$(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);

	        	for (var i = 0; i < elements.length; i++) 
				{
	            	$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
	        	}

				current = 0;
				
				$(elements[current]).show();
				
				if( settings.callback != null ) settings.callback( current, null, elements.length );
				
				if( settings.autoStart ) 
				{	
	                this.play();
	            }
			}
		}

		this.play = function()
		{
			var thisObj = this;
			
			timer = setTimeout( function(){ thisObj.next(); }, settings.timeout );

			state = 'PLAYING';
		};
		
		this.pause = function()
		{
			state = 'PAUSED';
			
			clearTimeout( timer );
		};
		
		this.display = function( n )
		{
			this.pause();
			
			var old = current;
			current = n;
			
			if( current >= elements.length ) current = 0;
			if( current < 0 ) current = elements.length - 1;
			
			this.switchImage( current, old );
		};
		
		this.next = function()
		{
			var old = current;
			current++;
			
			if( current >= elements.length ) current = 0;
			
			this.switchImage( current, old );
		};
		
		this.previous = function()
		{
			var old = current;
			current--;
			
			if( current < 0) current = elements.length - 1;
			
			this.switchImage( current, old );
		};
		
		this.switchImage = function( toShow, toHide )
		{	
			if( settings.callback != null ) settings.callback( toShow, toHide, elements.length );
			
			$( elements[toHide] ).stop( true );
			//$( elements[toHide] ).fadeTo(settings.speed, 0);
			
			$( elements[toShow] ).stop( true );
            $( elements[toShow] ).fadeTo(settings.speed, 1, function() {
							removeFilter($(this)[0]);
							$( elements[toHide] ).hide();	
						});
			z_count++;
			$( elements[toShow] ).css( 'z-index', z_count );
			
			if( state != 'PAUSED' ) this.play();
		};
		
		// start it
		
		this.init();

	};

	$.fn.slideshow = function(options)
	{
		return this.each(function()
		{
			var element = $(this);
			
			// Return early if this element already has a plugin instance
			if (element.data('slideshow')) return;

			// pass options to plugin constructor
			var slideshow = new Slideshow(this, options);

			// Store plugin object in this element's data
			element.data('slideshow', slideshow);
		});
	};
	
})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}
