var currentPage		= 0;
var graphicCurrent	= 0;
var typeList		= false;
var typeListPage	= 0;
var graphicList		= false;
var graphicListPage = 0;
var typeCurrent 	= 0;
var lastTypeStep	= '1-1';
var lastGraphicStep	= '1-1';
var pPage			= 0;

$(document).ready(function() {
	//Onglets principaux
	$('#type').click(function() {
		$(this).css('border-bottom-color', 'white');
		$('#graphic').css('border-bottom-color', 'black');
		
		$('#typeContent').show();
		$('#graphicContent').hide();
		$('#list').text('List of fonts');
		currentPage = 0;
		pPage 		= 0;
		loadView('typeProjects');
	}).mouseover(function() {
		if(currentPage == 1) $(this).css('background', '#ccc');
	}).mouseout(function() {
		$(this).css('background', '#fff');
	});

	$('#graphic').click(function() {
		$(this).css('border-bottom-color', 'white');
		$('#type').css('border-bottom-color', 'black');

		$('#typeContent').hide();
		$('#graphicContent').show();
		$('#list').text('List of projects');
		currentPage = 1;
		pPage		= 0;
		loadView('graphicProjects');
	}).mouseover(function() {
		if(currentPage == 0) $(this).css('background', '#ccc');
	}).mouseout(function() {
		$(this).css('background', '#fff');
	});
	
	$('#about').click(function() {
		$('#content').hide();
		$('#informations').hide();

		$.ajax({
   			type: "POST",
   			url: "/ajax/getAbout.php",
   			success: function(msg){
   				var data = msg.split('###');
   				$('#aboutCol1Content').html(data[0]);
   				$('#aboutCol2').html(data[1]);
   				
   				if ($('#aboutCol1Content').height() > 343) {
  	 				$("#aboutSlider").slider({
  	 					orientation: 'vertical',
  	 					min: 0,
  	 					max: $('#aboutCol1Content').height()-343,
  	 					value: $('#aboutCol1Content').height()-343,
  	 					slide: function(event, ui) {
  	 						var top = $('#aboutCol1Content').height()-343 -ui.value;
  	 						$('#aboutCol1Content').css('top', -top +'px');
  	 					}
  	 				});
   				}
   			}
 		});

		positionAbout();
		
		$('#aboutPage').fadeIn('fast');
		$('#aboutBackground').css('opacity', 0).show();
		$('#aboutBackground').fadeTo('fast', 0.4).css('width', $(window).width()+'px').css('height', $(window).height()+'px');		
	}).mouseover(function() {
		$(this).css('text-decoration', 'none');
	}).mouseout(function() {
		$(this).css('text-decoration', 'underline');
	});
	
	$(window).scroll(function (eventObject) {
		$('#aboutBackground').css('left', $(window).scrollLeft()+'px');
		$('#aboutBackground').css('top', $(window).scrollTop()+'px');
    });
    
    $('#aboutBackground').click(function() {
    	closeAbout(); 
    });
    
    $(document).keyup(function(e) {
    	if(e.keyCode == 27) {
    		closeAbout();
    	}
    });
    
	$(window).resize(function() {
		positionAbout();
	});	
	
	//Gestion de la view
	$('.view').mousemove(function(e) {
		if (e.clientX-16 < 346) {
			$(this).css('cursor', 'pointer');
			$('#prevLink').children().css('text-decoration', 'none');
			$('#nextLink').children().css('text-decoration', 'underline');
		} else {
			$(this).css('cursor', 'pointer');
			$('#prevLink').children().css('text-decoration', 'underline');
			$('#nextLink').children().css('text-decoration', 'none');
		}
	}).mouseout(function() {
			$('#prevLink').children().css('text-decoration', 'underline');
			$('#nextLink').children().css('text-decoration', 'underline');		
	}).click(function(e) {
		if (e.clientX-16 < 346) {
			$('#prevLink').trigger('click');
		} else {
			$('#nextLink').trigger('click');		
		}
	});
	
	//Liens suivant/précédent
	$('#prevLink').click(function () {
		if (currentPage == 0) {
			typeCurrent--;		

			if (typeCurrent < 0) typeCurrent = typeObjets.length-1;

			lastTypeStep = '#typeSection'+typeObjets[typeCurrent];
		
			$.localScroll.goTo({
				target: '#typeProjects',
				queue:true,
				duration:750
			}, {hash: lastTypeStep});
			getLegend(typeObjets[typeCurrent]);
		} else {
			graphicCurrent--;

			if (graphicCurrent < 0) graphicCurrent = graphicObjets.length-1;

			lastGraphicStep = '#sectionGraphic'+graphicObjets[graphicCurrent];
			$.localScroll.goTo({
				target: '#graphicProjects',
				queue:true,
				duration:750
			}, {hash: lastGraphicStep});
			getLegend(graphicObjets[graphicCurrent]);
		}
	}).mouseover(function (){
		var c = $(this).children();
		c.css('text-decoration', 'none');
	}).mouseout(function (){
		var c = $(this).children();
		c.css('text-decoration', 'underline');
	});
	
	$('#nextLink').click(function () {
		if (currentPage == 0) {
			typeCurrent++;		

			if (typeCurrent > typeObjets.length-1) typeCurrent = 0;

			lastTypeStep = '#typeSection'+typeObjets[typeCurrent];
			
			$.localScroll.goTo({
				target: '#typeProjects',
				queue:true,
				duration:750
			}, {hash: lastTypeStep});
			getLegend(typeObjets[typeCurrent]);
		} else {
			graphicCurrent++;

			if (graphicCurrent > graphicObjets.length-1) graphicCurrent = 0;

			lastGraphicStep = '#sectionGraphic'+graphicObjets[graphicCurrent];
			$.localScroll.goTo({
				target: '#graphicProjects',
				queue:true,
				duration:750
			}, {hash: lastGraphicStep});
			getLegend(graphicObjets[graphicCurrent]);
		}
	}).mouseover(function (){
		var c = $(this).children();
		c.css('text-decoration', 'none');
	}).mouseout(function (){
		var c = $(this).children();
		c.css('text-decoration', 'underline');
	});
	
	//Clique "list of..."
	$('#list').click(function() {
		$('#projectList').text('');
		
		if (currentPage == 0) typeList = true
		if (currentPage == 1) graphicList = true

		$('#projectList').load('/ajax/projectList.php', {start: pPage, data:currentPage}, function() {
			$('#projectList').fadeIn('fast');
			$('#typeContent').fadeOut('fast');
			$('#graphicContent').fadeOut('fast');
			$('.navigation').fadeOut('fast');
			$('#informations').fadeOut('fast');

			$('.thumb').mouseover(function() {
				$(this).children().filter('.listInfos').show();
			}).mouseout(function() {
				$(this).children().filter('.listInfos').hide();
			});
		});
	});
	
	//Mise en place des views
	setupView('graphicProjects');
	setupView('typeProjects');
	
	//Légendes de base
	if (currentPage == 0) {
		getLegend(typeObjets[typeCurrent]);	
	} else {
		getLegend(graphicObjets[graphicCurrent]);
	}
});

