$(document).ready(function() {

    // Acoordeon
    $('.sanfona').each(function() {
        $(".sanfona").accordion({
            autoHeight: false,
            navigation: true,
            collapsible: true
        });        
    });    

    // Change styles in the page

    $('#topper .menu li').last().css('border-right', 'none');
    $('#footerContent .menu li').last().css('border-right', 'none');
    $('#breadcrumb li:first').addClass('firstbread');
    $('#breadcrumb li').last().addClass('lastbread');
    $('#allPages li:first').addClass('firstLink');
    $('#allPages li').last().addClass('lastLink');
    $('#location li').parent().find("ul").css('display', 'none');

    // Set the description of images

    $('img.descrMark').each(function() {
        var titleImage = $(this).attr('title');
        $(this).wrap('<div class="descrImg"></div>').after('<p>' + titleImage + '</p>').attr('title', '');
    });

    // Start HTML5 placeholder

    $('[placeholder]').focus(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
        }
    }).blur(function() {
        var input = $(this);
        if (input.val() == '' || input.val() == input.attr('placeholder')) {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
        }
    }).blur();

    $('[placeholder]').parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
            }
        })
    });

    // Start Font Size Resize

    var originalFontSize = $('#content').css('font-size');
    $('#content').css('font-size', originalFontSize);

    // Increase Font Size

    $(".increaseFont").click(function() {
        var currentFontSize = $('#content').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 1.2;
        $('#content').css('font-size', newFontSize);
        return false;
    });

    // Decrease Font Size

    $(".decreaseFont").click(function() {
        var currentFontSize = $('#content').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 0.8;
        $('#content').css('font-size', newFontSize);
        return false;
    });

    // Tooltip home

    $('.sBox').hover(
		function() {
		    this.tip = $(this).find("span").attr("title");
		    var ttitle = $(this).find("span").text();
		    $(this).append(
			'<div class="tooltip"><div class="tail"></div><div class="tip"><h4>'
					+ ttitle
					+ '</h4>'
					+ this.tip
			+ '</div></div>'
		);
		    $(this).find("span").attr("title", "");
		    $('.tooltip').fadeIn(300);
		},
	    function() {
	        $('.tooltip').fadeOut(100);
	        $(this).children(".tooltip").remove();
	        $(this).find("span").attr("title", this.tip);
	    }
	);

    // Tooltip produtos de valor agregado

    $('#valor li span').hover(
		function() {
		    this.tip = this.title;
		    var ttitle = $(this).text();
		    $(this).append(
			'<div class="tooltip"><div class="tail"></div><div class="tip">'
					+ this.tip
			+ '</div></div>'
		);
		    this.title = "";
		    $('.tooltip').fadeIn(300);
		},
	function() {
	    $('.tooltip').fadeOut(100);
	    $(this).children().remove();
	    this.title = this.tip;
	}
	);

    // Tooltip produtos de canais

    $('#canais span').hover(
		function() {
		    this.tip = this.title;
		    var ttitle = $(this).text();
		    $(this).append(
			'<div class="tooltip"><div class="tail"></div><div class="tip">'
					+ this.tip
			+ '</div></div>'
		);
		    this.title = "";
		    $('.tooltip').fadeIn(300);
		},
	function() {
	    $('.tooltip').fadeOut(100);
	    $(this).children().remove();
	    this.title = this.tip;
	}
	);

    // location hover
    $('#location li').click(
		function() {
		    $(this).parent().find('ul').toggle('fast');
		    $(this).toggleClass('active');
		}
	);

    $('#downloadArea a').prepend('- ');

    // Download area - Doc Link
    $('#downloadArea a[href$=".doc"]').addClass('doc');

    // Download area - Pdf Link
    $('#downloadArea a[href$=".pdf"]').addClass('pdf');

    // Tranform UL to Select

    $('.selectList li.selectedItem ul').hide();

    $('.selectList li.selectedItem a').click(function() {
        $(this).next().slideToggle();
    });

    // Vertical Menu
    $('aside .searchFilter ul').hide();

    $('aside .searchFilter a').click(function() {
        $(this).toggleClass("ativo");
        $(this).next().slideToggle();
    });

    // Calendar pick

    // $("#dateCalendar").datepicker($.datepicker.regional['pt']);
    //$("#calendarInput").datepicker();      

    // msgError

    $('#msgError').hide();
    $('#msgError').show('slow');
    $('#msgError .closeMsg').click(function() {
        $(this).parent("#msgError").hide('slow');
    });

    // Z-index Hack

    var zIndexNumber = 1000;
    $('.selectList').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });

    // newsletter
    $("#newsForm .btnEmailNewsApply").bind("click", function() {
        var nome = $("#newsForm .txtNomeNews").val();
        var email = $("#newsForm .txtEmailNews").val();

        $.ajax({
            type: "POST",
            url: appUrl_ + "Handlers/EmailHandler.aspx/salvar",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{nome: '" + escape(nome) + "', email: '" + escape(email) + "'}",
            success: function(json) {
                if (json.d.MensagemErro != null && json.d.MensagemErro.length > 0) {
                    $("#newsForm .retorno").show();
                    exibirMensagem(json.d.MensagemErro, "#newsForm .retorno", "");
                }
                else {
                    $("#newsForm .txtNomeNews").val("");
                    $("#newsForm .txtEmailNews").val("");
                    $("#newsForm .div3 .campos").remove();
                    $("#newsForm .div3").html(json.d.HTML);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // alert(XMLHttpRequest.responseText);
            }
        });

        return false;
    });

});


// Exibe uma mensagem por um periodo de tempo
function exibirMensagem(msg, div, css) {
    //var div_ = document.createElement("div");
    $(div).html(msg);

    if (css != null && css.length > 0)
        $(div).attr("class", $(div).attr("class") + css);

    $(div).show();

    setTimeout(function() {
    $(div).fadeOut("slow", function() {
        $(div).hide();
        if (css != null && css.length > 0)
            $(div).removeClass(css);
        });
    }, 4000);
}

//desabilita o botao de submit ao clicar
function disableSubmitButton(obj, grupo) {
    if (grupo == null) grupo = '';

    if (typeof (Page_ClientValidate) == 'function') {
        if (Page_ClientValidate(grupo) == false) {
            return false;
        }
        obj.disabled = true;
    }
}
