var firstStud = true;

function showtab(num) {
	$('c0').className = 'c1u ln';
	if ($('c1')) $('c1').className = 'c1u ln';
	$('c2').className = 'c1u ln';
	$('c3').className = 'c1u ln';
	$('c'+num).className = 'c1s ln';
	

	if ($('tagList')) 		$('tagList').hide();
	if ($('studentsList'))	$('studentsList').hide();
	$('home').hide();
	$('informations').hide();
	$('contacts').hide();

	if (num == 0) 	$('home').show();
	if (num == 0 && $('tagList')) 	$('tagList').show();
	if (num == 1) 	$('home').show();
	if (num == 1) 	$('studentsList').show();
	if (num == 2) 	$('informations').show();
	if (num == 3) 	$('contacts').show();

	if (firstStud && num == 1) {
		var obj = $('students');
		selects(obj);
		obj.init();
		firstStud = false;
	}	
}

function thumbSwitch(id, file) {
	$(id).src = file;
}

function search(mode, id, reset) {
	if (id != undefined && id != '') $('tag_'+id).toggleClassName('tagSel');
	
	try {
		if (reset == undefined) reset = false;
	
		showLoading();
	
		var student = $('students').value;
		var tags	= $$('.tag');
		var link	= '';

		for (var i = 0; i < tags.length; i++) {
			var tag = tags[i];
			if (mode != 0 || reset) tag.removeClassName('tagSel');
			if (tag.hasClassName('tagSel')) link	+= "tag[]="+tag.id.replace(/tag_/g, '')+"&";	
		}	

		if (mode == 0) {
			$('students').selectedIndex = 0;
			link = link.substring(0, link.length-1);
		} else {
			link = "students="+student;
		}
		
		if (link != 'students=0') {
			url_Set(link.replace(/\[\]/g, ''));
		} else {
			url_Set('');		
		}
	
		new Ajax.Updater('gallery', '/ajax/gallery.php?mode='+mode, {onComplete: hideLoading(), asynchronous:true, parameters:link});
	} catch (e) {
		//alert(e);
	}
}

function showLoading() {
	//$('loading').show(); 
}

function hideLoading() {
	$('loading').hide();
}

function showImg(img) {
	$('image_mask').show();
	$('image').update("<a href='javascript:$(\"image_mask\").hide();$(\"image\").hide();'><img src='/"+img+"' /></a>");
	$('image').show();
}

/*  Droppably standalone, version 1.1
 *  (c) 2007 EC A L  / Lionel Tardy
 *
 *  http://www.droppably.com/
 *  
 *  Droppably is free software published under a creative commons
 *  license.
 *
 *  You are free to:
 *  - to share, to copy, distribute and transmit the work
 *   
 *  Under the following conditions: 
 *  - Attribution. You must attribute the work in the manner 
 *    specified by the author or licensor (but not in any way 
 *    that suggests that they endorse you or your use of the work).
 *  - Noncommercial. You may not use this work for commercial
 *    purposes.
 *  - No Derivative Works. You may not alter, transform, or build
 *    upon this work.
 *
 *  For more informations, please visit.
 *  http://creativecommons.org/licenses/by-nc-nd/2.5/ch/
 *
/*--------------------------------------------------------------------------*/

var url_last = '_';

function url_Retrive() {
	if (location.hash != url_last) {

		//analyse du hash
		if (location.hash == '' || location.hash == '#') {
			search();
			url_last = location.hash;
		} else {
			var cleanHash = location.hash.split('#')[1];

			new Ajax.Updater('gallery', '/ajax/gallery.php', {onComplete: hideLoading(), asynchronous:true, parameters:cleanHash.replace(/tag/g, 'tag%5B%5D')});
			url_last = location.hash;
			
			/*recoche les cases*/
			var d = document.getElementsByName('tag');
			for(var i=0; i<d.length; i++) {
				d[i].checked = false;
				d[i].sync();
			}
			
			var tagList = cleanHash.replace(/=/g, '-').split('&');

			for (var i = 0; i < tagList.length; i++) {
				if (tagList[i].split("-")[0] == 'tag') {
					$(tagList[i]).checked = true;
					$(tagList[i]).sync();
				}
			}
		}
	}
}

function url_Set(curAnchor) {		
	var new_hash = "#"+curAnchor;
	
	if (location.hash != new_hash) {
		location.hash = new_hash;
		url_last      = new_hash;
	}
}

new PeriodicalExecuter(url_Retrive, 0.5);