jQuery(document).ready(function($) {
	$('.mw_widget_area:not(.wedgt_box_active)').css("display","none");
	$('.mw_widget_tab_title li').click(function(){
		$('.mw_widget_tab_title .tab_active').removeClass('tab_active');
		var tabid = $(this).addClass('tab_active').attr("id").replace("wdg_tab_","");
		$('.mw_widget_area').css("display","none");
		$('#wdgt_box_'+tabid).css("display","block");
	});
	
	$('#the_post_comments').css('display','none');
	$('.post_tabs_link').click(function(){
		var activo = $('.post_tab_active').removeClass('post_tab_active').attr("href");
		$(this).addClass('post_tab_active');
		$(activo).css('display','none');
		$($(this).attr('href')).css('display','block');
		return false;
	});
	
	if(jQuery('.al_group').size()>1) {
		agenda_libre();
	}
	
	if(jQuery('.chunk_page').size()>0) {
		gallery_pager();
	}
	
	jQuery(".set_li, .al_post").click(function(){
		window.location.href = jQuery("a",this).attr("href");
	});
	
	jQuery('a.new-window').click(function(){
	    window.open(this.href);
	    return false;
    });

	sets_animation_start();
	
	$('#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 
	});
});

var al_groups = 0;
var anima = false;
var agenda_libre = function(){
	jQuery('.al_group').each(function(){
		al_groups++
	});
	jQuery('.al_control_button').click(function(){
		var idactivo = jQuery('.al_group_active').attr("id").replace("al_group_","");
		if(jQuery(this).attr("id") == 'prev'){
			if(((idactivo*1) > 1) && !anima){
				anima = true;
				jQuery(".al_group_active").animate({width: "1px"}, 1500 );
				jQuery("#al_group_"+((idactivo*1)-1)).animate({width: "794px"}, 1500, reactivate_anim );
				jQuery(".al_group_active").removeClass("al_group_active").addClass("al_group_inactive");
				jQuery("#al_group_"+((idactivo*1)-1)).removeClass("al_group_inactive").addClass("al_group_active");
			}
		}else{
			if(((idactivo*1) < al_groups) && !anima){
				anima = true;
				jQuery(".al_group_active").animate({width: "1px"}, 1500 );
				jQuery("#al_group_"+((idactivo*1)+1)).animate({width: "794px"}, 1500, reactivate_anim );
				jQuery(".al_group_active").removeClass("al_group_active").addClass("al_group_inactive");
				jQuery("#al_group_"+((idactivo*1)+1)).removeClass("al_group_inactive").addClass("al_group_active");
			}
		}
		return false;
	});
}

var reactivate_anim = function() {
	anima = false;
}

var sets = 0;
var slide_timer = null;
var cur_img = 1;
var sets_animation_start = function(){
	jQuery('.set_img').animate({opacity: 0},1).each(function(){
		sets++;
	});
	
	jQuery('.next_set_link').click(function(){
		if(slide_timer) {
			clearTimeout(slide_timer);
		}
		sets_animation_engine(0);
	});
	
	sets_animation_engine(1);
}
var sets_animation_engine = function(img_id){
	if(slide_timer) {
		clearTimeout(slide_timer);
	}
	if(img_id==0) {
		img_id = (cur_img==sets) ? 1 : ((cur_img*1)+1);
	}
	cur_img=img_id;
	jQuery(".set_img:visible").animate({opacity: 0},300);
	jQuery("#set_img_"+img_id).animate({opacity: 1},500);
	jQuery(".hometop_sets_list_active_li").removeClass("hometop_sets_list_active_li");
	jQuery("#set_li_"+img_id).addClass("hometop_sets_list_active_li");
	slide_timer = setTimeout('sets_animation_engine(0)',7000);
}

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