
//Javascript do projeto
var Busca = {
	// valor do campo busca
	_value : null,
	_elem : null,

	ativa : function(){
		this._elem = document.getElementById('busca');
		if(this._value == null && this._elem.value == 'buscar no site'){
			this._value = this._elem.value;
			this._elem.value = '';
		}else{
			if(this._value == 'buscar no site' && this._elem.value == ''){
				this._elem.value = 'buscar no site';
				this._value = null;
			}else{
				if(this._value == 'buscar no site' && this._elem.value == 'buscar no site'){
					// faz nada
				}
			}
		}
	}
};
var Uteis = {
	get_flash: function(arquivo, largura, altura, bgcolor, id, qualidade, alinhamento,transparente,pgphp){
		var obj = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largura+'" height="'+altura+'" id="'+id+'" align="'+alinhamento+'">';
		obj+= '<param name="allowScriptAccess" value="sameDomain" />';
		obj+= '<param name="movie" value="'+arquivo+'" />';
		obj+= '<param name="quality" value="'+qualidade+'" />';
		if(transparente == true) {
			obj+= '<param name="wmode" value="transparent" />';
		}
		obj+= '<param name="bgcolor" value="'+bgcolor+'" />';
		obj+= '<param name="FlashVars" value="pg='+pgphp+'" />';
		obj+= '<embed src="'+arquivo+'" '+((transparente==true)?'wmode="transparent"':'')+'" quality="'+qualidade+'" bgcolor="'+bgcolor+'" FlashVars="pg='+pgphp+'" width="'+largura+'" height="'+altura+'" name="'+id+'" align="'+alinhamento+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		obj+= '</object>';
		document.write(obj);
	},
	url: function(_url){
		document.location.href = _url;
	},
	urlBlank: function(_url){
		window.open(_url);
	},
	popup : function(url, width, height){
		var features = 'width='+width+',height='+height+',scrollbars=1,resizable=1';
		window.open(url, 'popup', features);
	},
	ge : function(elementId){
		var element = document.getElementById(elementId);
		return element;
	},
	ga : function(elementsTag){
		var elements = document.getElementsByTagName(elementsTag);
		return elements;
	},
	format: function (src, mask) {
		var i = src.value.length;
		var saida = mask.substring(0,1);
		var texto = mask.substring(i)
		if (texto.substring(0,1) != saida) {
			src.value += texto.substring(0,1);
		}
	},
	limpaInput : function(element){
		if(element.value == 'busca'){element.value = '';}
	},
	submitOnChange : function(id){
		window.location = "index.php?id=/fotos&cd_matia="+id;
	}
};

var Texto = {
	_p : null, // array contendo todos os elementos P
	_tam : null, // tamanho do array
	_fonteMin : 1.0, // tamanho mínimo da fonte (em)
	_fonteMax : 2.0, // tamanho máximo da fonte (em)
	_fonteAtual : 1.2, // tamanho atual da fonte (para esse projeto o padrão é 1.2)
	/**
	 * Função que aumenta o tamanho da fonte
	 * os elementos afetados são os parágrafos
	 */
	maior : function(){
		this.getVars();
		if(this._fonteAtual+0.1 < this._fonteMax){
			while(this._tam--){
				//this._p[this._tam].style.fontSize = this._fonteAtual + 0.1 + 'em';
				this._p.style.fontSize = this._fonteAtual + 0.1 + 'em';
			}
			this._fonteAtual += 0.1;
		}
	},
	/**
	 * Função que diminui o tamanho da fonte
	 * os elementos afetados são os parágrafos
	 */
	menor : function(){
		this.getVars();
		if(this._fonteAtual-0.1 > this._fonteMin){
			while(this._tam--){
				//this._p[this._tam].style.fontSize = this._fonteAtual - 0.1 + 'em';
				this._p.style.fontSize = this._fonteAtual - 0.1 + 'em';
			}
			this._fonteAtual -= 0.1;
		}
	},
	/**
	 * Função que coleta as informações sobre os parágrafos
	 * e inicializa todas as variáveis.
	 */
	getVars : function(){
		/*
		this._p = document.getElementById('conteudo').getElementsByTagName('p');
		this._tam = this._p.length;
		if(this._tam > 0){
			if(this._p[0].style.fontSize != ''){
				this._fonteAtual = parseFloat(this._p[0].style.fontSize);
			}
		}else{
			//window.alert('Nenhum texto foi encontrado');
		}
		*/
		this._p = document.getElementById('conteudo_texto');
		this._tam = 1;
		if(this._p.style.fontSize != ''){
			this._fonteAtual = parseFloat(this._p.style.fontSize);
		}
	}
};

