var moving = false;
var $j = jQuery.noConflict();
$j('document').ready(function(){
    $j('a').live('click', function(e){
        if($j(this).attr('href') == '#'){
            e.preventDefault();
        } else {
            if($j(this).hasClass('a-ajax')){
                link = $j(this).attr('href').replace("#","");
                link = link.replace("/","");
                target = localPrefix+link+localSufix;
                window.location.hash = "/"+link;
                e.preventDefault();
            }
        }
    });
    
    //Carrega o Home
    if(window.location.hash == undefined || window.location.hash < 1){
        target = localPrefix+local+localSufix;
        $j('.central-content').load(target);
    }
    
    //Realiza o ajax.
    $j.history.init(function(url) {
        abreAjax();
    });
    
    $j('.msg-fechar').live('click',function(e){
        e.preventDefault();
        $j('.msg-bg .msg-box').slideUp(300, function(){
            $j('.msg-bg .msg-box h6').html('');
            $j('.msg-bg .msg-box .msg-content').html('');
            $j('.msg-bg').fadeOut(500);
        });
    });
    
    //Menu Superior do Head, e seus Sub-menus.
    $j('.sub-menu li').mouseenter(function(){
        $j(this).children('a').css('color', '#fff');
    }).mouseleave(function(){
        $j(this).children('a').css('color', '#333');
    });
    
    $j('.item-institucional, .item-projetos, .item-noticias, .item-imprensa, .item-estatistica').mouseenter(function(){
        $j(this).children('.sub-container').slideDown(200);
    }).mouseleave(function(){
        $j('.sub-sub').hide();
        $j(this).children('.sub-container').fadeOut(100);
    });
    
    $j('.item-historico').mouseenter(function(){
        $j(this).children('.sub-sub').slideDown(200);
    }).mouseleave(function(){
        $j(this).children('.sub-sub').fadeOut(100);
    });
    
    $j('.sub-container .sub-menu > li').live('click', function(){
       $j(this).parent().parent().fadeOut(400); 
    });
    
    $j('.input-default').live('focusin', function(){
        if($j(this).attr('alt') == undefined){
            var v = $j(this).val();
            $j(this).attr('alt', v);
        }
        if($j(this).val() == $j(this).attr('alt')){
            $j(this).val('');
        }
    });
    
    $j('.input-default').live('focusout', function(){
        if($j(this).val() == ''){
            $j(this).val($j(this).attr('alt'));
        }
    });
    
    $j('textarea').live('focusin', function(){
        if($j(this).text() == 'mensagem'){
            $j(this).text(''); 
        }
    });
    $j('textarea').live('focusout', function(){
        if($j(this).text() == ''){
            $j(this).text('mensagem');
        }
    });
    
    $j('.fake-select h4, .fake-select .button-select').live('click', function(e){
        $j(this).siblings('ul.select-menu').slideToggle(200); 
    });
    
    $j('.fake-select').live('mouseleave', function(e){
        if(moving == false){
            if($j(this).children('ul.select-menu').is(':visible')){
                moving = true;
                $j(this).children('ul.select-menu').slideUp(200, function(){
                    moving = false;
                });
            }
        } 
    });
    
    $j('.fake-select ul.select-menu li').live('click', function(){
        var v = $j(this).attr('rel');
        var t = $j(this).text();
        $j(this).parent().siblings('h4').text(t);
        $j(this).parent().siblings('input').val(v);
        moving = true;
        $j(this).parent().slideUp(250, function(){
            moving = false;
        })
    });
});


function abreAjax(){
    var hash = window.location.hash;
    if(hash.length > 1){
        hash = hash.replace("#","");
        hash = hash.replace("/","");
        target = localPrefix+hash+localSufix;
        $j('.central-content').load(target, function(response, status, xhr){
            if (status == "error") {
                window.location = home;
            }
        });
    }
}
