$(function() {
	$('#news').toggle(function(){
		
	},function(){
		
	});
});

//feed english
$(function() {
	
	$('#idiomaEnglish').click(function(){
		$("#noticiasCastellano").css({'display':'none'});
		$("#noticiasCatala").css({'display':'none'});
		$("#noticiasSeccionCastellano").css({'display':'none'});
		$("#noticiasSeccionCatala").css({'display':'none'});
		initializeHomeEnglish();
	});
	
	//seccion home english//
	var $noticiaHomeContainerEnglish = $('#noticiasEnglish .widgetWordpressImagen');
	var $noticiaHomeTitleEnglish	= $('#noticiasEnglish .noticiatitle');
	var $noticiaHomeDescEnglish		= $('#noticiasEnglish .noticiadesc');
	var $noticiaHomeFechaEnglish	= $('#noticiasEnglish .noticiafecha');
	
	var feedEnglish;
	var itemsFeedEnglish;
	
	var wordpress_url_feed_english = "http://thepinkertonesnews.wordpress.com/feed/?callback=?";
	
	var $noticiasSeccionNoticias = $('#noticiasSeccionEnglish .noticiasSeccionNoticias');
	
	$(document).ready(function() {
		feedEnglish = new google.feeds.Feed(wordpress_url_feed_english);		
		google.setOnLoadCallback();
	});
	
	function initializeHomeEnglish(){
		feedEnglish.setNumEntries(1);
		
		feedEnglish.load(function(xml){
	        if (!xml.error) {
				itemsFeedEnglish = xml.feed;
				
				//console.log("english feed --> ", itemsFeedEnglish);
				
				var noticia = itemsFeedEnglish.entries[0];
				
				var titulo = noticia.title;
				var descripcion = noticia.contentSnippet;
				var fecha = noticia.publishedDate.substring(5, 16);
				
				var urlImg = '';
				if (noticia.mediaGroups[0].contents[1] != null) {
					urlImg = noticia.mediaGroups[0].contents[1].url;
				} else if (noticia.mediaGroups[0].contents[0] != null) {
					urlImg = noticia.mediaGroups[0].contents[0].url;
				}
				
				var link = noticia.link;
				
				if (urlImg != ''){
				
					$('<img />').load(function(){
						var $this = $(this);
						
						resizeWordpress($this);
						
						$noticiaHomeContainerEnglish.empty().append($this);
						
						$this.bind('click', function(e){
							$("#contenido").fadeOut("400", "linear");
							$("li#newsLi > a").click();
						});
						
						$noticiaHomeTitleEnglish.empty().html("<br/>" + titulo);
						$noticiaHomeDescEnglish.empty().html(descripcion);
						$noticiaHomeFechaEnglish.empty().html(fecha);
						
						$('#noticiasEnglish').fadeIn("200", "linear");
						$noticiaHomeContainerEnglish.fadeIn("200", "linear");
						$noticiaHomeTitleEnglish.fadeIn("200", "linear");
						$noticiaHomeDescEnglish.fadeIn("200", "linear");
						$noticiaHomeFechaEnglish.fadeIn("200", "linear");
						
					}).attr('src', urlImg);
				
				}
				
				cargarSeccionNoticiasEnglish();
				
			}
		});
	}
	
	function cargarSeccionNoticiasEnglish(){
		
		feedEnglish.setNumEntries(100);
		
		feedEnglish.load(function(xml){
	        if (!xml.error) {
				itemsFeedEnglish = xml.feed;
		
				$noticiasSeccionNoticias.html("");
				
				for(var i = 0; i < itemsFeedEnglish.entries.length; i++){
					var noticia = itemsFeedEnglish.entries[i];			
					
					var titulo = noticia.title;
									
					var descripcion = noticia.contentSnippet;
					var html = noticia.content;
					var fecha = noticia.publishedDate.substring(5, 16);
					var link = noticia.link;
					
					var newHtml = tratamosHtmlBuscarLinks(html);			
					
					var htmlNoticia = "<div class=\"noticiasTitulo\"><span class=\"noticiasTituloSpan\">" + titulo + "</span></div>" +
									  "<div class=\"noticiasFecha\"><span class=\"noticiasFechaSpan\">" + fecha +"</span></div>" +
									  "<div class=\"noticiasHtml\">" + newHtml + "</div>";
					$noticiasSeccionNoticias.append(htmlNoticia);
					
					if (i < itemsFeedEnglish.entries.length-1){			
						$noticiasSeccionNoticias.append("<div class=\"noticiasSeparador\"></div>");
					}
						
				}
			
				$('#noticiasSeccionEnglish').fadeIn("200", "linear");
			}
		});
	}
	
});

