jQuery(document).ready(function($) {
    
    jQuery('a.new-window').click(function(){
	    window.open(this.href);
	    return false;
    });
    
    termometro();
    
    featured_inicio();
    
    lente_inicio();
    /*
    jQuery('.set_li').hover(function(){
	clearTimeout(lente_timer);
	var setid = jQuery(this).attr('id').replace('set_li_','');
	jQuery(".active_set_title").removeClass('active_set_title');
	jQuery(".set_img:visible").animate({opacity: 0},800,function(){});
	jQuery("#set_img_"+setid).css('display','block');
	jQuery("#set_img_"+setid).animate({opacity: 1},800,function(){});
	jQuery("#set_li_"+setid).addClass('active_set_title');
    },function(){
	lente_engine(0);
    });*/
    
    if(jQuery('.chunk_page').size()>0) {
	gallery_pager();
    }
    
    jQuery(".set_li, .al_post").click(function(){
	window.location.href = jQuery("a",this).attr("href");
    });
    
    jQuery('#home_slide_list li').click(function(){
	if(jQuery(this).attr('class').replace('first_thmb ','') != 'active_thmb'){
	    var id = jQuery(this).attr('id').replace('ft_thmb_','');
	    featured_animation_engine(id);
	}
	return false;
    });
	
    $('.nav').supersubs({
        minWidth: 		9,						// requires em unit.
        maxWidth: 		25,						// requires em unit.
        extraWidth: 	0						// extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
    }).superfish({
	hoverClass:		'nav-hover',			// the class applied to hovered list items 
    //	pathClass:		'overideThisToUse',		// the class you have applied to list items that lead to the current page 
	pathLevels:		1,						// the number of levels of submenus that remain open or are restored using pathClass 
	delay:			400,					// the delay in milliseconds that the mouse can remain outside a submenu without it closing 
	animation:		{opacity:'show'},		// an object equivalent to first parameter of jQuery’s .animate() method 
	speed:			'normal',				// speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
	autoArrows:		false,					// if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
	disableHI:		false					// set to true to disable hoverIntent detection 
    //	onInit:			function(){},			// callback function fires once Superfish is initialised – 'this' is the containing ul 
    //	onBeforeShow:	function(){},			// callback function fires just before reveal animation begins – 'this' is the ul about to open 
    //	onShow:			function(){},			// callback function fires once reveal animation completed – 'this' is the opened ul 
    //	onHide:			function(){}			// callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
	});
});

// TERMOMETRO
var termometro = function(){
    jQuery('.termo_answerform strong').click(function(){
	if(jQuery(this.parentNode).attr('id') == 'inactive_now'){
	    return false;
	}
	
	if(jQuery(this).attr('class') == 'termo_q_g'){
	    jQuery(this).addClass('termo_q_ok');
	}else{
	    jQuery(this).addClass('termo_q_bad');
	}
	jQuery(this.parentNode).attr('id','inactive_now')
	jQuery('strong',this.parentNode).addClass('termo_q_clicked').removeClass('termo_q_opt');
	jQuery('.termo_answer',this.parentNode.parentNode).css('display','block');
    });
}

var gallery_curpage = 0;
var gallery_totpage = 0;
var gallery_working = false;
var gallery_pager = function() {
	jQuery('.chunk_visible').show();
	jQuery(".gallery_showthis").click(function(){
		jQuery(".chunk_page a.active").removeClass("active");
		jQuery("#gallery_loader").css("display","block").fadeOut(5000);
		jQuery("#gallery_currentphoto").attr("src",jQuery(this).attr("rel"));
		jQuery(this).addClass("active");
		return false;
	});
	gallery_totpage = jQuery(".chunk_page").size() - 1;
	jQuery(".chunk_navigation p a").click(function(){
		if(jQuery(this).hasClass("chn_inactive") || gallery_working) {
			return false;
		}
		gallery_working = true;
		go_to = (jQuery(this).hasClass("chn_prev")) ? gallery_curpage-1 : gallery_curpage+1;
		jQuery(".chunk_page").eq(gallery_curpage).slideUp("slow");
		jQuery(".chunk_page").eq(go_to).slideDown("slow",function(){ gallery_working=false; });
		gallery_curpage = go_to;
		jQuery(".chn_inactive").removeClass("chn_inactive");
		if (gallery_curpage==gallery_totpage) {
			jQuery(".chn_next").addClass("chn_inactive");
		}
		if (gallery_curpage==0) {
			jQuery(".chn_prev").addClass("chn_inactive");
		}
		jQuery(".chunk_navigation span").html("P&aacute;gina " + (gallery_curpage+1) + " de " + (gallery_totpage+1));
		return false;
	});
}


// INICIO SLIDESHOW
var images = 0;
var slide_timer = null;
var cur_img = 1;
var featured_inicio = function(){
    jQuery('.ft_big_img').each(function(){
	images++;
	jQuery(this).css('display','none')
    });
    jQuery('.ft_big_img:not(:first)').animate({opacity: 0},1)
    featured_animation_engine(1);
}
var featured_animation_engine = function(img_id){
    if(images > 1){
	if(slide_timer) {
	    clearTimeout(slide_timer);
	}
	if(img_id==0) {
	    img_id = (cur_img==images) ? 1 : ((cur_img*1)+1);
	}
	cur_img=img_id;
	jQuery(".active_thmb").removeClass('active_thmb');
	jQuery(".ft_big_img:visible").animate({opacity: 0},800,function(){jQuery(this).css('display','none')});
	jQuery("#ft_img_"+img_id).css('display','block');
	jQuery("#ft_img_"+img_id).animate({opacity: 1},800,function(){});
	jQuery("#ft_thmb_"+img_id).addClass('active_thmb');
	slide_timer = setTimeout('featured_animation_engine(0)',5000);
    }else{
	jQuery('#ft_img_1').animate({opacity: 1},1);
    }
}


// LENTE
var imgs = 0;
var lente_timer = null;
var cur_set = 1;
var lente_inicio = function(){
    jQuery('.set_li').each(function(){
	imgs++;
    });
    jQuery('.set_img:not(:first)').animate({opacity: 0},1)
    lente_engine(0);
}
var lente_engine = function(set_id){
    if(imgs > 1){
	if(slide_timer) {
	    clearTimeout(lente_timer);
	}
	if(set_id==0) {
	    set_id = (cur_set==imgs) ? 1 : ((cur_set*1)+1);
	}
	cur_set=set_id;
	jQuery(".active_set_title").removeClass('active_set_title');
	jQuery(".set_img:visible").animate({opacity: 0},800,function(){});
	jQuery("#set_img_"+set_id).css('display','block');
	jQuery("#set_img_"+set_id).animate({opacity: 1},800,function(){});
	jQuery("#set_li_"+set_id).addClass('active_set_title');
	lente_timer = setTimeout('lente_engine(0)',5000);
    }else{
	jQuery('#ft_img_1').animate({opacity: 1},1);
    }
}