$.getScript('js/jquery.akModal.js');
var temp_func;
function printIt(getSlug){
$('.txt *').removeClass().attr('style','');
    $.post("print.php",{
        title: $('h1').html(),
        content: $('.txt').html(),
        siteName: $('title').text(),
        siteURL: $('base').attr('href').substring(0,$('base').attr('href').length-1),
        slug: getSlug
    },function(html){
        $('body *').css('display','none');
        $('body').css('background','transparent none');
        $('body').append('<div id="print"><!-- --></div>');
        $('#print').css('display','block');
        $('#print').html(html);
    });
}
function showInfo(regiao){
    var regioes = {
        "norte": 'mapa_norte',
        "sul": 'mapa_sul',
        "centro-oeste": 'mapa_centrooeste',
        "nordeste": 'mapa_nordeste',
        "sudeste": 'mapa_sudeste',
        "desc": 'mapa_desc'
    };

    for(x in regioes){
        element = document.getElementById(regioes[x]);
        if(element){
            if(regiao==x) $(element).show('slow');
            else element.style.display = 'none';
        }
        element = undefined;
    }
    return false;
}

function removerAcento(palavra) {
    com_acento = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ";
    sem_acento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC";
    nova="";
    for(i=0;i<palavra.length;i++) {
        if (com_acento.search(palavra.substr(i,1))>=0) {
            nova+=sem_acento.substr(com_acento.search(palavra.substr(i,1)),1);
        }
        else {
            nova+=palavra.substr(i,1);
        }
    }
    return nova;
}

$(document).ready(function(){
    //ie6 png fix
    if(typeof(DD_belatedPNG)!='undefined')
        DD_belatedPNG.fixPng($('#cabecalho').get(0));

    //common for all projects
    $('a[rel=external]').attr('target','_blank');
    if(!$.support.htmlSerialize)
        $('a[rel=sidebar]').click(function(){bookmarksite(this);});
    $('a[rel=suggest]').click(function(){$.showAkModal(this.href,this.title,650,450);return false;});
    $('a[rel=print]').click(function(){printIt(location.pathname);return false;});
    $('a[rel=back]').click(function(){history.go(-1);});
    $('a[rel=map]').click(function(){$.showAkModal(this.href,'Como Chegar',900,510);return false;});
    //js ballooon
    $('body').append('<div class="jsballoon"><div class="jsballoon_text"><!-- --><\/div><div class="jsballoon_arrowdown"><!----><\/div><\/div>');
    if($('#box_especificacoes_tecnicas').size()){
        var $h3_childs = $('#box_especificacoes_tecnicas h3');
        $h3_childs.each(function(){
            $this = $(this);
            var className;
            className = removerAcento($this.text().toLowerCase().replace(' ','_'));
            $this.addClass('icon')
            .css('background-image','url(images/tit_'+className+'.gif)');
        });
    }
    //jquery carousel
    if($('.boxGaleria_album').size()){
            if($().jCarouselLite){
                $('.boxGaleria_album').jCarouselLite({
                    btnPrev: '.boxGaleria_prev',
                    btnNext: '.boxGaleria_next',
                    visible: 4,
                    circular: false
                });
                if($('.boxGaleria_album li').size()<=4){
                    $('.boxGaleria_next').addClass('disabled');
                }
            }
            else{
                $.getScript('js/jquery.jcarousellite.js', function(){
                $('.boxGaleria_album').jCarouselLite({
                    btnPrev: '.boxGaleria_prev',
                    btnNext: '.boxGaleria_next',
                    visible: 4,
                    circular: false
                });
                if($('.boxGaleria_album li').size()<=4){
                    $('.boxGaleria_next').addClass('disabled');
                }
                });
            }
    }
    //jquery carousel
    if($('.boxMaisVideos_album').size()){
            if($().jCarouselLite){
                $('.boxMaisVideos_album div').jCarouselLite({
                    btnPrev: '.boxMaisVideos_prev',
                    btnNext: '.boxMaisVideos_next',
                    visible: 4,
                    circular: false
                });
                if($('.boxMaisVideos_album li').size()<=4){
                    $('.boxMaisVideos_next').addClass('disabled');
                }
            }
            else{
                $.getScript('js/jquery.jcarousellite.js', function(){
                   $('.boxMaisVideos_album div').jCarouselLite({
                        btnPrev: '.boxMaisVideos_prev',
                        btnNext: '.boxMaisVideos_next',
                        visible: 4,
                        circular: false
                    });
                if($('.boxMaisVideos_album li').size()<=4){
                    $('.boxMaisVideos_next').addClass('disabled');
                }
                });
            }
    }
    //jquery fontsizer
    if($('.recursos_aumentar_texto').size()){
        temp_func = function(){
            $('.recursos_aumentar_texto')
                .click(function(){
                $.FontSizer.IncreaseSize();
                return false;
            });
            $('.recursos_diminuir_texto').click(function(){
                $.FontSizer.DecreaseSize();
                return false;
            });
        }
        if($.FontSizer){
            temp_func();
        }
        else{
            $.getScript('js/jquery.fontsizer.js', temp_func());
        }
    }
    if($('.ItemPicText2,.ItemDateText').size()){
        var $itemPicText2 = $('.ItemPicText2,.ItemDateText');
        $itemPicText2.filter(':last').css('border-bottom','8px solid #F6F6F6');
        $itemPicText2.filter(':odd').css('background-color','transparent');
        delete $itemPicText2;
    }
    //show text width jsballoon
    if($('#rodape .bot_links').size())
        $('#rodape .bot_links').mousemove(function(e){
            var $jsballoon = $('.jsballoon');
            $jsballoon.css('display','block');
            if($jsballoon.attr('currenttimeid'))
                window.clearInterval($jsballoon.attr('currenttimeid'));
            $jsballoon = $('.jsballoon');
            var text = $('.bot_links').next();
            $jsballoon.children('.jsballoon_text').html('<ul>'+text.html()+'</ul>');
            $jsballoon.css({top: (e.pageY-$jsballoon.height()-20), left: (e.pageX-20)});
        }).mouseout(function(){
            $jsballoon = $('.jsballoon');
            var currenttimeid = window.setTimeout(function(){$jsballoon.css('display','none');}, 5000);
            $jsballoon.attr('currenttimeid', currenttimeid);
        });
        $('#corpo').hover(function(){
            var $jsballoon = $('.jsballoon');
            $jsballoon.css('display','none');
        });
        $('.jquery_dd').hover(
            function(){
                $(this).addClass('hover');
            },
            function(){
                $(this).removeClass('hover');
            }
        );
        if($('.jquery_dd li.selected a').size())
            $('.jquery_dd_label').text($('.jquery_dd li.selected a').text()).attr('href','javascript:;');
        
        $('#cabecalho .menu_navegacao li').hover(
            function(){
                var $ul = $(this).css('z-index',1).children('ul');
                if($ul.size()){
                    $ul.css('display','block');
                }
            },
            function(){
                var $ul = $(this).css('z-index',0).children('ul');
                if($ul.size()){
                    $ul.css('display','none');
                }
            }
        );
        if($('.formLL').size()){
            $.getScript('js/forms.js');
        }
        $('#bot_agendar').click(function(){
            try{
            $('#formAgendarTeste').show('slow');
            $(this).parent().next().remove();
            $(this).remove();
            return false;
            }catch(e){
                alert(e);
                return false;
            }
        });
        if($('.box_concessionarias_text .vcard').size())
            $('.box_concessionarias_text .vcard').each(function(){
                var $this = $(this);
                var titulo = $this.find('.fn').text();
                var cidade = $this.find('.locality').text();
                var estado = $this.find('.region').text();
                var latitude = $this.find('.latitude').text();
                var longitude= $this.find('.longitude').text();
                $this.prepend('<strong>'+cidade+' - '+estado+'</strong><br />');
                $this.append('<a href="javascript:;" onclick="return showMap($(this).parent().get())" class="map">ver mapa</a>');
            });
});