//feed catala
$(function() {
	
	$('#idiomaCatala').click(function(){
		$("#noticiasCastellano").css({'display':'none'});
		$("#noticiasEnglish").css({'display':'none'});
		$("#noticiasSeccionCastellano").css({'display':'none'});
		$("#noticiasSeccionEnglish").css({'display':'none'});
		initializeHomeCatala();
	});
	
	//seccion home catala//
	var $noticiaHomeContainerCatala = $('#noticiasCatala .widgetWordpressImagen');
	var $noticiaHomeTitleCatala	= $('#noticiasCatala .noticiatitle');
	var $noticiaHomeDescCatala	= $('#noticiasCatala .noticiadesc');
	var $noticiaHomeFechaCatala	= $('#noticiasCatala .noticiafecha');
	
	var feedCatala;
	var itemsFeedCatala;
	
	var wordpress_url_feed_catala = "http://thepinkertonesnoticies.wordpress.com/feed/?callback=?";
	
	var $noticiasSeccionNoticias = $('#noticiasSeccionCatala .noticiasSeccionNoticias');		
	
	$(document).ready(function() {
		feedCatala = new google.feeds.Feed(wordpress_url_feed_catala);		
		google.setOnLoadCallback();
	});
	
	function initializeHomeCatala(){
		feedCatala.setNumEntries(1);
		
		feedCatala.load(function(xml){
	        if (!xml.error) {
				itemsFeedCatala = xml.feed;
				
				//console.log("catala feed --> ", itemsFeedCatala);
				
				var noticia = itemsFeedCatala.entries[0];
				
				var titulo = noticia.title;
				var descripcion = noticia.contentSnippet;
				var fecha = noticia.publishedDate.substring(5, 16);
				
				var urlImg = '';
				if (noticia.mediaGroups[0].contents[1] != null) {
					urlImg = noticia.mediaGroups[0].contents[1].url;
				} else if (noticia.mediaGroups[0].contents[0] != null) {
					urlImg = noticia.mediaGroups[0].contents[0].url;
				}
				
				var link = noticia.link;
				
				if (urlImg != ''){
				
					$('<img />').load(function(){
						var $this = $(this);
						
						resizeWordpress($this);
						
						$noticiaHomeContainerCatala.empty().append($this);
						
						$this.bind('click', function(e){
							$("#contenido").fadeOut("400", "linear");
							$("li#newsLi > a").click();
						});
						
						$noticiaHomeTitleCatala.empty().html("<br/>" + titulo);
						$noticiaHomeDescCatala.empty().html(descripcion);
						$noticiaHomeFechaCatala.empty().html(fecha);
						
						$('#noticiasCatala').fadeIn("200", "linear");
						$noticiaHomeContainerCatala.fadeIn("200", "linear");
						$noticiaHomeTitleCatala.fadeIn("200", "linear");
						$noticiaHomeDescCatala.fadeIn("200", "linear");
						$noticiaHomeFechaCatala.fadeIn("200", "linear");
						
					}).attr('src', urlImg);
					
				}
				
				cargarSeccionNoticiasCatala();
			}
		});
	}
	
	function cargarSeccionNoticiasCatala(){
		
		feedCatala.setNumEntries(100);
		
		feedCatala.load(function(xml){
	        if (!xml.error) {
				itemsFeedCatala = xml.feed;
		
				$noticiasSeccionNoticias.html("");
				
				for(var i = 0; i < itemsFeedCatala.entries.length; i++){
					var noticia = itemsFeedCatala.entries[i];			
					
					var titulo = noticia.title;
									
					var descripcion = noticia.contentSnippet;
					var html = noticia.content;
					var fecha = noticia.publishedDate.substring(5, 16);
					var link = noticia.link;
					
					var newHtml = tratamosHtmlBuscarLinks(html);			
					
					var htmlNoticia = "<div class=\"noticiasTitulo\"><span class=\"noticiasTituloSpan\">" + titulo + "</span></div>" +
									  "<div class=\"noticiasFecha\"><span class=\"noticiasFechaSpan\">" + fecha +"</span></div>" +
									  "<div class=\"noticiasHtml\">" + newHtml + "</div>";
					$noticiasSeccionNoticias.append(htmlNoticia);
					
					if (i < itemsFeedCatala.entries.length-1){			
						$noticiasSeccionNoticias.append("<div class=\"noticiasSeparador\"></div>");
					}
						
				}
			
				$('#noticiasSeccionCatala').fadeIn("200", "linear");
			}
		});
	}
	
});

