/* @Rotador de html con fade version 1.0
* Autor: Claudio Romano Cherņac
* lanacion.com  03-2009
*/

jQuery.fn.slide = function(auto){
	return this.each(function(i){
		var active = 0; // Para el slide
		var isFade = false;
		var timer;

		var cnt = 1;
		if(auto){
			loopSlide = function(){
				timer = setTimeout("loopSlide()", 15000);
				cantidad = jQuery("#slide .destacada").size();
				//jQuery("#slide .destacada:eq("+active+")").fadeOut("fast", function(){active=cnt;});
				jQuery("#slide .destacada").fadeOut("fast", function(){active=cnt;});
				jQuery("#slide .destacada:eq("+cnt+")").fadeIn("fast", function(){cnt=(cnt==(cantidad-1))?0:cnt+1;});
				jQuery("#slide .preview a").fadeTo(10, 0.6).removeClass("active");
				jQuery("#slide .preview a:eq("+cnt+")").fadeTo(10, 1).addClass("active");
			}
			//name = jQuery(this).attr("id");
			timer = setTimeout("loopSlide()", 15000);
		}// if auto

		jQuery(this).find(".preview a").each(function(index){
			jQuery(this).fadeTo(10, 0.6);
			jQuery(this).hover(function(){
				jQuery(this).fadeTo(10, 1);
			}, function(){
				jQuery(this).fadeTo(10, 0.6);
				return false;
			});
			jQuery(this).click(function(e){
				e.preventDefault();
				if(timer!=null){clearTimeout(timer);}
				if(jQuery(this).parents().children(".destacada:eq("+index+")").attr("class")!='destacada showing' && !isFade){
					isFade = true;
					jQuery(this).parents().children(".destacada").fadeOut("fast", function(){
						//jQuery(this).attr("class","destacada hide");
						jQuery(this).removeClass("showing");
						jQuery(this).addClass("hide");
						isFade = false;
					});
					jQuery(this).parents().children(".destacada:eq("+index+")").fadeIn("fast", function(){
						//jQuery(this).attr("class","destacada showing");
						jQuery(this).removeClass("hide");
						jQuery(this).addClass("showing");
						
						isFade = false;
					});
					jQuery("#slide .preview a").fadeTo(10, 0.6).removeClass("active");
					jQuery(this).fadeTo(10, 1).addClass("active");
					//jQuery("#slide .preview a").fadeTo(10, 0.6).attr("class", "");
					//jQuery(this).fadeTo(10, 1).attr("class", "active");
				}
				return false;
			});
		});

		jQuery("#slide .preview a:eq(0)").fadeTo(10, 1).addClass("active");

	});
}





jQuery.fn.imagerotator = function(){

	return this.each(function(i){
		container = jQuery(this);
		var isFade = false; // Para que no cambie si estoy en el mismo elemento
		jQuery(this).find(".preview a").each(function(index){

			jQuery(this).hover(function(){
				if(jQuery(this).parents().children(".destacada:eq("+index+")").attr("class")!='destacada showing' && !isFade){
					isFade = true;
					jQuery(this).parents().children(".destacada").fadeOut("fast", function(){
						jQuery(this).attr("class","destacada hide");
						isFade = false;
					});
					jQuery(this).parents().children(".destacada:eq("+index+")").fadeIn("fast", function(){
						jQuery(this).attr("class","destacada showing");
						isFade = false;
					});
				}
			}, function(){
				return false;
			});

			jQuery(this).click(function(e){
				e.preventDefault();
				// Esta es la funcionalidad anterior, que mostraba la nota
				return false;
			});

		});
	});
}

jQuery.timer = function (interval, callback){
	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
};




