/**
 * Intercambia entre las pestañas de hoy, mañana y pasado en el módulo de guiaTv.
 *
 * @param	string	groupId	Id del grupo a seleccionar.
 * @param 	string 	tab		Pestaña que queremos activar.
 * @return	void
 */
function set_guia_tv_tab(groupId, tab)
{
	if (!jQuery('#' + groupId + ' a[id=menuGuiaTV_' + tab + ']').hasClass('active')) {
		jQuery('#' + groupId + ' div[id^=guiaTV_]').hide('slow');
		jQuery('#' + groupId + ' div[id=guiaTV_' + tab + ']').show();
		jQuery('#' + groupId + ' a[id^=menuGuiaTV_]').removeClass('active');
		jQuery('#' + groupId + ' a[id=menuGuiaTV_' + tab + ']').addClass('active');
	}
}

//--------------------------------------------------------------------------

/**
 * Intercambia entre las pestañas de más comentado, más visto en el módulo de lo más.
 *
 * @param	string	groupId	Id del grupo a seleccionar.
 * @param	string	tab		Pestaña que queremos activar.
 * @return	void
 */
function set_lo_mas_tab(groupId, tab)
{
	if (!jQuery('#' + groupId + ' a[id=menuLomas_' + tab + ']').hasClass('active')) {
		jQuery('#' + groupId + ' div[id^=lomas_]').hide('slow');
		jQuery('#' + groupId + ' div[id=lomas_' + tab + ']').show();
		jQuery('#' + groupId + ' a[id^=menuLomas_]').removeClass('active');
		jQuery('#' + groupId + ' a[id=menuLomas_' + tab + ']').addClass('active');
	}
}



//--------------------------------------------------------------------------

/**
 * Recarga la vista del video player para los módulos de 1 y 2 columnas comboVideos.
 *
 * @param	object	elemento	Elemento DOM que ejecuta la función.
 * @param	string	claseCargar	Clase en la que se cargaran los datos obtenidos mediante AJAX.
 * @param 	void
 */
function rvplayer(elemento, claseCargar)
{
	if( elementoId	= jQuery(elemento).attr('id') )
	{
		//Obtenemos las variales

		var videoId				= elementoId.split('_')[0];
		var showId				= elementoId.split('_')[1];
		var bdId				= elementoId.split('_')[2];
		var url 				= APP_URL + 'ajax/rvplayer';
		var width 				= 320;
		var height 				= 240;
		var crop				= 'crop1';
		var partesClaseCargar 	= claseCargar.split('_');
		var groupId 			= partesClaseCargar[partesClaseCargar.length - 1];
		var linkId 				= jQuery(elemento).attr('link');
		var titleId 			= jQuery(elemento).attr('name');
		var imgId 				= jQuery(elemento).attr('img');

		if (claseCargar.indexOf('2col') != -1) {
			width 	= 444;
			height 	= 250;
			crop	= 'crop70';
		}

		//Lanzamos los datos
		jQuery.ajax({
			type		: 'post',
			url			: url,
			data		: {
							videoId	: videoId,
							showId	: showId,
							bdId	: bdId,
							width	: width,
							height	: height,
							crop	: crop,
							groupId	: groupId,
							linkId	: linkId,
							titleId	: titleId,
							imgId	: imgId
			},
			dataType	: 'json',
			async		: false,
			beforeSend	: function( XMLHttpRequest ) {
				//console.log( 'loading...' );
			},
			complete	: function( XMLHttpRequest, textStatus ) {
				//console.log( 'loaded' );
			},
			error		: function( XMLHttpRequest, textStatus, errorThrown ) {
				//alert( textStatus );
			},
			success		: function( data, textStatus, XMLHttpRequest ) {

				if(data)
				{
					var title 		= 'Sin titulo';
					var description = 'Sin descripcion';

					if(data.title)
						title = (data.title.length > 50) ? data.title.substr(0, 50) + '...'	: data.title;

					if(data.description)
						description	= (data.description.length > 200)	? data.description.substr(0, 200) + '...'	: data.description;

					if ( data.view )
						jQuery('.' + claseCargar).html(data.view);

				}

				return false;
			}
		});


	}// fin del elementoID
}

//--------------------------------------------------------------------------

/**
 * Recarga la vista del video player para los módulos de 1 y 2 columnas comboVideos.
 *
 * @param	object	elemento	Elemento DOM que ejecuta la función.
 * @param	string	claseCargar	Clase en la que se cargaran los datos obtenidos mediante AJAX.
 * @param 	void
 */
