$(document).ready(function(){

	// Ancora
	var anchor = window.location.hash;
	
	if(anchor == '#download'){

             setTimeout(function(){  
		$('#s4-workspace').animate({
			scrollTop: $(".env-download").offset().top
		}, 1000);
             }, 3000); 
	}


})
function header_init(){

		/* Verifica se é IPAD/IPOD/IPHONE */
//		if (/iPad|iPod|iPhone/.test(navigator.userAgent)){
//		    document.write('<link rel="stylesheet" type="text/css" href="../css/ipad-iphone.css">');
//		};
				
	/*	var htmlLanguage='';
		var liEnUS='<li class="en-US"><a href="/en-US" title="English" ><img src="../img/flag-eua.png" alt="English" /></a></li>';
		var liPtBR='<li class="pt-BR"><a href="/pt-BR" title="Português"><img src="../img/flag-br.png" alt="Português" /></a></li>';
		var liEsES='<li class="es-ES"><a href="/es-ES"  title="Español" ><img src="../img/flag-sp.png" alt="Español" /></a></li>'
		var liZhHK='<li class="zh-hk"><a href="/zh-hk"  title="Chinese" ><img src="../img/flag-ch.png" alt="Chinese" /></a></li>'


		htmlLanguage = '<ul>';
		
		if(nvaFx.Variations.CurrentLanguage=='pt-BR')
		{
			htmlLanguage += liPtBR;
			htmlLanguage += liEnUS;
			htmlLanguage += liEsES;
			htmlLanguage += liZhHK;
		}	
		

		if(nvaFx.Variations.CurrentLanguage=='en-US')
		{
			htmlLanguage += liEnUS;
			htmlLanguage += liPtBR;
			htmlLanguage += liEsES;
			htmlLanguage += liZhHK;
		}	
		
		
		htmlLanguage += '</ul>';		
		
		$('.language').html(htmlLanguage);*/


		aToVideo();
        menuActive();

		/* Fim Tras. Todos a em Player*/
		/* Tras. Todos a externo em _blank*/

		//$("a[href^='http:']:not([href*='http://" + window.location.host + "'][target='_blank'])").live('click', function(){
            //$(this).attr('target','_blank');
        //});
		/* Fim Tras. Todos a externo em _blank*/

        $('.busca').click(function(){
            resetSearch(  $(this)  );
        });

        $('.busca').blur(function(){
            if(   $(this).val() ==  ""){
                $(this).val("Busca");
            }
        });

        $('.send').click(function(){
            if(  $('.busca').val() ==  ""  ){
                return false
            }
        });

		createLanguage();

        function createLanguage(){
			//$('.' + nvaFx.Variations.CurrentLanguage).remove();

			$('li.en-US').find('a').attr('href', changelanguage('en-US'));
			$('li.pt-BR').find('a').attr('href',changelanguage('pt-BR'));

			$('li.es-ES').find('a').attr('href', changelanguage('es-ES'));
			$('li.zh-hk').find('a').attr('href', 'http://www.embraer.com.cn/');

			//provisoriamente excluir Espanhol e Chinês
			//voltar css com a imagem de background da seta
			$('.es-ES').remove();
        }


        $('ul.top-search li.language li:first-child').show();
        $('ul.top-search li.language > li:first-child').addClass('active');

        activeSelectLanguage();

        _statusLanguage = false;
        _selectedLanguage = 0;
        timers = new Array();
        _number = $('.language ul li').size();

        if(_number <= 1){
            $('.language').css('background','none');
        }

        function activeSelectLanguage(){
            $('.language ul li').each(function(index){
                $(this).click(function() {
                    if($(this).attr('class')){
                        if(!_statusLanguage){
                            refreshLanguage(index);
                            //$(this).delay(3000, hideLanguageAutomatic );
                            //window.setTimeout = function(code, interval) {
                              timers.push(setTimeout(hideLanguageAutomatic, 3000));
                            //}
                            //
                            _statusLanguage = true;
                        }else{
                            clearTimeouts();
                            refreshLanguageHide(index);
                        }
                    }else{
                        clearTimeouts();
                        refreshLanguageHide(index);
                    }
                });
            });
        }

        function refreshLanguage(_idx){
            $('.language ul li').each(function(index){
                $(this).show();
            });
        }

        function refreshLanguageHide(_idx){
            $('.language ul li').each(function(index){
                if(index == _idx){
                    $(this).show();
                    _selectedLanguage = index;
                    $(this).addClass('active');
                }else{
                    $(this).hide();
                    $(this).removeClass('active');
                }
            });
        _statusLanguage = false;
        }

        function hideLanguageAutomatic(){
            if(_statusLanguage){
                clearTimeouts();
                //refreshLanguageHide(_selectedLanguage);
            }
        }

        function clearTimeouts() {
          for (var i= 0;i < timers.length; i++) {
            clearTimeout(timers[i]);
          }
          resetTimeouts();
        }

        function resetTimeouts() {
          timers = new Array();
          refreshLanguageHide(_selectedLanguage);
        }

        function resetSearch(_ref){
            $this = _ref;
            if(   $this.val() ==  "Busca"  || $this.val() ==  "BUSCA" ){
                $this.val("");
            }
        }

        $('#header ul.navigation li').each(function(index) {
            $(this).mouseup(function() {
               $(this).removeClass('active');
            });
            $(this).mousedown(function() {
                resetNavigation(index);
                $(this).addClass('active');
            });
            $(this).mouseover(function() {
                resetNavigationAll();
            });
            $(this).mouseout(function() {
                resetNavigationAll();
            });
        });

        function resetNavigation(_idx){
            $('#header ul.navigation li').each(function(index) {
                if(index != _idx){
                    $(this).removeClass('active');
                }
            });
        }

        function resetNavigationAll(){
            $('#header ul.navigation li').each(function(index) {
                $(this).removeClass('active');
            });
        }



//});
}

function aToVideo(){
    $('a.player').each(function(index) {
    _rel = $(this).attr("rel").split("|");
    if(_rel != "undefined" && _rel != ""){

        _w = _rel[0];
        _h = _rel[1];

    }else{
        _w = 720;
        _h = 480;
    }
    $(this).youtubin({
        swfWidth : _w,
        swfHeight : _h
    });
 });
}


function menuActive(){

    var _url  = window.location.pathname;
        _url  = _url.toLowerCase();
    var _href = '';

	urlComp = _url.split('http://www.embraer.com/')[0]+'/'+_url.split('http://www.embraer.com/')[1]+'/'+_url.split('http://www.embraer.com/')[2]+'/';

    $('.navigation li a').each(function(){
    
        _href = $(this).attr('href');
        _href = _href.toLowerCase();

        if(_href == urlComp ){
        	$(this).addClass('selected');
        }
    
    });
    
}
