var SEJA = {
	'timer':new Date().getFullString(),
	'ok': false
}

/* configurando swf upload */

function tiraAcento(text) { 
  text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'a'); 
  text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'e'); 
  text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'i'); 
  text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'o'); 
  text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'u'); 
  text = text.replace(new RegExp('[Ç]','gi'), 'c');
  text = text.replace( " ", "_" );
  return text; 
} 

function uploadCompleteToHidden(file) {
	if (this.getStats().files_queued === 0) {
		//document.getElementById(this.customSettings.cancelButtonId).disabled = true;
	}
	document.getElementById('arquivoTrabalhe').value = tiraAcento(nameNew);
}
var swfu;

var settings = {
	flash_url:"/media/padrao/swf/swfupload.swf",
	upload_url:'/upload/?t='+SEJA.timer,	// Relative to the SWF file
	file_size_limit:"10 MB",
	file_post_name:"pubFile",

	file_types: "*.pdf;*.doc;*.docx;*.txt;",
	file_types_description:"Documentos",

	file_upload_limit:0,
	file_queue_limit:0,
	custom_settings:{
		progressTarget:"fsUploadProgress",
		cancelButtonId:"btnCancel"
	},
	debug:false,

	// Button settings
	button_image_url:"/media/padrao/img/selectFile.gif",	// Relative to the Flash file
	button_width:"30",
	button_height:"25",
	button_placeholder_id:"spanButtonPlaceHolder",
	button_text:'',
	button_text_style:'',
	button_text_left_padding:0,
	button_text_top_padding:0,
	
	// The event handler functions are defined in handlers.js
	
	file_queued_handler:fileQueued,
	file_queue_error_handler:fileQueueError,
	file_dialog_complete_handler:fileDialogComplete,
	upload_start_handler:uploadStart,
	upload_progress_handler:uploadProgress,
	upload_error_handler:uploadError,
	upload_success_handler:uploadSuccess,
	upload_complete_handler:uploadCompleteToHidden,
	queue_complete_handler:null	// Queue plugin event
};

var tmContato;

function setClear(id, campos){
	if(tmContato != null) clearTimeout(tmContato);
	tmContato = setTimeout(function(){
		for(m in campos) $("#"+m).val("");
		$('#'+id).html("");
	}, 5000);	
}

var baseForm = function(pt){ 
	for (var k in pt){ this[k] = pt[k]; }
}
baseForm.prototype = {
	'status': 0,
	'mensagensCampos': {},	
	'mensagens': {
		'send': '',
		'complete': '',
		'error': ''
	},	
	'global': false,
	'type': "POST",
	'url': "",
	'cache': false,
	'data': {},
	'dataType': "text",	
	'onPreValidade': function(){},
	'onValidadeError': function(msg){},
	'extraValidade': function(){},
	'presend': function(m){},
	'success': function(msg){},
	'error': function(){},	
	'validade': function(){
		if(this.status  != 0) return;
		this.onPreValidade();

		for(m in this.mensagensCampos){
			if($("#"+m).val() == ""){
				$("#"+m).focus();
				this.onValidadeError(this.mensagensCampos[m]);
				return false;
			}
		}

		if(this.extraValidade() == false) return false;

		this.presend(this.mensagens.send);
	},	
	'send': function(){
		if(this.validade() == false) return;

		this.status = 1;

		var obj = this;
		$.ajax({
			'global': obj.global,
			'type': obj.type,
			'url': obj.url,
			'cache': obj.cache,
			'data': obj.data,
			'dataType': obj.dataType,
			'success': function(m){
				obj.status = 0;			
				obj.success(m);
			},
			'error': function(){
				obj.status = 0;			
				obj.error(m);
			}
		});
	}	
}

function changeForm(o){
	var a = {
		'contato':'',
		'trabalhe':'',
		'cadastre':''
	}

	for(b in a) $("#"+b).hide();

	$("#"+o).show();
}

