/* 
	SLIDER using CSS and BACKGROUND POSITION
	NOTE : Requires array "slider_images" set before slider_setup() is called 
*/

	var slide_duration = 4000;
	var transition_duration = 1000;
	var transition_steps = 2; // lower number = smoother
	var maxoffset = 50;
	var currently_loaded = -1;
	var slider_width = 0;
	var slide_width = 0;
	var current_slide = -1;
	var timer_id;
	var slide_src = '';
	var offset = 0;
	
	var is_busy = false;
	
		
	// 1.	preload all images
		function slider_setup() {
			currently_loaded++;
			var imageObj = new Image();
			if (currently_loaded < slider_images.length) { // preload next image
				imageObj.src = slider_images[currently_loaded];
				imageObj.onLoad = slider_setup();
			} else { // begin slider
				window.setTimeout("slider_init()", 100);
			}
		}
		
	// 2.	images preloaded, setup slider	
		function slider_init() {
			// get width of slider element
				window.setInterval("move_offset()", 40);
				check_slider_width();
			
			// "width_check"
				var i = document.getElementById("width_check");
				i.style.position = "absolute";
				i.style.top = "-1000px";
				i.style.left = "-1500px";
			
			// startup
				next_slide();
		}
	
	// 3.	Next slide
		function next_slide() {
			if (!is_busy) {
				current_slide++;
				if (current_slide >= slider_images.length) { current_slide = 0; }
				change_slide();
			}
		}
		
		function prev_slide() {
			if (!is_busy) {
				current_slide--;
				if (current_slide < 0) { current_slide = slider_images.length-1; }
				change_slide();
			}
		}
		
		function change_slide() {
			window.clearInterval(timer_id);
			offset = 65;
			is_busy = true;
			slide_src = slider_images[current_slide];
			
			// get width of current slide
				document.getElementById("width_check").onload = function() { }
				document.getElementById("width_check").src = '';
				document.getElementById("width_check").onload = width_pause;
				document.getElementById("width_check").src = slide_src;
				
			// hand over to onload event to figure out image width
		}
		
		function width_pause() { 
			var w = document.getElementById("width_check").offsetWidth;
			
			if (w == 0) {
				window.setTimeout("width_pause()", 50);
			} else {
				next_slide_continue();
			}
		}
		
		function next_slide_continue() {
			// get width of current slide
				slide_width = document.getElementById("width_check").offsetWidth;
				
			// calculate half-way
				var halfway = Math.round((slider_width - slide_width)/2);
			
			// set new slide
				setOpacity(1, "slider");
				document.getElementById("slider").style.backgroundImage = 'url("' + slide_src + '")';
				document.getElementById("slider").style.backgroundPosition = halfway + 'px 0';
				document.getElementById("slider").style.backgroundRepeat = "no-repeat";
			
			// fade in
				fadeTo("slider", 100, (transition_duration/2));
				window.setTimeout("set_notbusy()", (transition_duration/2))
				timer_id = window.setInterval("fadeout_slide()", (slide_duration+(transition_duration/2)));
				
			// slide in
			var ms = slide_duration+transition_duration;
		}
		
		function set_notbusy() { is_busy = false; }
		
		function move_offset() { offset -= 1; }
		
		function update_location() {
			var halfway = Math.round((slider_width - slide_width)/2);
			var x = halfway + offset;
			document.getElementById("slider").style.backgroundPosition = x + 'px center';
		}


	// 4.	Fade out slide
		function fadeout_slide() {
			is_busy = true;
			window.clearInterval(timer_id);
			fadeTo("slider", 1, (transition_duration/2));
			window.setTimeout("set_notbusy()", (transition_duration/2));
			window.setTimeout("next_slide()", (transition_duration/2)+10);
		}
		
		
	
	
/*	
	Check for window resize
*/
		function check_slider_width() {
			if (document.getElementById("slider")) {
				slider_width = document.getElementById("slider").offsetWidth;
				update_location();
			}
		}

		window.setInterval("check_slider_width()", 40);