function closeAbout() {
	$('#content').show(); //css('opacity', 1);
	
	//reposition
	var hash = '';
	var view = '';
	if (currentPage == 0) {
		view = 'typeProjects';
		hash = lastTypeStep; //typeObjets[typeCurrent];	
	} else {
		view = 'graphicProjects';
		hash = lastGraphicStep; //graphicObjets[graphicCurrent];
	}
	
	$.localScroll.goTo({
			target: '#'+view,
			queue:true,
			duration:0
		}, {hash: hash});
	
	if (!typeList && !graphicList) $('#informations').show();
	$('#aboutPage').fadeOut('fast');
	$('#aboutBackground').fadeOut('fast');	
}

function positionAbout() {
	var left = ($(window).width()/2 - 257);
	if (left < 16) left = 16;
		
	var top = ($(window).height()/2 - 197);
	if (top < 52) top = 52;
	$('#aboutPage').css('left', left + "px" );
	$('#aboutPage').css('top', top + "px" );
	$('#aboutBackground').css('width', $(window).width()+'px').css('height', $(window).height()+'px');
}

function showProject(id, viewId) {
		
	if (viewId == 0) {
		var view 	= 'typeProjects';
		var section = 'typeSection';
		
		typeList = false;
		
		$('#graphicContent').fadeOut('fast');
		$('#typeContent').show();
		$('#typeContent').css('opacity', 0);

		$.localScroll.goTo({
			target: '#'+view,
			queue:true,
			duration:0
		}, {hash: '#'+section+id});	

		setTimeout("$('#typeContent').css('opacity', 1);", 250);
		
		lastTypeStep = '#'+section+id;
		
		for (var i = 0; i < typeObjets.length; i++) {
			if (typeObjets[i] == id) typeCurrent = i;		
		}
	} else {
		var view 	= 'graphicProjects';
		var section = 'sectionGraphic';

		graphicList = false;
		
		$('#typeContent').fadeOut('fast');
		$('#graphicContent').show();
		$('#graphicContent').css('opacity', 0);

		$.localScroll.goTo({
			target: '#'+view,
			queue:true,
			duration:0
		}, {hash: '#'+section+id});
		
		setTimeout("$('#graphicContent').css('opacity', 1);", 250);

		lastGraphicStep = '#'+section+id;
		
		
		for (var i = 0; i < graphicObjets.length; i++) {
			if (graphicObjets[i] == id) graphicCurrent = i;		
		}
	}
	
	getLegend(id);
	showViews();
}