$(document).ready(function(){
	var CONTATO = new baseForm({
		'mensagens': {
			'send': mensagens.contato.mensagens.enviando,
			'complete': mensagens.contato.mensagens.sucesso,
			'error': mensagens.contato.mensagens.erro
		},
		'url': "/"+getLinguagem()+"/contatos/",
		'mensagensCampos': mensagens.contato.validar,
		limparCampos: mensagens.contato.campos,
		'presend': function(m){
			this.data = {
				'a': 'contato',
				'nome': $("#nome").val(),
				'email': $("#email").val(),
				'telefone': $("#telefone").val(),
				'assunto': comboAssunto.value,
				'mensagem': $("#mensagem").val()
			}
			$('#mensagens').html(m);
		},
		'onPreValidade': function(){
			$('#mensagens').html("");
		},
		'onValidadeError': function(m){
			$('#mensagens').html(m);
		},
		'extraValidade': function(){
			if(!$("#email").val().is_mail()){
				$("#email").select();
				$('#mensagens').html(this.mensagensCampos['email']);
				return false;
			}
		},
		'success': function(msg){
			if(msg == "0") $('#mensagens').html(this.mensagens.complete);
			else $('#mensagens').html(this.mensagens.error);
			setClear('mensagens', this.limparCampos);
		},
		'error': function(){
			$('#mensagens').html(this.mensagens.error);
			setClear('mensagens', this.limparCampos);
		}
	});

	$('#sendContato').click(function () {
		CONTATO.send();
	});

	var TRABALHE = new baseForm({
		mensagens: {
			'send': mensagens.trabalhe.mensagens.enviando,
			'complete': mensagens.trabalhe.mensagens.sucesso,
			'error': mensagens.trabalhe.mensagens.erro
		},
		url: "/"+getLinguagem()+"/contatos/",
		mensagensCampos: mensagens.trabalhe.validar,
		limparCampos: mensagens.trabalhe.campos,
		presend: function(m){
			this.data = {
				'a': 'trabalhe',
				'nome': $("#nomeTrabelhe").val(),
				'email': $("#emailTrabelhe").val(),
				'telefone': $("#telefoneTrabelhe").val(),
				'arquivo': 'trabalhe/'+$("#arquivoTrabalhe").val(),
				'cidade': $("#cidadeTrabelhe").val(),
				'estado': $("#estadoTrabelhe").val(),	
				'area': $("#areaTrabalhe").val()	
			}
			$('#mensagens2').html(m);
		},
		onPreValidade: function(){
			$('#mensagens2').html("");
		},
		onValidadeError: function(m){
			$('#mensagens2').html(m);
		},
		extraValidade: function(){
			if(!$("#emailTrabelhe").val().is_mail()){
				$("#emailTrabelhe").select();
				$('#mensagens2').html(this.mensagensCampos['emailTrabelhe']);
				return false;
			}
		},
		success: function(msg){
			if(msg == "0") $('#mensagens2').html(this.mensagens.complete);
			else $('#mensagens2').html(this.mensagens.error);
			setClear('mensagens2', this.limparCampos);
		},
		error: function(){
			$('#mensagens2').html(this.mensagens.error);
			setClear('mensagens2', this.limparCampos);
		}
	});
	
	$('#sendTrabalhe').click(function () {
		TRABALHE.send();
	});
	
	var CADASTRE = new baseForm({
		mensagens: {
			'send': mensagens.cadastre.mensagens.enviando,
			'complete': mensagens.cadastre.mensagens.sucesso,
			'error': mensagens.cadastre.mensagens.erro
		},
		url: "/"+getLinguagem()+"/contatos/",
		mensagensCampos: mensagens.cadastre.validar,
		limparCampos: mensagens.cadastre.campos,
		presend: function(m){
			this.data = {
				'a': 'cadastre',
				'nome': $("#nomeCadastre").val(),
				'email': $("#emailCadastre").val(),
				'telefone': $("#telefoneCadastre").val(),
				'celular': $("#celularCadastre").val(),				
				'cidade': $("#cidadeCadastre").val(),
				'estado': $("#estadoCadastre").val()
			}
			$('#mensagens3').html(m);
		},
		onPreValidade: function(){
			$('#mensagens3').html("");
		},
		onValidadeError: function(m){
			$('#mensagens3').html(m);
		},
		extraValidade: function(){
			if(!$("#emailCadastre").val().is_mail()){
				$("#emailCadastre").select();
				$('#mensagens3').html(this.mensagensCampos['emailCadastre']);
				return false;
			}
		},
		success: function(msg){
			if(msg == "0") $('#mensagens3').html(this.mensagens.complete);
			else $('#mensagens3').html(this.mensagens.error);
			setClear('mensagens3', this.limparCampos);
		},
		error: function(){
			$('#mensagens3').html(this.mensagens.error);
			setClear('mensagens3', this.limparCampos);
		}
	});
	
	$('#sendCadastre').click(function () {
		CADASTRE.send();
	});

	$('.mascara_fone').mask('(99) 9999-9999');
	
	swfu = new SWFUpload(settings);

});