function showMap(hcard){
    var $hcard = $(hcard);
    $hcard.siblings().removeClass('selected');
    var $localizacao = $('#box_localizacao');
    if($hcard.find('.geo').size()=='0'){
         //getGeoPosition(hcard);
        $localizacao.hide('slow');
        return false;
    }
    $localizacao.show('slow');
    $localizacao.find('h1').html($hcard.find('.fn').text()+'<br />'+$hcard.find('.locality').text()+' - '+$hcard.find('.region').text());
    $hcard.addClass('selected');
        $localizacao.find('.bot_ver_mapa')
        .attr('href','mapa.php?latitude='+$hcard.find('.latitude').text()+'&amp;longitude='+$hcard.find('.longitude').text()+'&amp;address='+$hcard.find('.adr').text()+'&amp;title='+$hcard.find('.fn').text())
        .attr('title',$hcard.find('.fn').text());
        initMap($hcard.find('.latitude').text(),$hcard.find('.longitude').text());
    return false;
}

function getGeoPosition(hcard) {
    var $hcard = $(hcard);
    var $localizacao = $('#box_localizacao');
    
    var latlng = new google.maps.LatLng(-51.925280, -14.235004);
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_cont"), myOptions);

    geocoder = new google.maps.Geocoder();
    var address = $hcard.find('.street-address').text()+ ', '+ $hcard.find('.locality').text()+ ', '+ $hcard.find('.region').text()+ ', BRA';
    
    alert(typeof(geocoder));
    if (geocoder) {
        alert('ok');
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map, 
                    position: results[0].geometry.location
                });
                var markerLatLng = marker.getPosition();

                alert(markerLatLng.lat());
                
                $localizacao.find('.bot_ver_mapa')
                .attr('href','mapa.php?latitude='+markerLatLng.lat()+'&amp;longitude='+markerLatLng.lng()+'&amp;address='+$hcard.find('.adr').text()+'&amp;title='+$hcard.find('.fn').text())
                .attr('title',$hcard.find('.fn').text());
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });

    }
}

function  initMap(longitude,latitude){
    var latlng = new google.maps.LatLng(longitude, latitude);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapa_cont"), myOptions);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
}

$(window).load(function(){
    if((location.href).search("version=print")!=-1)
        $('a[rel=print]').click();
});