$(function() {
		
	var wordpress_url_feed = "http://feeds.feedburner.com/ThePinkerTonesWordpress?format=xml";
	
	var $noticiaSeccionBlog		= $('#wordpressHtml');
	var $noticiaTitulo			= $('#wordpressTituloSpan');
	var $noticiaFecha			= $('#wordpressFechaSpan');
	var $noticiaCategorias		= $('#wordpressCategoriesSpan');
	var $noticiasWordpress		= $('#wordpressNoticiasList');	
	
	var noticiaSeleccionada	= 0;
	
	var itemsFeed;
	var feed;
	
	$('#blog').toggle(function(){
		cargarMenuNoticias();
		cargarSeccionBlog();
	},function(){
		
	});
	
	$(document).ready(function() {
		feed = new google.feeds.Feed(wordpress_url_feed);
		
		google.setOnLoadCallback(initializeFeed);
	});
	
	function initializeFeed(){
		feed.setNumEntries(10);
		
		feed.load(function(xml){
	        if (!xml.error) {
				itemsFeed = xml.feed;
			}
		});
	}
	
	function cargarMenuNoticias(){
		
		$noticiasWordpress.fadeOut("0", "linear");
		$noticiasWordpress.empty();
		
		for(var i = 0; i < itemsFeed.entries.length; i++){
			var noticia = itemsFeed.entries[i];
			
			var titulo = noticia.title;
			var fecha = noticia.publishedDate.substring(5, 16);
			var link = noticia.link;
			
			$noticiasWordpress.append("<li id='noticiaLi" + i + "'><div class='thumbNoticia' id='noticia" + i + "'></div><span class='tituloNoticias'>" + titulo + "</span><br/><span class='fechaNoticias'> " + fecha + "</span></li>");
		}
		
		cargarThumbsNoticias();
		
		$noticiasWordpress.fadeIn("100", "linear");
	}
	
	function cargarThumbsNoticias(){
		
		for(var i = 0; i < itemsFeed.entries.length; i++){
			var noticia = itemsFeed.entries[i];
			
			if (noticia.mediaGroups[0].contents[1] != null){
			
				var urlImg = noticia.mediaGroups[0].contents[1].url;
				
				$imagen = $('<img />');
				
				$imagen.data({
					'index'			:i
				});
				
				$imagen.load(function(){
					var $this = $(this);
					
					resizeWordpressNoticias($this);
					
					$("#noticia" + $this.data('index')).append($this);
					
					$this.bind('click', function(e){
						if (noticiaSeleccionada != $this.data('index')){
							$("li#noticiaLi" + noticiaSeleccionada + " > span.tituloNoticias").css("color","#000000");
							noticiaSeleccionada = $this.data('index');
							cargarSeccionBlog();
						}
					});
					
				}).attr('src', urlImg);
			}
		}
		
	}
	
	function cargarSeccionBlog(){
		$noticiaSeccionBlog.fadeOut(0, "linear");
		$noticiaTitulo.fadeOut(0, "linear");
		$noticiaFecha.fadeOut(0, "linear");
		$noticiaCategorias.fadeOut(0, "linear");
		
		var noticia = itemsFeed.entries[noticiaSeleccionada]
						
		var titulo = noticia.title;
		
		var descripcion = noticia.contentSnippet;
		var html = noticia.content;
		var fecha = noticia.publishedDate.substring(5, 16);
		var categoriasTxt = categorizar(noticia.categories);
		var link = noticia.link;
		
		if (noticia.mediaGroups[0].contents[1] != null){
			var urlImg = noticia.mediaGroups[0].contents[1].url;
			
			$('<img />').load(function(){
				var $this = $(this);
			}).attr('src', urlImg);
		}
		
		var newHtml = tratamosHtmlBuscarLinks(html);
				
		$noticiaSeccionBlog.empty().html(newHtml);
		
		$noticiaTitulo.empty().html(titulo);
		$noticiaFecha.empty().html(fecha);
		//$noticiaCategorias.empty().html(categoriasTxt);
		
		$noticiaSeccionBlog.fadeIn(200, "linear");
		$noticiaTitulo.fadeIn(200, "linear");
		$noticiaFecha.fadeIn(200, "linear");
		$noticiaCategorias.fadeIn(200, "linear");
	
		$("li#noticiaLi" + noticiaSeleccionada + " > span.tituloNoticias").css("color","#C51C23");
	
	}
	
	function categorizar(categorias){
		var result = "";
		
		for (var i = 0; i < categorias.length; i++){
			if (i == 0){
				var categoriasTxtAux = "";
				if (idiomaActual == "castellano"){
					categoriasTxtAux = "Categor&iacute;as";
				} else if (idiomaActual == "english") {
					categoriasTxtAux = "Categories";
				} else {
					categoriasTxtAux = "Categories";
				}
				
				result += "<strong><span class='negro'>" + categoriasTxtAux + "</span></strong>: ";
			}
			result += categorias[i];
			if (i < categorias.length - 1){
				result += ", ";
			}				
		}
		
		return result;
	}
	
	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 "Add a Comment";
		result = result.replace(/rel="nofollow"/gi, "rel=\"nofollow\" class=\"esconder\"");
		
		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;
		var marginW = (sobranteW / 2);
		var marginH = (sobranteH / 2);
		
		$image.css({'width':theImage.width+'px','height':theImage.height+'px'});
		$image.css({'margin-left':marginW+'px','margin-top':'12px'});
	}
	
	function resizeWordpressNoticias($image){
		var widthMargin	 = 0; 
		var windowW      = 100 - widthMargin;
		var heightMargin = 0;
		var windowH      = 100 - 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;
		var marginW = (sobranteW / 2);
		var marginH = (sobranteH / 2);
		
		$image.css({'width':theImage.width+'px','height':theImage.height+'px'});
		$image.css({'margin-left':marginW+'px','margin-top':'0px'});
	}

});