function reload_video_player(elemento, claseCargar)
{
	var elementoId	= jQuery(elemento).attr('id');
	var partesId	= elementoId.split('_');
	var videoId		= partesId[0];
	var showId		= partesId[1];
	var bdId		= partesId[2];
	var url = APP_URL + 'ajax/reload_video_player';
	var width = 320;
	var height = 240;
	var crop	= 'crop1';
	var partesClaseCargar = claseCargar.split('_');
	var groupId = partesClaseCargar[partesClaseCargar.length - 1];
	if (claseCargar.indexOf('2col') != -1) {
		width 	= 444;
		height 	= 250;
		crop	= 'crop70';
	}
	jQuery.ajax({
		type		: 'post',
		url			: url,
		data		: {rvp_video_id: videoId, rvp_video_width: width, rvp_video_height: height, rvp_show_id: showId, rvp_bd_id: bdId, rvp_crop:crop},
		dataType	: 'json',
		async		: false,
		beforeSend	: function( XMLHttpRequest ) {
			//console.log( 'loading...' );
		},
		complete	: function( XMLHttpRequest, textStatus ) {
			//console.log( 'loaded' );
		},
		error		: function( XMLHttpRequest, textStatus, errorThrown ) {
			//alert( textStatus );
		},
		success		: function( data, textStatus, XMLHttpRequest ) {
			var title 		= (data.title.length > 50)			? data.title.substr(0, 50) + '...'			: data.title;
			var description	= (data.description.length > 200)	? data.description.substr(0, 200) + '...'	: data.description;
			jQuery('.' + claseCargar).html(data.view);
			if (jQuery('#' + groupId + ' a[id=linkTitle]').length) {
				jQuery('#' + groupId + ' a[id=linkTitle]').attr('href', data.link);
				jQuery('#' + groupId + ' a[id=linkTitle]').attr('title', title);
				jQuery('#' + groupId + ' a[id=linkTitle]').html(title);
				jQuery('#' + groupId + ' p[id=description]').html(description);
			}
		}
	});
}

//--------------------------------------------------------------------------

/**
 * Recarga la vista del video player para los módulos de 1 y 2 columnas comboVideos.
 *
 * @param	object	elemento	Elemento DOM que ejecuta la función.
 * @param	string	claseCargar	Clase en la que se cargaran los datos obtenidos mediante AJAX.
 * @param 	void
 */
function reload_combo_video(elemento, claseCargar)
{
	var elementoId			= jQuery(elemento).attr('id');
	var edicion				= jQuery(elemento).attr('edicion');
	var url 				= APP_URL + 'ajax/reload_combo_video';
	var partesClaseCargar 	= claseCargar.split('_');
	var groupId 			= partesClaseCargar[partesClaseCargar.length - 1];

	jQuery.ajax({
		type		: 'post',
		url			: url,
		data		: {elementoId: elementoId },
		dataType	: 'json',
		async		: false,
		beforeSend	: function( XMLHttpRequest ) {
			//console.log( 'loading...' );
		},
		complete	: function( XMLHttpRequest, textStatus ) {
			//console.log( 'loaded' );
		},
		error		: function( XMLHttpRequest, textStatus, errorThrown ) {
			//console.log( textStatus );
		},
		success		: function( data, textStatus, XMLHttpRequest ) {
			
			if(data)
			{
				var title 		= (data.title.length > 50)			? data.title.substr(0, 50) + '...'			: data.title;
				var description	= (data.description.length > 200)	? data.description.substr(0, 200) + '...'	: data.description;
				
				if(edicion == 1)
				{
					jQuery('div[id=module_editor] div[class*='+claseCargar+']').html(data.view);
					jQuery('div[id=module_editor] div[id='+groupId+'] a[id=linkTitle]').attr('href', data.link).attr('title', title).html(title);
					jQuery('div[id=module_editor] div[id='+groupId+'] p[id=description]').html(description);
				}else{
					jQuery('.' + claseCargar).html(data.view);
					jQuery('#' + groupId + ' a[id=linkTitle]').attr('href', data.link);
					jQuery('#' + groupId + ' a[id=linkTitle]').attr('title', title);
					jQuery('#' + groupId + ' a[id=linkTitle]').html(title);
					jQuery('#' + groupId + ' p[id=description]').html(description);
				}
			}
		}
	});
}


