//
//
//	simpleSlide 1.6
//
//	website:http://www.simplesli.de/
//	author: David Drew
//	email:  david@ddrewdesign.com
//	
//

function simpleSlide(incoming_options) {
	jQuery(function($) {
		var options = {
			'status_width': 20,
			'status_color_inside': '#fff',
			'status_color_outside': '#aaa',
			'set_speed': 500,
			'fullscreen' : 'false',
			'swipe': 'false',
			'callback': 'function()'
		};
		
		$.extend(options, incoming_options);
			
		$.ss_options = options;
	
		$('.simpleSlide-slide').css('opacity', '0');
		
		$('.simpleSlide-tray').css('margin', '0');
		
		$('.simpleSlide-window').prepend('<span id="ssLoading" style="color: #808080;font-family:Helvetica, Arial, sans-serif;font-size: 12px; margin: 10px 0 0 10px;display: block">Loading slides...</span>');
		
		var no_of_images = $('.simpleSlide-slide img').size();
			
		if(no_of_images > 0) {
			
			var images = new Array();
			var i = 0;
			$('.simpleSlide-slide img').each( function() {
				images[i] = $(this).attr('src');
				i++;
			});
			
			i = 0;
			
			$(images).each( function(){
				var imageObj = new Image();
				imageObj.src = images[i];
                                
				if(imageObj.complete){
					no_of_images--;
					i++;
					if(no_of_images == 0) {
						ssInit();
					};
				} else { 
				    $(imageObj).load( function() {
				  	    no_of_images--;
					    i++;
					    if(no_of_images == 0){
					 	    ssInit();
					    };
				    });
				};
			});
		} else {
			ssInit();
		};	
	});
};

function ssInit(){
	/* Set the dimensions of each simpleSlide window and tray
	 * based on the size of the first 'slide' inside that window.
	 * Every slide within a given tray/window should be uniform in dimensions.
	 * Also, set Status Window size, if it's being used. Fire callback when finished.
	 */
	 
	jQuery(function($) {
		$('.simpleSlide-window').each( function() {	
			
			var window_contents = $(this).html();
			
			var cleaned_contents = removeWhiteSpace(window_contents);
			$(this).html(cleaned_contents);
														
			var slide_count = $(this).find('.simpleSlide-slide').size();		
			$(this).find('.simpleSlide-slide').css('display','block');
			var window_height = $(this).find('.simpleSlide-slide').first().outerHeight();		

			$(this).find('.simpleSlide-slide').css({
				'display':'inline',
				'float':'right',
				'padding-left': 4 + 'px'
			});

			//var window_width = $(this).find('.simpleSlide-slide').first().outerWidth();		
			window_width = 958;
			var window_rel = $(this).attr('rel');

			if($.ss_options.fullscreen == 'false'){
				$(this).css({
					'height': window_height,
					'width': window_width,
					'position': 'relative'
				});
			};			
					
			$(this).css('overflow','hidden');

			setTraySize(this, slide_count, window_width);
			
			setPaging(this);
	
			$(this).find('#ssLoading').remove();
					
			$(this).find('.simpleSlide-slide').animate({
				'opacity': '1'
			}, 400, "swing");
			
			$(this).find('.content_bar').css('opacity', '0');
			
			$(this).find('.brown_slide').animate({
				'opacity': '1'
			}, 400, "swing");
		
		});
		
		/* Fire callback after completion of image load and simpleSlide initialization */
		if(typeof($.ss_options.callback) == 'function'){
			$.ss_options.callback.call(this);
		};
		
		/* Gives each slide an 'alt' with the slide number */
		function setPaging(this_window) {
			var page_count = 1;
			
			$(this_window).find('.simpleSlide-slide').each( function() {
				$(this).attr('alt', page_count);
				page_count++;
			});
		};
		
		
		
		/* Sets size of the "tray" that holds the "slides" */
		function setTraySize(slideWindow, count, viewer_width) {
		
			//var slider_width = count * viewer_width;
			
			//image.width = ( 659 + padding 4 = 663 ) X 5 = 3315
			$(slideWindow).find('.simpleSlide-tray').css({
				'width': 3315 + 'px'
			});
			
			// Initial position of slide tray (container for images)
			//=  - (total width of tray(3315)) - window.width(958) = 2357
			$(slideWindow).find('.simpleSlide-tray').animate({
				'marginLeft': -2357
			}, 1, "swing");
			
			$(slideWindow).find('.simpleSlide-slide').css('display','inline-block');
			
		};	
		
		/* If user chooses to establish Status Window, this function will set
		 * the dimensions of the window based on the desired width.
		 * The window (and its inherent slide's) dimensions are a relative factor
		 * of the main window's size, so the status window will be proportionally
		 * the same as the main window and its tray.
		 */
		function setSimpleSlideStatus(this_rel, height, width, image_count) {
				
			var ratio = $.ss_options.status_width/width;
			var status_height = ratio*height;
			
			$('.simpleSlideStatus-tray[rel="' + this_rel + '"]')
				.css({
					 'width': $.ss_options.status_width * image_count,
					 'height': status_height,
					 'background-color': $.ss_options.status_color_outside
				});
			
			$('.simpleSlideStatus-window[rel="' + this_rel + '"]')
				.css({
					 'width': $.ss_options.status_width,
					 'height': status_height,
					 'background-color': $.ss_options.status_color_inside
				});
			
			if(jQuery.support.opacity){
				$('.simpleSlideStatus-window .simpleSlideStatus-tray[rel="' + this_rel + '"]')
				   .css({
					   'opacity': '.5',
					   'background-color': $.ss_options.status_color_inside
				});
			};
			   
			if(!jQuery.support.opacity){
				$('.simpleSlideStatus-window .simpleSlideStatus-tray[rel="' + this_rel + '"]').css({
					'filter': 'alpha(opacity=50)',
					'background-color': $.ss_options.status_color_inside
				});
			};
		};
						
		/* Actuates upon the clicking of a left- or right-button classed element */
		$('.left-button, .right-button, .jump-to').live('click', function() {
			
			var rel = $(this).attr('rel');
			
			if (!$('div.simpleSlide-tray[rel="' + rel + '"]').is(':animated')) {
				simpleSlideAction(this, rel);
			};
		});
	});
};

