$(document).ready(function() {
    if ($.browser.msie && ($.browser.version <= 7)) { $("body").addClass("ie"); } //Adiciona uma classe para o IE6.
    $("#box928x347").append('<img src="/img/bg-box928x347.png" width="928" height="347" class="png" alt="" />');
    $("#box642x336").append('<img src="/img/bg-box642x336.png" width="642" height="336" class="png" alt="" />');
    $("#box255x336").append('<img src="/img/bg-box255x336.png" width="255" height="336" class="png" alt="" />');
    $("#box454x337").append('<img src="/img/bg-box454x337.png" width="454" height="337" class="png" alt="" />');
    $("#box283x337").append('<img src="/img/bg-box283x337.png" width="283" height="337" class="png" alt="" />');
    $("#boxOprograma").append('<img src="/img/bg-boxOprograma.png" width="550" height="94" class="png" alt="" />');
    $("#boxCidParticip").append('<img src="img/bg-boxCidParticip.png" width="318" height="84" alt="" class="png" />');
    $(".box318x98").append('<img src="img/bg-box318x98.png" width="318" height="98" alt="" class="png" />');
    $(".box304x75").append('<img src="img/bg-box304x75.png" width="304" height="75" alt="" class="png" />');
    $(".oPrograma").append('<img src="img/img-119x103.png" width="119" height="103" alt="" class="png foto119x103" />');
    $("#boxAtivPromo").append('<img src="img/bg-boxAtivPromo.png" width="248" height="84" alt="" class="png" /><img src="img/foto-piscina.png" width="207" height="180" alt="" class="png fotoPiscina" />');
    $("#box255x337").append('<img src="img/bg-box255x337.png" width="255" height="337" alt="" class="png" />');
    $("#box644x337").append('<img src="img/bg-box644x337.png" width="644" height="337" alt="" class="png" />');
    $("#boxClubinho").append('<img src="img/bg-boxClubinho.png" width="800" height="373" alt="" class="png" />');
    $(".boxDiarioObras").append('<img src="img/bg-boxDiarioObras.png" width="229" height="243" alt="" class="png" />')
    //$("ul.listCidade li div").hide();

    $("ul.listCidade > li > a").click(function() {
        $("ul.listCidade li .accor").slideUp();
        $("ul.listCidade li a").removeClass("hover");
        if ($(this).next().is(":visible")) {
            $(this).removeClass("hover").next().slideUp();
        } else {
            $(this).addClass("hover").next().slideDown();
        }
        return false;
    });


    $("ul.listCidadeSub > li > a").click(function() {
        $("ul.listCidadeSub > li > a").removeClass("hover");
        $("ul.listCidadeSub li div").hide();
        $cidade = $(this).attr('href').split("#")[1];
        if ($(this).next().is(":visible")) {
            $(this).removeClass("hover").next().slideUp();
        } else {
            $(this).addClass("hover").next().slideDown();
        }
        $.ajax({
            data: $(".conteudoMateria").serialize(),
            dataType: "html",
            url: '/Site/listaMaterias.ashx?idCidade=' + $cidade,
            success: function(html) {
                paginarNoticias(1, '', '', $cidade);
            }
        });
        return false;
    });
    $("ul.listCidadeSub li a.hover").trigger("click");

    data = new Date();
    $mesAtual = data.getMonth() + 1;
    $("#mes" + $mesAtual).addClass("hover");


    $("ul.listMes > li > a").click(function() {
        $ano = $(this).attr('href').split("#(")[1].split(")-")[0];
        $mes = $(this).attr('href').split("#(")[1].split(")-")[1];
        paginarNoticias(1, $ano, $mes, '');
        return false;
    });
    $("ul.listMes.home li a.hover").trigger("click");

    $(".boxDiarioObras ul").find("li:last").addClass("noBorder");

    $("#listaMaterias").each(function() {
        paginarNoticias(1, '', '', '');
        return false;
    });

    /*
    $("#formNavPrefeitura select option").click(function() {
    if ($(this).val()) {
    window.open($(this).val(), "_blank");
    }
    });
    */
    $("#formNavPrefeitura select").change(function() {
        if ($(this).val() != '') {
            window.open($(this).val(), "_blank");
        }
    });


    $('.listAlbum').jcarousel({ scroll: 2, animation: 1000, easing: 'easeOutCubic' });

    // Galeria

    carregaImagem($('ul.listAlbum li a:first').attr('href'));

    $('ul.listAlbum li a:first').addClass('hover').find('img').stop().fadeTo(0, 1.0);

    $('ul.listAlbum li a').hover(function() {
        $(this).addClass('over').find('img').stop().fadeTo(1000, 1.0);
        $('ul#navImage li a').each(function() {
            if (!($(this).hasClass('hover')) && !($(this).hasClass('over'))) {
                $(this).find('img').stop().fadeTo(1000, 0.6);
            }
        });
    }, function() {
        $('ul.listAlbum li a').removeClass('over');
        $('ul.listAlbum li a').each(function() {
            if (!($(this).hasClass('hover'))) {
                $(this).find('img').stop().fadeTo(1000, 0.6);
            }
        });
    });

    $('ul.listAlbum a').click(function() {
        $(this).blur();
        $("#imgAntesDepois").addClass("hide");
        $('ul.listAlbum li a').removeClass('hover');
        $(this).addClass('hover').find('img').stop().fadeTo(1000, 1.0);
        $('ul.listAlbum li a').each(function() {
            if (!($(this).hasClass('hover'))) {
                $(this).find('img').stop().fadeTo(1000, 0.6);
            }
        });
        carregaImagem($(this).attr('href'));
        return false;
    });
    // Fim da galeria


    ScrollInicio = setTimeout('$(".scroll-pane").jScrollPane({ scrollbarWidth: 9, scrollbarMargin: 10, showArrows: true });', 200);
    $scrollSul = setTimeout('$("#scrollSul").jScrollPane({ scrollbarWidth: 9, scrollbarMargin: 10, showArrows: true })', 2000);
    $scrollNorte = setTimeout('$("#scrollNorte").jScrollPane({ scrollbarWidth: 9, scrollbarMargin: 10, showArrows: true })', 100);

    $("#boxVideoPrograma").flash({ src: '/swf/flvplayer.swf', width: 320, height: 260, wmode: 'transparent', allowfullscreen: 'true', allowscriptaccess: 'always', flashvars: { file: ['flv/video1.flv'].join(' '), backcolor: ['0x007AB6'].join(' '), frontcolor: ['0Xffffff'].join(' '), image: ['/img/video1.jpg'].join(' ')} }, { version: 8 });


    $(".videoPlayer").each(function() {
        $flv = $(this).attr("rel");
        //alert($flv);
        $(this).flash({ src: '/swf/flvplayer.swf', width: 320, height: 260, wmode: 'transparent', allowfullscreen: 'true', allowscriptaccess: 'always', flashvars: { file: [$flv].join(' '), backcolor: ['0x007AB6'].join(' '), frontcolor: ['0Xffffff'].join(' ')} }, { version: 8 });
    });

    //if($("body").hasClass("mapaBrasil")){ initMap() }  seta o mapa inicial da pagina de Cidades Participantes

    $("ul.listPraias a").click(function() { // Muda o mapa e apresenta informações sobre a praia
        $(".praia").hide();
        $("ul.listPraias a").removeClass("hover");
        $(this).addClass("hover");
        $praia = $(this).attr("href");
        $($praia).fadeIn("fast");

        $(".imgpraia").hide();
        $imgpraia = '#img-' + $praia.substring(1) + '';
        $($imgpraia).removeClass("fix hide");
        $($imgpraia).fadeIn("fast");

        /*, function(){
        $lat = $($praia).find(".geo .latitude").html();
        $lon = $($praia).find(".geo .longitude").html();
        loadMap($lat,$lon,14);
        });*/
        $scroll = setTimeout('$(".scroll-pane").jScrollPane({ scrollbarWidth: 9, scrollbarMargin: 10, showArrows: true })', 500);
    });

    $("#formContato").validate({ errorLabelContainer: $(".msgError"), errorContainer: $("#msgError"),
        submitHandler: function() {
            $.ajax({
                data: $("#formContato").serialize(),
                dataType: "json",
                url: '/Contato/EnviarEmail.ashx',
                success: function(json) {
                    if (json.msg == "OK") {
                        $("#msgSuccess").fadeIn();
                        $("#formContato input, #formContato select, #formContato textarea").val("");
                        //pageTracker._trackPageview('/contato/envio/sucesso/');
                    } else {
                        $("#msgSuccess").fadeIn();
                        //pageTracker._trackPageview('/contato/envio/erro/');
                    }
                }
            });
        }
    });

    //$("ul#navLitoral").idTabs();
    $("ul#navLitoral a").click(function() {
        $("ul#navLitoral a").removeClass("hover");
        $(this).addClass("hover");
        $(".tabNavLitoral").addClass("hide");
        $aba = $(this).attr("href");
        $($aba).hide().removeClass("hide").fadeIn();
        return false;
    });


    $("ul.tabLitoral li a").click(function() {
        $tab = $(this).attr("href").split("#")[1];
        $("ul.tabLitoral li a").removeClass("hover");
        $(this).addClass("hover");
        $(".tbLitoral").hide();
        $("#" + $tab).fadeIn();
        $scroll = setTimeout('$(".scroll-pane").jScrollPane({ scrollbarWidth: 9, scrollbarMargin: 10, showArrows: true })', 500);
        return false;
    });
    $("ul.tabLitoral li a.btnLitoralSul").trigger("click");

    $("input.ddd").mask("99");
    $("input.tel").mask("9999-9999");
    $("input.cep").mask("99999999");
    $("input.cpf").mask("999.999.999-99");
    $("input.cnpj").mask("99.999.999/9999-99");
    $(".fix, h2").ifixpng();

    $(".estado").estadoCidade();

});

