var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(typeof ie == "undefined")
{
	var ie = $.browser.msie;
}

var pagina_actual = 1;
var id_album = 0;
var cargando_ajax = false;
var habilitar_ajax = true;

function cargar(href, obj, sentido, callback_animacion, callback_load)
{
	if(habilitar_ajax)
	{
		transicion_out(sentido, function()
		{
			cargando_ajax = true;
			setTimeout("f_cargando_ajax()", 100);
			$(obj).load(href,function()
			{
				cargado_ajax();
				if(typeof callback_load == 'function')
				{
					callback_load.call();
				}
				transicion_in(obj, callback_animacion);
			});
		}, obj);
	}
}

function transicion_out(sentido, callback, obj)
{
	var valor;
	var ancho = $(obj).outerWidth();
	if(sentido=='izq')
		valor = '-='+ancho+'px';
	else
		valor = '+='+ancho+'px';
	$(obj).parent().css('height', $(obj).outerHeight()+'px');
	$(obj).css('position','absolute').animate({ 'left': valor }, { duration:500, easing: 'easeOutCirc', complete: function()
	{	
		if(sentido=='izq')
			$(obj).css('left',ancho+'px');
		else
			$(obj).css('left','-'+ancho+'px');
			
		if(typeof callback == 'function')
		{
			callback.call();
		}
	}});
}

function transicion_in(obj, callback)
{	
	$(obj).parent().css('height', $(obj).outerHeight()+'px');
	$(obj).animate({ 'left': '0px' }, {duration:500, easing: 'easeInCirc', complete: function()
	{
		$(obj).css('position', 'static');
		$(obj).parent().css('height','auto');
		habilitar_ajax = true;
		if(typeof callback == 'function')
		{
			callback.call();
		}
	}});
}

function f_cargando_ajax()
{	
	if(cargando_ajax)
	{
		$('#cargando').show();
		$('body').css('cursor', 'wait');
	}
}

function cargado_ajax()
{
	cargando_ajax = false;
	$('#cargando').hide();
	$('body').css('cursor', 'auto');
}

function is_int(s)
{
	return (s.search(/^[0-9]+$/) == 0);
}
function preload() 
{
	var imagenes=new Array();
	var obj;
	var obj_p;
	
	a=preload.arguments;
	if(document.getElementById('imagenes_hidden_preload'))
	{
		obj_p = document.getElementById('imagenes_hidden_preload');
	}
	else
	{
		obj_p = document.createElement('div');
		obj_p.id = 'imagenes_hidden_preload';
		document.body.insertBefore(obj_p, document.body.firstChild);
	}
	
	for(i=0; i<a.length; i++)
	{
    	if (a[i].indexOf("#")!=0)
    	{
			obj = document.createElement('img');
			obj.src = a[i];
			obj_p.appendChild(obj);
    	}
    }
}

function url_encode(s)
{
	return encodeURIComponent( s ).replace( /\%20/g, '+' ).replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' );
}

function fbox_cargar(obj)
{
	$.fancybox({
		'padding' : 0,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'centerOnScroll'	: true,
		'href' : obj.href,
		'title' : obj.title
	});
	return false;
}