var clickCount = 0;
function simpleSlideAction(action, rel_no) {
	jQuery(function($) {	
	
		var move_speed = $.ss_options.set_speed;
		var image_count = $('.simpleSlide-window[rel="' + rel_no + '"]').find('.simpleSlide-slide').size();
		var current_tray_margin = parseInt($('.simpleSlide-tray[rel="' + rel_no + '"]').css('marginLeft'), 10);
		//var window_width = $('.simpleSlide-window[rel="' + rel_no + '"]').innerWidth();
		var window_width = 663; // set this = size of image + padding (659 + 4)
		
		
		if($(action).is('.right-button')) {		
			
			if(current_tray_margin == -368) { // rewind the sequence to the start
				
				clickCount = 0;
				//var j_margin = current_tray_margin - ((image_count - 1) * window_width);
				var j_margin = -2357;
				$(this).find('.content_bar').css('opacity', '0');
			
				$(this).find('.brown_slide').animate({
					'opacity': '1'
				}, 400, "swing");
						
			} else {
				
				clickCount ++;
				var j_margin = current_tray_margin + window_width;
				
				if ( clickCount == 1 ) {
				
					$(this).find('.brown_slide').animate({
						'opacity': '0'
					}, 400, "swing");
				
					$(this).find('.green_slide').animate({
						'opacity': '1'
					}, 400, "swing");
					
				}else if ( clickCount == 2 ) {
					
					$(this).find('.green_slide').animate({
						'opacity': '0'
					}, 400, "swing");
				
					$(this).find('.grey_slide').animate({
						'opacity': '1'
					}, 400, "swing");
					
				}else if ( clickCount == 3 ) {
					
					$(this).find('.grey_slide').animate({
						'opacity': '0'
					}, 400, "swing");
				
					$(this).find('.kermit_slide').animate({
						'opacity': '1'
					}, 400, "swing");
					
				}
						
				};
				
			//alert(current_tray_margin);
			move(j_margin);
		};
	
		function move(new_margin) {
			$('.simpleSlide-tray[rel="' + rel_no + '"]').animate({
				'marginLeft': new_margin
			}, move_speed, "swing");	
		};
	});
};

function removeWhiteSpace(raw) {
	var cleaned_string = raw.replace(/[\r+\n+\t+]\s\s+/g, "");
	return cleaned_string;
};
