/** Configuration - Default values **/
var DELAY = 8000;  // Number of millisecond delay between switching images.



var images = [];
var image_link_urls = [];
var thumbs = [];
var titles = [];
var thumbnails = [];

var offset = 0;  // The offset is the index of the image currently being displayed.
var img;
var thm;
var temporary_div;

var timeoutID;


// Functions to run onload.
var window_onload_functions = [];

if ( window.onload ) {
	window_onload_functions.push(window.onload);
}

function add_window_onload_function ( func ) {
	if (window_onload_functions === null ) {
		func.apply(window);
	} else {
		window_onload_functions.push(func);
	}
}

window.onload = function () {
	for ( var i = 0 ; i < window_onload_functions.length ; i++ ) {
		window_onload_functions[i].apply(window, arguments);
	}
	window_onload_functions = null;
}

add_window_onload_function(
	function() {
		if (document.all&&document.getElementById) {
			navRoot = document.getElementById("nav");
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	});

Effect.FadeTransition = function(element) {
  element = $(element).cleanWhitespace();

  return new Effect.Appear(element, Object.extend({ 
	duration: 1.5,
	fps: 16,
    afterFinishInternal: function(effect) {
      effect.element.setOpacity(0.99999);
    }
    }, arguments[1] || {})
  );
}

/* Num is the number on the little icon that was clicked (1-based) */
function select_image(num) {
	if ( images.length == 0 ) {
		return;
	}
	
	clearTimeout(timeoutID);
	transition_to_image(num - 1);
}

// num is 0-based
function transition_to_image ( num, on_done ) {
	var image_div = $('image_div'), old_offset = offset;
	
	if ( images.length == 0 ) {
		return;
	}
	
	offset = num % images.length;
	
	if ( image_link_urls[offset] ) {
		image_div.up().addClassName("image_link");
	} else {
		image_div.up().removeClassName("image_link");
	}
	
	image_div.up().style.backgroundImage = "url('"+images[old_offset].src+"')";
	$('image-title').innerHTML = titles[old_offset];
	
	thumb_spans = $$('.home-featured-thumb');
	if (thumb_spans) {
		for (i = 1; i <= thumb_spans.length; i += 1) {
			thumb_spans[i-1].down().src = thumbs[(offset+i) % images.length].src
		}
	}
	
	if ( thumbnails[old_offset] ) {
		thumbnails[old_offset].removeClassName("selected");
	}
	
	if ( thumbnails[offset] ) {
		thumbnails[offset].addClassName("selected");
	}
	
	_remove_temporary_div();
	image_div.hide();
	
	if ( offset == old_offset ) {
		if ( on_done ) {
			on_done();
		}
		return;
	}
	
	$('image').src = images[offset].src;
	$('image-title').innerHTML = titles[offset];
	
	thumb_spans = $$('.home-featured-thumb');
	if (thumb_spans) {
		for (i = 1; i <= thumb_spans.length; i += 1) {
			thumb_spans[i-1].down().src = thumbs[(offset+i) % images.length].src
		}
	}
	
	Effect.FadeTransition(image_div, {afterFinish: function() {
		image_div.up().style.backgroundImage = "url('"+images[offset].src+"')";
		image_div.hide();
		
		if ( on_done ) {
			on_done();
		}
	}});
}

function _remove_temporary_div () {
	if ( temporary_div ) {
		if ( temporary_div.parentNode ) {
			temporary_div.parentNode.removeChild(temporary_div);
		}
		
		$('image').show();
		temporary_div = null;
	}
}

function remove_temporary_div () {
	_remove_temporary_div();
	
	if ( thumbnails[offset] ) {
		thumbnails[offset].addClassName("selected");
	}
	
	timeoutID = setTimeout(cycle_image, DELAY);
}

function select_div ( div ) {
	var image_div = $('image_div');
	
	_remove_temporary_div();
	temporary_div = div;
	
	clearTimeout(timeoutID);
	$('image').hide();
	
	if ( thumbnails[offset] ) {
		thumbnails[offset].removeClassName("selected");
	}
	
	image_div.hide();
	image_div.appendChild(temporary_div);
	Effect.FadeTransition(image_div);
}

function cycle_image () {
	if ( images.length == 0 ) {
		return;
	}
	
	clearTimeout(timeoutID);
	transition_to_image(offset + 1,
		function () { timeoutID = setTimeout(cycle_image, DELAY); });
}

function start_cycle () {
	if ( images.length == 0 ) {
		return;
	}
	
	clearTimeout(timeoutID);
	transition_to_image(0,
		function () { timeoutID = setTimeout(cycle_image, DELAY); });
}

function large_image_click () {
	if ( temporary_div ) {
		return;
	}
	
	// Select current image -- pause.
	select_image(offset+1);
	
	if ( image_link_urls[offset] ) {
		if ( /^https?:\/\//i.exec(image_link_urls[offset]) ) {
			window.open(image_link_urls[offset]);
		} else {
			window.location.href = image_link_urls[offset];
		}
	}
}

/** Overlay rollover **/
function overlay_init() {
	if ($('overlay')) {
		var o = $('overlay');
		o.hide();
		o.up().onmouseover=function() {
			if ($('image-title').innerHTML != '') o.show();
		};
		o.up().onmouseout=function() {
			o.hide();
		}
	}
}


/*** SCROLL BAR CODE ***/

var AuroraScroll = Class.create();
Object.extend(Object.extend(AuroraScroll.prototype, Control.Slider.prototype), {
	initialize: function (handle, track, content) {
		this.handle = $(handle);
		this.track = $(track);
		this.content = $(content);
		
		Control.Slider.prototype.initialize.apply(this, [this.handle, this.track, {axis: 'vertical'}]);
		this.options.onChange = this.scroll_content.bind(this);
		this.options.onSlide  = this.scroll_content.bind(this);
		
		this.update_size();
	},
	
	scroll_content: function (val, slider) {
		this.content.scrollTop = val*(this.content.scrollHeight - this.content.clientHeight);	
	},
	
	update_size: function () {
		if (this.content.scrollHeight <= this.content.clientHeight) {
			this.track.hide();
		} else {
			this.track.show();
		}
		
    	this.trackLength = this.maximumOffset() - this.minimumOffset();
		this.handleLength = this.isVertical() ? 
		  (this.handles[0].offsetHeight != 0 ? 
			this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) : 
		  (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth : 
			this.handles[0].style.width.replace(/px$/,""));
	}
});

function register_scroll ( handle, track, content ) {
	var scroll = new AuroraScroll(handle, track, content);
	var updater = window.setInterval(function () {
		scroll.update_size();
	}, 500);
	
	add_window_onload_function(function () {
		scroll.update_size();
		window.clearInterval(updater);
		updater = null;
	});
}