jQuery(document).ready(function($) {
	
    games_slide_functions();
    
    featured_inicio();
    
    if(jQuery('.chunk_page').size()>0) {
	gallery_pager();
    }
    
    jQuery(".set_li, .al_post").click(function(){
	window.location.href = jQuery("a",this).attr("href");
    });
    
    jQuery('#ft_thumbs_holder img').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);
	}
    });
	
    $('.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 reactivate_anim = function() {
    anima = false;
}

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

// GAMES SLIDE

var games_slides_count = 0;
var anima = false;

var games_slide_functions = function(){
    jQuery('.games_slide').each(function(i){
	jQuery(this).attr('id','games_slide_'+(i+1));
	games_slides_count = i;
    });
    if(games_slides_count > 0){
	jQuery('#games_home_section h3').after('<ul id="games_home_control"><li class="prev"><span>Prev</span></li><li class="next"><span>Next</span></li></ul>')
	jQuery('.games_slide:not(#games_slide_1)').css('width','1px');
	jQuery('#games_slide_1').addClass('active_games_slide');
	jQuery('#games_home_control li').click(function(){
	    var thisli = jQuery(this).attr('class');
	    var activeslide = jQuery('.active_games_slide').attr('id').replace('games_slide_','');
	    if(thisli == 'next' && !anima){
		if((activeslide*1) < (games_slides_count+1)){
		    anima = true;
		    jQuery('.active_games_slide').animate({width: "1px"},1000);
		    jQuery('#games_slide_'+((activeslide*1)+1)).animate({width: "630px"},1000,function(){
			jQuery('.active_games_slide').removeClass('active_games_slide');
			jQuery('#games_slide_'+((activeslide*1)+1)).addClass('active_games_slide');
			anima = false;
		    });
		}
	    }else{
		if(thisli == 'prev' && !anima){
		    if((activeslide*1) > 1 ){
			anima = true;
			jQuery('.active_games_slide').animate({width: "1px"},1000);
			jQuery('#games_slide_'+((activeslide*1)-1)).animate({width: "630px"},1000,function(){
			    jQuery('.active_games_slide').removeClass('active_games_slide');
			    jQuery('#games_slide_'+((activeslide*1)-1)).addClass('active_games_slide');
			    anima = 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('.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);
    }
}