jQuery(document).ready(function() {
	//Activa le carousel dil mondo
    jQuery('#mycarousel').jcarousel({
		auto: 2,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
    
    $("a[rel='ulgale']").colorbox({transition:"fade", maxWidth:640, maxHeight:480, scalePhotos:true});
    
    $('#juegos_home').innerfade({
			animationtype: 'fade',
			speed: 700,
			timeout: 4000,
			containerheight: '200px'
		});
	
	try{ //Si trae pestañas la ventana, las activo
		var allowTabChange = true;
		//Si guarda en cambio de pestaña deberia ser dentro de select:
		$('.cms_tabs').tabs({ 
			select: function(event, ui) { return allowTabChange; }, 
			show: function(event, ui) { allowTabChange = true; } 
		});
	}catch(e){}
    
    $('.cmd_contacto').button({icons:{primary:"ui-icon-mail-closed"}}).click(
    function(ev){
		ev.preventDefault(); 
		//Validaciones
		$('.contacto_gracias').hide();
		if($('#nombre').val()==''){
			$('.contacto_gracias').html('Por favor complete el nombre.');
			$('.contacto_gracias').fadeIn(1000);
			return false;
		}
		if($('#apellido').val()==''){
			$('.contacto_gracias').html('Por favor complete el apellido.');
			$('.contacto_gracias').fadeIn(1000);
			return false;
		}
		if($('#email').val()==''){
			$('.contacto_gracias').html('Por favor complete el email');
			$('.contacto_gracias').fadeIn(1000);
			return false;
		}
		
		if($('#mensaje').val()==''){
			$('.contacto_gracias').html('Por favor complete el mensaje');
			$('.contacto_gracias').fadeIn(1000);
			return false;
		}
		
		if(!isValidEmailAddress($("#email").val())){
			$('.contacto_gracias').html('Por favor complete el email con una dirección correcta.');
			$('.contacto_gracias').fadeIn(1000);
			return false;
		}
		$.ajax({
		url: "ajax.php",
		type: "POST",
		data: $('#frm_contacto').serialize(),
		dataType: 'html',
		success: function(data, textStatus, XMLHttpRequest){
			
			$('.contacto_gracias').hide().html(data).fadeIn("1000").delay("3000").fadeOut("1000");
			
		} //success
	}); //ajax
	});
	
	try{ 
		$('.anio').click(function(){
			buscaArt('articulo', $(this).attr('valor'));
		});
	}catch(e){}
	
	try{ 
		$('.anio_proyecto').click(function(){
			buscaArt('proyecto',$(this).attr('valor'));
		});
	}catch(e){}
	
	try{
		$('.cms_action_save').button({text: true,icons: { primary: 'ui-icon-check' } });
		
		$(".cms_action_save").click(function(ev){
			ev.preventDefault();
			if(!isValidEmailAddress($("#mail").val())){
				$('.mensaje').hide().html('Por favor complete el email con una dirección correcta.').fadeIn("1000").delay("3000").fadeOut("1000");
				return false;
			}
			guardaRegistro();
		});
		$(".cms_action_cancel").css('display','none');
	}catch(e){}
	
	try{
		$(".cms_action_search").button({text: false,icons: { primary: 'ui-icon-search' } });
	}catch(e){}
	
	try{
		$(".cms_action_search").click(function(ev){
			ev.preventDefault();
			window.location='listado.php?seccion=buscador&valor='+$('#cms_search').val();	
		});
	}catch(e){}
	
	try{
		$('#cms_search').keypress(function(ev) {
			if (ev.keyCode == 13) {
				window.location='listado.php?seccion=buscador&valor='+$('#cms_search').val();
			}
		});
	}catch(e){}
	
});

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function iniciar_galeria(id_galeria){
		$.ajax({
			url: "ajax.php",
			type: "POST",
			data: 'section=galeria&id_galeria='+id_galeria,
			dataType: 'html',
			success: function(data, textStatus, XMLHttpRequest){
				
				$('#oculto').html(data);
				$("a[rel='galeria1']").colorbox({transition:"fade", open:true, maxWidth:640, maxHeight:480, scalePhotos:true});
				
			} //success
		}); //ajax

}

function buscaArt($tipo, anio_art){
		$.ajax({
			url: "ajax.php",
			type: "POST",
			data: 'section='+$tipo+'&anio='+anio_art,
			dataType: 'html',
			success: function(data, textStatus, XMLHttpRequest){
				$('.mid_art').html(data);
			} //success
		}); //ajax

}

function guardaRegistro(){
		$.ajax({
			url: "ajax.php",
			type: "POST",
			data: $("#frmEdit").serialize(),
			dataType: 'html',
			success: function(data, textStatus, XMLHttpRequest){
				$('.mensaje').hide().html(data).fadeIn("1000").delay("3000").fadeOut("1000");
				$('#nombre').val('');
				$('#mail').val('');
				$('#pais').val('');
				$('#ciudad').val('');
			} //success
		}); //ajax

}

