
var global = {
	// all the global variables here
}

function clickLogin()
{
	login($('#user').val(),$('#password').val());
}
function showLoginForm()
{
	$.fancybox($("#loginPopup").html(),{
		"autoDimensions"	: true,
		"transitionIn"		: "fade",
		"padding"			: 0,
		"transitionOut"		: "fade"
		}); 
}

function getNews(id,lang,caller)
{
	$('.boxNewsBig').fadeOut('fast',function(){
			$('.left #loader').fadeIn('fast', function(){
				var noCacheRandom	=	Math.random(1000000);
				$.ajax({
					type: "POST",
					url: "/services/getNews.php?noCache=" + noCacheRandom,
					data: "id="+id+"&l="+lang,
					success: function(result){	
						$("#newsList li").attr("id","");
						$(caller).parent().attr("id","selected");
						showNews(result);
	
					}
				});
			});
	});	
	$('.login').fadeOut('fast');
}

function getNewsList(page, lang, caller)
{
	var dataObject	=	"page="+page;
		
	$('#newsList').fadeOut('fast',function(){
		$('.right #loader').fadeIn('fast', function(){
			var noCacheRandom	=	Math.random(1000000);
			$.ajax({
				type: "POST",
				url: "/services/getNewsList.php?noCache=" + noCacheRandom,
				data: dataObject,
				success: function(result){
					$('#newsList').html("");
					var newsEnsemble= jQuery.parseJSON(result);
					
					$.each(newsEnsemble.news, function(i, item) {
						$(".pager li").attr("id","");
						$(caller).parent().attr("id","selected"); 
						
						var argoument	=	newsEnsemble.news[i].id+",'it',this";
						$('#newsList').append('<li><a onclick="getNews('+argoument+');"><div class="chData">'+newsEnsemble.news[i].date+'</div><div class="chTitolo">'+newsEnsemble.news[i].title+'</div><div class="chSottotitolo">'+newsEnsemble.news[i].subtitle+'</div></a></li>');
					});
					$('.right #loader').fadeOut('fast', function(){$('#newsList').fadeIn('fast');});
				}
			});
		});
	});
}

function getCaseHistoryList(page, lang, caller)
{
	var dataObject	=	"page="+page;
	$('#newsList').fadeOut('fast',function(){
		$('.right #loader').fadeIn('fast', function(){
			var noCacheRandom	=	Math.random(1000000);
			$.ajax({
				type: "POST",
				url: "/services/getNewsList.php?noCache=" + noCacheRandom,
				data: dataObject,
				success: function(result){
					$('#newsList').html("");
					var newsEnsemble= jQuery.parseJSON(result);
					
					$.each(newsEnsemble.news, function(i, item) {
						$(".pager li").attr("id","");
						$(caller).parent().attr("id","selected"); 
						var argoument	=	newsEnsemble.news[i].id+",'it',this";
						$('#newsList').append('<li><a onclick="getCaseHistory('+argoument+');"><div class="chData">'+newsEnsemble.news[i].date+'</div><div class="chTitolo">'+newsEnsemble.news[i].title+'</div></a></li>');
					});
				}
			});
		});
	});
}

function loginAppened(result)
{
	var loginResult	=	JSON.parse(result);
	
	if (loginResult.status=="ok")
	{
		$('#fancybox-content #formLogin #outputQuote').html("Login effettuato");
		$('.errorOutput').html("Login effettuato");
		window.location.reload( false );
	}
	else
	{
		$('.errorOutput').html("Login errato");
		$('#fancybox-content #formLogin #outputQuote').html("Login errato");
	}
}

function showNews(data, caller)
{

	$("#newsList li").attr("color","#000000");
	
	var news	=	jQuery.parseJSON(data);
	
	if ($(".newsId").html()!=news.id)
	{
		$('.boxNewsBig').fadeOut(300,function(){
			$('#loader').fadeOut('fast',function(){
				$('.boxNewsBig p').html("");
				$('.newsId').html(news.id);
				$('.boxNewsBig .date').html(news.date);
				$('.boxNewsBig h4').html(news.title);
				if (news.content)
				{
					$('.login').hide();
					$('.boxNewsBig p').html(news.content);
				}
				else
					$('.login').show();

				$('.boxNewsBig ul').html("");
				
				if (news.attachments)
				{
					if (news.attachments.length>0)
					{
						for (i=0; i<news.attachments.length; i++)
							$('.boxNewsBig ul').append('<li><a target="_blank" href="/fileUpload/'+news.attachments[i].filename+'">'+news.attachments[i].title+'</a>');
					}
				}
				$('.boxNewsBig').fadeIn(300);
			});
		});
	}
}
	
