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

/*
 *	1 de abril de 2009 - by Emiliano Mateu
 **/

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

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", "998");
		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", "999");
		pic_next.fadeIn(this.fxDelay);
		pic_next.addClass("active");
		pic.removeClass("active");
		pic.css("z-index", "998");
	};

	this.init();
}

function parseFeaturedBlocks(){
	
	var featured_blocks_children = jQuery("#featured-blocks li");
	
	featured_blocks_children.hover(
			function(){
				jQuery(this).find("img").css("top", "-145px");
			}, 
			function(){
				jQuery(this).find("img").css("top", "0");
			}
	);
}
