/**
 * @author Chris
 */
var images = [];
$(document).ready(function() {
	$('#slideshow_link').click(function() {
		//get slideshow images
		if(SlideShow.images.length == 0) {
			$('.slideshow_path').each(function() {
				images.push($(this).html());	
			});
			SlideShow.images = images;
		}		

		if($('#slideshow_start_count').length) {
			var start = $('#slideshow_start_count').html();
			SlideShow.start(start - 1);
		} else {
			SlideShow.start();			
		}
	})
});

var SlideShow = {  
    parent:"body",  
	SlideShow_open: false,
    window_id: 'slideshow_container',  
    height:null,  
	width: null,
	current_index: 0,
	images: [],
	rendered: false,
	speed: 5000,
	paused: false,
	control_fade_timeout: null,
	opened: false,
	cached_image_count: 0,
	cached_image_index: 0,
	cached_images: [],
	paused_wake: false,
	index_wake: 0,
	scale_image: true,
    close:function()  
    {  
		this.paused_wake = this.paused;
		this.index_wake = this.current_index;
		SlideShow.pause();
		this.opened = false;
        $(".modal-window").hide();  
        $(".modal-overlay").hide();
		$(".modal-blocker").hide();
		$("#slideshow_control").hide();
		$('#slideshow_control_background').hide();
    },
	cacheImages: function(cache_indexes) {
		for (var x = 0; x < cache_indexes.length; x++) {
			var cache_index = cache_indexes[x];
			if (cache_index < 0) {
				cache_index = this.images.length - cache_index - 2;
			}
			else 
				if (cache_index >= this.images.length) {
					cache_index = cache_index - this.images.length;
				}
			var cache_image = new Image();
			cache_image.src = this.images[cache_index];
			this.cached_images[cache_index] = cache_image;
		}
	},
	start: function(start) {	
		this.opened = true;		

		if(!SlideShow.rendered) {
			var start = start || 0;
			this.current_index = start;
			this.cached_image_index = start;
			this.container = this.drawContainer();
			// add to the dom
			$(this.parent).append(this.container);
			
			this.postRenderStyling();
			this.addListeners();
			this.rendered = true;
			SlideShow.play();
		} else {
			start = this.index_wake;
			if(!this.paused_wake) {
				SlideShow.play();
			}
	        $(".modal-window").show(); 
			$("#slideshow_control").show(); 
			$('#slideshow_control_background').show();
	        $(".modal-overlay").show();
			$(".modal-blocker").show();
		}
		this.loadImage(images[start]);
	},
	postRenderStyling: function() {
		// IF IE6, make background the height of the doucument, and set the control to absolute position
		if($.browser.msie && $.browser.version.substr(0,1)=="6") {
			$('#slideshow_control').css('position', 'absolute');
			$('#slideshow_control_background').css('position', 'absolute');
			$('.modal-overlay').css('position', 'absolute');
			
			//start interval for IE6 slideshow_control moving
			this.move_control_interval = setInterval(function() {
				//move the control to 50px above scroll height
				var control_top = Math.round(($(window).height() + $(window).scrollTop()) - 100);
				$('#slideshow_control').css('top', control_top + 'px');
				$('#slideshow_control_background').css('top', control_top + 'px');
			}, 100);
		}
		
		//center the control box
		$('#slideshow_control').css('margin-left', -($('#slideshow_control').width() / 2)  -4  + 'px');
		$('#slideshow_control_background').css('margin-left', -($('#slideshow_control').width() / 2) -4  + 'px');
		
		var offset = $('#'+this.window_id).offset();
		var margin_top = 0 - offset.top;
		$('#'+this.window_id).css('margin-top', margin_top);	
	},
	addListeners: function() {
		// Hover effects for buttons
		
		// forward button over
		$('#slideshow_forward').hover(function() {
			$(this).children().attr('src', 'img/forward_hover.gif');
		},function(){
			$(this).children().attr('src', 'img/forward.gif');
		});
		
		// back button hover
		$('#slideshow_back').hover(function() {
			$(this).children().attr('src', 'img/back_hover.gif');
		},function(){
			$(this).children().attr('src', 'img/back.gif');
		});
		
		//play/pause hover on/off
		$('#slideshow_play_pause').hover(function() {
			var child_image = $(this).children().attr('src');
			if (child_image == 'img/play.gif') {
				$(this).children().attr('src', 'img/play_hover.gif');
			} else {
				$(this).children().attr('src', 'img/pause_hover.gif');
			}
		},function(){
			var child_image = $(this).children().attr('src');
			if (child_image == 'img/play_hover.gif') {
				$(this).children().attr('src', 'img/play.gif');
			} else {
				$(this).children().attr('src', 'img/pause.gif');
			}
		});
		
		// swap play/pause hover and start/pause the show
		$('#slideshow_play_pause').click(function() {
			if(SlideShow.paused) {
				$(this).children().attr('src', 'img/pause_hover.gif');
				SlideShow.play();
			} else {
				$(this).children().attr('src', 'img/play_hover.gif');
				SlideShow.pause();
			}
		});
		
		$('#slideshow_forward').mousedown(function() {
			var resume_show = false;
			if(!SlideShow.paused) {
				resume_show = true;
				SlideShow.pause();
			}
			
			SlideShow.showNext();
			
			if(resume_show) {
				SlideShow.play();
			}
		});
		$('#slideshow_back').mousedown(function() {
			var resume_show = false;
			if(!SlideShow.paused) {
				resume_show = true;
				SlideShow.pause();
			}
			
			SlideShow.showPrevious();
			
			if(resume_show) {
				SlideShow.play();
			}

		});
		
		$('#slideshow_close').click(function() {
			SlideShow.close();
		});
		
		$('#slideshow_scale').click(function() {
			if(!SlideShow.scale_image) {
				$(this).html('Resize On');
				SlideShow.scale_image = true;				
			} else {
				$(this).html('Resize Off');
				SlideShow.scale_image = false;
			}	
			SlideShow.loadImage(SlideShow.images[SlideShow.current_index]);
		});
		
		$('.modal-control').fadeTo('medium', 0);
		$('#slideshow_control_background').fadeTo('medium', 0);
  		$('.modal-control').hover(function() {
			clearTimeout(SlideShow.control_fade_timeout);
			$('#slideshow_control_background').fadeTo('medium', .7);
			$(this).fadeTo('medium', 1);
		}, function() {
			//set delay before hiding
			SlideShow.control_fade_timeout = 
				setTimeout(
					"$('#slideshow_control').fadeTo('medium', 0); $('#slideshow_control_background').fadeTo('medium', 0)", 
					750
				);
		});
  
        $(".close-window").click(function(){SlideShow.close();});  
        $(".modal-overlay").click(function(){SlideShow.close();});  
		
		// key specific bindings
		$(document).keyup(function(event){
		    if (event.keyCode == 27) {
				// close on escape
	        	SlideShow.close();
	    	}
			
			if (event.keyCode == 37) {
				//move backwards
				event.preventDefault();
				SlideShow.showPrevious();
			}
			
			if (event.keyCode == 39) {
				// move forwards
				event.preventDefault();
				SlideShow.showNext();
			}
		});
		
		$('#slideshow_delay_down').click(function(event) {
			SlideShow.changeDelay(-1000);
		});
		$('#slideshow_delay_up').click(function(event) {
			SlideShow.changeDelay(1000);
			event.preventDefault();
		});
		
		//window resize
		$(window).resize(function(){
			SlideShow.loadImage(SlideShow.images[SlideShow.current_index]);
		});

	},
	changeDelay: function(delay){
		var new_delay = SlideShow.speed + delay;
		if (2000 <= new_delay && new_delay <= 10000) {
			SlideShow.speed += delay;
		}
		
		//update div
		var text_delay = SlideShow.speed / 1000;
		if(text_delay < 10) {
			text_delay = '&nbsp;' + text_delay;
		}
		$('#slideshow_delay').html(text_delay);
		
		if (!SlideShow.paused) {
			//reset the interval
			SlideShow.pause();
			SlideShow.play();
		}
	},
	drawContainer: function() {
        var container = '\
		<div id="image_preload" > \
		</div> \
   		<div class="modal-overlay"></div> \
		<div id="' + this.window_id + '" class="modal-window"> \
	        <img id="slideshow_image" src=""> \
		</div> \
		<div id="slideshow_control_background" class="modal-control-background">&nbsp; \
		</div> \
		<div id="slideshow_control" class="modal-control"> \
				<div id="slideshow_speed"> \
					Delay:<span id="slideshow_delay_down"><img src="img/delay_down.gif"/></span> <span id="slideshow_delay">&nbsp;5</span> <span id="slideshow_delay_up"><img src="img/delay_up.gif"/></span> \
				</div> \
				<div id="slideshow_back" class="modal-control-button"><img src="img/back.gif"></div> \
				<div id="slideshow_play_pause" class="modal-control-button"><img src="img/pause.gif"></div> \
				<div id="slideshow_forward" class="modal-control-button"><img src="img/forward.gif"></div> \
				<div id="slideshow_scale" class="modal-control-button" style="margin-top:9px;margin-left:5px;margin-right:5px;font-size:14pt">Resize On</div> \
				<div id="slideshow_close">Exit</div> \
		</div> \
		<iframe class="modal-blocker"/>';
		return container;
	},
	showFirst: function() {
		this.current_index = 0;
		this.loadImage(this.images[this.current_index]);
	},
	showLast: function() {
		this.current_index = this.images.length - 1;
		this.loadImage(this.images[this.current_index]);
	},
	showNext: function() {
		if (this.current_index < this.images.length - 1) {
			this.current_index++;
			this.loadImage(this.images[this.current_index]);
		}
		else {
			// loop to start if at end
			this.showFirst();
		}
	},
	showPrevious: function() {
		if(this.current_index > 0) {
			this.current_index--;
			this.loadImage(this.images[this.current_index]);
		} else {
			this.showLast();
		}
	},
	loadImage: function(source) {
		clearInterval(SlideShow.interval);
		if(!this.cached_images[this.current_index]) {
			image = new Image();
			$(image).load(function() {
				for(var i = 0; i < SlideShow.images.length; i++) {
					if(SlideShow.images[i] == image.src ) {
						SlideShow.cached_images[i] = image;
						break;
					}
				}
				SlideShow.displayImage(image);
			});
			this.cacheImages([this.current_index+1, this.current_index+2, this.current_index-1]);
			image.src = source;
		} else {
			this.displayImage(this.cached_images[this.current_index]);	
		}

	},
	displayImage: function(image) {
		var width  = image.width;
		var height = image.height;
	
		if (SlideShow.scale_image) {
			var document_width = $(window).width();
			var width_difference = width - document_width;
			if (width_difference > 0) {
				//resize width and proportionally height
				var ratio = 1 - (width_difference / width);
				width = width - width_difference;
				height = Math.floor(height * ratio);
			}
			
			var document_height = $(window).height();
			var height_difference = height - document_height;
			if (height_difference > 0) {
				//resize width and proportionally height
				var ratio = 1 - (height_difference / height);
				height = height - height_difference;
				width = Math.floor(width * ratio);
			}
		}
		
		var image_element = $(
			'<img id="slideshow_image" src="' + image.src + 
			'" width="' + width + 'px"' +
			'" height="' + height + 'px">');
				
		//advance slideshow on image click
		$(image_element).click(function() {
			SlideShow.showNext();
		});
		
		$('#slideshow_image').replaceWith(image_element);
		$('#'+SlideShow.window_id).css('width', width + 'px');
		$('#'+SlideShow.window_id).css('height', height + 'px');
		$('#'+SlideShow.window_id).css('margin-left', (width / -2) + 'px');
		var offset = $('#'+SlideShow.window_id)
		
		var margin_top = 0 - height/2;
		$('#'+SlideShow.window_id).css('margin-top', margin_top + 'px');
		
		var offset = $('#'+SlideShow.window_id).offset();
		margin_top = margin_top + offset.top * -1;
		$('#'+SlideShow.window_id).css('margin-top', margin_top + 'px');
				
		if ($.browser.msie && $.browser.version.substr(0, 1) == "6") {
			$('#slideshow_control').css('top', height * .95 + 'px');
			$('#slideshow_control_background').css('top', height * .95 + 'px');
			$('.modal-overlay').css('height', $(document).height());
			$('.modal-blocker').css('height', $(document).height());
		}
		if(!SlideShow.paused) {
			SlideShow.play();
		}
	},
	pause: function () {
		SlideShow.paused = true;
		clearInterval(SlideShow.interval);
	},
	play: function () {
		SlideShow.paused = false;
		clearInterval(SlideShow.interval);
		SlideShow.interval = setInterval("SlideShow.showNext()", SlideShow.speed);
	}
}; 
