$(document).ready(function() {

    $('#selectIdioma').change(function() {

        var url = $('#selectIdioma').val();
        if (url != "") {
            window.location = url;
        }
    });

    $('#selectIdioma2').change(function() {

        var url = $('#selectIdioma2').val();
        if (url != "") {
            window.location = url;
        }
    });


   // $('span, div').removeClass('typeface-js').addClass('titulo');


    /*
    ---
    Gerador de código Header via Ajax
	
	-- */

    function queryString(parameter) {
        var loc = location.search.substring(1, location.search.length);
        var param_value = false;
        var params = loc.split("&");
        for (i = 0; i < params.length; i++) {
            param_name = params[i].substring(0, params[i].indexOf('='));
            if (param_name == parameter) {
                param_value = params[i].substring(params[i].indexOf('=') + 1)
            }
        }
        if (param_value) {
            return param_value;
        }
        else {
            return false;
        }
    }
    //alert(queryString('m1'));
    var languager = $('#language').html();
    var menu = ["inovationTecnology", "productsAplications", "sustainability", "carrers"];
    $.ajax({
        type: "GET",
        url: "main_menu_novo.aspx?idioma=" + languager,
        dataType: "text",
        success: function(xml) {
            var counter = 0;
            //$('#header\-1\-1 .navInst1').html('');
            //$('#header\-2\-1 .navPrinc').html('');
            var str = "";
            var str2 = "";
            var srt3 = "";
            var n = 0;
            var nc = 0;
            var nt = "";
            xml = xml.replace('<?xml version="1.0" encoding="UTF-8"?>', '');
            $('body').append('<div id="menu1" style="display:none">' + xml + '</div>');
            $('#menu1 .NIVEL_1').each(function() {

                var nome = $(this).children('.TXT_LABEL').html();
                var link = $(this).children('.URL_LNK').html();
                if ((counter == 2) || (counter == 3) || (counter == 4) || (counter == 6)) {


                    str2 = str2 + '<li class="menuButton ' + menu[n] + '"><a class="navPrincTitle" href="javascript:void(0)" title="' + nome + '">' + nome + '</a><ul class="princSubMenu" style="display:none;">';
                    arr = $(this).children('.NIVEL_2');
                    $(this).children('.NIVEL_2').each(function(i, val) {
                        var nome = $(this).children('.TXT_LABEL').html();
                        var link = $(this).children('.URL_LNK').html();
                        str2 = str2 + '<li';
                        if (i == arr.length - 1) {
                            str2 = str2 + ' class="last"';
                        }
                        str2 = str2 + '><a href="http://www.embraco.com/Default.aspx?tabid=' + link + '">' + nome + '</a>';
                        if ($(this).html() != "") {

                            var str3 = '';
                            str3 = str3 + '<ul class="secondarySubMenu">';


                            $(this).children('.NIVEL_3').each(function() {
                                var nome = $(this).children('.TXT_LABEL').html();
                                var link = $(this).children('.URL_LNK').html();
                                str3 = str3 + '<li><a href="http://www.embraco.com/Default.aspx?tabid=' + link + '">' + nome + '</a></li>'
                                //alert(nome);

                            });
                            str3 = str3 + '</ul>';
                            str2 = str2 + str3;
                        };
                        str2 = str2 + '</li>';
                    });
                    str2 = str2 + '</ul></li>'; n++;
                }
                else if (counter == 0 || counter == 1 || counter == 7 || counter == 8 || counter == 9) {
                    str = str + ('<li><a href="');
                    if ($(this).children('DIV').hasClass('NIVEL_2')) {
                        str = str + 'javascript:void(0)" rel="nvItem">' + nome + '</a>';
                        str = str + ('<div id="nvConheca" class="subMenuItem"><ul>');
                        $(this).children('.NIVEL_2').each(function() {
                            var nome = $(this).children('.TXT_LABEL').html();
                            var link = $(this).children('.URL_LNK').html();
                            str = str + ('<li>                        			 	<a href="default.aspx?tabid=' + link + '" title="Perfil e história">' + nome + '</a></li>')

                        });
                        str = str + ('</ul></div>');
                    }
                    else {
                        str = str + 'default.aspx?tabid=' + link + '" >' + nome + '</a>';
                    }


                    str = str + ('</li>');
                }



                counter++;
            });

            //$('#header\-1\-1 .navInst1').append(str);
            //$('#header\-2\-1 .navPrinc').append(str2);
            menu_institucional();

            /* Cria os links de linguagem */
            var c = 0;
            while (c <= 3) {
                var wid = $('.' + menu[c]).width();
                $('.' + menu[c] + ' ul li').css('width', wid + 'px');
                c++;
            }
            var loc = (('' + window.location).split('?'));
            if (loc[1]) {
                $('.selectLang select option').each(function() {
                    var n = ($(this).val());
                    if (n != '#') {
                        var n1 = n.split('?');
                        if (n1[1]) {
                            // $(this).val(n1[0] + '?' + loc[1] + '&' + n1[1])
                        }
                    }
                });
            }




            /* atualiza os NavIst2 */
            if (languager == 156) {
                var str = '<li><a href="http://portalr3.embraco.com.br/portalclientes/ingles/index.htm" title="Clientes">Clientes</a></li><li>';
                str = str + '<a href="#" rel="nvItem" title="Fornecedores">Fornecedores</a>';
                str = str + '<div id="nvFornecedores" class="subMenuItem">';
                str = str + '<ul class="navInst3">';
                str = str + '<li>';
                str = str + '<a href="http://portalr3.embraco.com.br/portalfornec/portugues/index.htm" title="Consulta pagamentos">Consulta pagamentos</a>';
                str = str + '</li>';
                str = str + '<li>';
                str = str + '<a href="http://www4.whirlpool.com.br" title="Portal de fornecedores">Portal de fornecedores</a>';
                str = str + '</li>';
                str = str + '<li class="lastItem">';
                str = str + '<a href="http://www.embraco.com/desktopmodules/DownloadsAdmin/Arquivos/manual_conduta_embraco2.pdf" title="código de conduta">código de conduta</a>';
                str = str + '</li>';
                str = str + '<li>';
                str = str + '<a href="/desktopmodules/DownloadsAdmin/Arquivos/ANNEX_04_port.doc" title="substância nocivas - declaração">substância nocivas - declaração</a>';
                str = str + '</li>';
                str = str + '<li>';
                str = str + '<a href="http://www.embraco.com/desktopmodules/DownloadsAdmin/Arquivos/PTGTPP2420_port_rev08.pdf" title="substância nocivas - especificação">substância nocivas - especificação</a>';
                str = str + '</li>';
                str = str + '<li class="lastItem">';
                str = str + '&nbsp;';
                str = str + '</li>';
                str = str + '</ul>';
                str = str + '</div>';
                str = str + '</li>';
                str = str + '<li><a href="http://www.embraco.com/Default.aspx?tabid=118" title="Contatos">Contatos</a></li>';
                str = str + '</ul>';
                $('.navInst2').html(str);
            }
            else if (languager == 157) {
                var str = '<li><a title="Customers" href="http://portalr3.embraco.com.br/portalclientes/ingles/index.htm">Customers</a></li>';
                str = str + '<li>';
                str = str + '<a title="Suppliers" rel="nvItem" href="#">Suppliers</a>';
                str = str + '<div class="subMenuItem" id="nvFornecedores">';
                str = str + '<ul class="navInst3">';
                str = str + '    <li>';
                str = str + '    <a title="Payments" href="http://portalr3.embraco.com.br/portalfornec/ingles/index.htm">Payments</a>';
                str = str + '    </li>';
                str = str + '    <li>';
                str = str + '    <a title="e-sourcing" href="https://www4.whirlpool.com.br/Whirlpool/content/logon_en_US.jsp">e-sourcing</a>';
                str = str + '    </li>';
                str = str + '    <li class="lastItem">';
                str = str + '    <a title="Code of conduct" href="desktopmodules/DownloadsAdmin/Arquivos/BVgKgAAIGU.pdf">Code of conduct</a>';
                str = str + '    </li>';
                str = str + '    <li>';
                str = str + '    <a title="hazardous substances - declaration" href="desktopmodules/DownloadsAdmin/Arquivos/suDav5ELhX.doc">hazardous substances - declaration</a>';
                str = str + '    </li>';
                str = str + '    <li>';
                str = str + '    <a title="hazardous substances - specification" href="desktopmodules/DownloadsAdmin/Arquivos/le44Tu0BlP.pdf">hazardous substances - specification</a>';
                str = str + '    </li>';
                str = str + '    <li class="lastItem">';
                str = str + '    &nbsp;';
                str = str + '    </li>';
                str = str + '</ul>';
                str = str + '</div>';
                str = str + '</li>';
                str = str + '<li><a title="Contact us" href="Default.aspx?tabid=217">Contact us</a></li>';
                str = str + '</ul>';
                $('.navInst2').html(str);
            }
            else if (languager == 158) {

                var str = '<li><a href="http://portalr3.embraco.com.br/portalclientes/ingles/index.htm" title="Clientes">Clientes</a></li>';
                str = str + ' <li>';
                str = str + ' <a href="#" rel="nvItem" title="Proveedores">Proveedores</a>';
                str = str + '<div id="nvFornecedores" class="subMenuItem">';
                str = str + '<ul class="navInst3">';
                str = str + '   <li>';
                str = str + '   <a href="http://portalr3.embraco.com.br/portalfornec/ingles/index.htm" title="Consulta del Pago">Consulta del Pago</a>';
                str = str + '  </li>';
                str = str + '   <li>';
                str = str + '   <a href="https://www4.whirlpool.com.br/Whirlpool/content/logon_en_US.jsp" title="Portal del provedor">Portal del provedor</a>';
                str = str + '   </li>';
                str = str + '   <li class="lastItem">';
                str = str + '   <a href="desktopmodules/DownloadsAdmin/Arquivos/BVgKgAAIGU.pdf" title="código de conducta">código de conducta</a>';
                str = str + '  </li>';
                str = str + '   <li>';
                str = str + '  <a href="desktopmodules/DownloadsAdmin/Arquivos/suDav5ELhX.doc" title="substancia nocivas - declaración">substancia nocivas - declaración</a>';
                str = str + ' </li>';
                str = str + '<li>';
                str = str + '<a href="desktopmodules/DownloadsAdmin/Arquivos/le44Tu0BlP.pdf" title="substância nocivas - especificación">substância nocivas - especificación</a>';
                str = str + ' </li>';
                str = str + ' <li class="lastItem">';
                str = str + ' &nbsp;';
                str = str + ' </li>';
                str = str + '</ul>';
                str = str + ' </div>';
                str = str + '</li>';
                str = str + '<li><a href="Default.aspx?tabid=313" title="Contactos">Contactos</a></li>';
                str = str + '</ul>';
                $('.navInst2').html(str);
            }
            else if (languager == 339) {
                var str = '<ul class="navInst2"><!--li><a href="#" title="客戶">客戶</a></li-->';
                str = str + ' <li><a rel="nvItem" title="供应商" style="font-family: franklin gothic heavy; font-size: 14px; font-weight: 100;" href="#">供应商</a>';
                str = str + ' <div class="subMenuItem" id="nvFornecedores">';
                str = str + ' <ul class="navInst3"><!--li>';
                str = str + '    <a href="http://portalr3.embraco.com.br/portalfornec/ingles/index.htm" title="諮詢付款">諮詢付款</a>';
                str = str + '    </li>';
                str = str + '    <li>';
                str = str + '    <a href="#" title="电子采购">电子采购</a>';
                str = str + '    </li-->';
                str = str + '    <li class="lastItem"><a title="行為準則" style="font-family: franklin gothic heavy; font-size: 14px; font-weight: 100;" href="desktopmodules/DownloadsAdmin/Arquivos/Code-of-Conduct_CN.pdf">行為準則</a> </li>';
                str = str + '   <li><a title="有害物质 - 声明" style="font-family: franklin gothic heavy; font-size: 14px; font-weight: 100;" href="desktopmodules/DownloadsAdmin/Arquivos/suDav5ELhX.doc">有害物质 - 声明 </a></li>';
                str = str + '    <li><a title="有害物質 - 規範" style="font-family: franklin gothic heavy; font-size: 14px; font-weight: 100;" href="desktopmodules/DownloadsAdmin/Arquivos/le44Tu0BlP.pdf">有害物質 - 規範</a> <!--li class="lastItem">';
                str = str + '    &nbsp;';
                str = str + '  </li--></li>';
                str = str + '  </ul>';
                str = str + ' </div>';
                str = str + ' </li>';
                str = str + ' <li><a title="联系我们" style="font-family: franklin gothic heavy; font-size: 14px; font-weight: 100;" href="Default.aspx?tabid=388">联系我们</a> </li>';
                str = str + ' </ul>';
                //$('.navInst2').html(str);
            }


            $('ul.navInst2 li a[rel=nvItem]').click(function() {
                if ($(this).parents('li').find('div.subMenuItem').is(':visible')) {
                    $('ul.navInst1 li, ul.navInst2 li').removeClass('ativo');
                    $(this).parent('li').addClass('ativo');
                    $(this).parent('li').find('div.subMenuItem').slideUp(100);
                    $('#header-1, #header-1-1, #header-1-2').animate({ height: '34px' }, 300);
                } else {
                    $('ul.navInst1 li, ul.navInst2 li').removeClass('ativo');
                    $(this).parent('li').addClass('ativo');
                    $('#header-1, #header-1-1, #header-1-2').animate({ height: '103px' }, 300);
                    $('ul.navInst1 li, ul.navInst2 li').find('div.subMenuItem').slideUp(100);
                    $(this).parent('li').find('div.subMenuItem').slideDown(100);
                }
            });
        }
    });





    /*
    156 = portugues
    157 = ingles
    158 = espanhol
    339 = chines
    */










    /*$("#header-2-1 ul.navPrinc li:eq(0) a").css({ backgroundColor: '#018591' });
    $("#header-2-1 ul.navPrinc li:eq(1) a").css({ backgroundColor: '#69b29e' });
    $("#header-2-1 ul.navPrinc li:eq(2) a").css({ backgroundColor: '#c5dd89' });
    $("#header-2-1 ul.navPrinc li:eq(3) a").css({ backgroundColor: '#ffa415' });
    */



    $("div.cpoBusca input, div.cpoBusca label").focus(function() {
        $(this).parent('div').children('label').hide();
    }).blur(function() {
        if ($(this).val() == '') {
            $(this).parent('div').children('label').show();
        }
    });
    $("#depoimentos").jcarousel({
        scroll: 1,
        initCallback: depoimentosCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });


    var vl = $('.selectLang select option:eq(0)').html();
    $('.selectLang .fakeSelectLang').html(vl);

    //fim ready
});