function loadMap(lat,lon,zoom) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("mapa"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_SATELLITE_MAP);
		map.setCenter(new GLatLng(lat, lon), zoom);
		var marker = new GMarker(map.getCenter());
		marker.bindInfoWindow('');
		map.addOverlay(marker);
	}
}
function initMap() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("mapa"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_SATELLITE_MAP);
		map.setCenter(new GLatLng(-23.5, -46.5), 4);
		var marker = new GMarker(map.getCenter());
	}
}

function carregaImagem($imagemUrl){
	$('#boxAlvo').html('<img id="imgLoading" src="/img/loading.gif" width="54" height="55" alt="Carregando" />').animate({opacity: 1.0}, 1000, function(){
		$img = new Image();
		$($img).load(function(){
			$('#boxAlvo').html(this);
	   		$(this).css('display', 'none').fadeIn('slow');
			$wait = setTimeout('$("#imgAntesDepois").removeClass("hide");', 400);
	    }).error(function(){
	      	$('#boxAlvo').append('<p class="error">Ocorreu um erro durante o carregamento da imagem.</p>');
	    }).attr('src', $imagemUrl).width('100%').height('100%');
	});
}

function paginarNoticias(page, ano, mes, cidade){

	if(ano == null){
		ano = $("div.navigation input#hdnAno").val();	
	}

	if(mes == null){
		mes = $("div.navigation input#hdnMes").val();	
	}

	if(cidade == null){
		cidade = $("div.navigation input#hdnIdCidade").val();	
	}
	$.ajax({
		//data: $(".conteudoMateria").serialize(),
		dataType: "html",
		url: '/Site/listaMaterias.ashx?mes=' + mes + '&ano=' + ano + '&idCidade=' + cidade + '&page=' + page,
		success: function(html) {
			$(".conteudoMateria").html(html);
		}
	});
}


