/**
 * General Javascript functions
 */
$(document).ready(function() {	
	
    $("div .btn").css("opacity", "0.5").hover(
		function () {
			$(this).removeClass("btn").addClass("btn_hover").css("opacity", "1");
		}, 
		function () {
			$(this).removeClass("btn_hover").addClass("btn").css("opacity", "0.5");
		}
	);

    $("div .btnIndex").hover(
		function () {
			$(this).removeClass("btnIndex").addClass("btnIndex_hover");
		},
		function () {
			$(this).removeClass("btnIndex_hover").addClass("btnIndex");
		}
	);
	
    /**
	 * Sanitace the target: _blank
	 */
    $("a[rel='external']").live("click", function(){ this.target = "_blank"; });

    /**
     * Create the menú actions
     */
	createMenu("ddMenu");
});

/**
 * Menú actions funcionality
 */
function createMenu(id){
	var timeout         = 500;
	var closetimer		= 0;	
	
    $('.header_menu li ul').hide();
	$('#' + id + ' > li').bind('mouseover', ddMenu_open);
	$('#' + id + ' > li').bind('mouseout',  ddMenu_timer);

	function ddMenu_open(){	
		ddMenu_canceltimer();
		$('.header_menu li ul:visible').not($(this).find('ul').eq(0)).hide("fast");
		$(this).find('ul').eq(0).show("fast");
	}

	function ddMenu_close(){
		$('.header_menu li ul:visible').hide("fast");
	}

	function ddMenu_timer(){
		ddMenu_canceltimer();
        closetimer = window.setTimeout(ddMenu_close, timeout);
	}

	function ddMenu_canceltimer(){	
		if(closetimer){				
            window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	
	$(document).bind("click", function(){ ddMenu_close() });
}