/**
 * @author florent@ezoulou.be 20092010 - feel free to use
 */
// filters initialization
var viewRecent = false;
var viewAvailable = false;
// page inititlization
var currentPage = null;

$(document).ready(function(){ 
	// remove now obsolete html style. height it set for the no-javascript users. 
	$("#gallery .images").css({height:"auto"});
	// activate filters system 
	activateFiltersSystem ();
	// minimal check before page activation
	if ($("#gallery .image").length>6) activatePageSystem ();
});

function activatePageSystem () {
	// variables needed for the pages mechanism
	var tnl = $("#gallery .line").length; 	// total number of lines
	var lpp = 2;							// default lines per page
	// if window is tall enough, we allow more row of images per page
	if ($(window).height() >= hl) {
		lpp = 3;
		if ($(window).height() >= hl2) lpp = 4;
	}
	//  calcuate the number of pages
	var np =0;										
	if (tnl <= lpp) np = 1;
	else np = Math.ceil(tnl/lpp);
	// we detect if we need to activate the paged mechanism
	if (np>1){
		// creates the pages
		for (var i = 1; i <= np; i++) {
			$("#gallery .images").append('<div class="page" id="page'+i+'"></div>');
			// directely hides all pages after first one (avoid flickering → the page will be hidden again after → when activating first page)
			if (i>1) $('#gallery .page#page'+i).hide();
		}
		// places lines into pages		
		for (var i = 0; i < tnl; i++) {
			var line = $("#gallery .line:eq(0)").clone();
			$("#gallery .line:eq(0)").remove();
			var pn = Math.floor(i/lpp);
			line.appendTo("#gallery .images .page:eq("+pn+")")
		}
		// build navigation
		$("#gallery").append('<div class="navigation"><a class="prev" href="#" title="'+prevTxt+'">'+prevTxt+'</a><a class="next" href="#" title="'+nextTxt+'">'+nextTxt+'</a></div><p class="pager" />');
		// adds pager item
		for (var i = 1; i <= np; i++) {
			$("#gallery .pager").append('<a title="page '+i+'" href="#page'+i+'">'+i+'</a>');
		}
	}
}

function activateFiltersSystem () {
	// prepare filters : build html
//	$("#gallery").prepend('<div class="filters">'+displayOnly+'<a class="recent" href="#" title="'+filterRecent+'">'+filterRecent+'</a><span> - </span><a class="available" href="#" title="'+filterAvailable+'">'+filterAvailable+'</a></div>');
	$("#gallery").prepend('<div class="filters">'+displayOnly+'<a class="available" href="#" title="'+filterAvailable+'">'+filterAvailable+'</a></div>');
	// filters button events
	$("#gallery .filters .recent").click(function () {
		if (viewRecent) {
			$(this).removeClass("selected");
			viewRecent = false;								
		} else {
			$(this).addClass("selected");
			viewRecent = true;
		}
		filtersImages();
		return false;
	});
	$("#gallery .filters .available").click(function () {
		if (viewAvailable) {
			$(this).removeClass("selected");
			viewAvailable = false;
		} else {
			$(this).addClass("selected");
			viewAvailable = true;
		}
		filtersImages();
		return false;
	});
}

function activatePage(id) {
	// we declare the page
	currentPage = id;
	// we hide other pages 
	$('#gallery .page').hide();
	// we show requested page
	$('#gallery .page#page'+id).fadeIn();	
	// we adjust navigation height
	var nh = $("#gallery .images").height()+'px';
	$("#gallery .navigation").css({height:nh});	
	// we reset pager					
	$("#gallery .pager a").removeClass("selected");
	// pager : activate current page 
	$('#gallery .pager a:eq('+(id-1)+')').addClass("selected");
	// activate next
	if (id < $("#gallery .page").length) {
		$("#gallery .navigation .next").show();
		$("#gallery .navigation .next").attr("href", "#page"+(id+1));
	} else {
		$("#gallery .navigation .next").hide();
	}
	// activate prev
	if (id > 1) {
		$("#gallery .navigation .prev").show();						
		$("#gallery .navigation .prev").attr("href", "#page"+(id-1)); 
	} else {
		$("#gallery .navigation .prev").hide();
	}
}

function updatePage() {
	var currentHash = window.location.hash;
	// Nothing's changed since last polled.
	if (currentHash =="#page"+currentPage || (currentHash=='' && currentPage == 1)) return; 
	// in case we directely coming into the ligtbox
	if (currentPage == null && currentHash == '#lightbox') {
		window.location.hash = "";
		activatePage(1);
	}
	else {
		// if an anchor has been specified in url. This is the main navigation system!!. 
		if (currentHash.indexOf('#page') == 0) {
			var pageToShow = currentHash.substring(5, currentHash.length) * 1;
			activatePage(pageToShow);
		}
		else {
			if (currentHash == "") {
				activatePage(1);
			}
		}
	}
}


/*
 *  explain what case is covered by this tests !:-)
 *  
 *  if (window.location.hash=='') 
 */
function filtersImages() {
	for (var i = 0; i < $("#gallery .image").length; i++) {	
		var date = $("#gallery .image:eq("+i+")").attr("class");
		date = date.substr(date.indexOf("date_")+5);
		if (date.indexOf(" ") > 0) date = date.substr(0, date.indexOf(" "));
		date = parseInt(date);
		if (
			(viewRecent && date < 2006) 
			||
			(viewAvailable && $("#gallery .image:eq("+i+")").hasClass("stock_"))
		) {		
			$("#gallery .image:eq("+i+")").fadeTo(500, 0.25);
		} else {
			$("#gallery .image:eq("+i+")").fadeTo(500, 1);
		}
	}					
}				