var Validate = {
	email : function(mail){
		var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
			if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
			if(er.test(mail.value)){
				return true;
			}
    }else{
			return false;
    }
	},

	cpf : function(cpf){
		var c = cpf;
		if((c = c.replace(/[^\d]/g,"").split("")).length != 11) return false;
		if(new RegExp("^" + c[0] + "{11}$").test(c.join(""))) return false;
		for(var s = 10, n = 0, i = 0; s >= 2; n += c[i++] * s--);
		if(c[9] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
		for(var s = 11, n = 0, i = 0; s >= 2; n += c[i++] * s--);
		if(c[10] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
		return true;
	},

	cnpj : function(cnpj){
		var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = cnpj;
		if((c = c.replace(/[^\d]/g,"").split("")).length != 14) return false;
		for(var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
		if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
		for(var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
		if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
		return true;
	},

	inputs : function(form){
		var inputs = form.getElementsByTagName('input');
		var i = inputs.length;
		var msg = "";
		var erMail = new RegExp(/email+|e-mail+/);
		var erCnpj = new RegExp(/cnpj+|CNPJ+/);
		while(i--){
			if(inputs[i].type == 'text'){
				if(inputs[i].title != '' && inputs[i].value == ''){
					msg += "O campo "+inputs[i].title+" é obrigatório\n";
					inputs[i].style.backgroundColor = "#ffe4cc";
				}else{
					// verifica se é um campo email, para fazer a validação
					if(erMail.test(inputs[i].name)){
						if(Validate.email(inputs[i].value)){
							inputs[i].style.backgroundColor = "";
						}else{
							msg += "O campo "+inputs[i].title+" é inválido\n";
							inputs[i].style.backgroundColor = "#ffe4cc";
						}
					// Verifica se é um cnpj
					}else if (erCnpj.test(inputs[i].name)){
						if(Validate.cnpj(inputs[i].value)){
							inputs[i].style.backgroundColor = "";
						}else{
							msg += "O campo "+inputs[i].title+" é inválido\n";
							inputs[i].style.backgroundColor = "#ffe4cc";
						}
					}else{
						inputs[i].style.backgroundColor = "";
					}
				}
			}// if text
		}
		return msg;
	},

	textareas : function(form){
		var textareas = form.getElementsByTagName('textarea');
		var i = textareas.length;
		var msg = "";
		while(i--){
			if(textareas[i].title != '' && textareas[i].value == ''){
				msg += "O campo "+textareas[i].title+" é obrigatório\n";
				textareas[i].style.backgroundColor = "#ffe4cc";
			}else{
				textareas[i].style.backgroundColor = "";
			}
		}
		return msg;
	},

	selects : function(form){
		var selects = form.getElementsByTagName('select');
		var i = selects.length;
		var msg = "";
		while(i--){
			if(selects[i].title != '' && selects[i].value == ''){
				msg += "O campo "+selects[i].title+" é obrigatório\n";
				selects[i].style.backgroundColor = "#ffe4cc";
			}else{
				selects[i].style.backgroundColor = "";
			}
		}
		return msg;
	}
};/* Validate */

var Forms = {
	_form : null,
	_msg : '',
	_dados : null,

	submit : function(form_name){
		this._form = document.getElementById(form_name);
		this._msg += Validate.inputs(this._form);
		this._msg += Validate.textareas(this._form);
		if(this._msg == ''){
			this._msg = '';
			this._form.submit();
		}else{
			alert(this._msg);
			this._msg = '';
		}
	},

	showHide : function(id){
		var elem = document.getElementById(id);
		if(elem.style.display == 'none'){
			elem.style.display = '';
		}else{
			elem.style.display = 'none';
		}
	},

	submitAjax : function(form_name){
		this._form = document.getElementById(form_name);
		this._msg += Validate.inputs(this._form);
		this._msg += Validate.textareas(this._form);
		if(this._msg == ''){
			this._msg = '';
			
			switch(this._form.id){
				case 'form_indicar' :
					this.getDadosFormIndicar();
				break;
				case 'form_erro' :
					this.getDadosFormErro();
				break;
			}
			//this._form.submit();
			var form = $("#" + form_name);
			var data = $(form).serialize();
			
			$.ajax( {
				url: $(form).attr("action"),
				type: $(form).attr("method"),
				data: data,
				cache: false,
				async: true,
				success: function(html){
					alert(html);
				}
			});
			
		}else{
			alert(this._msg);
			this._msg = '';
		}
		return false;
	},
	
	getDadosFormIndicar : function(){
		this._dados = { 
									tipo : this._form.tipo.value,
									amigo_nome : this._form.amigo_nome.value,
									amigo_email : this._form.amigo_email.value,
									seu_nome : this._form.seu_nome.value,
									seu_email : this._form.seu_email.value,
									amigo_msg : this._form.amigo_msg.value
									};
	},
	
	getDadosFormErro : function(){
		this._dados = { 
									tipo : this._form.tipo.value,
									seu_nome : this._form.nome.value,
									seu_email : this._form.email.value,
									erro_msg : this._form.mensagem.value
									};
	}
};

// chamando o lightbox
$(function() {
	// Use this example, or...
	$('a[@rel*=lightbox]').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.6,
	imageLoading: 'imagens/lightbox-ico-loading.gif',
	imageBtnClose: 'imagens/lightbox-btn-close.gif',
	imageBtnPrev: 'imagens/lightbox-btn-prev.gif',
	imageBtnNext: 'imagens/lightbox-btn-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Imagem',
	txtOf: 'de'
   });
});
$(function() {
	// Use this example, or...
	$('a[@rel*=coisa]').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.6,
	imageLoading: 'imagens/lightbox-ico-loading.gif',
	imageBtnClose: 'imagens/lightbox-btn-close.gif',
	imageBtnPrev: 'imagens/lightbox-btn-prev.gif',
	imageBtnNext: 'imagens/lightbox-btn-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Imagem',
	txtOf: 'de'
   });
});

var TimelineExtra = {
	onclick : function(elem){
		var dec = document.getElementById('aba_decadas');
		var emp = document.getElementById('aba_empresas');
		dec.className = 'timeline_aba_off';
		emp.className = 'timeline_aba_off';
		elem.className = 'timeline_aba_on';
		if(elem.id == 'aba_empresas'){
			document.getElementById('my-timeline').style.display = 'none';
			document.getElementById('time_empresas').style.display = 'block';
		}else{
			document.getElementById('time_empresas').style.display = 'none';
			document.getElementById('my-timeline').style.display = 'block';
		}
	}
};
