$(document).ready(function () {
	//portada
	var html_procesa_portada = "";

	borraselect = function(){
		$('#menu-destacamos').removeClass('select');
		$('#menu-explora').removeClass('select');
		$('#menu-fedello').removeClass('select');
	}

	encheoco = function(){
		$('#contido-destacados').html('<div class="loading"><img alt="cargando..." src="/main/templates/v1/img/load.gif" /> <div>cargando...</div></div>');
	}
	
	cambiadestacado = function(){
		$('#contido-destacados').html('<div class="loading2"><img alt="cargando..." src="/main/templates/v1/img/load.gif" /> <div>cargando...</div></div>');
	}
	
	procesaPortada = function (cambia_menu) {
		$('#contido-destacados').html(html_procesa_portada);
		
		if (cambia_menu) {
			borraselect();
			cal.addClass('select');
		}
	}

	$('#menu-destacados li').click(function(){
		encheoco();

		cal = $(this);
		
		$.ajax({
		  url: $(this).find(':first').attr("href"),
		  cache: false,
		  success: function(html){
			html_procesa_portada = html;
		    setTimeout("procesaPortada(true)",200);
		  }
		});
		

     return false;
	});
	
	$('#controlador-destacados a').live('click', function() {
			cambiadestacado();
			$.ajax({
			  url: $(this).attr("href"),
			  cache: false,
			  success: function(html){
				html_procesa_portada = html;
			    setTimeout("procesaPortada()",200);
			  }
			});	
			return false;
	});

	$('#contido-destacados ul.menu-opcions a').live('click', function () {
		encheoco();
		
		$('#contido-destacados').load($(this).attr('href'));

		return false;
	})

	$('.bgustame').click(function () {
        var href = $(this).attr('href');
		var link = $(this);

        $.ajax({
                url: href,
                cache: false,
                success: function (html) {
						$('#lista-gustame').prepend(html);
						$(link).remove();
                        return true;
                }
        });

        return false;
	});
	
	$('#mais-eventos').live('click', function () {
		var offset = $('#esta-pasando li:last').attr("id");
		offset = offset.replace('date_','');
		var href = $(this).attr('href') + '?offset=' + offset;
		
		$.get(href, function(texto) {
			$('#esta-pasando').append(texto);
		});
		
		return false;
	});

	//Buscador: enviar
	$('#buscador').submit(function () {
		var input = $('input.text', this);
		var texto = $(input).val();
		texto = $.trim(texto);
		
		if (!texto || $(input).attr('defaultValue') == texto) {
			$(input).focus();
			
			return false;
		}
		
		document.location.href = $(this).attr('action') + texto;

		return false;
	});
	
	
	if($('#embedCode').length > 0){
		code = player.embed({
			width: 560,
			height: 315
			}).getEmbedCode(true); 
		$('#embedCode').val(code);
	}
	
	$('img').fixBroken();
	
	
	//Amosar/ocultar formulario para editar usuario
	$('#boton_editar_usuario').click(function() {
		$('#editar_usuario').slideToggle('normal');
		return false;
	});
});

$.fn.fixBroken = function(){
	return this.each(function(){
		var tag = $(this);
		var alt_img = '/main/templates/v1/img/process.gif';
		tag.error(function() { // this adds the onerror event to images
			tag.attr("src",alt_img); // change the src attribute of the image
			return true;
		});
	});
}

