
jQuery(document).ready(function () {
	
	
	loadContactForm();	
	loadEnviaVideo();
	loadEncuestaForm();
	loadSms();

	initTooltip('[class*=tooltip_link][title]'); 
	jQuery('#mycarousel').jcarousel();
	
	jQuery('.galeria_listado_video > a').live('click', function(eve){
		eve.preventDefault();
		
		var link = jQuery(this).attr("href");
		
		$.ajax({
			url: link,
			type: "GET",
			dataType: "html",
			beforeSend: function(){
//				showBusy(page);
			},	
		  	success: function(html) {
		    	nuevoVideo(html);
		 	}
		});	
	});	
	
	jQuery('.galeria_listado_video > a.galeria_video_cortina').live('click', function(eve){
		
		eve.preventDefault();
		var link = jQuery(this).attr("href");
		
		$.ajax({
			url: link,
			type: "GET",
			dataType: "html",
			beforeSend: function(){

			},	
		  	success: function(html) {
		    	nuevaGaleria(html);
		 	}
		});	
	});
	
	jQuery('.galeria_listado_video > a.thumb_pic').live('click', function(eve){
		eve.preventDefault();
		
		var link = jQuery(this).attr("href");
		
		var page = jQuery(this).attr("page");
		
		$.ajax({
			url: link+'/'+page,
			type: "GET",
			dataType: "html",
			beforeSend: function(){
//				showBusy(page);
			},	
		  	success: function(html) {
		    	nuevaPhoto(html);
		 	}
		});	
	});
	
	
	jQuery('a.mp_anterior').live('click', function(eve){
		eve.preventDefault();
		
		var link = jQuery(this).attr("href");
		
		var page = jQuery(this).attr("page");
		
		$.ajax({
			url: link+'/'+page,
			type: "GET",
			dataType: "html",
			beforeSend: function(){
//				showBusy(page);
			},	
		  	success: function(html) {
		    	nuevaPhoto(html);
		 	}
		});	
	});
	
	jQuery('a.mp_siguiente').live('click', function(eve){
		eve.preventDefault();
		
		var link = jQuery(this).attr("href");
		
		var page = jQuery(this).attr("page");
		
		$.ajax({
			url: link+'/'+page,
			type: "GET",
			dataType: "html",
			beforeSend: function(){
//				showBusy(page);
			},	
		  	success: function(html) {
		    	nuevaPhoto(html);
		 	}
		});	
	});
	
	jQuery ('#fileQueue span div.cancel').live('click',function(eve){
		eve.preventDefault();
		 $('#fileQueue br').hide();
		 $('#fileQueue span').hide();
		
	});
	
	jQuery('#filtro_date_start_photo').datepicker({
		   onSelect: function(dateText, inst) { 			   
			   desde=jQuery('#fechaDesde').val();
			   hasta=jQuery('#fechaHasta').val();
			   act=jQuery('#select').val();
			   turno=jQuery('#turno').val();
			   if((desde!='')&&(hasta!='')){
					window.location.href = APP_URL + 'campusphoto/filtros/fecha/' + act + '/' + turno + '/' + desde + '/' + hasta
			   }
		   },
		   altField: '#fechaDesde',
		   altFormat: 'dd-mm-yy',
		   dateFormat: 'dd/mm/yy'
		});
	
	jQuery('#filtro_date_end_photo').datepicker({
		   onSelect: function(dateText, inst) {
			   desde=jQuery('#fechaDesde').val();
			   hasta=jQuery('#fechaHasta').val();
			   act=jQuery('#select').val();
			   turno=jQuery('#turno').val();
			   if((desde!='')&&(hasta!='')){
					window.location.href = APP_URL + 'campusphoto/filtros/fecha/' + act + '/' + turno + '/' + desde + '/' + hasta
			   }
			   
		   },

		   altField: '#fechaHasta',
		   altFormat: 'dd-mm-yy',
		   dateFormat: 'dd/mm/yy'
		});
	
	jQuery('#filtro_date_start_tv').datepicker({
		   onSelect: function(dateText, inst) { 			   
			   desde=jQuery('#fechaDesde').val();
			   hasta=jQuery('#fechaHasta').val();
			   act=jQuery('#select').val();
			   turno=jQuery('#turno').val();
			   if((desde!='')&&(hasta!='')){
					window.location.href = APP_URL + 'campustv/filtros/fecha/' + act + '/' + turno + '/' + desde + '/' + hasta
			   }
		   },
		   altField: '#fechaDesde',
		   altFormat: 'dd-mm-yy',
		   dateFormat: 'dd/mm/yy'
		});
	
	jQuery('#filtro_date_end_tv').datepicker({
		   onSelect: function(dateText, inst) {
			   desde=jQuery('#fechaDesde').val();
			   hasta=jQuery('#fechaHasta').val();
			   act=jQuery('#select').val();
			   turno=jQuery('#turno').val();
			   if((desde!='')&&(hasta!='')){
					window.location.href = APP_URL + 'campustv/filtros/fecha/' + act + '/' + turno + '/' + desde + '/' + hasta
			   }
			   
		   },

		   altField: '#fechaHasta',
		   altFormat: 'dd-mm-yy',
		   dateFormat: 'dd/mm/yy'
		});
});


