var image_fader = {
	
	current_index : 0,
	
	section_tabs : [],
	
	images : [],
	
	fade_effect : "",
	
	slide_show : "",
	
	initialize : function() {
		
		var section_tab_methods = {	
			init_hover : function(element) {
				Event.observe(element, 'mouseover', image_fader.change_images);
				Event.observe(element, 'mouseout', image_fader.start_slide_show);
			},
			remove_active_class : function(element) {
				element.removeClassName('current_section');
			}
		};		
		Element.addMethods('li', section_tab_methods);					
		
		var image_link_methods = {	
			image_link_hovers : function(element) {
				Event.observe(element, 'mouseover', image_fader.stop_slide_show);
				Event.observe(element, 'mouseout', image_fader.start_slide_show);
			}
		};		
		Element.addMethods('a', image_link_methods)
		
		image_fader.images = $$("div#images a");
		image_fader.images.invoke('image_link_hovers');
		image_fader.images.reject(function(element, i) {return (i == 0);}).invoke('hide');
		
		image_fader.section_tabs = $$("ul#site_sections li");
		
		image_fader.section_tabs.invoke('init_hover');
		
		setTimeout(image_fader.start_slide_show, 10);
	},
	
	
	change_images : function(event) {
		image_fader.slide_show.stop();
		
		image_fader.section_tabs.invoke('remove_active_class');
		
		list_item = Event.findElement(event, 'li');
		
		list_item.addClassName('current_section');
		
		if (image_fader.current_index != image_fader.section_tabs.indexOf(list_item))
		{
			image_fader.current_index = image_fader.section_tabs.indexOf(list_item);
		
			if (image_fader.fade_effect != "")
				image_fader.fade_effect.cancel();
	
			$('image_fader').style.display = "block";
			$('image_fader').setOpacity(.6);
	
			image_fader.fade_effect = Effect.Fade('image_fader', {duration : .5});
		}
			
		image_fader.images.reject(function(element, i) {return (i == image_fader.current_index);}).invoke('hide');					
		image_fader.images.select(function(element, i) {return(i == image_fader.current_index);}).invoke('show');
	},
	
	set_next_tab_as_current : function() {
		if (image_fader.current_index < (image_fader.images.size() - 1))
			image_fader.current_index = image_fader.current_index + 1;
		else
			image_fader.current_index = 0;
	},
	
	change_to_next_image : function() {
		image_fader.set_next_tab_as_current();
		image_fader.section_tabs.invoke('remove_active_class');
		
		image_fader.section_tabs[image_fader.current_index].addClassName('current_section');
		
		$('image_fader').show();
		$('image_fader').setOpacity(.6);
		Effect.Fade('image_fader', {duration : .5});
		image_fader.images.reject(function(element, i) {return (i == image_fader.current_index);}).invoke('hide');					
		image_fader.images.select(function(element, i) {return(i == image_fader.current_index);}).invoke('show');
	},
	
	start_slide_show : function() {
		image_fader.slide_show = new PeriodicalExecuter(image_fader.change_to_next_image, 4);
	},
	
	stop_slide_show : function() {
		if (image_fader.slide_show != "")
			image_fader.slide_show.stop();
		
		//image_fader.slide_show = "";
	}
};

document.observe("dom:loaded", image_fader.initialize);