jQuery(document).ready(function() {
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 100, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 100, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	jQuery("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	jQuery("ul#topnav li").hoverIntent(config);
	
	if(jQuery('#slider').length) {
		jQuery('#slider').nivoSlider({
			effect:'fade',
			animSpeed:500,
			pauseTime:5000,
			directionNav:false,
			keyboardNav:false,
			pauseOnHover:true
		});
	}
	
	jQuery('input[type="text"]').focus(function() {
        if (this.value == this.defaultValue){
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    jQuery('input[type="text"]').blur(function() {
        if (this.value == ''){
            this.value = this.defaultValue;
        }
	});
    jQuery('textarea').focus(function() {
        if (this.value == this.defaultValue){
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    jQuery('textarea').blur(function() {
        if (this.value == ''){
            this.value = this.defaultValue;
        }
    });
	

});

function hide_comments() {
	var visible = jQuery('#hide_comments').attr('class');
	if(visible == 'visible') {
		jQuery('#commentlist').fadeOut('fast');
		jQuery('#comment_form').fadeOut('fast');
		jQuery('#paginated-comments-pages').fadeOut('fast');
		jQuery('#hide_comments').text('Exibir Comentários');
		jQuery('#hide_comments').removeClass('visible');
		jQuery('#hide_comments').addClass('invisible');
	} else {
		jQuery('#commentlist').fadeIn('fast');
		jQuery('#comment_form').fadeIn('fast');
		jQuery('#paginated-comments-pages').fadeIn('fast');
		jQuery('#hide_comments').text('Ocultar Comentários');
		jQuery('#hide_comments').removeClass('invisible');
		jQuery('#hide_comments').addClass('visible');
	}
}
	
function megaHoverOver(){
	jQuery(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery);

    jQuery(this).find(".menulink").addClass("selected");

    if ( jQuery(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;

        jQuery(this).find(".row").each(function() {	//for each row...
        	jQuery(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        jQuery(this).find(".sub").css({'width' :biggestRow}); //Set width
        jQuery(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

    	jQuery(this).calcSubWidth();  //Call function to calculate width of all ul's
    	jQuery(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
	jQuery(this).find(".menulink").removeClass("selected");
	jQuery(this).find(".sub").stop().fadeTo(100, 0, function() { //Fade to 0 opactiy
		jQuery(this).hide();  //after fading, hide it
	});
}