$(document).ready(function()
{
	
	activar_fbox();
	
	$('.radio .sin_tilde').click(function()
	{
		name = $(this).parent().children('input').attr('name');
		$('.radio input[name='+name+']').parent().children('.tilde').hide();
		$('.radio input[name='+name+']').parent().children('.sin_tilde').show();
		$(this).hide();
		$(this).parent().children('.tilde').show();
		$(this).parent().children('input').click();
	});
	
	$('.radio .sin_tilde').hover(function()
	{
		$(this).css('background-image','url(img/estructura/columna_derecha/radio_hov.jpg)');
	},function()
	{
		$(this).css('background-image','url(img/estructura/columna_derecha/radio.jpg)');
	});
	
	$('.imagen_hover').live({
		mouseover: function()
		{
			if($(this).attr('alt'))
			{	
				$(this).data('src', $(this).attr('src'));
				$(this).attr('src', $(this).attr('alt'));
			}
		},
		mouseout: function()
		{
			if($(this).attr('alt'))
				$(this).attr('src',$(this).data('src'));
		}
	});
	
	$('.imagen_hover').click(function()
	{
		if($(this).attr('href'))
			window.location.href=$(this).attr('href');
	});
	
	$('textarea').keydown(function(event)
	{
		if($(this).attr('maxlength'))
		{
			maxlength=$(this).attr('maxlength');
			if($(this).val().length >= maxlength)
				if(event.keyCode > 34 && event.keyCode < 41 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9)
					return true;
				else
					return false;
		}
	});
	
	$('textarea').keyup(function()
	{
		if($(this).attr('maxlength'))
		{
			maxlength=$(this).attr('maxlength');
			if($(this).val().length > maxlength)
				$(this).val($(this).val().substring(0,maxlength));
		}
	});
	
	$('textarea').blur(function()
	{
		if($(this).attr('maxlength'))
		{
			maxlength=$(this).attr('maxlength');
			if($(this).val().length > maxlength)
				$(this).val($(this).val().substring(0,maxlength));
		}
	});
	
	/* ##### ALEARA ########################################################################################################## */
	
	$('#encabezado .derecha .menu .buscador .input').focus(function()
	{
		$(this).css('background-image','url(img/estructura/encabezado/buscador_act.jpg)');
	});
	$('#encabezado .derecha .menu .buscador .input').blur(function()
	{
		$(this).css('background-image','url(img/estructura/encabezado/buscador.jpg)');
	});
	
	$('#buscar').click(function()
	{
		$('#form_buscador').submit();
	});
	
	$('#form_buscador').submit(function()
	{
		if($('#encabezado .derecha .menu .buscador .input').val().length<3)
		{
			alert('Tu búsqueda debe ser de al menos 3 caracteres.');
			return false;
		}
		return true;
	})
	
	$('#encabezado .listado li a').hover(function()
	{
		str = $(this).parent().parent().parent().attr('class');
		if(str != 'amupeja' && str != 'osalara' && str != 'aleara')
			str = 'aleara';
		if(! $(this).parent().hasClass('sel'))
			$(this).parent().css('background','url(img/estructura/encabezado/item_'+str+'.jpg) left center no-repeat');
	},function()
	{
		if(! $(this).parent().hasClass('sel'))
			$(this).parent().css('background','none');
	});
	
	$('#columna_derecha .imagen .video').each(function()
	{
		arriba = ($(this).parent().outerHeight() - $(this).outerHeight()) / 2;
		izquierda = ($(this).parent().outerWidth() - $(this).outerWidth()) / 2;
		$(this).css('top',arriba+'px');
		$(this).css('left',izquierda+'px');
	});
	
	if(! ie)
	{
		$('#columna_derecha .imagen img').load(function()
		{
			video = $(this).parent().find('.video');
			arriba = ($(this).outerHeight() - video.outerHeight()) / 2;
			izquierda = ($(this).outerWidth() - video.outerWidth()) / 2;
			video.css('top',arriba+'px');
			video.css('left',izquierda+'px');
		});
	}
	
	$('#recargar_agenda').click(function()
	{
		$('#agenda').load('agenda.php');
	});
	
	$('#seccion .menu li a').click(function() 
	{
		var id = $(this).attr('alt');
		
		if(id == 'delegacion_interior')
			cargar_sede_central();
		
		if(id && $('#'+id).css('display')=='none')
		{
			$('#seccion .contenido').each(function()
			{
				if($(this).css('display')!='none')
				{
					$(this).fadeOut(function()
					{
						$('#'+id).fadeIn({ useDisplay:true });
					});
				}
			});
			$('#seccion .menu li').removeClass('seleccionado');
			$(this).parent().addClass('seleccionado');
		}
	});
	
	$('#mapa_interior area').hover(function()
	{
		provincia = $(this).attr('alt');
		if(provincia)
		{
			imagen = 'img/secciones/mapa/'+provincia+'.jpg';
			$('#imagen_mapa').attr('src', imagen);
		}
	}, function()
	{
		if($(this).attr('alt'))
			$('#imagen_mapa').attr('src','img/secciones/mapa/mapa.jpg');
	});
	
	$('#mapa_interior area').click(function()
	{
		var provincia = $(this).attr('alt');
		if(provincia && $('#'+provincia).css('display')=='none')
		{
			$('#seccion .delegacion').each(function() 
			{
				if($(this).css('display')!='none')
					$(this).slideUp(function() { $('#'+provincia).slideDown();});
			});
		}
	})
	
	$('#especialistas_link').click(function()
	{
		if($('#especialistas_div').css('display')=='none')
			$('#especialistas_div').slideDown();
		else
			$('#especialistas_div').slideUp();
	});
	
	$('#columna_derecha .votar').click(function()
	{
		respuesta = $('input:checked[name=respuesta]').val();
		if(respuesta==undefined)
		{
			alert("Debes seleccionar una respuesta para votar.");
			return false;
		}
		
		$(this).attr('href', $(this).attr('href') + '&rta='+respuesta);
		return true;
	});
	
	$('#imagenes .imagen').live({
		mouseenter: function()
		{
			$(this).find('.epigrafe').animate({height: 'show'}, 100);
		},
		mouseleave: function()
		{
			$(this).find('.epigrafe').animate({height: 'hide'}, 100);
		}
	});
	
	ajustar_epigrafes();

	$('.paginador_imagenes .siguiente').click(function()
	{
		cargar('imagenes.php?p='+(pagina_actual+1)+'&a='+id_album, '#ajax', 'izq', null, function(){ajustar_epigrafes();activar_fbox();});
	});
	
	$('.paginador_imagenes .anterior').click(function()
	{
		cargar('imagenes.php?p='+(pagina_actual-1)+'&a='+id_album, '#ajax', 'der', null, function(){ajustar_epigrafes();activar_fbox();});
	});
	
});

$(window).load(function()
{
	encolar_rotar_imagen();
});

function encolar_rotar_imagen()
{
	setTimeout(rotar_imagen, 4000);
}

function rotar_imagen()
{
	$('#imagenes-rotativas').dCargar('imagenes-rotativas.php', 400, 'fadeOut', 'fadeIn', null, null, encolar_rotar_imagen);
}

function activar_fbox()
{
	$(".fbox_img").fancybox({
		'padding' : 0,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'centerOnScroll'	: true
	});
}

function cargar_sede_central()
{
	if($('#sede_central').css('display')=='none')
	{
		$('#seccion .delegacion').hide();
		$('#sede_central').show();
	}
}

function ajustar_epigrafes()
{
	$('#imagenes .imagen').each(function()
	{
		$(this).find('.epigrafe').hide();
		
		if(chrome)
			$(this).find('img').load(function() {$(this).parent().find('.epigrafe').css('width', $(this).width()+'px');});
		else
			$(this).find('.epigrafe').css('width', $(this).find('img').width()+'px');
	});
}



