/** v7
*****************************************************************************************************
* @versao		$ Id: index 10381 2010-09-22 12:06:53Z Anderson $
* @programador  Anderson de Oliveira Contreira.
* @mail         anderson@listocomconteudo.com.br
* @mail			ander-manos@hotmail.com
*****************************************************************************************************
*/

$(function(){
	/* begin languages flags */
	$("#langs li a").hover(function(){
		var src  = $(this).children().attr("src");
		var regex = new RegExp('hover','i');
			if(!$(this).hasClass("selecionado")){
				if(src.match(regex)){
					src = src.replace('_hover','');
					$(this).children().attr("src",src);
				}else{
					src = src.replace('.png','_hover.png');
					$(this).children().attr("src",src);
				}
			}
	},function(){
		$("#langs li a").each(function(){
			var src  = $(this).children().attr("src");
			if(!$(this).hasClass("selecionado")){
					src = src.replace('_hover','');
					$(this).children().attr("src",src);
			}
		});
	});
	/* end languages flags */
	/* begin search */
	$(".busca").focus(function(){
		$(this).val("");
	});
	$(".busca").blur(function(){
		if($(this).val() === ""){
			var v = $(this).attr('rel');
			$(this).val(v);
		}
	});
	/* end search */
	/* menuleft tabs */
	/* var menuespc = ['produtos'];
	var pagina = $("#cabecalho h2").text().toLowerCase();   
	alert(menuespc);
	alert(pagina);
	if(menuespc.indexOf(pagina) != -1){
		//$("#menuleft ul li:first-child a:first-child").addClass("ativo");
	} */
	//$("#menuleft ul li ul").hide();
	//$("#menuleft ul li ul li ul").hide();
	$("#menuleft ul li ul").parent().addClass("hasSubMenu");
	$(".hasSubMenu ul").hide();
	var first = $("#menuleft li.hasSubMenu")[0];
	var second = $("#menuleft > ul > li");
	
	$(".menu_mais").children("img").addClass("setad");
	
	if(window.location.href.toString().indexOf('/certificacoes/')>-1){}else{
		$(first).children().show();
		$(first).children("a").children("span").children("img").attr("src",window._dir+"html/img/setau.jpg");
		$(first).children("a").children("span").children("img").removeClass("setad");
		$(first).children("a").children("span").children("img").addClass("setau");
		
		if(window.location.href.toString().indexOf('/produtos/')>-1 || window.location.href.toString().indexOf('/productos/')>-1 || window.location.href.toString().indexOf('/products/')>-1){
			
			$(second).children().show();
			$(second).children("a").children("span").children("img").attr("src",window._dir+"html/img/setau.jpg");
			$(second).children("a").children("span").children("img").removeClass("setad");
			$(second).children("a").children("span").children("img").addClass("setau");
		}
	}
	
	$("#menuleft ul li.hasSubMenu a").click(function(){	
		//console.log("click");
		if($(this).parent().has("ul").length > 0){
			if($(this).parent().children("ul").is(":hidden")){
				$(this).parent().children("ul").show("blind");
				$(this).children("span").children("img").attr("src",window._dir+"html/img/setau.jpg");
				$(this).children("span").children("img").removeClass("setad");
				$(this).children("span").children("img").addClass("setau");
			}else{
				$(this).parent().children("ul").hide("blind");
				$(this).children("span").children("img").attr("src",window._dir+"html/img/setad.jpg");
				$(this).children("span").children("img").removeClass("setau");
				$(this).children("span").children("img").addClass("setad");
			}
			return false;
		}else{
			return true;
		}
	});
	
	/* o menu que estiver selecionado deve estar aberto */
	
	/* ************************************************** */
	/* ************************************************** */
	/* ************************************************** */
	var url = document.location.href;
	$("#menuleft li a").each(function(){
		//console.log($(this).attr("href"))
		if(url === $(this).attr("href")){
			$(this).css({"color":"#004529","font-weight":"bold"});
			//console.log("po");
			var i = 0;    
			var parent = $(this).parent(); 
			while($(parent).attr("id") != "menuleft"){
				if(i++ == 20) break;
					//console.log(parent);
					$(parent).show();
					var parent = $(parent).parent();
					//console.log($(parent).attr("id"));
					//console.log(i)
			} 
		}
	});
	
	
	
	/* menuleft tabs */
	$("tr").click(function(){
		if($(this).has("td .linkurl a").length > 0){
			var href = $(this).find("td .linkurl a").attr("href");
			//alert(href);
			//window.location.href = href;
			//window.location = 
		}
	});
	
	var _nnn = 0;
	
	/*
		
		$("a.fancybox[rel=galery]").each(function(){
				$(this).attr('title',$(this).parent().find('.img_desc').text());
				$(this).attr('rel',$(this).attr('href'));
				$(this).attr('href','#galeria');
			}).click(function(){
			if($("._ox").size() < 1){
				$('#corpo').before("<a name='galeria' id='galeria'></a>");
				$('#cabecalho').append("<div class='_ox' style='position:absolute; display:none; top:-62px; left:0px; z-index:11000;'><img src='http://www.guararapes.com.br/imagens/fundo.png' alt='' style='margin-left:-56px;margin-top:-38px;' /><img id='_oxi' src='"+$(this).attr('rel')+"' style='position:absolute;left:149px; top:116px; width:640px;'  alt='' /><div id='_oxl' style='width:136px; position:absolute; height:484px; left:11px; top:107px; overflow:auto; overflow-x:hidden;'></div><div id='_oxx' style='position:absolute; text-shadow:0px 1px #fff; bottom:66px; left:170px;'>"+$(this).attr('title')+"</div><a href='javascript:$(\"._ox\").fadeOut(500);void(0);' style='position:absolute; right:42px; top:35px;'><img src='http://www.guararapes.com.br/imagens/fechar.png' alt='' /></a></div>");
				$('.galeria > .img > a').each(function(){
					$('#_oxl').html($('#_oxl').html()+"<a id=\"_"+_nnn+"\" href='javascript:$(\"#_oxi\").attr(\"src\",$(\"#_"+_nnn+"\").attr(\"rel\"));$(\"#_oxx\").text($(\"#_"+_nnn+"\").attr(\"title\"));void(0);' rel='"+$(this).attr('rel')+"' title='"+$(this).attr('title')+"'><img src='"+$(this).attr('rel')+"' style='width:120px; padding-top:5px;' /></a>");
					_nnn++;
				}); 
			}else{
				$('#_oxi').attr('src',$(this).attr('rel'));
				$('#_oxx').text($(this).attr('title'));
			}
			$("._ox").fadeIn(500);	
		});
*/

	if($("a.fancybox[rel=galery]").size() > 0){
		lembra1 = "";
		lembra2 = "";
		$('.galeria > .img').each(function(){
			_img = $(this).find('a').attr('href');
			_des = $(this).find('.img_desc').text();
			lembra1 += _img + "|";
			lembra2 += _des + "|";
		});	
		lk = "imagens="+lembra1.substr(0,lembra1.length-1)+"&tit="+lembra2.substr(0,lembra2.length-1);
		
		$('.galeria').html('<object height="320" width="530" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">\
					<param value="http://www.guararapes.com.br/flashs/galeria3d.swf?'+lk+'" name="movie">\
					<param value="high" name="quality">\
					<param value="transparent" name="wmode">\
					<param value="'+lk+'" name="flashvars">\
					<param name="allowScriptAccess" value="always" />\
					<param name="allowFullScreen" value="true" />\
					<embed height="320" width="530" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" quality="high" src="http://www.guararapes.com.br/flashs/galeria3d.swf?'+lk+'" id="fInspectorSwf_1308571773632" allowscriptaccess="always" allowfullscreen="true" flashvars="'+lk+'"></object>');
		

	}else{
	
		$("a.fancybox[rel=galery]").fancybox({
					'titleShow'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'titlePosition' : 'over',
					'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
						return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
					}
		});
	}
		
		$("a.fancybox[rel=galery2]").fancybox({
					'titleShow'		: true,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'titlePosition' : 'over',
					'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
						return '<span id="fancybox-title-over" style="background:none;"><a href="'+title+'" target="_blank" style="font-weight:bold;">Baixar em formato PDF</a></span>';
					}
		});
		
		$('a.fancybox[rel=viewimg]').fancybox({
					'width'			: '63%',
					'height'		: '100%',
					'titleShow'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'titlePosition' : 'over',
					'type'			: 'iframe'
			
		}); 
		
		$("a.fancybox[rel=viewimgHTML]").fancybox({
					'width'			: 720,
					'height'		: 460,
					'titleShow'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'titlePosition' : 'over', 
					'type'			: 'iframe',
					'showNavArrows'	:	false
			
		});
		
		$("a.fancybox[rel=viewimgHTML2]").fancybox({
					'width'			: 495,
					'height'		: 306,
					'titleShow'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'titlePosition' : 'over', 
					'type'			: 'iframe',
					'showNavArrows'	:	false
		});
	
	
	
 /* if($("#certificacoes ul li").length > 1){ */
		$("#certificacoes ul").cycle();
	/* } */
	if($("#full-width-slider .wrapper ul li").length > 1){
		
		$("#full-width-slider .wrapper ul li").hide();
		//loader = new Image();
		//loader.src = _dir+"html/img/loader.gif";
		//loader.id = "iloader";
		//$("#full-width-slider .wrapper ul").append(loader);
		//$("#iloader").css({display: "block",height: "32px", margin:" 0 auto",  position: "relative",  top: "225px",  width: "32px",  "z-index": "5000"});
		//console.log("aqqq v4");
		//var _preloadimg = new Array('http://www.guararapes.com.br/imagens/slides_imagens/imag_qua_01.jpg','http://www.guararapes.com.br/imagens/slides_imagens/imag_qua_02.jpg','http://www.guararapes.com.br/imagens/slides_imagens/imag_qua_03.jpg','http://www.guararapes.com.br/imagens/slides_imagens/imag_qua_04.jpg','http://www.guararapes.com.br/imagens/slides_imagens/imag_qua_05.jpg');
		window.setTimeout(function(){
		//console.log("oppp");
		for(a in _preloadimg){
			var im = new Image();
				im.src = _preloadimg[a];
		}
		//console.log(im.src);
		
		$(im).load(function(){
			//console.log("OKz");
			window.setTimeout(
			function(){
				//$("#iloader").remove();
				$("#full-width-slider .wrapper ul li").show();
				slider();
			},1000);
		});		
		},2000);
		//console.log(_preloadimg[(_preloadimg.length-1)]);
		
	}
	if($('.banner div').length > 0){
		$('.banner').cycle();
	}
	
	/* formulários */
	$("textarea").keydown(function(){
		//console.clear();
		//console.log($(this).val().length);
		var vallen = $(this).val().length;
		var minlen = 35;
		var atulen = $(".tlen b").text();
		//var nohtml = new RegExp("(<(.*)>|[\[](.*)[\]])","i");
		/* A expressao regular cru funcionou muito melhor que o objeto */
		var nohtml = /\[(.*)\]|<(.*)>/i;
		if(ret = nohtml.exec($(this).val())){
			/* #debug */
			/* console.clear(); */
			/* console.log("sim"); */
			/* console.log(ret); */
			
			if(ret != undefined){
				/* remove os codigos perigosos */
				var text = $(this).val();
					text = text.replace(ret[0],"");
					$(this).val(text);
			}
		}
		if(parseInt(vallen) <= parseInt(minlen)){
			var vtlen = minlen-parseInt(vallen);
		}else{
			//console.log("ok");
			var vtlen = "OK";
		}
		$(".tlen b").text(vtlen);
	})
	$("select").focus(function(){
		var label = $(this).prev();
		if(label !== undefined){
			if(label[0] !== undefined){			
				var tag = label[0].tagName;
				/* var exc = ["fone","celular"]; */
				if(tag === "LABEL"){ 
					/* if(exc.indexOf(label.attr("for")) == -1){ */
						$(label).hide();
					/* }      */
				} 
			}	
		}
		/* if($(this).val() === ""){
			$(label).show();
		} */
	});
	$("select").change(function(){
		var label = $(this).prev();
		if(label !== undefined){
			if(label[0] !== undefined){			
				var tag = label[0].tagName;
				/* var exc = ["fone","celular"]; */
				if(tag === "LABEL"){ 
					/* if(exc.indexOf(label.attr("for")) == -1){ */
						$(label).hide();
					/* }      */
				} 
			}
		}
		if($(this).val() === ""){
			$(label).show();
		}
	});
	$("input,textarea").focus(function(){
		var label = $(this).prev();
		if(label !== undefined){
			if(label[0] !== undefined){			
				var tag = label[0].tagName;
				/* var exc = ["fone","celular"]; */
				if(tag === "LABEL"){ 
					/* if(exc.indexOf(label.attr("for")) == -1){ */
						$(label).hide();
					/* }      */
				}
			}			
		}
	});
	$("input,textarea").blur(function(){
		var label = $(this).prev();
		if(label !== undefined){
			if(label[0] !== undefined){			
				var tag = label[0].tagName;
				/* var exc = ["fone","celular"]; */
				if(tag === "LABEL"){
					/* if(exc.indexOf(label.attr("for")) == -1){ */
						if($(this).val() == null || $(this).val() == "" || $(this).val() == " "){
							$(label).show();
						}
					/* }   */
				}
			}
		}
	});
	$("label").click(function(){
		var input = $(this).next();
        $(input).trigger("focus");
	});
	
	/* opera bugfix && IE*/
	if(location.href == "http://www.guararapes.com.br/"){
		$("a").click(function(){
			var link = $(this).attr("href");
			document.location.href= link;
		});
	}
	
});

function slider(){
	//console.log("slider");
	//console.log(_preloadimg);
		for(a in _preloadimg){
			img = "<img src='"+_preloadimg[a]+"' alt='slide_"+a+"' />"
			$("#slider_"+a).append(img);
		}
		//console.log("slider s");
		$('#full-width-slider .wrapper ul').cycle({
			pager: "#slider-selector",
			timeout:config.timeout
		});
		$("#slider-selector a").text(" ");
	}
	
function linkClick(sel,id,ip){
	var date   = new Date();
	var year   = date.getFullYear();
	var month  = date.getMonth();
	(month < 10)?month = "0"+month:month;
	var day    = date.getDate();
	(day < 10)?day = "0"+day:day;
	var hour   = date.getHours();
	(hour < 10)?hour = "0"+hour:hour;
	var minute = date.getMinutes();
	(minute < 10)?minute = "0"+minute:minute;
	var second = date.getSeconds();
	(second < 10)?second = "0"+second:second;
	var now = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	window.location.href = window.location.href+"?_i=1&_id="+id+"&ip="+ip+"&now="+now;
}

