/*REPLACE CHARACTER*/
function replaceCharacter(txt){
	var txt = txt.toLowerCase();
	txt = txt.replace(/[áàãâä]/g,'a');
	txt = txt.replace(/[éèêë&]/g,'e');
	txt = txt.replace(/[íìîï]/g,'i');
	txt = txt.replace(/[óòõôö]/g,'o');
	txt = txt.replace(/[úùûü]/g,'u');
	txt = txt.replace(/[ç]/g,'c');
	
	for(var i = 0; i < txt.length; i++){
		if(' _0123456789abcdefghijklmnopqrstuvwxyz'.indexOf(txt.charAt(i)) == -1){
			txt = txt.replace(txt.charAt(i), ' ');
		}
	}
	
	txt = txt.replace(/[ ]/g, '_');
	txt = txt.replace(/[.]/g, '_');
	return txt;
}

/*VALUE FOCUS*/
function valueFocus(){
	var inputs = $('input:text');
	var textareas = $('textarea');
	
	function returnValue(obj){
		/*FOCUS*/
		obj.focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		})
		
		/*BLUR*/
		obj.blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
			}
		})
	}
	
	/*INPUT*/
	for(var i = 0; i < inputs.length; i++){
		returnValue($(inputs[i]));
	}
	
	/*TEXTAREA*/
	for(var i = 0; i < textareas.length; i++){
		returnValue($(textareas[i]));
	}
}

/*SPLIT TXT*/
function substringTXT(txt, quant){
	if(txt.length > quant){
		txt = txt.substr(0, txt.substr(0, quant).lastIndexOf(' ')) + '...';
	}
	
	return txt;
}

/*MAPS*/
function initMaps(){
	setTimeout(function(){
		if($('#maps').is(':visible')){
			$('body').attr('onunload', 'GUnload()');
				
			var maps = new GMap2(document.getElementById('maps'));
			var marker = new GLatLng(-23.5909811, -46.683059);
			maps.setCenter(marker, 17);
			maps.addOverlay(new GMarker(marker));
			
			maps.addControl(new GLargeMapControl());
			maps.addControl(new GMapTypeControl());
		}
	}, 3000)
}

/*BOX SELECT*/
function boxSelect(){
	$('.box_select').focus();
}

/*IPHONE*/
function iPhone(){
	/*MENU*/
	$('.iphone #page .content_header').append('<ul class="menu"><li><a href="' + $('.bt_sobre_a_talent').attr('href') + '">A Talent</a></li><li><a href="' + $('.bt_localizacao').attr('href') + '">Contato</a></li><li><a href="#" onclick="boxSelect(); return false;">Menu</a><select class="box_select" width="0" height="0" onchange="window.location.href=this.options[this.selectedIndex].value"><option>Menu</option><option value="' + $('.bt_clientes_e_trabalhos').attr('href') + '">Clientes</option><option value="' + $('.bt_veja_os_classicos').attr('href') + '">Clássicos</option><option value="?tag=imprensa">Últimas na imprensa</option></select></li></ul>');
	$('.iphone #page .content_header ul li:last').css('border', 0);
	
	/*REMOVE*/
	$('#sidebar, #header, .bt_receba_atualizacoes_via_rss, .info, .postar_url').remove();
	
	/*HOME IPHONE*/
	if($('#content').attr('class').indexOf('home_blog') != -1){
		/*REMOVE*/
		$('embed, object, #content .post .entry h2, #content .post .entry h3, #content .post .entry h4, #content .post .entry h5, #content .post .entry h6').remove();

		var viewParagraphs;
		var linkPost;
		
		for(var i = 0; i < $('#content').find('.post').length; i++){
			viewParagraphs = 0;
			
			for(var j = 0; j < $('#content').find('.post').eq(i).find('img').length; j++){
				if(j == 0){
					$('#content').find('.post').eq(i).addClass('box_image');
					$('#content').find('.post').eq(i).find('img').eq(0).show();
					$('#content').find('.post').eq(i).prepend($('#content').find('.post').eq(i).find('img').eq(0).clone());
					
				}else{
					$('#content').find('.post').eq(i).find('img').eq(j).hide('fast');
				}
			}

			for(var k = 0; k < $('#content').find('.post').eq(i).find('p').length; k++){
				if($('#content').find('.post').eq(i).find('p').eq(0).text() == '...' || $('#content').find('.post').eq(i).find('p').eq(0).text() == ''){
					viewParagraphs = 1;
					
					$('#content').find('.post').eq(i).find('p').eq(0).hide('fast');
				}
				
				if(k == viewParagraphs){
					$('#content').find('.post').eq(i).find('p').eq(viewParagraphs).removeAttr('style');
					$('#content').find('.post').eq(i).find('p').eq(viewParagraphs).html('<a href="#">' + substringTXT($('#content').find('.post').eq(i).find('p').eq(viewParagraphs).text(), 150) + '</a>');
					
				}else{
					$('#content').find('.post').eq(i).find('p').eq(k).hide('fast');
				}
			}
			
			$('#content').find('.post').eq(i).append('<a href="#" class="bt_leia_mais">Leia mais »</a>');
			
			linkPost = $('#content').find('.post').eq(i).find('h2 a').attr('href');
			
			$('#content').find('.post').eq(i).find('h2').html('<a href="#">' +  substringTXT($('#content').find('.post').eq(i).find('h2').text(), 32) + '</a>');
			$('#content').find('.post').eq(i).find('a').attr('href', linkPost);
			$('#content').find('.post').eq(i).find('a').removeAttr('target');
		}
	}
	
	if($('#maps').is(':visible')){
		$('#maps').parent().append('<p><a href="http://www.google.com.br/maps?f=q&source=s_q&hl=pt-BR&geocode=&q=Av.+Pres.+Juscelino+Kubitschek,+1455+04543-011+-+S%C3%A3o+Paulo&sll=-22.154565,-51.376769&sspn=0.006489,0.009645&ie=UTF8&z=17&iwloc=r0" target="_blank">Ir para o google maps<a/></p>');
	}
	
	/*SHOW*/
	$('#page').fadeIn('show');
}

/*INIT*/
function init(){
	/*VALUE FOCUS*/
	valueFocus();
	
	/*ADD CLASS*/
	for(var i = 0; i < $('#sidebar li h2').length; i++){
		$('#sidebar li h2').parent().eq(i).addClass('box_' + replaceCharacter($('#sidebar li h2').eq(i).html()));
		$('#sidebar li h2').eq(i).addClass('tit_' + replaceCharacter($('#sidebar li h2').eq(i).html()));
	}
	
	for(var i = 0; i < $('#sidebar li li').length; i++){
		$('#sidebar li li').eq(i).addClass('box_' + replaceCharacter($('#sidebar li li a').eq(i).html()));
	}
	
	for(var i = 0; i < $('#sidebar li a').length; i++){
		$('#sidebar li a').eq(i).addClass('bt_' + replaceCharacter($('#sidebar li a').eq(i).html()));
	}
	
	$('#content').addClass(replaceCharacter($('#content .post h2').html()));
	
	/*INDIQUE ESTE POST*/
	$('.bt_indique_este_post a').click(function(){
		$(this).parents('.post').find('.postar_url').toggle('show', function(){
			$('.postar_url textarea').blur();
			$(this).parents('.post').find('.postar_url textarea').select();
		})
		
		return false;
	})
	
	/*INIT MAPS*/
	initMaps();
}

/*ONLOAD*/
$(document).ready(function(){
	window.setTimeout(function(){
		/*INIT*/
		init();
	}, 1)
})