//feed castellano
$(function() {
	
	$('#idiomaCastellano').click(function(){
		$("#noticiasEnglish").css({'display':'none'});
		$("#noticiasCatala").css({'display':'none'});
		$("#noticiasSeccionEnglish").css({'display':'none'});
		$("#noticiasSeccionCatala").css({'display':'none'});
		initializeHomeCastellano();
	});
	
	//seccion home castellano//
	var $noticiaHomeContainerCastellano = $('#noticiasCastellano .widgetWordpressImagen');
	var $noticiaHomeTitleCastellano		= $('#noticiasCastellano .noticiatitle');
	var $noticiaHomeDescCastellano		= $('#noticiasCastellano .noticiadesc');
	var $noticiaHomeFechaCastellano		= $('#noticiasCastellano .noticiafecha');
	
	var feedCastellano;
	var itemsFeedCastellano;
	
	var wordpress_url_feed_castellano = "http://thepinkertonesnoticias.wordpress.com/feed/?callback=?";
	
	var $noticiasSeccionNoticias = $('#noticiasSeccionCastellano .noticiasSeccionNoticias');		
	
	$(document).ready(function() {
		feedCastellano = new google.feeds.Feed(wordpress_url_feed_castellano);		
		google.setOnLoadCallback();
	});
	
	function initializeHomeCastellano() {
		feedCastellano.setNumEntries(1);
		
		
		
		feedCastellano.load(function(xml){
	        if (!xml.error) {
				itemsFeedCastellano = xml.feed;
				
				//console.log("castellano feed --> ", itemsFeedCastellano);
				
				var noticia = itemsFeedCastellano.entries[0];
				
				var titulo = noticia.title;
				var descripcion = noticia.contentSnippet;
				var fecha = noticia.publishedDate.substring(5, 16);
				
				var urlImg = '';
				if (noticia.mediaGroups[0].contents[1] != null) {
					urlImg = noticia.mediaGroups[0].contents[1].url;
				} else if (noticia.mediaGroups[0].contents[0] != null) {
					urlImg = noticia.mediaGroups[0].contents[0].url;
				}
				
				var link = noticia.link;
				
				if (urlImg != ''){
				
					$('<img />').load(function(){
						var $this = $(this);
						
						resizeWordpress($this);
						
						$noticiaHomeContainerCastellano.empty().append($this);
						
						$this.bind('click', function(e){
							$("#contenido").fadeOut("400", "linear");
							$("li#newsLi > a").click();
						});
						
						$noticiaHomeTitleCastellano.empty().html("<br/>" + titulo);
						$noticiaHomeDescCastellano.empty().html(descripcion);
						$noticiaHomeFechaCastellano.empty().html(fecha);
						
						$('#noticiasCastellano').fadeIn("200", "linear");
						$noticiaHomeContainerCastellano.fadeIn("200", "linear");
						$noticiaHomeTitleCastellano.fadeIn("200", "linear");
						$noticiaHomeDescCastellano.fadeIn("200", "linear");
						$noticiaHomeFechaCastellano.fadeIn("200", "linear");
					
					}).attr('src', urlImg);
				
				}
				
				cargarSeccionNoticiasCastellano();
				
			}
        });

    }
	
	function cargarSeccionNoticiasCastellano(){
		
		feedCastellano.setNumEntries(100);
		
		feedCastellano.load(function(xml){
	        if (!xml.error) {
				itemsFeedCastellano = xml.feed;
		
				$noticiasSeccionNoticias.html("");
				
				for(var i = 0; i < itemsFeedCastellano.entries.length; i++){
					var noticia = itemsFeedCastellano.entries[i];			
					
					var titulo = noticia.title;
									
					var descripcion = noticia.contentSnippet;
					var html = noticia.content;
					var fecha = noticia.publishedDate.substring(5, 16);
					var link = noticia.link;
					
					var newHtml = tratamosHtmlBuscarLinks(html);			
					
					var htmlNoticia = "<div class=\"noticiasTitulo\"><span class=\"noticiasTituloSpan\">" + titulo + "</span></div>" +
									  "<div class=\"noticiasFecha\"><span class=\"noticiasFechaSpan\">" + fecha +"</span></div>" +
									  "<div class=\"noticiasHtml\">" + newHtml + "</div>";
					$noticiasSeccionNoticias.append(htmlNoticia);
					
					if (i < itemsFeedCastellano.entries.length-1){			
						$noticiasSeccionNoticias.append("<div class=\"noticiasSeparador\"></div>");
					}
						
				}
				
				$('#noticiasSeccionCastellano').fadeIn("200", "linear");
				
			}
		});
	}
	
});

