//make dollar private and an alias of jQuery
function fecharModal(){
	jQuery("#hotsite").jqmHide();
}

jQuery.preloadImages("../images/btn_agro.gif", "../images/btn_agro.gif", "../images/btn_express.gif", "../images/btn_food.gif", "../images/btn_saude.gif", "../images/btn_solutions.gif", "../images/btn_transports.gif", "../images/btn_bomi.gif");

(function($){
	//When document is ready
	$(document).ready(function(){
	
		//change page's title
		if( $("#page .componentheading").size() > 0 ) document.title = HOT_WORD + " > " + document.title;
				
		showHotsite = function(){
			$("#hotsite").jqm({zIndex:1000, overlay:80}).jqmShow();
		}
	
		//aux var for MAIN MENU
		var on = false;
	
		//constants collection
		var constants = new Object;
		
		//browser detection		
		constants.MSIE6   = $.browser.msie && $.browser.version.indexOf("6") == 0;
		constants.MSIE7   = $.browser.msie && $.browser.version.indexOf("7") == 0;
		constants.MOZILLA = $.browser.mozilla;
				
		//prevent horizontal scroll when screen is not that large
		//at 1128
		if( $(window).width() <= 1135 ){
			if(constants.MSIE6) $(".wrapper_detail").css("width","70px");
			if(constants.MSIE7) $(".wrapper_detail").css("width","71px");
			if(!$.browser.msie) $(".wrapper_detail").css("width","72px");
		} 
		
		//at 1024
		if( $(window).width() <= 1007 ){
			$(".wrapper_detail").hide();
			$(".main_wrapper").css("background","none")
			$(".inner_wrapper").css("marginTop","6px");
			
			//move the kid
			$(".moduletable_kid").css("left","10px");
		}
		
		//first menu item doesn't have description
		$(".moduletable_luftnav li").each(function(){
			if( $(this).hasClass( (HOT_WORD.toLowerCase()).replace("luft ","") ) )
				$(this).addClass("noHover");
		});
		
		//top menu rollover actions
		$(".moduletable_luftnav li:not(.noHover)").hover(
			function(){
				$("a", this).not(".link").addClass("hover");
				$(".inner:hidden", this).parent().show().end().show();
				$(".mask:hidden", this).show();
				$(".mask",this).height( $(".inner",this).height() + 32 );
			},function(){
				$(".mask:visible", this).hide();
				$(".inner:visible", this).hide(1,function(){
					$(this).parents().siblings("a").not(".link").removeClass("hover");
				});
			}
		);
		
		//add arrow in menu items (couldn't find a solution in Joomla =/ )
		$(".mainlevel_menu").each(function(){
			$(this).html( "<span>></span> "+$(this).html() )
			if($.browser.msie) $(this).focus();
		});
		
		//re-align Luft's companies description when it's near the end
		$(".moduletable_luftnav li").each(function(i){
			if(i >= 5){
				$(this).addClass("invert");
			}
		});
		
		
		//add modification to first item of each 'newsflash module' (to let the title be on the side of the thumbnail
		$(".vert_homenews").each(function(){
			$("li", this).each(function(i){
				if(i == 0){ //is the first news of each 'newsflash module'?
					if( $("img",this).size() ){ //news has an img? (added inside its content)
						title = $("h4", this).clone();
						date  = $("em", this).clone();
										
						$("h4", this).remove();
						$("em", this).remove();
						
						$("p img", this).after(title).after(date);
					}
				}else //2nd, 3rd, 4th news item?
					$(this).addClass("hideImg");
			});
		});
		
		//fix strange bug in FF @ newsflash module
		if(constants.MOZILLA) $(".vert_homenews li:first").css("height","100px");
		
		//remove first news item @ trends archive
		$(".moduletable_trends_archive li:first").remove();
		
		//png fix
		$(".moduletable_kid div").ifixpng();
		
		//remove last item detail
		$(".moduletable_luftnav2 li:last").addClass("last");
		
		//add ballon indicator on the submenu
		$("#mainlevel_menu ul").each(function(){
			$(this).before("<div class=\"revert_arrow\">&nbsp;</div>").after("<div class=\"mask\">&nbsp;</div>");
		});
		
		//toggle submenu actions	
		showSubmenu = function(link){
			var who = $(link).siblings();
			$(who).filter(":not(.mask)").fadeIn("fast").end().filter(".mask").show();
			$(who).filter(".mask").height( $(who).filter("ul").height() );
			$(who).filter(".mask").width( $(who).filter("ul").width() );
			
			//IE6 fix for comboboxes
			if( $("#filter").size() > 0 ) $("#filter").addClass("invisible");
		}
		
		hideOthers = function(link){
			$(link).parent().siblings().children().filter(":not(a)").hide();
			
			//IE6 fix for comboboxes
			if( constants.MSIE6 && $("#filter").size() > 0 ) $("#filter").removeClass("invisible");
		}
		
		hideMenu = function(){		
			$(".mainlevel_menu").siblings().filter(":not(.mask)").fadeOut("fast").end().filter(".mask").hide();
			
			//IE6 fix for comboboxes
			if( constants.MSIE6 && $("#filter").size() > 0 ) $("#filter").removeClass("invisible");
		}
		
		$("#mainlevel_menu li a").hover(function(){
			hideOthers(this);
			showSubmenu(this);
			on = true;
		},function(){
			on = false
		});
		
		$(".mainlevel_menu").siblings("ul").hover(function(){
			on = true;
		},function(){
			on = false
		});
		
		//add label overlay effect
		$("#fale_nome").example("seu nome");
		$("#fale_email").example("seu e-mail");
		$("#fale_mensagem").example("sua mensagem");
		$("#webinforma_login").example("seu login");
		$("#webinforma_password").example("sua senha");
		
		//accordion automatic trigger
		href = window.location.href;
		if( href.indexOf("com_cliente") > -1 ||  href.indexOf("com_case") > -1 ){ //in Client's page
			id = href.split("#")[1];
			if(id){
				$("#head-"+id).addClass("active").prepend("<em class=\"toggler\">(x)</em>").next().slideDown();
			}else{
				$(".clients_list .header:first").addClass("active").prepend("<em class=\"toggler\">(x)</em>").next().slideDown();			
			}
		}
		
		//accortion handler
		$(".clients_list .header").click(function(){
			if( $(this).next().is(":hidden") ){
				me = $(this);
				me.siblings().not( me.next() ).filter(".header").find("em").remove().end().end().filter(".content").slideUp()
				me.siblings(".header").removeClass("active")
				me.addClass("active").prepend("<em class=\"toggler\">(x)</em>").next().slideDown();
			}else{			
				me = $(this);
				me.removeClass("active").find("em").remove().end().next().slideUp();
				
				//fix for IE6
				if( constants.MSIE6 && $("#page").size() > 0 )  $("#page").height("550px");
			}
		});	
		
		//basic validation for Contact Forms
		if( $(".form-validate").size() > 0 ) 
			$(".form-validate").validate(); 
		
		//Disable Combobox when empty
		if( $("#cidade") )
			if( $("#cidade option").size() == 0 ) 
				$("#cidade").attr("disabled","disabled");
						
		//hide inactive submenu 
		setInterval(function(){ 
			if (!on) { 
				hideMenu(); 
			} 
		}, 2000);
				
	});
	
})(jQuery);
