/***************************************
*  Autor: Humberto C. Gobatto          *
*  Comandos JQuery                     *
*  Data: Outubro/2010                  *
****************************************/
function chamajanela(id) {
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$('#mask').fadeIn(1000);	

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
		  
	$(id).position({
	  my: "center",
	  at: "center",
	  of: "#mask"
	});
	
	$(id).css('background-image', 'url(images/404.jpg)');
	$(id).css('background-repeat', 'no-repeat');
	
	//transition effect
	$(id).fadeIn(2000); 
}

function erro(page){
	$(document).ready(function() {	
	
		//Put in the DIV id you want to display
		chamajanela('#404');
		
		//if close button is clicked
		$('.window').click(function () {
			$('#mask').hide();
			$('.window').hide();
		});		
		
		//if mask is clicked
		$('#mask').click(function () {
			$(this).hide();
			$('.window').hide();
		});			
	
	});
}


function limitChars(texto, limite, info){
		var text = $('#'+texto).val();
		var textlength = text.length;

		if(textlength > limite)
		{
			//$('#' + info).html('Você ultrapassou o limite de '+limite+' caracteres');
			$('#'+texto).val(text.substring(0,limite));
			return false;
		}
		else
		{
			if ((limite - textlength) != 0) 
			$('#' + info).html('Restam '+ (limite - textlength) +' caracteres'); else
			$('#' + info).html('Foram digitados 200 caracteres.');
			return true;
		}
	}
	$().ready(function(){
		$('#msg_c').keyup(function(){
			limitChars('msg_c', 200, 'left')
		})
	});

//---------- Meio ---------
var winH = $(window).height();
var winW = $(window).width();

$(document).ready(function(){
	$("ul.subnav").parent().append("<span></span>"); 
	$("ul.topnav li span").click(function() { 
		$(this).parent().find("ul.subnav").slideDown('fast').show(); 
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); 
		});
		}).hover(function() { 
			$(this).addClass("subhover"); 
		}, function(){	
			$(this).removeClass("subhover"); 
	});

	$('#codigo').dialog({
		autoOpen: false,
		closeOnEscape: true,
		width: 350,
		height: 400,
		modal: true,
		resizable: false,
		buttons: {
			"Enviar": function() { 
				$("#liberar").submit();
				
			}, 
			"Cancelar": function() {
				$("#liberar").resetForm(); 
				$(this).dialog("close"); 
			} 
		}
	});
	
	$('#info').dialog({
		autoOpen: false,
		closeOnEscape: true,
		width: 300,
		height: 420,
		modal: true,
		resizable: false,
		buttons: {
			"Enviar": function() { 
				$("#contato").submit();
				
			}, 
			"Cancelar": function() {
				$("#contato").resetForm(); 
				$(this).dialog("close"); 
			} 
		}
	});	

	$('.abrir').click(function() { 
		$('#codigo').fadeIn('slow').dialog('open');
	});
	
	$('.abrir2').click(function() { 
		$('#info').fadeIn('slow').dialog('open');
	});	
	
});

function response(data) {
	$("#liberar").resetForm();
	$('#codigo').dialog('close');
	$("#resposta").html(data);

/*	$('#resposta').css('top',  winH/2-$('#resposta').height()/2);
	$('#resposta').css('left', winW/2-$('#resposta').width()/2);*/
	
	$("#resposta").position({
	  my: "center center",
	  at: "center center"
  	});


	$('#resposta').fadeIn().delay(10000).fadeOut('slow'); 
	
	$('#resposta').click(function (e) {
		e.preventDefault();
		$('#resposta').hide();
	});		
};

function response2(data) {
	$("#contato").resetForm();
	$('#info').dialog('close');
	$("#resposta").html(data);

/*	$('#resposta').css('top',  winH/2-$('#resposta').height()/2);
	$('#resposta').css('left', winW/2-$('#resposta').width()/2);*/
	
	$("#resposta").position({
	  my: "center center",
	  at: "center center"
  	});


	$('#resposta').fadeIn().delay(10000).fadeOut('slow'); 
	
	$('#resposta').click(function (e) {
		e.preventDefault();
		$('#resposta').hide();
	});		
};
			
$(function(){

	$("#liberar").validate({

		submitHandler: function(form) {
			$(form).ajaxSubmit({
				dataType: 'post',
				success: response
			});
			},
			rules:{
			nome:{
				required: true, minlength: 2
			},
			empresa:{
				required: false
			},
			email:{
				required: true, email: true
			},
			software:{
				required: true
			},
			licenca:{
				required: true
			},
			serie:{
				required: true, minlength: 14
			}
		},
		// Define as mensagens de erro para cada regra
		messages:{
			nome:{
				required: "Digite o seu nome",
				minlength: "O seu nome deve conter, no mínimo, 2 caracteres"
			},
			email:{
				required: "Digite o seu e-mail",
				email: "Digite um e-mail válido"
			},
			software:{
				required: "Selecione o Software desejado"
			},
			licenca:{
				required: "Selecione o tipo de licença desejado"
			},
			serie:{
				required: "Digite o Nº de Série informado ao abrir o Software",
				minlength: "O Nº de série deve conter 14 caracteres"
			}
		}

	});

	$("#contato").validate({

		submitHandler: function(form) {
			$(form).ajaxSubmit({
				dataType: 'post',
				success: response2
			});
			},
			rules:{
			nome_c:{
				required: true, minlength: 2
			},
			empresa_c:{
				required: false
			},
			email_c:{
				required: true, email: true
			},
			tipo_c:{
				required: true
			},
			msg_c:{
				required: true, maxlength: 200
			}
		},
		// Define as mensagens de erro para cada regra
		messages:{
			nome_c:{
				required: "Digite o seu nome",
				minlength: "O seu nome deve conter, no mínimo, 2 caracteres"
			},
			email_c:{
				required: "Digite o seu e-mail para receber a liberação",
				email: "Digite um e-mail válido"
			},
			tipo_c:{
				required: "Selecione o tipo de contato"
			},
			msg_c:{
				required: "Digite sua mensagem",
				maxlength: "Mensagem deve conter 200 caracteres no máximo."
			}
		}

	});

});


//--------------------------------------