function paginarGalerias (act, page)
{
	$.ajax({
		url: "campusphoto/paginado/"+act+"/"+page,
		type: "GET",
		dataType: "html",
		beforeSend: function(){
//			showBusy(page);
		},	
	  	success: function(html) {
	    	updateSidebarCampusPhoto(html,page);
	 	}
	});	
}

function paginarEstapasando (act, page)
{
	$.ajax({
		url: "estapasando/paginado/"+act+"/"+page,
		type: "GET",
		dataType: "html",
		beforeSend: function(){
//			showBusy(page);
		},	
	  	success: function(html) {
	  		console.log(html);
	    	updateSidebarEstapasando(html);
	 	}
	});
}


var pos=1;
function sigFotos ()
{ 
	var anterior="#fotos"+pos;
	pos++;
	var siguiente="#fotos"+pos;
	$(anterior).css("display", "none");
	$(siguiente).css("display", "block");
}

function antFotos ()
{ 
	var anterior="#fotos"+pos;
	pos--;
	var siguiente="#fotos"+pos;
	$(anterior).css("display", "none");
	$(siguiente).css("display", "block");
}

function updateSidebarEstapasando(html)
{
	var mostrar = $(html).find("#estapasando");
	$('.sidebar_right').html(mostrar);
}


function updateSidebarCampusPhoto(html,page)
{
	var tags = $('.galeria_sidebar').find(".galeria_etiquetas");	
//	$('.galeria_sidebar').html(tags);
	var mostrar = $(html).find(".galeria_listado_videos");
	$('.galeria_sidebar').html(mostrar);
	$('.galeria_sidebar').append(tags);
}


function nuevaPhoto(html){
	
	var mostrar = $(html).find(".detalle_noticia");
	$('.enganchePhoto').html(mostrar);
//	window.setTimeout( function(){
//		
//	}, 500);	
}

function nuevaGaleria(html){

	var mostrar = $(html).find(".detalle_noticia");
	$('.engancheGaleria').html(mostrar);
//	window.setTimeout( function(){
//		
//	}, 500);	
}

function nuevoVideo(html){
	
	var mostrar = $(html).find(".detalle_noticia");
	$('.engancheVideo').html(mostrar);
//	window.setTimeout( function(){
//		
//	}, 500);	
}

function filterArchivedActivitiesByYear(act, select)
{
	window.location.href = APP_URL + 'archivodeactividadespasadas/listado/'+ act + '/' + select.value;
}

function filterGalleriesByActivity(select)
{
	window.location.href = APP_URL + 'campusphoto/filtros/actividad/' + select.value;
}

function filterGalleriesByShift(act, turno)
{
	window.location.href = APP_URL + 'campusphoto/filtros/turno/' + act + '/' + turno.value
}

function filterVideosByActivity(select){
    //window.location.href = APP_URL + 'campustv/ver/' + select.value;
    window.location.href = APP_URL + 'campustv/filtros/actividad/' + select.value;
}

function filterVideosByShift(act, turno)
{
	
	window.location.href = APP_URL + 'campustv/filtros/turno/' + act + '/' + turno.value
	//window.location.href = APP_URL + 'campustv/ver/'+act+'/0/turno' + '/' + turno.value;
	
	
}

function filterTweetsByTurn(title, act, select){
    window.location.href = APP_URL + 'actividad/detalle/' + title + '/' + act + '/' + select.value;
}

function filterNewsByActivity(select){
    window.location.href = APP_URL + 'noticia/listado/' + select.value;
}