function tratamosHtmlBuscarLinks(html){
	var result = "";
	var htmlLinksEspacios = "";
	var htmlLinksOtros = "";
	
	var partesEspacio = html.split(" http://");
	if (partesEspacio.length > 0) {
	
		htmlLinksEspacios += partesEspacio[0];
		
		for (var i = 0; i < partesEspacio.length; i = i + 1){
			if (i <  partesEspacio.length - 1) {
				//buscamos el primer símbolo no esperado en un link: o un espacio o un '<'
				var j = 0;
				var nuevoLink = "";
				for (j; j < partesEspacio[i+1].length; j++){
					
					if (partesEspacio[i+1].charAt(j) == " " || partesEspacio[i+1].charAt(j) == "<"){
						nuevoLink =  " <a href='http://" + partesEspacio[i+1].substring(0, j) + "' target='_blank'>http://" + partesEspacio[i+1].substring(0, j) + "</a>";
						break;
					}
				}
				
				if (nuevoLink != "" || j == partesEspacio[i+1].length){
					if (nuevoLink != "") {
						htmlLinksEspacios += nuevoLink + partesEspacio[i+1].substring(j, partesEspacio[i+1].length);
					} else {
						htmlLinksEspacios += " <a href='http://" + partesEspacio[i+1].substring(0, partesEspacio[i+1].length) + "' target='_blank'>http://" + partesEspacio[i+1].substring(0, partesEspacio[i+1].length) + "</a>";  
					}
				}
			} 
			
		}
		
	} else {
		htmlLinksEspacios = html;
	}
	
	var partesOtros = htmlLinksEspacios.split(">http://");
	if (partesOtros.length > 0) {
	
		htmlLinksOtros += partesOtros[0];
		
		for (var i = 0; i < partesOtros.length; i = i + 1){
			if (i <  partesOtros.length - 1) {
				//buscamos el primer símbolo no esperado en un link: o un espacio o un '<'
				var j = 0;
				var nuevoLink = "";
				for (j; j < partesOtros[i+1].length; j++){
					
					if (partesOtros[i+1].charAt(j) == " " || partesOtros[i+1].charAt(j) == "<"){
						nuevoLink =  " <a href='http://" + partesOtros[i+1].substring(0, j) + "' target='_blank'>http://" + partesOtros[i+1].substring(0, j) + "</a>";
						break;
					}
				}
				
				if (nuevoLink != "" || j == partesOtros[i+1].length){
					if (nuevoLink != "") {
						htmlLinksOtros += nuevoLink + partesOtros[i+1].substring(j, partesOtros[i+1].length);
					} else {
						htmlLinksOtros += " <a href='http://" + partesOtros[i+1].substring(0, partesOtros[i+1].length) + "' target='_blank'>http://" + partesOtros[i+1].substring(0, partesOtros[i+1].length) + "</a>";  
					}
				}
			} 
			
		}
		
	} else {
		htmlLinksOtros = htmlLinksEspacios;
	}
	
	result = htmlLinksOtros;
	
	//quitamos el "Filed under";
	var partesFiledUnder = result.split("<br>Filed under:");
	if (partesFiledUnder.length > 0) {
		result = partesFiledUnder[0];
	} 
			
	return result;
}

function resizeWordpress($image){
	var widthMargin	 = 0; 
	var windowW      = 260 - widthMargin;
	var heightMargin = 0;
	var windowH      = 320 - heightMargin;		
	var theImage     = new Image();
	theImage.src     = $image.attr("src");
	var imgwidth     = theImage.width;
	var imgheight    = theImage.height;
	
	if((imgwidth > windowW)||(imgheight > windowH)){
		if(imgwidth > imgheight){
			var newwidth 	= windowW;
			var ratio 		= imgwidth / windowW;
			var newheight	= imgheight / ratio;
			theImage.height = newheight;
			theImage.width	= newwidth;
			if(newheight>windowH){
				var newnewheight= windowH;
				var newratio 	= newheight/windowH;
				var newnewwidth = newwidth/newratio;
				theImage.width 	= newnewwidth;
				theImage.height	= newnewheight;
			}
		}
		else{
			var newheight = windowH;
			var ratio = imgheight / windowH;
			var newwidth = imgwidth / ratio;
			theImage.height = newheight;
			theImage.width= newwidth;
			if(newwidth>windowW){
				var newnewwidth = windowW;
				var newratio = newwidth/windowW;
				var newnewheight =newheight/newratio;
				theImage.height = newnewheight;
				theImage.width= newnewwidth;
			}
		}
	}
	
	//para calcular el margin que debemos dejar para centrar la imagen
	var sobranteW = windowW - theImage.width;
	var sobranteH = windowH - theImage.height;
	//centrado
	//var marginW = (sobranteW / 2);
	//izquierda
	var marginW = 0;
	var marginH = (sobranteH / 2);
	
	//$image.css({'width':theImage.width+'px','height':theImage.height+'px'});
	$image.css({'width':'260px'});
	$image.css({'margin-left':marginW+'px','margin-top':'12px'});
}