//--------------------------------------------------------------------------

/**
 * Función que mueve la galería de imágenes de 1 columna a la derecha.
 *
 * @param	string	groupId	Id del grupo que contiene la galería.
 * @return	void
 */
function move_gallerie(groupId,direccion)
{


	//console.log(jQuery('div[class*=cover_module_editor_container]'));

	var capa = jQuery('div[class*=cover_module_editor_container]');

	//if( jQuery('[id=module_editor]').dialog( "isOpen" ) )

	if( capa.css('display') == 'block' )
	{
		var groupId 	= jQuery('div[id=module_editor] div[id=work_area] div[class*=cover_group]').attr('id');
		var numPictures = jQuery('div[id=module_editor] div[id=work_area] div[class*=cover_group] b[id=numPictures]').html() ;
		var numPicture  = jQuery('div[id=module_editor] div[id=work_area] div[class*=cover_group] b[id=currentPicture]').html();

	}else{

		//Caso que estoy en portada
		var numPictures = jQuery('#' + groupId + ' b[id=numPictures]').html() ;
		var numPicture  = jQuery('#' + groupId + ' b[id=currentPicture]').html() ;

	}

	if(direccion == 'right')
	{

		if (numPicture == numPictures){
 	 	 	numPicture = 1
	 	}else{
	 	 	numPicture++;
	 	}

	}else{

		if (numPicture == 1)
	 	{
	 	 	numPicture = numPictures;
	 	}else{
	 	 	numPicture--;
	 	}
	}


	cargar(numPicture, groupId);

}

//--------------------------------------------------------------------------

/**
 * Función que realiza la llamada AJAX y carga la vista con la imagen actual.
 *
 * @param	integer	numPicture	Número de fotografía actual a cargar.
 * @param	string	groupId		Id del grupo que contiene la galería de imágenes.
 * @return	void
 */
function cargar(numPicture, groupId)
{

	//Inicializamos las variables

	var img 		= false;
	var partes 		= false;
	var galleryId 	= false;
	var showId 		= false;
	var bdId 		= false;

	if(groupId != null)
	{

		if( jQuery('div[id='+groupId+'][class*=cover_group] img[id^=imgPrincipal_]').size() > 0 )
		{
			if ( img = jQuery('div[id='+groupId+'][class*=cover_group] img[id^=imgPrincipal_]').attr('id') )
			{

				galleryId = img.split('_')[1];
				showId = img.split('_')[2];
				bdId = img.split('_')[3];
			}

			if (!isNaN(galleryId)) {
				var url = APP_URL + 'galerias/get_gallery';
				jQuery.ajax({
					type		: 'post',
					url			: url,
					data		: {
									asg_num_picture	: numPicture,
									asg_galleryId	: galleryId,
									asg_show_id		: showId,
									asg_bd_id		: bdId,
									asg_group_id	: groupId,
									asg_crop		: 'crop1'
					},
					dataType	: 'html',
					async		: false,
					success		: function( data, textStatus, XMLHttpRequest ) {

						//if( jQuery('[id=module_editor]').dialog( "isOpen" ) )
						//if( jQuery('div[id=contenedor_cm]').length > 0 )

						var capa = jQuery('div[class*=cover_module_editor_container]');

						if( capa.css('display')== 'block')
						{
							jQuery('div[id=module_editor] div[id=work_area] div[id='+groupId+'] .gallery_images').html(data);
							jQuery('div[id=module_editor] div[id=work_area] div[id='+groupId+'] b[id=currentPicture]').html(numPicture);
						}else{
							jQuery('div[id='+groupId+'][class*=cover_group] div[class*=gallery_images]').html(data);
							jQuery('div[id='+groupId+'][class*=cover_group] b[id=currentPicture]').html(numPicture);
						}
					}
				});

			}

		}
	}else{
		//console.log("No tiene grupo id");
	}// fin del if

}



function reload_video_stiky(id, groupId, bdId, showId)
{

			var url = APP_URL + 'ajax/get_sticky';
			jQuery.ajax({
				type		: 'post',
				url			: url,
				data		: {
								id		: id,
								groupId	: groupId,
								bdId	: bdId,
								showId	: showId
				},
				async		: false,
				success		: function( data, textStatus, XMLHttpRequest ) {

					if(data)
					{
						jQuery('div[id=vds_'+groupId+']').html(data);

					}
				}
			});


}

