$(document).ready(function(){

	$('.desarrollador a').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

	$('#socialnetworks a').hover (
		function() {
		$(this).stop().animate({marginTop: "10px"},200)
		},
		function() {
		$(this).stop().animate({marginTop: "2px"},200)
		}
	);

	var transparente = 0.5, opaco = 0.9, duracion = 250;
		//.css('opacity',transparente)
		$('#platform li').hover(function() {
			if (!$(this).hasClass('plataforma')) {$(this).fadeTo(duracion,opaco)};
		}, function() {
			if (!$(this).hasClass('plataforma')) {$(this).fadeTo(duracion,transparente)};
		}
	);

	// $('a[rel*=popup]').fancybox();

	$("#altaprograma").fancybox({
		'width'				: '50%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	$("#enlace_roto").fancybox({
		'width'				: '50%',
		'height'			: '50%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	$(".puntuacion a[rel='popup']").fancybox({
		'width'				: 700,
		'height'			: 420,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'showNavArrows'  	: false
	});

	$(".program a[rel='popup']").fancybox({
		'width'				: 700,
		'height'			: 420,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'showNavArrows'  	: false
	});

	$('.add_favorito').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/favorito", {id_programa: $(this).attr('rel')});
		$(this).parent().show();
		return false;
	})

	$('div.error a').click( function(){
		$(this).parent().slideUp('100');
	});

	$('p.notice a').click( function(){
		$(this).parent().parent().slideUp('100');
		return false;
	});

	$("#imagenes a").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	$(".imagenes a[rel^=prettyPhoto]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

/*
	$(".imagen_fancy").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type' : 'iframe',
		'autoDimensions' : true,
		'showNavArrows'  	: false
	});
*/

        $(".appvideo").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'type'			: 'iframe',
		'width'         	: 517,
		'height'        	: 540,
		'showNavArrows'  	: false
	});


	$(".imagen_html").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'	: 'elastic',
		'type'				: 'iframe',

		'autoScale'			: true,
		'autoDimensions'  : true,
		'showNavArrows'  	: true,
		'showCloseButton' : true

	});


	jQuery.fn.fadeToggle = function(speed, easing, callback) {
	   return this.animate({opacity: 'toggle'}, speed, easing, callback);

	};
	$("#status a.login").click(function (event) {
			event.preventDefault();
			//alert('Big blue alabaaaama');
			$("#login").fadeToggle()
        });

	$('.enviar').click(function(){
		$(this).parents('form').submit();
		return false;
	})

	/*
	$('.anadir').click( function() {
		return false;
	})*/


	$('.anadir').hover(
		function() {
			$('ul',this).stop(true, true).slideDown('50');
			return false;
		},
		function() {
			$('ul',this).stop(true, true).slideUp('50');
			return false;
		}
	)

	$('.compartir').hover(
		function() {
			$('ul',this).stop(true, true).slideDown('50');
			return false;
		},
		function() {
			$('ul',this).stop(true, true).slideUp('50');
		}
	)

	/*$("#status a.login").click(function() {
		$('#login').slideDown('slow');
		return false;
	})*/

	//$("#breadcrumb").jBreadCrumb();

	$('.vota_programa').click(function() {
		$('.mensaje_votar_programa').hide().load("http://"+document.domain+"/servicios/votar_programa", { id_programa: $(this).attr('rel')});
		$('.mensaje_votar_programa').fadeIn('slow');
		return false;
	})

	$('.truco .megusta').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_truco", {id_truco: $(this).attr('rel'), voto: 0});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.truco .nomegusta').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_truco", {id_truco: $(this).attr('rel'), voto: 1});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.votar_buena_respuesta').live('click',function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_respuesta", {id_respuesta: $(this).attr('rel'), voto: 0});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.votar_mala_respuesta').live('click',function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_respuesta", {id_respuesta: $(this).attr('rel'), voto: 1});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.comentario .megusta').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_comentario", {id_comentario: $(this).attr('rel'), voto: 0});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.comentario .nomegusta').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/votar_comentario", {id_comentario: $(this).attr('rel'), voto: 1});
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.solicitud_amistad').click(function() {
		if(confirm("¿Realmente quieres enviar una solicitud de amistad a esta persona?")) {
			$(this).parent().hide();
			$(this).parent().load("http://"+document.domain+"/servicios/solicitar_amistad", {id_usuario: $(this).attr('rel')});
			$(this).parent().fadeIn('slow');
		}
		return false;
	})

	$('.solicitud_amistad_popup').fancybox({
		'width'				: 450,
		'height'			: 205,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	$('.mas_respuestas_duda').click(function() {
		//Mostramos las respuestas
		$.post("http://"+document.domain+"/servicios/mas_respuestas_duda",{ id_duda:$(this).attr('rel'), primer_registro:$('.contenedor_respuestas_mostradas').text()} ,function(data)
		{
			$('.contenedor_respuestas_duda').append(data);
		});
		//Actualizamos la informacion de paginación.
		$.post("http://"+document.domain+"/servicios/numero_respuestas_pagina",{ id_duda:$(this).attr('rel'), primer_registro:$('.contenedor_respuestas_mostradas').text()} ,function(data)
		{
			if (data==0) {
				//Si no hay mas respuestas, eliminamos el enlace de la paginación.
				$('.mas_respuestas_duda').remove();
			} else {
				//Si hay más respuestas, actualizamos los valores para la paginación.
				var respuestas_mostradas=parseInt($('.contenedor_respuestas_mostradas').text())+parseInt(data);
				$('.contenedor_respuestas_mostradas').text(respuestas_mostradas);
			}
		});
		return false;
	})

	$('.hacer_comentario').live('click',function() {
		$('.contenedor_enviar_comentario').hide().load("http://"+document.domain+"/servicios/mostrar_formulario_comentario", {url_formulario:$(this).attr('rel')});
		$('.contenedor_enviar_comentario').fadeIn('slow');
		return false;
	})

	$('.cancelar_comentario').live('click',function() {
		/*$('.contenedor_enviar_comentario').hide().load("<?=base_url()?>/servicios/cancelar_comentario", {url_formulario:$(this).attr('rel')});
		$('.contenedor_enviar_comentario').fadeIn('slow');*/
		$('.contenedor_enviar_comentario').fadeOut();
		return false;
	})

	$('.enviar_truco').click(function() {
		$('.contenedor_enviar_truco').hide().load("http://"+document.domain+"/servicios/mostrar_formulario_truco", {url_formulario:$(this).attr('href')});
		$('.contenedor_enviar_truco').fadeIn('slow');
		return false;
	})

	$('.cancelar_truco').live('click',function() {
		/*$('.contenedor_enviar_truco').hide().load("<?=base_url()?>/servicios/cancelar_truco", {url_formulario:$(this).attr('rel')});
		$('.contenedor_enviar_truco').fadeIn('slow');*/
		$('.contenedor_enviar_truco').fadeOut();
		return false;
	})

	$('.enviar_duda').live('click',function() {
		$('.contenedor_enviar_duda').hide().load("http://"+document.domain+"/servicios/mostrar_formulario_duda", {url_formulario:$(this).attr('href')});
		$('.contenedor_enviar_duda').fadeIn('slow');
		return false;
	})

	$('.cancelar_duda').live('click',function() {
		$('.contenedor_enviar_duda').fadeOut();
		return false;
	})

	$('.responder_duda').live('click',function() {
		$('.contenedor_enviar_respuesta_duda').hide().load("http://"+document.domain+"/servicios/mostrar_formulario_respuesta_duda", {url_formulario:$(this).attr('href')});
		$('.contenedor_enviar_respuesta_duda').fadeIn('slow');
		return false;
	})

	$('.cancelar_respuesta_duda').live('click',function() {
		$('.contenedor_enviar_respuesta_duda').fadeOut();
		return false;
	})

	$('.mas_actividad_usuario').click(function() {
		//Mostramos las respuestas
		$.post("http://"+document.domain+"/servicios/mas_actividad_usuario",{ id_usuario:$(this).attr('rel'), primer_registro:$('.contenedor_actividades_mostradas').text()} ,function(data)
		{
			$('.contenedor_actividades').append(data);
		});
		//Actualizamos la informacion de paginación.
		$.post("http://"+document.domain+"/servicios/numero_actividades_pagina",{ id_usuario:$(this).attr('rel'), primer_registro:$('.contenedor_actividades_mostradas').text()} ,function(data)
		{
			if (data==0) {
				//Si no hay mas actividades, eliminamos el enlace de la paginación.
				$('.mas_actividad_usuario').remove();
			} else {
				//Si hay más actividades, actualizamos los valores para la paginación.
				var actividades_mostradas=parseInt($('.contenedor_actividades_mostradas').text())+parseInt(data);
				$('.contenedor_actividades_mostradas').text(actividades_mostradas);
			}
		});
		return false;
	})

	$('.anadir ul a').click(function() {
		$(this).parent().parent().parent().hide().load("http://"+document.domain+"/servicios/add_programa_zona", {id_programa:$('.tematica_programa').attr('rel'), id_zona:$(this).attr('rel')});
		$(this).parent().parent().parent().fadeIn('slow');
		return false;
	})

	$('.zona_tematica_pertenece').click(function() {
		var zona=$(this);
		var confirma=confirm('¿Desea dejar de pertenecer a esta zona temática?');
		if (confirma) {
			$.post("http://"+document.domain+"/servicios/dejar_zona_tematica", {id_zona:$(this).attr('rel')}, function(data)
			{
				if (data==0) {
					zona.parent().parent().remove();
				} else {
					zona.parent().parent().prepend(data);
				}
			})
		}
		return false;
	})

	$('.nologeado').click(function() {
		$(this).parent().hide().load("http://"+document.domain+"/servicios/mensaje_no_logeado");
		$(this).parent().fadeIn('slow');
		return false;
	})

	$('.searchbutton').click(function(){
		if (($('#search').val() != '') && ($('#search').val() != 'Buscar...')) {
			$('#search-form').submit();
		}
		return false;
	})

	$('.more_categories').click(function(){
		var parametros=$(this).attr('rel').split(',');
		$.post("http://"+document.domain+"/servicios/obtener_categorias_extra", {id_programa:parametros[0], plataforma:parametros[1]}, function(data) {
			$('#menu').append(data);
		})
		$(this).fadeOut('slow');
		return false;
	})

        $('#bcategoria').change(
            function (){
			   $('<em class="loading">Cargando...</em>').appendTo($('#bsubcategoria').parent());
               $('#div_subcategoria').show();
               $('#div_tipo').hide();
               $('#div_subtipo').hide();
               $('#bsubcategoria').load("http://www.programas.com/servicios/obtener_sucategorias_avanzado",{id_cat:$(this).val()}, function(){ $('.loading').text('Carga completa').hide(500) });

            }
         );

          $('#bsubcategoria').change(
          function (){
			$('<em class="loading">Cargando...</em>').appendTo($('#btipo').parent());
            $('#div_subtipo').hide();
            $('#btipo').load("http://www.programas.com/servicios/obtener_tipos_avanzado",{id_subcat:$(this).val()}, function() {

               var num = $('#btipo option').size();

               if ( num < 2) {
                  $('#div_tipo').hide();
                   $('.loading').remove();
               }
               else{
                  $('#div_tipo').show();
                  $('.loading').text('Carga completa').hide(500).remove();
               }
            });
         }
      );

      $('#btipo').change(
           function (){
             $('#div_subtipo').show();
             $('<em class="loading">Cargando...</em>').appendTo($('#bsubtipo').parent());
             $('#bsubtipo').load("http://www.programas.com/servicios/obtener_subtipos_avanzado",{id_subtipo:$(this).val()}, function() {

               var num = $('#bsubtipo option').size();

               if ( num < 2) {
                  $('#div_subtipo').hide();
                  $('.loading').remove();
               }
               else{
                  $('#div_subtipo').show();
                   $('.loading').text('Carga completa').hide(500).remove();
               }
            });
           }
      );
});

