
// Hack for Mac IE - just show all slides
if (navigator.userAgent.match(/MSIE.*Mac/))
{
	window.onload = function(){
		var project_slides = document.getElementById('project_slides');
		project_slides.children[0].style.display = 'block';
		project_slides.children[1].style.display = 'block';
		project_slides.children[2].style.display = 'block';
		project_slides.children[3].style.display = 'block';
	};
}


// Write out a debug string to the top of document
// can be styled with class .debug {}
function debug_log(in_item) {
	new Insertion.Bottom($('page_container')," <p class=\"debug warning\"> "+String(in_item)+" </p> ")
}


var Igloo = {
	// Constants
	Version				: '1.0',
	Offset				:20,// offset of slides in pixels
	SlideWidth			:300, // width of slides in pixels
	Delay				:4000, //delay between slides
	TextDelay			:20000, //delay after slides with text on
	KEY_SPACE			:32,// space key code

	// Variables, filled in at runtime
	slide_container		:null,
	slides				:null,
	selectedIndex		:10,
		
	timer				:null,
	rel_url				:"",
	
	animating 			:false,
	
	// Set up the slideshow elements and listen to events
	// run on window load by function call at end of this file
	// 
	init : function () {
		

		Igloo.set_paths();
		
		// fade in the headlink
		new Effect.Appear('head_link', {from:0.1, to:1.0,duration:6.0} )
		
		// set up sound - is there a neater way to preload?
		// Adding the sound but not playing doesn't work
		// Pause seems to be caused mostly by loading sound module, 
		// so trigger that first by just playing silence
		//Sound.play(Igloo.rel_url + 'sounds/pop.mp3');
	//	Sound.play(Igloo.rel_url + 'sounds/silence.mp3');

		if ($('sub_navigation'))
			new Effect.Appear('sub_navigation', {from:0.1, to:1.0, duration:2.0});
		
		// only show on home page - should really only show once
		if ($('hints'))
			{
			window.setTimeout("new Effect.Appear('hints', {duration:0.5});", 3500);
			window.setTimeout("new Effect.Fade('hints', {duration:3.0});", 4500);
			}
			
		// extra instructions on each page
		if ($('extras'))
			{
			window.setTimeout("new Effect.Appear('extras', {duration:0.5,from:0.0,to:0.7});", 4500);
			window.setTimeout("new Effect.Fade('extras', {duration:3.0, from:0.7});", 7500);
			}
		

		if ($('loading'))
			new Effect.Fade('loading', { duration:0.5});	


		Igloo.slide_container = $('project_slides');
		
		if (!Igloo.slide_container)
			return;
		
		Igloo.slides = document.getElementsByClassName('slide');
	
		Igloo.attach();
		Igloo.position_all_slides();
		
		Igloo.capture_events();
		Igloo.play(Igloo.TextDelay);// First time through give them some time to read
	},
	
	
	// // get hold of the relative path to site root from this html page
	//
	//
	set_paths : function () {
		
		// used by add_control to add relative links to images
		// this assumes that slideshow.js links are relative links from pages
		// we don't use '/' as it breaks when viewing site locally
		scripts = $A(document.getElementsByTagName('script'));
		
		scripts.each(function (s) {
			if (s.src.endsWith("igloo.js"))
				{
				Igloo.rel_url = s.src.gsub("igloo.js","../");
				}
		});
	},
	
/* EVENTS */	
	
	// attach ourselves to events on slideshow links
	//
	//
	attach : function () {
		Igloo.slides.each(function (slide) {
			//Each slide should contain a link
			link = $A(slide.getElementsByTagName('a'))[0];
	
			 if (link && String(link.getAttribute('rel')) == "slide")
				{
				Event.observe(link,'mouseover',Igloo.respond_mouseover.bindAsEventListener(Igloo));
				Event.observe(link,'mouseout',Igloo.respond_mouseout.bindAsEventListener(Igloo));
				Event.observe(link,'click',Igloo.respond_click.bindAsEventListener(Igloo));
				// could do this for Safari 2.0
				link.onclick = function() {return false;};
				}
			
			});


	},



	// Respond to hovers on links 
	//
	//
	respond_mouseover : function (event) {
		link = 	Event.element(event);
		
	/*	link_slide = link.up().up();
		
		i = link_slide.iglooIndex
		if ((i == Igloo.selectedIndex )	|| (i == Igloo.selectedIndex + 1) )
			{
				
			}
		else
			{
			new Effect.Pulsate(link,{duration:0.2, from:0.9, to:1.0, pulses:1});		
			}
*/
	
	},
	
	
	// Respond to hovers out of links
	//
	//
	respond_mouseout : function (event) {
		link = 	Event.element(event);	

	},


	// Respond to clicks on links
	//
	//
	respond_click : function (event) {
	
		link = 	Event.element(event);
		
		// Get the parent of link and select it
		link_slide = link.up().up();
	
		if (link_slide)
			{
			
			// if it's the first selected slide, set link_slide to the one before
			// so we go backwards
			i = link_slide.iglooIndex;
			if (i == Igloo.selectedIndex && i > 0)	
				{
				link_slide = Igloo.slides[i-1]	
				}
			//	Sound.play(Igloo.rel_url + 'sounds/pop.mp3');
	
		
			Igloo.select_slide(link_slide,0.6);
			}
		else
			alert("Couldn't find slide");
	
			// tell browser to ignore event - doesn't work in Safari 2.0
			Event.stop(event);

	},



	/* watch out for the keyboard events we're interested in */
	capture_events: function() {
	
	Event.observe(document, 'keydown', function(event)
		{ 
		 key = event.which || event.keyCode;	
			
			switch(key) 
			{
			// for most of these events we ignore them if not playing
		    case Igloo.KEY_SPACE:
			if (Igloo.playing())
				Igloo.stop();
			else
				Igloo.step_forward();
			break;
			
			case Event.KEY_LEFT:
				Igloo.delay = Igloo.delay * 1.2; //make the delay longer for each back request
				Igloo.step_backward();
				Event.stop(event);
			break;

		    case Event.KEY_RIGHT:
				Igloo.delay = Igloo.delay * 0.95; //make the delay shorter for each forward request
				Igloo.step_forward();
				Event.stop(event);
			break;
			
			default:
			//	alert("key: " + key);
			break;
			}
			
		});
	},


/* Playing */

	playing : function () {
		return (Igloo.timer != null);
	},




	play : function (in_delay) {

		Igloo.stop();
		Igloo.timer = window.setTimeout("Igloo.step_forward();", in_delay);
	},
	
	action_start : function () {
		if (Element.visible('control_play'))
			{
			new Effect.SwitchOff('control_play');
			}
		new Effect.Appear('control_pause', {duration:1.0});
		Igloo.play();
	},

	action_stop : function () {
		
		new Effect.SwitchOff('control_pause');
		new Effect.Appear('control_play', {duration:1.0});
		Igloo.stop();
	},
	
	stop : function () {
	//clear our timer
	if (Igloo.timer)
		{
		window.clearTimeout(Igloo.timer);
		Igloo.timer = null;
		}
	},

	step_forward : function () {
		// Make sure play controls are showing
		if ($('play_controls'))
			$('play_controls').show();

				
		// Stop one before end of array
		if (Igloo.selectedIndex == Igloo.slides.length -2)
			Igloo.stop();
		else
			{// select the next slide
			slide = Igloo.slides[Igloo.selectedIndex+1];
			Igloo.select_slide(slide,0.4);
			delay = slide.hasClassName('slide_text') ? Igloo.TextDelay : Igloo.Delay;	
			Igloo.play(delay);// reset the timer again
			}
	},


	step_backward : function () {

		// Stop at end of array
		if (Igloo.selectedIndex == 0)
			Igloo.stop();
		else
			{// select the next slide
			slide = Igloo.slides[Igloo.selectedIndex-1];
			Igloo.select_slide(slide,0.4);	
			delay = slide.hasClassName('slide_text') ? Igloo.TextDelay : Igloo.Delay;	
			Igloo.play(delay);// reset the timer again
			}
	},



/* Slide positions */

	animation_over : function(event) {

	// Change classnames of slides after animation so that they become transparent. 
	// Change requested by Martin - we now do this after animation finishes
	Igloo.slides.each(function (slide) {
		i = slide.iglooIndex;
		s = Igloo.selectedIndex
		if (i == s || i == s+1)
			{
			slide.addClassName('selected_slide');	
			}
		else
			{
		slide.removeClassName('selected_slide');	
			}
			
	});

	// callback from timer to make sure animations finished before new one starts	
	Igloo.animating = false;
	
	},
	
	
	

	// Called from clicks or once from init to select a slide
	select_slide : function (slideToSelect,duration) {
		Igloo.stop();
		
		// if we're already animating, don't do anything
		if (Igloo.animating) return;
	
		// set our flag to say we're animating, and set timer to remove flag after we are finished
		Igloo.animating = true;
		setTimeout(function(){Igloo.animation_over()},600);
	
		i_to_select = slideToSelect.iglooIndex;
		if (i_to_select == 0 || i_to_select == Igloo.slides.length -1)
			{
			// warn that we are at end of range
			new Effect.Pulsate(slideToSelect,{duration:0.4, from:0.8, pulses:2});
			}
		
		
		// restrict to end of range -1 
		i_to_select = i_to_select == Igloo.slides.length -1 ? i_to_select - 1: i_to_select;
		Igloo.selectedIndex = i_to_select;

		// Walk through the slides, positioning them according to their relation to selected slide
		// We want to end up with the following
		// ||| sel sel+1 |||
		// An accordian with only the selected slides fully shown - others are cropped
		Igloo.slides.each(function (slide) {
			Igloo.position_slide(slide,duration)
			i = slide.iglooIndex;
			s = Igloo.selectedIndex
		
			if (i == s || i == s+1)
				{
				// A one off change to let the user know they've seen it
				slide.addClassName('seen_slide');	
		
				slide.addClassName('selected_slide');	
				}
			else
				{
			//	slide.removeClassName('selected_slide');	
				}
				
		});

	
		
	},





	position_slide : function (in_slide, duration) {
		desired_offset = 0
		in_slide.show();
		if (in_slide.iglooIndex == Igloo.selectedIndex) 
			desired_offset = (Igloo.Offset*(in_slide.iglooIndex - 1)) + Igloo.SlideWidth;
		else
			desired_offset = Igloo.slide_offset(in_slide);
		
		desired_offset = Math.round(desired_offset);
		desired_offset = desired_offset - (desired_offset % 2);
		
		current_offset = in_slide.offsetLeft;

		if (duration > 0)
			{
			new Effect.Move(in_slide, {x: desired_offset - current_offset, y: 0, duration : 0.3});
			}
		else
			{
			in_slide.style.left = desired_offset+"px";
			}
		

		
	},

	
	slide_offset : function (in_slide) {
		if (in_slide.hasClassName('singleton'))
			return 510;
		else if (in_slide.iglooIndex < Igloo.selectedIndex)
			return Igloo.Offset*in_slide.iglooIndex;
		else if (in_slide.iglooIndex > Igloo.selectedIndex)
			return Igloo.Offset*(in_slide.iglooIndex-Igloo.selected_count()) + Igloo.selected_width();
	},
	
	selected_width : function () {
		return Igloo.SlideWidth * Igloo.selected_count();
	},
	
	selected_count : function () {
		return 2;
	},
	
	first_selected_index : function() {
		// for now just return selectedIndex
		Igloo.selectedIndex;
	},

	// Called from init to position slides initially

	position_all_slides : function () {
		
		// first position the slide container off to left of its container
		if (Igloo.slide_container)
			{
			Igloo.slide_container.style.left = "-" + (Igloo.SlideWidth - Igloo.Offset) + "px";
			}
	
		index = 0;
		reverse_index = Igloo.slides.length;
		
		Igloo.slides.each(function (slide) {
			slide.iglooIndex = index;// store this for future use in slide object
			slide.style.zIndex = reverse_index;
			Igloo.position_slide(slide,0)
			new Effect.BlindDown(slide, {duration:(0.8 * index/2.5) + 0.3});
			index ++;
			reverse_index --;
		});

		

		Igloo.select_slide(Igloo.slides[0],0);
	
	},

	show_all_slides : function () {
		Igloo.slides.each(function (slide) {
			Element.show(slide);
		});
	
	},

	
	hide_all_slides : function () {
		Igloo.slides.each(function (slide) {
			Element.hide(slide);
		});
	
	}

	
};


Event.observe(window,'load',Igloo.init,false);	










