/* 
TPlayer >>> v.1.0
=========================
jordi@tempomedialab.com

HTML:
<div id="tplayer-1">
	<img src="public/htmls/f1.jpg" width="252" height="163" />
	<img src="public/htmls/f2.jpg" width="252" height="163" class="dnone" />
	<img src="public/htmls/f3.jpg" width="252" height="163" class="dnone" />
	<ul>
		<li><a href="#"><img src="public/htmls/bt.gif" width="9" height="9" /></a></li>
	</ul>
</div>


   	$('#tplayer-1').tplayer()


Version. 1.01 ... first release
version  1.1 ... autoplay mode


*/

(function($){ 
    $.fn.extend({
	
		tPlayer: function(options) {  
	
		var defaults = {  
			version: '1.1',
			// class to select images to see not the first!
 			classImg: 'img.dnone',
 			autoMode: false,
			interval: 4000,
			stopOnInteraction: true
	  	}
	
		var options = $.extend(defaults, options);

		var cycle=function() {
			
			//console.log('Actual: '+options.actual)
			if(options.actual == options.images.length-1) next = 0;
			else next = options.actual+1;
			
			isAuto = true;
			options.butons[next].trigger("click");

		};
		
		var isAuto = false; // To know if event is triggeret from interval
		

    	return this.each(function() {  
  			obj = $(this);
			// The image
			var image = $('img:first',obj);
			
			
			options.actual = 0;
			options.images = [];
			$('img:first, ' + options.classImg, obj).each(function(){
				options.images[options.images.length] = this.src;
			})
			
			if(options.images.length == 1) {
				$('ul li', obj).toggle();
				return;
			}
				
			// li button
			var bt = $('ul li', obj);

			// Allways ofbutton namebuton(off.gif)
			tmp = $('img', bt).attr('src').split(".");
			options.btoff_src = tmp[tmp.length-2] + "off.gif";
			options.bton_src = tmp[tmp.length-2] + ".gif";
			
			// PT tho the showing image
			options.ptactual = $('img', bt)
			
			
			// add butons for each image
			for(i=0; i<options.images.length-1; i++){
				$('ul', obj).append( bt.clone() );
			}
			
			// array de botons
			options.butons = []
			
			// Add a index to compute array position...and swap btoff
			$('ul li a', obj).each( function(n) {  
				this.index = n;
				// array de botons
				options.butons[n] = $(this); 
				$(this).children('img').attr({src:options.btoff_src});
			} )
			// Activate bton
			options.ptactual.attr({src: options.bton_src });
			
			$('ul li a', obj).click(function() {

				var ell = $(this);
				options.ptactual.attr({src:options.btoff_src}) 
				ell.children('img').attr({src: options.bton_src})
				//options.actual = this.index;
				options.ptactual = ell.children('img')
				// index
				options.actual = this.index
				image.fadeOut(300, function() {  
					 image.attr({'src': options.images[options.actual]  });
					 image.fadeIn(300)
					//if(options.onshow) options.onshow()
					});
				
				// if automode, reset timer, perhaps stop?
				if(options.autoMode==true && isAuto==false) {
					window.clearInterval(options.inte);
					if(options.stopOnInteraction==false) options.inte = window.setInterval(cycle,options.interval);
				}
				
				isAuto=false;
				//if(options.onclick) options.onclick()
				return false;
			});
			
			// If automode, autoplay
			if(options.autoMode==true) {
				options.inte = window.setInterval(cycle,options.interval)
			}
			
			
    	});  
 	}
  	
    }); 
})(jQuery);


