$(document).ready(function(){
	

	// Emulação de :focus para IE6
	$('form input:text, form textarea, form select').focus(function(){$(this).addClass('formFocus')});
	$('form input:text, form textarea, form select').blur(function(){$(this).removeClass('formFocus')});
	// Correção de layout para presença/ausência de vídeo na página de vaga
	if($('div#vaga div#vaga-video').length > 0) {
		$('div#vaga div#vaga-intro').width($('div#vaga').width() - $('div#vaga div#vaga-logo').width() - $('div#vaga div#vaga-video').width() - 35);
	} else {
		$('div#vaga div#vaga-intro').width($('div#vaga').width() - $('div#vaga div#vaga-logo').width() - 30);
	}
	
	// Correção de layout pré-replace para elementos <p> no header
	//$('div#header p').css('font-size','16px');
	
	// Correção de layout do menu local no IE
	if($.browser.msie == true) {
		if($('div#menu ul#menu-local li').length > 0) {
			$('div#menu ul#menu-local li:last').css('margin-bottom','-10px');
		}
	} else if($.browser.mozilla == true) {
		// Correção de layout do menu de filtragem de vagas no Mozilla
		$('div#vagas-forms select, div#vagas-forms input').css({position: 'relative', top: '-1px'});
	} else if($.browser.safari == true || $.browser.chrome == true) {
		// Correção de layout dos destaques da home no WebKit
		//$('ul#destaques-home li:not(:first)').css('margin-top','-2px');
	}
	
    // Flash replacement padrão com DIN Neuzeit Grotesk Light
	// Usa a configuração de corpo e cor de fonte do elemento sendo substituido
	var stdReplace = ['div#main.intro h3, div#main.produtos h3, div#main.pesquisas h3, div#main.valores h3'];
	$(stdReplace.join(', ')).each(function(){
		if($('object, embed',this).length > 0) return; 
		$(this).flash(
	        {src:'media/swf/text_din_neuzeit_grotesk_light.swf'},{version:8},
	         function(htmlOptions) {
				htmlOptions.flashvars.txt = textTransform(this.innerHTML, $(this).css('text-transform')).replace(/%/,'%25');
	            htmlOptions.flashvars.css = '* { leading: 6; color: ' + rgbToHex($(this).css('color')) + '; text-align: ' + $(this).css('text-align') + '; }';
	            htmlOptions.flashvars.fitToArea = 'no';
	            htmlOptions.flashvars.fontSize = $(this).css('font-size').substring(0, $(this).css('font-size').toString().length - 2);
	            htmlOptions.wmode = 'transparent';
				htmlOptions.height = $(this).height() + (htmlOptions.flashvars.fontSize/1.25);
	            htmlOptions.width = $(this).width()+7;
	            $(this).empty().addClass('replaced').prepend($.fn.flash.transform(htmlOptions));
			}  
		);               
	});
  
	// Flash replacement padrão com DIN Neuzeit Bold Condensed
	// Usa a configuração de corpo e cor de fonte do elemento sendo substituido
	//var stdReplace = ['div#header p', 'ul#destaques-home li span', 'div#main h2', 'div#main h3', 'ol#etapas-processo li h4'];
	var stdReplace = ['div#main h2', 'div#main h3', 'ol#etapas-processo li h4'];
	$(stdReplace.join(', ')).each(function(){
		if($('object, embed',this).length > 0) return;
		$(this).flash(
	        {src:'media/swf/text_din_neuzeit_bd_cd.swf'},{version:8},
	        function(htmlOptions) {
				htmlOptions.flashvars.txt = textTransform(this.innerHTML, $(this).css('text-transform')).replace(/%/,'%25');
	            htmlOptions.flashvars.css = '* { color: ' + rgbToHex($(this).css('color')) + '; text-align: ' + $(this).css('text-align') + '; }';
	            htmlOptions.flashvars.fitToArea = 'no';
	            htmlOptions.flashvars.fontSize = $(this).css('font-size').substring(0, $(this).css('font-size').toString().length - 2);
	            htmlOptions.wmode = 'transparent';
				htmlOptions.height = $(this).height();
	            htmlOptions.width = $(this).width()+10;
	            $(this).empty().addClass('replaced').prepend($.fn.flash.transform(htmlOptions));
			}            
	    );
	});
    
    // Flash replacement para página Números
    $('div#main.numeros div#contador strong').flash(
        {src:'media/swf/text_din_neuzeit_bd_cd.swf'},{version:8},
        function(htmlOptions) {
			htmlOptions.wmode = 'transparent';
			htmlOptions.flashvars.txt = textTransform(this.innerHTML, $(this).css('text-transform')).replace(/%/,'%25');
            htmlOptions.flashvars.css = '* { leading: 10; color: ' + rgbToHex($(this).css('color')) + '; text-align: ' + $(this).css('text-align') + '; }';
            htmlOptions.flashvars.fitToArea = 'no';
            htmlOptions.flashvars.fontSize = $(this).css('font-size').substring(0, $(this).css('font-size').toString().length - 2);
			htmlOptions.height = $(this).height() + 9;
            htmlOptions.width = $(this).width();
            $(this).empty().addClass('replaced').prepend($.fn.flash.transform(htmlOptions));
		}            
    );
    
    // Flash replacement para páginas de Contato e resultado de Contato
    $('div#main.contato p.intro, div#main.contato-erro p.intro, div#main.contato-sucesso p.intro').flash(
        {src:'media/swf/text_din_neuzeit_grotesk_light.swf'},{version:8},
        function(htmlOptions) {
			htmlOptions.flashvars.txt = textTransform(this.innerHTML, $(this).css('text-transform')).replace(/%/,'%25');
            htmlOptions.flashvars.fitToArea = 'no';
            htmlOptions.flashvars.css = '* { leading: 5; color: ' + rgbToHex($(this).css('color')) + '; text-align: ' + $(this).css('text-align') + '; }';
            htmlOptions.flashvars.fontSize = $(this).css('font-size').substring(0, $(this).css('font-size').toString().length - 2);
			htmlOptions.height = $(this).height();
            htmlOptions.width = $(this).width();
            $(this).empty().addClass('replaced').prepend($.fn.flash.transform(htmlOptions));
		}            
    );
    
	// Nossos Escritórios
	if($('div#main.escritorios ul li').length > 0) {
		$('div#main.escritorios ul li[class!=selected]').hide();
		var header = $('div#main.escritorios>p:first').hide().text();
		var units = [];
		$('div#main.escritorios ul li').each(function(){ units.push($(this).attr('title') + ' ' + $('address span', this).text()); });
		$('div#main.escritorios ul').before('<select id="select-escritorios"></select>');
		$('div#main.escritorios select#select-escritorios').append('<option value="--">' + header + '</option>');
		for(var i = 0; i < units.length; i++) {
			$('div#main.escritorios select#select-escritorios').append('<option value="' + i + '">' + units[i] + '</option>');	
		}
		$('div#main.escritorios select#select-escritorios').change(function(){
			$('div#main.escritorios ul li:not(:hidden)').hide();
			$('div#main.escritorios ul li:eq(' + $(this).val() + ')').show();
		});
	}
    
	// Itens de formulário com checkbox + select ou checkbox + textinput
	$('div#wizard p.checkbox-hide').each(function(){
		var select = $('select, input:text',this).css('visibility','hidden');
		$('input:checkbox', this).click(function(){
			if($(this)[0].checked) {
				select.css('visibility','hidden');
			} else {
				select.css('visibility','visible');
			}
		});
	});
	
	// Blocos ocultos de formulário
	$('*[class*=dependsOn-]').each(function(index){

		var hide = $(this).hide();
		var start = $(this).attr('class').indexOf('dependsOn-');
		var dependsOn = $('input[name=' + $(this).attr('class').substring(start + 'dependsOn-'.length) + ']');
		var type = dependsOn.attr('type');
		
		dependsOn.click(function(){
			showHideDependent(type, $(this), hide);
		});
		
		showHideDependent(type, $(this), hide, true);
		
	});
	
	function showHideDependent(type, dependsOn, object, force) {
		var hide = false;
		if(force) {
			hide = true;
		} else {
			switch(type) {
				case 'checkbox':
					hide = dependsOn.is(':checked');
					if($.browser.msie && $.browser.version < 7) hide = !hide;
					break;
				case 'radio':
					hide = (dependsOn.val() == '0' || dependsOn.val() == 'N');
					break;
			}
		}
		hide ? object.hide() : object.show();
	}
	
	
	// Tranformação de <select class="slider"> em slider"
	$('select.slider').each(function(){
			$(this).parents('p').height(21);
			var sel = $(this).hide();
			var opt;
			var id  = 'slider-' + $(this).attr('name');
			var options = {
					min: 0,
					max: $('option',this).length - 1,
					steps: $('option',this).length - 1,
					startValue: $('option:selected',this)[0].index,
					change: function(e, ui) {
							$('option', sel).removeAttr('selected');
							$('option:eq(' + ui.value + ')', sel).attr('selected','selected');
							$('span.ui-slider-handle-tooltip',this).text($('option:selected', sel).text());
						}
				};
			options.slide = options.change;
			
			$(this).after('<div id="' + id + '"></div>');
			var bgPos = '50% ';
			if($.browser.msie) {
				bgPos += ((($(this).height() + 3) * ($('option',this).length - 1)) - 1) + 'px';
			} else {
				bgPos += ((($(this).height() + 3) * ($('option',this).length - 2))) + 'px';
			}
			$('div#' + id).slider(options).css('background-position',bgPos);
				
			// This creates the tooltips to the right of the entire slider
			$('div.ui-slider',this.parentNode).append('<span class="ui-slider-handle-tooltip" />');
			$('span.ui-slider-handle-tooltip',$(this).next()).text($('option:selected',this).text());
			$(this).next().hover(function(){
				//$('span.ui-slider-handle-tooltip',this).fadeIn();
				$('span.ui-slider-handle-tooltip',this).show();
			}, function(){
				$('span.ui-slider-handle-tooltip',this).hide();
			});
		});
	
	// Replacement de radio buttons
	$('input:radio').checkbox({cls:'jquery-radio',empty:'media/img/geral/empty.png'});
	$('input:checkbox').checkbox({cls:'jquery-check',empty:'media/img/geral/empty.png'});
	
	// Eolas fix for IE
	if($.browser.msie == true){
		$('embed').each(function(){
			this.outerHTML = this.outerHTML;
		});
	}
	 
});

// Executa trasformações de case em strings
function textTransform(txt, trans) {
	switch(trans) {
		case 'lowercase':	return txt.toLowerCase();	break;
		case 'uppercase':	return txt.toUpperCase();	break;
		default:			return txt.toString();		break;
	}
}

// Converte cores RGB para valores HEX, hash (#) opcional
function rgbToHex(red, green, blue, noHash)
{
	if(red.indexOf('#') == 0) {
		return red;
	}
	if(green == undefined && blue == undefined && red.indexOf('rgb') == 0) {
		red = red.substring(4,red.length-1).split(',');
		blue = parseInt(red[2]);
		green = parseInt(red[1]);
		red = parseInt(red[0]);
	}
	red = red.toString(16);
	green = green.toString(16);
	blue = blue.toString(16);
	red = red == 0 ? red + red : red;
	green = green == 0 ? green + green : green;
	blue = blue == 0 ? blue + blue : blue;
    return (noHash === true ? '' : '#') + red + green + blue;
}
