
function geo() {
	if (!document.getElementsByTagName) // Check for browser support
			return false;
	if (rot13) // Initiate ROT13 only if needed
		var map = rot13init(); 
	var tooltip_js_on = '';
	var tooltip_js_off = '';
	var links = document.getElementsByTagName('a'); // Get all anchors
	function geo_decode(anchor) { // function to recompose the orginal address
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = linktext; // IE Fix
			}
		}
	}
	for (var l = 0 ; l < links.length ; l++) { // Loop through the anchors
		var anchor = links[l];
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = address; // IE Fix
			}	

			links[l].onclick = function() { // Encode links when clicked
				geo_decode(this);
			}
			links[l].onmouseover = function() { // Display tooltip when links are hovered
				if (this.getAttribute('title') == tooltip_js_off) { // Set custom tooltip if specified
					this.setAttribute('title',tooltip_js_on);
					geo_decode(this); // Encode links when hovered (so that the address appears correctly in the browser's status bar)
				}
			}
		}
	}
}

var rot13 = 0;

function rot13init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i)] = s.charAt((i+13)%26);
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
	return map;
}

function str_rot13(a,map) {
	var s = "";
	for (var i = 0 ; i < a.length ; i++) {
		var b = a.charAt(i);
		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
	}
	return s;
}


function controlValueOnFocus(control) {
	c = $(control);
	if (c.val() == c.defaultValue) {
		c.val() = '';
	}
}

function controlValueOnBlur(control) {
	if (control.value == '') {
		control.value = control.defaultValue;
	}
}

function submitSearch() {
	if ( (document.search.term.value != 'Rechercher...') && (document.search.term.value !='Search...') ) {
		document.search.submit();
	}
}

function submitForm( id ) {
	form = $('#' + id + ' form');
	if (form) {
		var options = { target: '#' + id };
		form.ajaxSubmit(options);
	}
}


function toggleprojects() {
	$('#projectsmenu').slideToggle("fast");
	return false;
}

function toggleres() {
	$('#com-content').slideUp("fast");
	$('#instit-content').slideUp("fast");
	$('#res-content').slideToggle("fast");
	state = $('#res-content').css('height');
	if (state == '1px' || state == 'auto') {
		$('#res-toggler').addClass('active');
		$('#com-toggler').removeClass('active');
		$('#instit-toggler').removeClass('active');
	}
	else {
		$('#res-toggler').removeClass('active');
	}
	return false;
}

function togglecom() {
	$('#instit-content').slideUp("fast");
	$('#res-content').slideUp("fast");
	$('#com-content').slideToggle("fast");
	state = $('#com-content').css('height');
	if (state == '1px' || state == 'auto') {
		$('#com-toggler').addClass('active');
		$('#instit-toggler').removeClass('active');
		$('#res-toggler').removeClass('active');
	}
	else {
		$('#com-toggler').removeClass('active');
	}
	return false;
}

function toggleinstit() {
	$('#com-content').slideUp("fast");
	$('#res-content').slideUp("fast");
	$('#instit-content').slideToggle("fast");
	state = $('#instit-content').css('height');
	if (state == '1px' || state == 'auto') {
		$('#instit-toggler').addClass('active');
		$('#com-toggler').removeClass('active');
		$('#res-toggler').removeClass('active');
	}
	else {
		$('#instit-toggler').removeClass('active');
	}
	return false;
}

function update_ui() {
		$('#counter').html( counter + ' / ' + total );
		for(i=1;i<=total;i=i+1) {
			if (i != counter) {
				$('img#image' + i + '_small').attr('src', '/WProjects/sepia_' + type + '/' + images_small[i] );
			}
			else {
				$('img#image' + i + '_small').attr('src', images_small[i] );
			}
		}
		$('#project-holder img').attr('src', images[counter]);
}

var counter = 1;

$(document).ready(function(){
	/* Decrypt mails */
	geo();
	$('a.projets').bind('click', toggleprojects);
	$('a#res-toggler').bind('click', toggleres);
	$('a#com-toggler').bind('click', togglecom);
	$('a#instit-toggler').bind('click', toggleinstit);
	if ( _open_menu ) {
		$(_open_menu).toggle();
		$('#projectsmenu').toggle();
		$(_active_cat).addClass('active');
		$(_active_project).addClass('active');
	}
	$('a#credits-toggler').bind('click', function(){
		$('#credits').show();
		$('#donnees').hide();
		$('#description').hide();
		return false;
	});
	$('a#donnees-toggler').bind('click', function(){
		$('#donnees').show();
		$('#credits').hide();
		$('#description').hide();
		return false;
	});
	$('a#description-toggler').bind('click', function(){
		$('#description').show();
		$('#credits').hide();
		$('#donnees').hide();
		return false;
	});

	$('#button-left').bind('click', function(){
		counter = counter - 1;
		if (counter < 1) counter = total;
		update_ui();
	});

	$('#button-right').bind('click', function(){
		counter = counter + 1;
		if (counter > total) counter = 1;
		$('#counter').html( counter + ' / ' + total );
		update_ui();
	});
	
	$('img#image1_small').bind('click', function(){
		counter = 1;
		update_ui();
	});

	$('img#image2_small').bind('click', function(){
		if (total > 1) counter = 2;
		update_ui();
	});

	$('img#image3_small').bind('click', function(){
		if (total > 2) counter = 3;
		update_ui();
	});

	$('img#image4_small').bind('click', function(){
		if (total > 3) counter = 4;
		update_ui();
	});
	
	$('#site-selector-res').hover(
		function(){
			$('#site-res').fadeIn('slow');
		},
		function(){
			$('#site-res').fadeOut('slow');
		}
	);

	$('#site-selector-comm').hover(
		function(){
			$('#site-comm').fadeIn('slow');
		},
		function(){
			$('#site-comm').fadeOut('slow');
		}
	);

	$('#site-selector-instit').hover(
		function(){
			$('#site-instit').fadeIn('slow');
		},
		function(){
			$('#site-instit').fadeOut('slow');
		}
	);

});



