/**
 * Description for file
 *
 * JS version $version
 *
 * <code>
 * <ul class="slideshow">
 * 		<li><img src="001.jpg" /></li>
 * 		<li><img src="002.jpg" /></li>
 * 		<li><img src="003.jpg" /></li>
 * 	</ul>
 * 	</code>
 *
 * @author      Firstname Lastname <internet@promacx.ch>
 * @copyright   PROMACX AG / Wasserwerkgasse 20 / 3011 CH-Bern
 * @link        www.promacx.ch
 * @licence     http://www.opensource.org/licenses/mit-license.php The MIT License
 * @version     0.0.1
 */

jQuery.fn.slideShow = function (settings) 
{	
	//alert("slideshow");
	
	// Overwride default settings...
	var settings = jQuery.extend({
		interval:5,
		fade:1,
		images:[]
	}, settings);
	
	// Find sources...
	var childs = this.find('img');
	var sources = new Array();
	childs.each(function (){
		var source = jQuery(this).attr('src');
		if (source != null){
			sources.push(source);	
		}
	});
	settings.images = settings.images.concat(sources);
	
	// Remove current html...
	this.empty();
	
	// Create new slideshow...
	var slideshow = new jQuery.pmx.slideShow.object(this, settings);
	slideshow.start();
	
}

// Create namespace...
if (jQuery.pmx == null) jQuery.pmx = new Object();

// SlideShow...
jQuery.pmx.slideShow = {
	count: 0,
	instances: {},
	object: function (view, settings){
		
		this._id = 'jquery-slideshow-'+(++jQuery.pmx.slideShow.count);
		this._view = view;
		this._settings = settings;
		
		jQuery.pmx.slideShow.instances[this._id] = this;
		//alert(settings.images)
	
		this.start = function (){
			//alert('Debug: start '+this._id);
			var s = this._settings.interval;
			this.interval = window.setInterval('jQuery.pmx.slideShow.instances[\''+this._id+'\'].next()', s*1000);
			this.next();
		};
		
		this.stop = function (){
		
		};
		
		this.next = function (){
			//alert('Debug: next '+this._id);
			var source = this._settings.images.shift();
			var img = new Image();
			img.owner = this;
			img.onload = function (){this.owner.onImageLoad(this);}
			img.onerror = function (){this.owner.onImageError(this);}
			img.src = source;
			//alert(this._settings.images);
			this._settings.images.push(source);
		};
		
		this.prev = function (){
			
		};
		
		this.addImage = function (){
		
		};
		
		this.removeImage = function (){
		
		};
		
		this.onAnimationFinished = function (){
			//alert('Debug: finished '+this);
			var self = jQuery(this);
			self.parent().prev().remove();
		};
	
	
		this.onImageLoad = function (image){
			//alert('Debug: image loaded '+image.src);
			var image = jQuery('<img src="'+image.src+'" style="visibility:hidden; position:absolute; left:0px; top:0px;" />');
			
			var defaultWidth = 666;//self.width();
			var defaultHeight = 600;//self.height();
			var startWidth = defaultWidth/100*90;
			var startHeight = defaultHeight/100*90;
			var finalWidth = defaultWidth/100*110;
			var finalHeight = defaultHeight/100*110;
			
			var s = this._settings.fade;
			//image.width(startWidth);
			//image.height(startHeight);
			image.css("opacity", 0);
			image.css("visibility", "visible");
			
			image.animate({
				//width:finalWidth,
				//height:finalHeight,
				opacity:1
			}, s*1000, jQuery.easing.easeout, this.onAnimationFinished);
			
			image.appendTo(this._view);
			image.wrap('<li></li>');
		};
	
		
		this.onImageError = function (){
			//alert('Debug: error '+this);
			var self = jQuery(this);
			self.parent().remove();
		};
		
		//this.start();
	}
}