function loadProjectPage(view, start) {
	pPage = start;
	
	if (view == 0) typeListPage = start;
	if (view == 1) graphicListPage = start;
	
	$('#projectList').load('/ajax/projectList.php', {start: start, data:view}, function() {
		$('.thumb').mouseover(function() {
			$(this).children().filter('.listInfos').show();
		}).mouseout(function() {
			$(this).children().filter('.listInfos').hide();
		});;
	});
}

function getLegend(currentProject) {
	$('#informations').fadeOut(200, function() {
		$('#informations').load('/ajax/getLegend.php', {data:currentProject}, function() {	$('#informations').fadeIn(200);});
	});
}

function showViews() {
	$('#projectList').hide();
	$('.navigation').show();
}

function loadView(viewName) {
	if ((graphicList && currentPage == 1) || (typeList && currentPage == 0)) {
		$('#projectList').text('');
		
		if (currentPage == 0) typeList = true
		if (currentPage == 1) graphicList = true

		var pPage = (currentPage == 0) ? typeListPage : graphicListPage;
		
		$('#typeContent').hide();
		$('#graphicContent').hide();
		$('.navigation').hide();
		$('#informations').hide();

		$('#projectList').load('/ajax/projectList.php', {start: pPage, data:currentPage}, function() {
			$('#projectList').show();

			$('.thumb').mouseover(function() {
				$(this).children().filter('.listInfos').show();
			}).mouseout(function() {
				$(this).children().filter('.listInfos').hide();
			});
		});
		return;
	}

	showViews();
	
	var view = $('#'+viewName);
	
	if (view.attr('id') == undefined) {
		$.ajax({
   			type: "POST",
   			url: "/ajax/getView.php",
   			data: "id="+viewName,
   			success: function(msg){
   				var data = msg.split('###');
  				data[1] = data[1].replace(/"/g, '');
 				
 				if (viewName == 'typeProjects') 	{
 					$("#typeContent").html(data[0]);
 					typeObjets = data[1].split(',');
					getLegend(typeObjets[typeCurrent]);
 				} else {
					$("#graphicContent").html(data[0]);
					graphicObjets = data[1].split(',');
					getLegend(graphicObjets[graphicCurrent]);
				}
   			}
 		});
	} else {
		if (viewName == 'typeProjects') 	{
			getLegend(typeObjets[typeCurrent]);
		} else {
			getLegend(graphicObjets[graphicCurrent]);
		}

		if (viewName == 'typeProjects') 	{
			$.localScroll.goTo({
				target: '#'+viewName,
				queue:true,
				duration:0
			}, {hash: lastTypeStep});				
		} else {
			$.localScroll.goTo({
				target: '#'+viewName,
				queue:true,
				duration:0
			}, {hash: lastGraphicStep});					
		}
	}	
}

function setupView(viewName) {
	var view = $('#'+viewName);
	
	if (view.attr('id') == undefined) {
		$.localScroll.defaults.axis = 'xy';
	
		$.localScroll.hash({
			target: '#'+viewName,
			queue:true,
			duration:750
		});

		$.localScroll({
			target: '#'+viewName,
			queue:true,
			duration:750,
			hash:true,
			onBefore:function( e, anchor, $target ){
				// The 'this' is the settings object, can be modified
			},
			onAfter:function( anchor, settings ){
				// The 'this' contains the scrolled element (#content)
			}
		});
	}	
}
