/**
 * @author    Alexei Betancourt Brito
 * @copyright Copyright (c) 2010, Grupo Humidif
 * @link      http://www.humidif.com
 * @package   jQuery 1.3.2
 * @category  Plugin
 * @licence   GPL MIT
 * 
 * Plugin para el las noticias del sitio.
 */
$.humidifNews = {
	/**
	 * @description: Funcion que realiza una peticion a un script PHP para obtener un listado de noticias.
	 */	
	welcomeNews: function(){
		$.humidifAjax.getData('div#news-div','get',base_url + 'news/welcome/');
	},
	/**
	 * @description: Funcion que realiza una peticion AJAX pasando como parametros el titulo y el id de la noticia
	 */
	readOne: function(){
		// Obtenemos el titulo de la noticia que se desea leer
		url_params = window.location.pathname;
		url_array  = url_params.split("/");
		news_title = url_array[3];
		news_id    = url_array[4];
		// Si no se ha enviado por parametros alguna noticia establecemos el
		// valor de la variable $news_title como una cadena vacia.
		news_title = (news_title == '') ? '' : news_title;
		$.humidifAjax.getData('div#news-div','get',base_url + 'news/one/' + news_title + '/' + news_id + '/');
		// Asignamos los eventos a la imagen principal
		/** AGREGAR EL CODIGO DEL EVENTO DE LA IMAGEN */
	},
	readOneActions: function(){
		// Obtenemos el id de la noticia de la que va a mostrar la galeria
		url_params = window.location.pathname;
		url_array  = url_params.split("/");
		news_id    = url_array[4];
		// Definimos la propagacion de eventos para tener control de los elementos cargados mediante AJAX
		$('#news-main-image, #news-gallery-thumbs').live('click', function(){
			action = ($(this).is('#news-gallery-thumbs')) ? $.humidifNews.showGallery(news_id, $(this).attr('value')) : $.humidifNews.showGallery(news_id, 0);
		});
	},
	/**
	 * @description: Funcion que construye la galeria correspondiente a una noticia.
	 * 
	 * @param {Integer} $newsid
	 * @param {Integer} $selected
	 * @param {Integer} $total
	 */
	showGallery: function($newsid, $selected){
		$.humidifAjax.getData('body','get',base_url+'news/news_gallery/'+$newsid+'/'+$selected+'/');
	},
	/**
	 * @description: Asigna 
	 */
	manageGallery: function(selected){
		// Calculamos la posicion del contenedor de los thumbnails
		$total_thumbs = $('img#gallery-thumbs').length;
		$total_space  = ($total_thumbs * 45) + ($total_thumbs - 1) * 10;
		$margin_left  = Math.round($total_space / 2);
		$('div#gallery-images').css({'width' : $total_space + 'px', 'margin-left' : '-' + $margin_left + 'px'});
		// Mostramos la imagen seleccionada
		selected_img = $('div#gallery-images img:eq('+selected+')').attr('src').replace(/_thumb./i, '.');
		$('div#gallery-main-image').html('<img src="'+selected_img+'" height="440" width="440" />');
		// Asignamos los eventos a cada thumbnail
		$('img#gallery-thumbs').each(function(){
			$(this).bind('click', function(){
				// Obtenemos la imagen del thumb y remplazamos una seccion de la
				// ruta para obtener la imagen ampliada
				image = $(this).attr('src');
				image = image.replace(/_thumb./i, '.');
				// Remplazamos la imagen ampliada
				$('div#gallery-main-image > img').attr('src', image);
			});
		});
		$('img#close-btn').bind('click', function(){
			$('div#gallery-content, div#gallery-back').fadeOut(450,function(){
				$('div#gallery-content, div#gallery-back').remove();
			});
		});
	}
}
