
window.addEvent('domready', function() {
	// rollOvers pour les images
	// Application : class="roll"
	//preload images
	var aPreLoad = new Array();
	var aPreLoadi = 0;
	
	//do rollover
	$$('img.roll', 'input.roll').each(function(el){
		//let's preload
		aPreLoad[aPreLoadi] = new Image();
		aPreLoad[aPreLoadi].src = el.src.replace(el.src.replace(/_off([_\.])/, '_on$1'));
		aPreLoadi++;
	
		el.addEvent('mouseover',function(){
			this.setAttribute('src',this.src.replace(/_off([_\.])/, '_on$1'));
		});
	
		el.addEvent('mouseout',function(){
			this.setAttribute('src',this.src.replace(/_on([_\.])/, '_off$1'));
		});
	});

	//
	// acces client
	//
	
	$('acces-client').getElement('.toggle').addEvent('click', function(event){
		event.stop(); //Prevents the browser from following the link.
		
		var img = $('acces-client').getElement('.bg');
		var parent = this.getParent();
		if (parseInt(parent.getStyle('height')) > 26) {
			parent.set('tween', {duration: 'short', onComplete: function(){img.src = img.src.replace(/_on([_\.])/, '_off$1');}});
			parent.tween('height', 26);
		} else {
			img.src = img.src.replace(/_off([_\.])/, '_on$1');
			parent.set('tween', {duration: 'short', onComplete: null});
			parent.tween('height', 161);
		}
	});

	
	//
	// simple tonik tab switcher
	//
	var current_tab = null
	
	$$('.tab').each(function(el) {
		
		id = el.href.replace(/(.*)#(.*)/, '$2');
		el.tab_content = $(id);
		
		if (el.hasClass('on'))
			current_tab = el;
		else {
			//hide content of the tab
			if (el.tab_content != undefined)
				el.tab_content.addClass('hide'); 
		}
		
		el.onclick = function() {
			current_tab.removeClass('on');
			current_tab.tab_content.addClass('hide');
			var arrow = current_tab.getElement('img');
			
			arrow.src = arrow.src.replace(/_on([_\.])/, '_off$1');
			
			el.addClass('on');
			el.tab_content.removeClass('hide');
			
			arrow = el.getElement('img');
			arrow.src = arrow.src.replace(/_off([_\.])/, '_on$1');
			
			current_tab = el;
		}
	});
	
	//
	// projects slideshow
	//
	if ($('project_nav') != undefined) {

		load_slideshow();

		// try to load the page with a project "anchor"
		if (location.hash) {
			if (anchor = location.hash.replace('#', '')) {
				view_slideshow(anchor);
			}
		}
	}

});

// slideshow
var projects;
var currentProject = 1;

function load_slideshow() {
	//var feature = $('feature');
	//var featured = feature.getElement('.featured');
	//var banner = feature.getElement('.banner');
	//var nav = $('project_nav');
	//var projects_container = $('projects');
	
	// set image menu on
	//var img_menu = $('menu').getElement('.load_slideshow img');
	//if (img_menu) {
	//	img_menu.removeClass('roll');
	//	img_menu.removeEvents('mouseover');
	//	img_menu.removeEvents('mouseout');
	//	img_menu.src = img_menu.src.replace(/_off([_\.])/, '_on$1');
	//}
	
	//if (banner) banner.tween('opacity', 0);
	//if (nav) nav.setStyle('opacity', 0).setStyle('display', 'block').tween('opacity', 1);
	//if (featured) featured.tween('left', '-925');
	//if (projects_container) projects_container.setStyles({position: 'absolute', top: 0, left:925}).tween('left', 0);
	
	projects = $$('#projects .project');
	currentProject = 1;
}

function next_slideshow() {
	if (currentProject < projects.length) {
		currentProject++;
		
		var pos = currentProject == 1 ? 0: '-' + (925 * (currentProject-1));
		$('projects').tween('left', pos);
		
		//location.href = location.href.replace(/#(.*)/, '') + '#' + projects[currentProject-1].title;
		location.hash = '#' + projects[currentProject-1].title;
		slideshow_change_page();
	} else {
		page_slideshow(1);
	}
}

function previous_slideshow() {
	if (currentProject > 1) {
		currentProject--;
		
		var pos = currentProject == 1 ? 0: '-' + (925 * (currentProject-1));
		$('projects').tween('left', pos);
		
		//location.href = location.href.replace(/#(.*)/, '') + '#' + projects[currentProject-1].title;
		location.hash = '#' + projects[currentProject-1].title;
		slideshow_change_page();
	} else {
		page_slideshow(projects.length);
	}
}

function page_slideshow(index) {
	currentProject = index-1;
	next_slideshow();
}

function view_slideshow(title) {
	projects.each(function(el, index) {
		if (el.title == title) {
			currentProject = index; // start with 0 and page start with 1 so it will point the previous one ;)
			next_slideshow();
		}
	});
}

function slideshow_change_page() {
	$$('#project_nav .page').each(function(el, index) {
		if (index == currentProject-1)
			el.src = el.src.replace(/_off([_\.])/, '_on$1');
		else
			el.src = el.src.replace(/_on([_\.])/, '_off$1');
	});
}
