/*
 *	Funciones aplicadas sólo en homepage
**/

jQuery(document).ready(function(){
	var photogallery = new PhotoGallery;
	var pastillas	 = new ParsePastillas;
});

/*
 *	1 de abril de 2009 - by Emiliano Mateu
 **/
function PhotoGallery(){

	this.looping;
	this.fxDelay	     		= 700;
	this.picDelay				= 4000;
	this.photogallery			= jQuery("#photogallery ul");
	this.photogallery_li 		= this.photogallery.children();
	this.photogallery_numbers	= "";

	this.init = function (){
		this.setup();
		this.loop();
		this.pick();
	};

	this.setup = function(){
		
		this.photogallery_li.css("z-index", "98");
		this.photogallery_li.first().addClass("first active");
		this.photogallery_li.last().addClass("last");
		this.photogallery_li.not(":first").hide();
		
		var k = 1;
		var j = "";

		this.photogallery_li.each(function(){
		    jQuery(this).attr("id", "photogalleryN"+k);
		    j = j+"<span id='photogalleryCN"+k+"'>"+k+"</span>";
		    k++;
		});

		this.photogallery.parent().append("<div id='photogallery_numbers'>"+j+"</div>");

		this.photogallery_numbers = jQuery("#photogallery_numbers span");
		this.photogallery_numbers.first().addClass("active");

	};

	this.loop = function(){		
		var obj = this;

		this.looping = setInterval(function(){
			var pic 			= jQuery("#photogallery li.active"); 
			var pic_next 		= pic.next(); if(pic.hasClass("last")) pic_next = obj.photogallery_li.first();
			var pic_next_id		= pic_next.attr("id").match(/\d+/);    
			var count_number 	= jQuery("#photogalleryCN"+pic_next_id);
	
			obj.galleryTransition(count_number, pic, pic_next);
		}, 	obj.picDelay);
	};

	this.pick = function(){
		var obj = this;
	
		this.photogallery_numbers.click(function(){
			obj.looping = clearInterval(obj.looping); // corto el loop
		
			var picked		  	= jQuery(this).attr("id").match(/\d+/);
			var pic 			= jQuery("#photogallery li.active");
			var pic_next 		= jQuery("#photogalleryN"+picked);
			var count_number 	= jQuery("#photogalleryCN"+picked);
	
			obj.galleryTransition(count_number, pic, pic_next);			
			obj.loop(); // reanudo el loop
		});

	};

	this.galleryTransition = function(count_number, pic, pic_next){
		count_number.siblings().removeClass("active");
		count_number.addClass("active");
		
		pic.siblings().hide();
		pic_next.css("z-index", "99");
		pic_next.fadeIn(this.fxDelay);
		pic_next.addClass("active");
		pic.removeClass("active");
		pic.css("z-index", "98");
	};

	this.init();
}

/*
 *	4 de junio de 2009 - Mateu
 **/
function ParsePastillas(){
	this.wrapper				= jQuery("#pastillas");
	this.browser				= BROWSER;
	this.pastillas  			= new Array();
	
	this.init = function(){
		this.parseItems();
	};
	
	this.parseItems = function(){
		var _this 		= this;		
		var is_webkit	= false;
		var i			= 0;
		
		if ( this.browser.indexOf("AppleWebKit") != -1){
			is_webkit 	= true;
		}
		
		this.wrapper.find(".pastillas-item").each(function(){
			i++;
			var id = i;
			
			var element 			= jQuery(this);
			var element_children 	= element.children().children();
			
			if(element_children.html() != null){
				
				element_children.unwrap();
				element_children = element.children();
				
				element_children_first  = element_children.first();
				element_children_last   = element_children.last();
				
				if(element_children_first.html() == element_children_last.html())
				{
					element.prepend("<div class='pastillas-content'><div/>");
					element_children_last.wrap("<div class='pastillas-link' />");
				}
				else
				{
					element_children_first.wrap("<div class='pastillas-content' />");
					element_children_last.wrap("<div class='pastillas-link' />");	
				}
				
				var link_div			= element.find(".pastillas-link")
				var link_img			= link_div.find("img");
				var link_width 			= 0;
				
				var content_div			= element.find(".pastillas-content");
				var content_img			= content_div.find("img");
				var has_content			= content_img.attr("src");
							
				/*
				 *  El width/height de la imagen lo puedo levantar luego del evento load, 
				 *  pues webkit no lo define hasta despues de dicho evento
				 * */
				if(is_webkit){
					jQuery(link_img).load(function() {
						link_width = link_img.width();
						element.width(link_width);				
					});
				} else {
					link_width = link_img.width();
					element.width(link_width);
				}
				
				/*
				 *  Guardo el height de la imagen de .pastillas-content y posiciono .pastillas-content 
				 * */
				if(has_content){
					var content_height = 0;
					content_div.show();
					if(is_webkit){
						jQuery(content_img).load(function() {
							content_height = content_img.height();
							content_div.height(content_height);
							content_div.css("top", "-"+content_height+"px");
							content_div.hide();
						});
					} else {
						content_height = content_img.height();
						content_div.height(content_height);
						content_div.css("top", "-"+content_height+"px");
						content_div.hide();
					}
					
				}
				
				_this.pastillas[id] 				= new Array();
				_this.pastillas[id]["id"]			= id;
				_this.pastillas[id]["link_img"] 	= link_img;
				_this.pastillas[id]["content_div"] 	= content_div;
				_this.pastillas[id]["animating"]	= false;
				_this.pastillas[id]["showing"]		= false;
				
				
				/*
				 * Defino el evento hover de .pastillas-link (mostrar/ocultar .pastillas-content)
				 **/
				element.hover(
					function(){
						_this.showContent(id);
					},
					function(){
						_this.hideContent(id);
					}
				);			
			}
		});
	};
	
	this.showContent = function(i){
		var _this = this;
		//if( _this.pastillas[i]["animating"] == false && _this.pastillas[i]["showing"] == false){
					
			_this.pastillas[i]["animating"]  = true;
			
			_this.pastillas[i]["link_img"].css("top", "-70px");
			_this.pastillas[i]["content_div"].fadeIn(800, function(){
				_this.pastillas[i]["animating"] = false;
				_this.pastillas[i]["showing"]   = true;
			});				
		//}
	};
	
	this.hideContent = function(i){		
		var _this = this;
		//if( _this.pastillas[i]["animating"] == false && _this.pastillas[i]["showing"] == true){
			
			_this.pastillas[i]["animating"]  = true;	
			
			_this.pastillas[i]["link_img"].css("top", "0");			
			_this.pastillas[i]["content_div"].fadeOut(400, function(){
				_this.pastillas[i]["animating"] = false;
				_this.pastillas[i]["showing"]   = false;
			});
		//}
	}
	
	this.init();	
}