$(document).ready(function(){
	$("#dept").change(function () {
		$("#dept option:selected").each(function () {
				var selected = $(this).val();
				$.post("/user/city", { id: selected }, function(data){
					$("#city").html(data);            
				});   
        });
	});


	$("#login-button").click(function () {
		$("#login-form").delay(200).slideToggle(); 
	});


    $("#date").change(function(){
        var now = new Date();
        var datebox = $(this).val().split("-");
        var birthdate = new Date(datebox[0], datebox[1], datebox[2]);
        var years = Math.floor((now.getTime()-birthdate.getTime())/(365.25*24*60*60*1000));
        if(years < 18){
           alert("Tu edad es de "+years+" AÃ±os, debes llenar el formulario de registro de acudiente mayor de edad.");
           $('#dialog-form').dialog('open');
        }else{
            $("#guardian_form").css('display', 'none');        
        }
    });
    
	$("#date").datepicker({
		   showOn: "both",
		   dateFormat: "yy-mm-dd", 
		   buttonImage: "/public/default/img/calendar_view_month.png", 
		   buttonImageOnly: true,
		   changeYear: true,
		   yearRange: "-95:+0"
	});
	
	$("#date2").datepicker({
		   showOn: "both",
		   dateFormat: "yy-mm-dd", 
		   buttonImage: "/public/default/img/calendar_view_month.png", 
		   buttonImageOnly: true,
		   changeYear: true,
		   yearRange: "-95:-18"
	   });
	
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog-form").dialog("destroy");
	var name = $("#guardian_name"),
	firstLastName = $("#guardian_first_last_name"),
	secondLastName = $("#guardian_second_last_name"),
	birthdate = $("#date2"),
	identification = $("#number"),
	idtype = $("#type"),
	allFields = $([]).add(name).add(firstLastName).add(secondLastName).add(birthdate).add(identification).add(idtype),
	tips = $(".validateTips");
	
	function updateTips(t) {
		tips.text(t);
		tips.addClass('ui-state-highlight');
		setTimeout(function() {
		tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Numero de caracteres de " + n + " debe estar entre "+min+" y "+max+".");
			return false;
		} else {
			return true;
		}
	}
	function checkRegexp(o,regexp,n) {
		if (!( regexp.test(o.val()))){
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}

	$("#dialog-form").dialog({
		autoOpen: false,
		height: 400,
		width: 350,
		modal: true,
		buttons:{
			'Guardar': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');
				bValid = bValid && checkLength(name,"name",3,16);
				bValid = bValid && checkLength(firstLastName,"firstLastName",3,16);
				bValid = bValid && checkLength(secondLastName,"secondLastName",3,16);
				bValid = bValid && checkLength(identification,"identification",3,16);
				bValid = bValid && checkRegexp(name,/^[a-zA-Z áéíóúÁÉÍÓÚÑñ]*$/,"El nombre solo puede contener letras de la a a la z.");
				bValid = bValid && checkRegexp(firstLastName,/^[a-zA-Z áéíóúÁÉÍÓÚÑñ]*$/,"Los Apellidos solo puede contener letras de la a a la z.");
				bValid = bValid && checkRegexp(secondLastName,/^[a-zA-Z áéíóúÁÉÍÓÚÑñ]*$/,"Los Apellidos solo puede contener letras de la a a la z.");
				bValid = bValid && checkRegexp(identification,/^[a-zA-Z0-9]*$/,"El documento de identificacion solo debe contener numeros y letras");
				if (bValid) {
					$.post("/guardian/add",{ name: name.val() , firstLastName: firstLastName.val() , secondLastName:  secondLastName.val() , identification: identification.val(), birthdate: birthdate.val() , type: idtype.val()},function(data){$("#guardian").val(data);}); 
					$(this).dialog('close');           
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

$("#next").click(function(){
	var next = $('.step-orange').next();
	if(next.attr('id') == 'contact_div_form')
		$('#next').css('visibility', 'hidden');
	else
		$('#next').css('visibility', 'visible');
	if($('.step-orange').attr('class') == 'step-orange first')
		$('#back').css('visibility', 'visible');
	
	if($('.step-orange').attr('id') != 'contact_div_form'){
		//Get radios
		var title = $(":radio:checked."+$('.step-orange').attr('id')).next().next().attr('title');
		var options = $(":radio:checked."+$('.step-orange').attr('id')).next().next().clone();
		if (title != null && options != null){
			$('.resume').append("<div id='content-"+$('.step-orange').attr('id')+"'>");
			$("#content-"+$('.step-orange').attr('id')).append('<div class="contact-list-title">' + title);
			$("#content-"+$('.step-orange').attr('id')).append(options);			
		}			
		//get checkbox
		var i = 0;
		$(":checkbox:checked."+$('.step-orange').attr('id')).each(function(){
			if(i == 0){
				$('.resume').append("<div id='content-"+$('.step-orange').attr('id')+"'>");
				$("#content-"+$('.step-orange').attr('id')).append('<div class="contact-list-title">' + $(this).next().next().attr('title'));
				i = 1;
			}
			$("#content-"+$('.step-orange').attr('id')).append($(this).next().next().clone());
					
		});	
	}
	
	if($('.step-orange').attr('class') == 'step-orange first')
		$('.step-orange').attr('class', 'step first');
	else
		$('.step-orange').attr('class', 'step');
	

	var element = next.attr('id').split("_");
	var id = element[0]+'_'+element[2];
	$('.contact-form').css('visibility', 'hidden');
	$('#'+id).css('visibility', 'visible');
	next.attr('class', 'step-orange');
			
	});	
$("#back").click(function(){
	var back = $('.step-orange').prev();
	if(back.attr('class') != 'step first')
		$('#back').css('visibility', 'visible');
	else
		$('#back').css('visibility', 'hidden');
	if(back.attr('class') != 'contact_div_form')
		$('#next').css('visibility', 'visible');
	
	
	$("#content-"+back.attr('id')).remove();
	
	$('.step-orange').attr('class', 'step');
	

	var element = back.attr('id').split("_");
	var id = element[0]+'_'+element[2];
	$('.contact-form').css('visibility', 'hidden');
	$('#'+id).css('visibility', 'visible');
	if(back.attr('class') == 'step first')
		back.attr('class', 'step-orange first');
	else
		back.attr('class', 'step-orange');
			
	});	

$("#dialog-talent").dialog("destroy");
$("#dialog-talent").dialog({
	autoOpen: false,
	height: 529,
	width: 990,
	modal: true,
	zIndex: 996,
	close: function() {
	$("#dialog-talent").html('<img src="/public/default/img/loading.gif" alt="loading....." style="margin-top:18%; margin-left:45%;" />');
	}
});
$('.picsfoot').click(function(){ 
	var selected = $(this).attr('id').split("_");
	$.post("/talents/profile", { id: selected[1] }, function(data){
		$("#dialog-talent").html(data);            
	});
	Shadowbox.init({
		modal: true,
		displayNav: true
		});
	$('#dialog-talent').dialog('open');
});

$("#terms-content").dialog("destroy");
$("#terms-content").dialog({
	autoOpen: false,
	height: 529,
	width: 800,
	modal: true,
	zIndex: 996,
	close: function() {
	}
});
$('#terms').click(function(event){
	event.preventDefault();
	$('#terms-content').dialog('open');
});
$('#accept').click(function(){
	$('#acceptsTerms').attr('checked', 'checked');
	$('#terms-content').dialog('close');
});
	   
});