//function votarEncuestas (args)
//{
//	var split_args = new Array ();
//	split_args = args.split("_");
//	jQuery.ajax({
//		url:		"encuesta/votar",
//		type:		'post',
//		dataType:	'json',
//		data:		{voto:split_args[0],itemId:split_args[1]},
//		success:	function (data) {
//						if (data.result == 'ok') {
//							console.log('ok ' + data.avg);
//						} else {
//							console.log('ko ' + data.avg);
//						}
//		}
//	});
//}

function votarVideo (args)
{
	var split_args = new Array ();
	split_args = args.split("_");
	jQuery.ajax({
		url:		"campustv/votar",
		type:		'post',
		dataType:	'json',
		data:		{voto:split_args[0],itemId:split_args[1]},
		success:	function (data) {
						if (data.result == 'ok') {
							jQuery('p[id=votoGuardado]').css({'display':'block','margin-botton':'5px'});
							
							jQuery('#votar_' + split_args[1]).html('<span> ' + data.avg + ' /  <b>' + data.votos + ' votos </b></span>');
							//console.log('ok ' + data.avg);
						} else {
							//console.log('ko ' + data.avg);
						}
		}
	});
}

function votarPhoto (args)
{
	var split_args = new Array ();
	split_args = args.split("_");
	jQuery.ajax({
		url:		"campusphoto/votar",
		type:		'post',
		dataType:	'json',
		data:		{voto:split_args[0],itemId:split_args[1]},
		success:	function (data) {
						if (data.result == 'ok') {
							jQuery('p[id=votoGuardado]').css({'display':'block','margin-botton':'5px'});
							
							jQuery('#votar_' + split_args[1]).html('<span> ' + data.avg + ' /  <b>' + data.votos + ' votos </b></span>');
							//console.log('ok ' + data.avg);
						} else {
							//console.log('ko ' + data.avg);
						}
		}
	});
}

function descargarPhoto (args)
{
	
	jQuery.ajax({
		url:		"campusphoto/descargar",
		type:		'post',
		dataType:	'json',
		data:		{itemId:args},
		success:	function (data) {
						if (data.result == 'ok') {
							console.log('ok ' + data.avg);
						} else {
							console.log('ko ' + data.avg);
						}
		}
	});
}

function loadSms()
{
	jQuery('input#submitSms').live('click', function () {
		var url	= jQuery(this).attr('href');
		
		jQuery.ajax({
			url:		url,
			type:		'post',
			dataType:	'json',
			data:		'sendSms=true&' + jQuery('form[id=smsForm]').serialize(),
			beforeSend:	function () {
			
			},
			success:	function (data, textStatus, XMLHttpRequest) {
							if (data.result == 'ok') {
								jQuery('div[id=smsEnviado]').show();
//								jQuery('div[id=' + layerForm + ']').hide();
//								jQuery('div[id=contactOk]').show();
								//Se ha enviado. Mostramos vista de cargando.
//								console.log('ok ' + layerForm);
							} else {
								jQuery('div[id=smsNoEnviado]').show();
								//Errores. Mostramos errores
//								var html = '<div class="ko">' + data.message + '</div>';
//								jQuery('div[id=' + layerErrors + ']').html(html);
//								jQuery('div[id=' + layerErrors + ']').show();
							}
			},
			error:		function (XMLHttpRequest, textStatus, errorThrown) {				
								alert ('Hubo un error en el envio del sms');
//								var html = '<div class="error">Se ha producido un error al enviar el mensaje. Inténtelo de nuevo o contacte con el administrador.</div>';
//								jQuery('div[id=' + layerErrors + ']').html(html);
//								jQuery('div[id=' + layerErrors + ']').show();
			}
		});
		return false;
	});
}

function loadEncuestaForm()
{
	jQuery('div#submitEncuestaForm').live('click', function () {
		var url	= APP_URL + 'encuesta/votar';
//		var layers = jQuery(this).attr('rel');
//		var layerErrors = layers.split('#')[0];
//		var layerForm = layers.split('#')[1];
//		//Ocultamos errores.
//		jQuery('div[id=' + layerErrors + ']').hide();
//		console.log(jQuery('form[id=contactForm]').serialize());submitEncuestaForm
		jQuery.ajax({
			url:		url,
			type:		'post',
			dataType:	'json',
			data:		'sendEncuestaForm=true&' + jQuery('form[id=encuestaForm]').serialize(),
			beforeSend:	function () {
			
			},
			success:	function (data, textStatus, XMLHttpRequest) {
							if (data.result == 'ok') {
								jQuery('div[id=formulario_encuesta]').css('display','none');
								jQuery('div[id=resultados_encuesta]').css('display','block');
								//Se ha enviado. Mostramos vista de cargando.
//								console.log('ok ' + layerForm);
							} else {
								alert ('Hubo un error en la votacion');
								//Errores. Mostramos errores
//								var html = '<div class="ko">' + data.message + '</div>';
//								jQuery('div[id=' + layerErrors + ']').html(html);
//								jQuery('div[id=' + layerErrors + ']').show();
							}
			},
			error:		function (XMLHttpRequest, textStatus, errorThrown) {
								alert ('Hubo un error en la votacion');
//								var html = '<div class="error">Se ha producido un error al enviar el mensaje. Inténtelo de nuevo o contacte con el administrador.</div>';
//								jQuery('div[id=' + layerErrors + ']').html(html);
//								jQuery('div[id=' + layerErrors + ']').show();
			}
		});
		return false;
	});
}

