var rss="http://leyendasdelrockfestival.blogspot.com/feeds/posts/default";
$(document).ready(function(){
	$(".noticia").each(function(i,e){
		$(this).click(function(){alert(0);});
	});
});

jQuery(function($) {
	if(isOnline())
	{		
		loadRss();
	}
	else
	{
		var div=document.getElementById("rss-feeds");
		var contenido;
		if (localStorage.getItem("feed") === null) {		
			new Toast('no hay conexion a internet',Toast.LENGTH_SHORT,0);
		}
		else if (localStorage.getItem("feed") !== null) 
		{
			contenido=localStorage.getItem("feed");
		}
		div.innerHTML=contenido;
	}
	
})
var oldContent="";
function loadRss()
{
	
	$("#rss-feeds").rss(rss, {
	  limit: -1,
	  effect: 'slideFastSynced',		  
	  entryTemplate: '<div class="noticia" id={title}><div><h2><a href="#" class="noticia_title">{title}</a></h2><p><span link={url}><img src="./img/facebook.png" class="social facebook"/><img src="./img/twitter.png" class="social twitter"/></span></p><br/><p>{body}</p></div></div>',
	  success: function(){
			var div=document.getElementById("rss-feeds");
			var contenido=div.innerHTML;			
			while(contenido.indexOf("<object")!=-1)
			{
				var obj=desc.substring(contenido.indexOf("<object"),contenido.indexOf("</object>"));				
				var data=obj.substring(obj.indexOf("data"),obj.indexOf("style")-1);
				var videoID=data.substring(data.lastIndexOf("/"),data.indexOf("?"));
				var video=" <iframe class=\"youtube-player\" type=\"text/html\" " +
						"width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/"+videoID+"\" frameborder=\"0\"></body></html>";
				contenido=contenido.replace(obj, video);
			}			
			localStorage.setItem("feed",contenido);
			setLinkClicks();			
			setH2Click();
			setSocialClicks();
		}
	  });
}
function loadDetail(id)
{	
	var div=document.getElementById("rss-feeds");	
	oldContent=div.innerHTML;

	id="#"+id;	
	var content=$(id).html();
	
	var contenido="<div class=\"noticia\">"+content+"</div><a href=\"#\" onclick='goBack()'>volver</a>";		
	div.innerHTML=contenido;
	setLinkClicks();
}
function goBack()
{
	var div=document.getElementById("rss-feeds");	
	div.innerHTML=oldContent;
	setLinkClicks();
	setH2Click();
	setSocialClicks();
}
function isOnline() {
    return navigator.onLine;
}
function setLinkClicks()
{
	$(".noticia a").each(function(i,e){
		if($(this).hasClass("noticia_title")==false)
		{
			$(this).click(function(e){
				e.preventDefault();
				var direccion =$(this).attr("href");
				if(window.MozActivity)
				{			
					var intent = new MozActivity({
						name: "view",
						data: {
							type: "url",
							url: direccion 
						}
					});
				}
				else
				{
					var win = window.open(direccion, '_blank');
					win.focus();
				}
			});
		}
	});
}
function setH2Click()
{
	$(".noticia_title").each(function(i,e){
		$(this).click(function(e){
			$(".noticia").each(function(j,e)
			{				
				if(i==j)
				{
					var attribute=$(this).attr("id");
					loadDetail(attribute);
				}
			});
			
		});					
	});			
}
function setSocialClicks()
{
	$(".facebook").each(function(i,e)
	{
		$(this).click(function(e){			
			var _url=$(this).parent().attr("link");
			$(".noticia").each(function(j,e)
			{				
				if(i==j)
				{
					var attribute=$(this).attr("id");
					var content=getDetailContent(attribute);					
					loadFB(_url,content);
				}
			});					
		});
	});
	
	$(".twitter").each(function(i,e)
	{
		$(this).click(function(e){			
			var _url=$(this).parent().attr("link");
			$(".noticia").each(function(j,e)
			{				
				if(i==j)
				{
					var attribute=$(this).attr("id");
					var content=getDetailContent(attribute);					
					loadTwitter(_url,content);
				}
			});					
		});
	});
}

function getDetailContent(id)
{	
	var div=document.getElementById("rss-feeds");	
	oldContent=div.innerHTML;

	id="#"+id;	
	var content=$(id).html();
	return content;
}


function loadTwitter(_url,content)
{
content=content.replace(/noticia[0-9]+/ig,"");
content=content.replace(/(<([^>]+)>)/ig,"");
var twtTitle  = content;
var twtUrl    = _url;
var maxLength = 140 - (twtUrl.length + 1);


if (twtTitle.length > maxLength) 
{
	twtTitle = twtTitle.substr(0, (maxLength - 3))+'...';
}

var twtLink = 'http://twitter.com/home?status='+encodeURIComponent(twtTitle + ' ' + twtUrl);
if(window.MozActivity)
{			
	var intent = new MozActivity({
		name: "view",
		data: {
			type: "url",
			url: twtLink 
		}
	});
}
else
{
	var win = window.open(twtLink, '_blank');
	win.focus();
}

}
function loadFB(_url,content)
{

content=content.replace(/noticia[0-9]+/ig,"");
content=content.replace(/(<([^>]+)>)/ig,"");
_url=encodeURIComponent(_url);
content=encodeURIComponent("Leyendas del rock");
uri=encodeURIComponent("http://www.leyendasdelrockfestival.com/");
linkFB= "https://www.facebook.com/dialog/feed?app_id=672470272806084&link="+_url+"&name="+content+"&redirect_uri="+uri;
if(window.MozActivity)
{			
	var intent = new MozActivity({
		name: "view",
		data: {
			type: "url",
			url: linkFB
		}
	});
}
else
{
	var win = window.open(twtLink, '_blank');
	win.focus();
}

}