$(document).ready(function() {
	// therror does not <3 frames
	if (top.location != location) {
		top.location.href = document.location.href;
	}
	
	$("#sidebar a.destacado").click(function(){
		try { urchinTracker("/destacado"); } catch(e){	}
	});
	
	$("#rss a.suscribete").click(function(){ return false; });
	
	// find sites
	var sites = {
		"twitter": ["http://twitter.com/therrorcom"],
		"googlereader": ["https://www.google.com/reader/view/feed/http%3A%2F%2Fwww.therror.com%2Ffeed"],
		"facebook": ["http://www.facebook.com/pages/therrorcom/124728563247"]
	};
	var feed_fav = $.cookie('feed_fav');
	if(!feed_fav){
		var sl = SocialHistory();
		var socialsites = new Array();
		if(sl.doesVisit("twitter")) socialsites.push("twitter");
		if(sl.doesVisit("googlereader")) socialsites.push("googlereader");
		if(sl.doesVisit("facebook")) socialsites.push("facebook");
		
		if(socialsites.length){
			feed_fav = socialsites[Math.ceil(Math.random()*socialsites.length)-1];
		} else {
			feed_fav = 0;
		}
		
		$.cookie('feed_fav', feed_fav, { expires:1 });
	}
	if(feed_fav && feed_fav!=0){
		$("#rss").removeAttr("class").addClass(feed_fav);
		if(sites[feed_fav]){
			$("#rss a.suscribete").unbind("click").attr("href", sites[feed_fav]);
		}
	}
	
	// acciones específicas
	if($("#rss.email").length){
		$("#rss form .inputsuscribe, #rss a.suscribete").click(function(){
			try { urchinTracker("/suscripcion/email"); } catch(e){	}
			$(this).val("");
			$("#rss form").stop(true);
			$("#rss a.suscribete").stop(true);
			$("#rss form").css({ opacity:1 }).show();
			$("#rss a.suscribete").css({ opacity:0 });
			$("#rss").unbind("mouseenter").unbind("mouseleave");
		});
		$("#rss").hover(
			function(){
				$("#rss form").stop(true, true);
				$("#rss a.suscribete").stop(true, true);
				$("#rss form").show().css({ opacity:0 }).animate({opacity:1}, 550);
				$("#rss a.suscribete").animate({opacity:0}, 550);
			},
			function(){
				$("#rss form").stop(true, true);
				$("#rss a.suscribete").stop(true, true);
				$("#rss").animate({ opacity:1 }, 1200, function(){
					if($("#rss form .inputsuscribe").val()=="tu email"){
						$("#rss form").animate({ opacity:0 }, 700, function(){ $(this).hide(); });
						$("#rss a.suscribete").animate({ opacity:1 }, 700);
					}
				});
		});
	} else {
		$("#rss a.suscribete").click(function(){
			try { urchinTracker("/suscripcion/"+$("#rss").attr("class")); } catch(e){	}
		});
	}
	
	$(".miniurl").each(function(){
		if(!$(this).find(".miniurl").length){
			$(this).mouseover(function(){
				$(this).find(".miniurl").show();
			});
			
			$(this).prepend("<span class='miniurl'></span>").find(".miniurl").hide().html($(this).attr("href")).mouseout(function(){ $(this).hide() });
		}
	});
	$(".spoiler_cont a.show").each(function(){
		$(this).toggle(function(){
			$(this).text("Ocultar spoiler");
			$(this).parents(".spoiler_cont:eq(0)").find(".spoiler:eq(0)").show();
		}, function(){
			$(this).text("Mostrar spoiler");
			$(this).parents(".spoiler_cont:eq(0)").find(".spoiler:eq(0)").hide();
		});
		
		$(this).prepend("<span class='miniurl'></span>").find(".miniurl").hide().html($(this).attr("href")).mouseout(function(){ $(this).hide() });
	});
    
	if($(".ajaxbasicaction").length){
		$(".ajaxbasicaction").each(function(){
			$(this).click(function(){
				if(!$(this).find("i.ajaxholder").length){ $(this).append('<i class="ajaxholder"></i>') }
				var ajaxurl = $(this).attr("alt");
				$(this).find("i.ajaxholder").load(ajaxurl);
			});
		});
	}
});