function loadContactForm()
{
	
	jQuery('input[id=submitContactForm]').live('click', function () {
		var url	= jQuery(this).attr('href');
		var layers = jQuery(this).attr('rel');
		var layerErrors = layers.split('#')[0];
		var layerForm = layers.split('#')[1];
		//Ocultamos errores.
		jQuery('div[id=' + layerErrors + ']').hide();
//		console.log(jQuery('form[id=contactForm]').serialize());
		jQuery.ajax({
			url:		url,
			type:		'post',
			dataType:	'json',
			data:		'sendContactForm=true&' + jQuery('form[id=contactForm]').serialize(),
			beforeSend:	function () {
			
			},
			success:	function (data, textStatus, XMLHttpRequest) {
							if (data.result == 'ok') {
								jQuery('div[id=' + layerForm + ']').hide();
								jQuery('div[id=contactOk]').css("display","block");
								//Se ha enviado. Mostramos vista de cargando.
//								console.log('ok ' + layerForm);
							} else {
								//Errores. Mostramos errores
								var html = '<div class="ko">' + data.message + '</div>';
								jQuery('div[id=' + layerErrors + ']').html(html);
								jQuery('div[id=' + layerErrors + ']').show();
							}
			},
			error:		function (XMLHttpRequest, textStatus, errorThrown) {
								var html = '<div class="error">Se ha producido un error al enviar el mensaje. Inténtelo de nuevo o contacte con el administrador.</div>';
								jQuery('div[id=' + layerErrors + ']').html(html);
								jQuery('div[id=' + layerErrors + ']').show();
			}
		});
		return false;
	});
}

function loadEnviaVideo()
{
	
	jQuery('input[id=submitEnviaVideos]').live('click', function () {
		var url	= jQuery(this).attr('href');
		var layers = jQuery(this).attr('rel');
		var layerErrors = layers.split('#')[0];
		var layerForm = layers.split('#')[1];
		//Ocultamos errores.
		jQuery('div[id=' + layerErrors + ']').hide();
//		console.log(jQuery('form[id=contactForm]').serialize());
		jQuery.ajax({
			url:		url,
			type:		'post',
			dataType:	'json',
			data:		'sendVideoForm=true&' + jQuery('form[id=enviaVideo]').serialize(),
			beforeSend:	function () {
			
			},
			success:	function (data, textStatus, XMLHttpRequest) {
							if (data.result == 'ok') {
								jQuery('div[id=' + layerForm + ']').hide();
								jQuery('div[id=contactOk]').show();
								//Se ha enviado. Mostramos vista de cargando.
//								console.log('ok ' + layerForm);
							} else {
								//Errores. Mostramos errores
								var html = '<div class="ko">' + data.message + '</div>';
								jQuery('div[id=' + layerErrors + ']').html(html);
								jQuery('div[id=' + layerErrors + ']').show();
							}
			},
			error:		function (XMLHttpRequest, textStatus, errorThrown) {
								var html = '<div class="error">Se ha producido un error al enviar el video. Inténtelo de nuevo o contacte con el administrador.</div>';
								jQuery('div[id=' + layerErrors + ']').html(html);
								jQuery('div[id=' + layerErrors + ']').show();
			}
		});
		return false;
	});
}

function shadowBox(title, url, tv)
{
	Shadowbox.open({
        content:    '<br /><br /><iframe width="560" height="349" src="http://www.youtube.com/embed/' + url + '" frameborder="0" allowfullscreen></iframe><br /><br /><a style="color:#FFF;font-weight:bold;" href="' + tv + '">Vota este v&iacute;deo y consulta sus estad&iacute;sticas en Campus TV</a>',
        player:     "html",
        title:      title,
        height:     420,
        width:      640
    });

}