function depoimentosCallback(carousel) {
    $('#dpNext').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#dpPrev').bind('click', function() {
        carousel.prev();
        return false;
    });
};


function menu_institucional(){
	// MENU INSTITUCIONAL - SUBMENU
    $('ul.navInst1 li a[rel=nvItem]').click(function() {
        if ($(this).parents('li').find('div.subMenuItem').is(':visible')) {
            $('ul.navInst1 li, ul.navInst2 li').removeClass('ativo');
            $(this).parent('li').addClass('ativo');
            $(this).parent('li').find('div.subMenuItem').slideUp(100);
            $('#header-1, #header-1-1, #header-1-2').animate({ height: '34px' }, 300);
        } else {
            $('ul.navInst1 li, ul.navInst2 li').removeClass('ativo');
            $(this).parent('li').addClass('ativo');
            $('#header-1, #header-1-1, #header-1-2').animate({ height: '103px' }, 300);
            $('ul.navInst1 li, ul.navInst2 li').find('div.subMenuItem').slideUp(100);
            $(this).parent('li').find('div.subMenuItem').slideDown(100);
        }
    });
        
        
        
   $('.menuButton').each(function(){
	var c = 0;
	while (c<=($(this).children('ul').children('li').length))
	{
	$(this).children('ul').children('li:eq('+c+')').addClass('id'+c);
	c++;
	}
	});
	var repor = false;
	$('.menuButton .princSubMenu li a').each(function() {
	 var valor1 = $(this).parent().parent().parent().attr('class');
	 var valor2 = valor1.split(' ');
	 var valor3 = $(this).attr('href');
	 		
	 		var valor5 = '';
	 		if ((valor3.indexOf('?'))!=-1)
	 		{
	 		 valor5 = '&'+(valor3.slice((valor3.indexOf('?')+1)));
	 		 valor3 = valor3.slice(0,(valor3.indexOf('?')));
	 		}
	 if($(this).parent().hasClass('last')){
	 $(this).parent().removeClass('last');
	 repor = true;
	 }
	 var valor4 = $(this).parent().attr('class');
	 if (repor){
	 $(this).parent().addClass('last');
	 repor=false;
	 }
	 $(this).attr('href',valor3+'?m1='+valor2[1]+'&sel='+valor4+valor5);
	 });
	 
	var urlVerify = '' + window.location;
	if ((urlVerify.indexOf('?')) != -1)
	{
	var menuItem = (''+(window.location)).slice((urlVerify.indexOf('?'))+1).split('&');	
	var contentUl = ($('.'+(((menuItem[0]).split('='))[1])+' .princSubMenu').html())
	$('#content\-2 #conteudo').before('<ul id="secondMenu"></ul>');
	$('#secondMenu').html(contentUl);
	$('#secondMenu li a br').replaceWith(" ");
	$('#secondMenu li').each(function(){
	if(($(this).hasClass((((menuItem[1]).split('='))[1])))){
	$(this).addClass('selected');
	if ($(this).has('ul').length)
	  {
	  var subMenu2 = $(this).children('ul').html();
	  $('#content\-2 #conteudo').prepend('<ul id="thirdMenu"></ul>');
	  $('#thirdMenu').html(subMenu2);
	  }
	}
	});
	}



	/*efeito de menu */
	//$('.princSubMenu').css('')




	var heig = 0;
//	alert(navigator.appName);
	//	alert(navigator.appVersion);

	var click = false;
	var c = 0;
	var open = false;

	$('.menuButton').click(function() {
	    c = 0;
	    //open = false;

	    if (click) { click = false; } else { click = true; }

	    $('.menuButton').each(function() {

	        if ($(this).children('.princSubMenu').css('display') == 'block') {
	            c++;
	        }
	    });


	    if (navigator.appName == 'Microsoft Internet Explorer' && click == true && c == 0) {
	        if (($('.webdoorContent .webdoor').css('z-index')) == '1') { $('.webdoorContent .webdoor').css('z-index', '-1') } else { $('.webdoorContent .webdoor').css('z-index', '1') };

	    }


	    c = 0;
	    $(this).children('.princSubMenu').slideToggle('slow', function() {

	        $('.menuButton').each(function() {

	            if ($(this).children('.princSubMenu').css('display') == 'block') {
	                c++;
	            }
	        });

	        if (navigator.appName == 'Microsoft Internet Explorer') {

	            heig = ($(this).css('height'));
	            $(this).css('height', heig);
	            //$('roductsAplications ul.princSubMenu').css('border-bottom','0px');

	            if (click == false && c == 0) {
	                if (($('.webdoorContent .webdoor').css('z-index')) == '1') { $('.webdoorContent .webdoor').css('z-index', '-1') } else { $('.webdoorContent .webdoor').css('z-index', '1') };

	            }

	        }

	    });

	});
	
	$("#header-2-1 ul.navPrinc li .navPrincTitle").mouseover(function() {
	$(this).animate({ paddingTop: '10px' }, 200);
	}).mouseout(function() {
	$(this).animate({ paddingTop: '5px' }, 200);
	});

	$("#header-2-1 ul.princSubMenu li a").mouseover(function() {
	$(this).animate({ fontSize: "1.1em" }, 200);
	}).mouseout(function() {
	$(this).animate({ fontSize: "1em" }, 200);
	});
    
    
   }