function showPub(data)
{
	var pub	=	jQuery.parseJSON(data);
	
	if ($(".newsId").html()!=pub.id)
	{
		$('.boxPubBig').fadeOut(300,function(){
			$('#loader').fadeOut('fast',function(){
				$('.newsId').html(pub.id);
				
				$('.boxPubBig h4').html(pub.title);
				$('.boxPubBig h5').html(pub.authors);
				$('.boxPubBig h6').html("ed. "+pub.edition);
				$('.boxPubBig p').html(pub.description);
				$('.boxPubBig ul').html("");
				if (pub.attachments!=null)
				{
					for (i=0; i<pub.attachments.length; i++)
					{
						if(pub.attachments[i].filetype == "1")
						{
							$('.boxPubBig img').attr("src","/common/obj/insertGeneralImage.php?_w=81&_h=114&_f="+pub.attachments[i].filename);
						}
						else if(pub.attachments[i].filetype == "2")
						{
							$('.boxPubBig img').attr("src","/common/obj/insertGeneralImage.php?_w=81&_h=114&_f="+pub.attachments[i].filename);
							$('.boxPubBig ul').append('<li><a target="_blank" href="/fileUpload/'+pub.attachments[i].filename+'">'+pub.attachments[i].title+'</a>');
						}
						else
						{
							$('.boxPubBig ul').append('<li><a target="_blank" href="/fileUpload/'+pub.attachments[i].filename+'">'+pub.attachments[i].title+'</a>');
						}
					}
				}
				$('.boxPubBig').fadeIn(300);
			});
		});
	}
}

function showCaseHistory(data)
{
	var cases	=	jQuery.parseJSON(data);
	
	if ($(".newsId").html()!=cases.id)
	{
		$('.boxCaseHistoryBig').fadeOut(300,function(){
			$('#loader').fadeOut('fast',function(){
				$('.boxCaseHistoryBig h4').html(cases.title);
				$('.boxCaseHistoryBig p').html(cases.description);
				$('.boxCaseHistoryBig ul').html("");
				if (cases.attachments.length>0)
				{
					
					for (i=0; i<cases.attachments.length; i++)
					{
						$('.boxCaseHistoryBig ul').append('<li><a target="_blank" href="/fileUpload/'+cases.attachments[i].filename+'">'+cases.attachments[i].title+'</a>');
					}
				}
				$('.boxCaseHistoryBig').fadeIn(300);
			});
		});
	}
}

function login(user,password){
	var noCacheRandom	=	Math.random(1000000);
	$.ajax({
		type: "POST",
		url: "/services/login.php?noCache=" + noCacheRandom,
		data: "user="+user+"&password="+password,
		success: function(result){
			loginAppened(result);
		}
	});
}

function getCaseHistory(id,lang,caller){
	$('.boxCaseHistoryBig').fadeOut('fast',function(){
		$('.left #loader').fadeIn('fast', function(){	
			var noCacheRandom	=	Math.random(1000000);	
			$.ajax({
				type: "POST",
				url: "/services/getCaseHistory.php?noCache=" + noCacheRandom,
				data: "id="+id+"&l="+lang,
				success: function(result){
					$("#newsList li").attr("id","");
					$(caller).parent().attr("id","selected");			
					showCaseHistory(result);
				}
			});
		});
	});
}

function getPub(id,lang,caller){
	$('.boxPubBig').fadeOut('fast',function(){
		$('.left #loader').fadeIn('fast', function(){
			var noCacheRandom	=	Math.random(1000000);
			$.ajax({
				type: "POST",
				url: "/services/getPub.php?noCache=" + noCacheRandom,
				data: "id="+id,
				success: function(result){
						$('.newsId').attr("src","");
						$('.boxPubBig img').attr("src","");
						$('.boxPubBig h4').html("");
						$('.boxPubBig h5').html("");
						$('.boxPubBig h6').html("");
						$('.boxPubBig p').html("");
						$('.boxPubBig ul').html("");
						$("#newsList li").attr("id","");
						$(caller).parent().attr("id","selected");
						showPub(result);
				}
			});
		});
	});
}

// remap jQuery to $
jQuery(function($){

	
	this.getNewsListLinked=function(n_news){
		var noCacheRandom	=	Math.random(1000000);
		$.ajax({
			type: "POST",
			url: "/services/getNewsListLinked.php?noCache=" + noCacheRandom,
			data: "num_news="+n_news,
			success: function(msg){
			$('.notizie').append( msg );
			}
		});
	}
});


$(document).ready(function(){
	$( "#fancybox-content #formLogin" ).live('submit', function(){
		var user 			= $("#fancybox-content #formLogin #user").val();
		var password 		= $('#fancybox-content #formLogin #password').val();
		var noCacheRandom	= Math.random(1000000);
		$.ajax({
			type: "POST",
			url: "/services/login.php?noCache=" + noCacheRandom,
			data: "user="+user+"&password="+password,
			success: function(result){
			loginAppened(result);
		}
		});
		return false;
	}); 
});



//check on console
function logger(message){
	if(console){
		console.log(message);
	}
}
