$(document).ready(function()
{
	var cacheImage = document.createElement('img');
	cacheImage.src = 'http://www.usuariodeinternet.es/micro/cargando-pag.png';
	
	if(document.location.href != 'http://micro.usuariodeinternet.es/ask' && !cookies('leer'))
	{
		$('.footer #pag_sig').depagify({container: '.container', filter: '.contenido', trigger: '#pieabajo', request: function(event, options) {
				$('.footer').fadeOut('fast',function() {
					$('#cargando_pag').fadeIn('fast');
  			});
			}, success: function(event, options) {
				$('#cargando_pag').hide();
				if(typeof _gaq != 'undefined')
				{
					var url = options.url.replace('http://micro.usuariodeinternet.es','');
					_gaq.push(['_trackPageview',url]);
				}
			}, effect: function() {
				$(this).fadeIn();
			}
		});
	}

	$('.permacompartir').live('click', function() {
		var padre = $(this).parent();
		var pe = padre.children('p');

		if(pe.length)
		{
			pe.slideToggle();
		}
		else
		{
			var url = padre.children('.permaurl').html();
			padre.append('<p><a href="#comp" onclick="compartir(\'facebook\',\''+url+'\')" title="compartir en Facebook"><img src="http://www.usuariodeinternet.es/micro/compartir-facebook.png" /></a> <a href="#comp" onclick="compartir(\'twitter\',\''+url+'\')" title="compartir en Twitter"><img src="http://www.usuariodeinternet.es/micro/compartir-twitter.png" /></a> <a href="#comp" onclick="compartir(\'meneame\',\''+url+'\')" title="compartir en Menéame"><img src="http://www.usuariodeinternet.es/micro/compartir-meneame.png" /></a> <a href="#comp" onclick="compartir(\'buzz\',\''+url+'\')" title="compartir en Google Buzz"><img src="http://www.usuariodeinternet.es/micro/compartir-google-buzz.png" /></a> <a href="#comp" onclick="compartir(\'email\',\''+url+'\')" title="compartir por Email"><img src="http://www.usuariodeinternet.es/micro/compartir-email.png" /></a></p>').children('p').slideToggle();
		}
		
		return false;
	});
	
	$('.permalink').live('hover', function (event) {
		if(event.type == 'mouseover')
		{
			$(this).addClass('permahover');
		}
		else
		{
			$(this).removeClass('permahover');
		}
 	});	
});

function compartir(servicio,url)
{
	var d = document, l=d.location, e = encodeURIComponent, w = 626, h = 350;
	
	switch(servicio)
	{
		case 'facebook':
			u = 'http://www.facebook.com/sharer.php?u='+e(url);
		break;
		
		case 'twitter':
			u = 'http://www.usuariodeinternet.es/micro/compartir.php?s=twitter&u='+e(url);
		break;
		
		case 'buzz':
			u = 'http://www.google.com/buzz/post?url='+e(url);
			w = 720;
			h = 360;
		break;
		
		case 'meneame':
			window.open('http://meneame.net/submit.php?url='+e(url));
			return;
		break;
		case 'email':
			u = 'http://www.usuariodeinternet.es/micro/compartir.php?s=email&u='+e(url);
		break;
	}
	
	a = function()
	{
		if(!window.open(u,'compartir','toolbar=0,status=0,resizable=1,width='+w+',height='+h))
		{
			l.href = u;
		}
	}

	if(/Firefox/.test(navigator.userAgent))
	{
		setTimeout(a,0);
	}
	else
	{
		a();
	}
	
	return false;

};

function paginacion()
{
	if(cookies('leer'))
	{
		// activarla
		cookies('borrar');	
	}
	else
	{
		// desactivarla
		cookies('crear');	
	}
};

function cookies(accion)
{
	switch(accion)
	{
		case 'crear':
			var date = new Date();
			// 2 semanas
			date.setTime(date.getTime()+(20160*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = "microusuario=1"+expires+"; path=/";
		break;
		
		case 'leer':
			var nameEQ = "microusuario=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++)
			{
				var c = ca[i];
				while(c.charAt(0) == ' ')
				{
					c = c.substring(1,c.length);
				}
				if(c.indexOf(nameEQ) == 0)
				{
					return c.substring(nameEQ.length,c.length);
				}
			}
			return null;
			
			case 'borrar':
				var date = new Date();
				date.setTime(date.getTime()+(-1*60*1000));
				var expires = "; expires="+date.toGMTString();
				document.cookie = "microusuario=nopaginacionautomatica"+expires+"; path=/";
			break;
		break;
	}
};

