
/* 
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)
			}
			
			
    	});  
 	}
  	
    }); 


	var TDebug = true;
	
	function log() {

	  if(TDebug !== true) return;

	  try {
	    console.log.apply( console, arguments );
	  } catch(e) {
	    try {
	      opera.postError.apply( opera, arguments );
		  return;
	    } catch(e){
			return;
		}
	  }
	}
	
	
	
	function textFx (target, opts){

		var ele = $('a', $(target));
		var strp = ele.text(); 
		var progress = 0;
		if($(target).data('init')===true) return;
		$(target).data('init', true);
		var duration;
		
		(opts.duration) ? duration = opts.duration : duration = 50;

		var timer = setInterval(function() {
			ele.text(strp.substring(0, progress++) + (progress & 1 ? '_' : ''));
           if (progress > strp.length) {
				clearInterval(timer);
				$(target).data('init', false);
				ele.text(strp)
			}
		
		}, duration);
		
		this.clean = function() {
			ele.text(strp)
			$(target).data('init', false);
			clearInterval(timer)
		}
		
	
	};
	
	$.fn.textWriter = function(opts) {
		
		
		
		return this.each(function(){
			var n;
			var ele = $(this);
			
			if(!opts.noclick) {
				
				$(this).bind('click', function(ev){
					window.location.href = $('a', $(this)).attr('href')
				});
			}
			

			$(this).bind('mouseenter', function(ev){
				n = new textFx(this, {});
			});
		
			
			//$(this).bind('mouseleave', function(ev) {
				//n.clean();
			//});
			
		})
		
	}
	
	$.TtextFx = function(opts, obj) {
		return new textFx(opts, obj);
	}
	
	
	
	$.fn.serv = function() {
		
		var elements = [1,2,3,4];
		var actual = 0;
		var init = true;
		//alert('hola')
		$.each(elements, function(){
			$('#p'+this).css({'display':'none'});
			$('#t'+this).css({'display':'none'});
		});
		
		
		function mostra( quin ) {
			
			if(init!==true) {
				$('#p'+actual).css({'display':'none'});
				$('#t'+actual).css({'display':'none'});
				Pare(actual).removeClass('sel');
			}
			init = false;
			if(document.all) {
				$('#p'+quin).css({'display':''});
				$('#t'+quin).css({'display':''});
			} else {
				$('#p'+quin).fadeIn(1000);
				$('#t'+quin).fadeIn(1000);
			}
			
			Pare(quin).addClass('sel');
			actual = quin;
			
		}
		
		function Pare(e) {
			it = $('#z'+e).parent().get(0);
			return $(it);
		}
		
		
		mostra(1);
		$('li a', $(this)).bind('click', function(){
			mostra(this.id.substring(1,2));
			return false;
		}).css({'cursor':'pointer'});
		
		
		
	}

	

	$.fn.wait = function(option, options) {
	        milli = 1000; 
	        if (option && (typeof option == 'function' || isNaN(option)) ) { 
	            options = option;
	        } else if (option) { 
	            milli = option;
	        }
	        // set defaults
	        var defaults = {
	            msec: milli,
	            onEnd: options
	        },
	        settings = $.extend({},defaults, options);

	        if(typeof settings.onEnd == 'function') {
	            this.each(function() {
	                setTimeout(settings.onEnd, settings.msec);
	            });
	            return this;
	        } else {
	            return this.queue('fx',
	            function() {
	                var self = this;
	                setTimeout(function() { $.dequeue(self); },settings.msec);
	            });
	        }

	 }
	
	
	
	
	


})(jQuery);
















