
function simpleSlide(incoming_options) {
	jQuery(function($) {
		var options = {
			'status_color_outside': '#aaa',
			'set_speed': 500,
			'tumbwidth': 70,
			'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">Lade...</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();
		};



    //Ein- und Ausblenden der Buttons
    $('.simpleSlide-window, .left-button, .right-button').bind('mouseout mouseover', function(e) {
    		if(e.type == 'mouseover'){
    		  $(this).children('.left-button, .right-button').stop(false,true).fadeIn();
    		} else {
    		  $(this).children('.left-button, .right-button').stop(false,true).fadeOut();
    		}
    	}
    );
    	
	});
};

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_rel = $(this).attr('rel');

			var window_height = $(this).find('.simpleSlide-slide[rel="'+window_rel+'"]').first().outerHeight();

			$(this).find('.simpleSlide-slide').css({
				'display':'inline',
				'float':'left'
			});

			var window_width = $(this).find('.simpleSlide-slide[rel="'+window_rel+'"]').first().outerWidth();		

			$(this).css({
					'height': window_height,
					'width': window_width,
					'position': 'relative',
					'overflow':'hidden'
				});
			
			setTraySize(this, slide_count, window_width);
			
			setSimpleSlideStatus(window_rel, window_height, window_width, slide_count);
			
			setPaging(this);
	
			$(this).find('#ssLoading').remove();
						
			if($.ss_options.swipe == 'true' && !$.browser.msie){
				simpleSwipe(this);
			};
					
			$(this).find('.simpleSlide-slide').animate({
				'opacity': '1'
			}, 300, "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;
			
			$(slideWindow).find('.simpleSlide-tray').css({
				'width': slider_width + 'px'
			});
			
			$(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 tumbwidth = $.ss_options.tumbwidth;
			var proportion = tumbwidth / width;
      var tumbheight= proportion * height;
			
			var tumb = new Array();	
			var i = 0;
			$('.simpleSlide-slide[rel="' + this_rel + '"] img').each( function() {
				tumb[i] = $(this).attr('src');
				$('<img style="float:left;" rel="'+this_rel+'" src="'+tumb[i]+'" alt="'+(i+1)+'" />').attr("class","myimage jump-to").appendTo('.simpleSlideStatus-tray[rel="'+this_rel+'"]');
				$('.simpleSlideStatus-number[rel="'+this_rel+'"]').append('<div class="slidernumber jump-to" rel="'+this_rel+'" alt="'+(i+1)+'" >'+(i+1)+'</div>');
				i++;
			});
			i = 0;
			
			$('.simpleSlideStatus-tray[rel="' + this_rel + '"]')
				.css({
					  'width': (tumbwidth+8) * image_count,
					  'height': tumbheight+8,
					  'overflow':'hidden'
			});
			
			$('.simpleSlideStatus-window[rel="' + this_rel + '"]')
				.css({
					'position': 'absolute',
					'height': tumbheight,
					'width': tumbwidth,
					'margin': '1px',
					'padding': '2px',
					'border': '1px solid orange',
					'z-index': '9'
			});
			
			$('.myimage').css({
					'width': tumbwidth,
					'margin': '1px',
					'padding': '2px',
					'padding': '2px',
					'border': '1px solid #ccc',
					'cursor':'pointer'
			});
			
			$('.simpleSlideStatus-number[rel="' + this_rel + '"]').css({
				'width':width
			});
			
			$('.slidernumber').css({
				  'font-family': 'Arial',
					'font-size': '10px',
					'float': 'left',
					'text-align': 'center',
					'color': '#000',
					'width': '12px',
					'height': '12px',
					'margin-left': '1px',
					'padding': '2px',
					'border': '1px solid #ccc',
					'cursor':'pointer'
			});
			$('.slidernumber[alt="1"]').css({'border': '1px solid orange'});
			
		};
						
		/* 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);
			};
		});
	});
};

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 window_width = $('.simpleSlide-window[rel="' + rel_no + '"]').innerWidth();
		var status_window_width = $('.simpleSlideStatus-window[rel="' + rel_no + '"]').innerWidth()+4;
		var status_tray_width = status_window_width * image_count;
		var current_tray_margin = parseInt($('.simpleSlide-tray[rel="' + rel_no + '"]').css('marginLeft'), 10);
		var current_status_window_margin = parseInt($('.simpleSlideStatus-tray .simpleSlideStatus-window[rel="' + rel_no + '"]').css('marginLeft'), 10);
		var current_status_tray_margin = parseInt($('.simpleSlideStatus-window .simpleSlideStatus-tray[rel="' + rel_no + '"]').css('marginLeft'), 10);
		
		if($(action).is('.jump-to')) {
			var to_page = $(action).attr('alt');
			var j_margin = (to_page - 1) * (window_width * (-1));
			var st_margin = (to_page - 1) * (status_window_width * (-1));
			var sw_margin = (to_page - 1) * (status_window_width);
			move(j_margin, sw_margin, st_margin);
		};
		
		if($(action).is('.left-button')) {		
			
			if( (current_tray_margin % window_width) != 0  ){
				var j_margin = 0;
				var st_margin = 0;
				var sw_margin = 0;
			
			}else{
				
				if(current_tray_margin == 0) {
					var j_margin = current_tray_margin - ((image_count - 1) * window_width);
					var st_margin = current_status_tray_margin - ((image_count - 1) * status_window_width);
					var sw_margin = current_status_window_margin + ((image_count - 1) * status_window_width);			
				} else {
					var j_margin = current_tray_margin + window_width;
					var st_margin = current_status_tray_margin + status_window_width;
					var sw_margin = current_status_window_margin - status_window_width;			
				};
			}
			move(j_margin, sw_margin, st_margin);
		};
		
		if($(action).is('.right-button')) {

			if( (current_tray_margin % window_width) != 0  ){
				var j_margin = 0;
				var st_margin = 0;
				var sw_margin = 0;

			}else{
			
				if(current_tray_margin == (image_count - 1) * (window_width * -1)) {
					var j_margin = 0;
					var st_margin = 0;
					var sw_margin = 0;					
					
				} else {
					var j_margin = current_tray_margin - window_width;
					var st_margin = current_status_tray_margin - status_window_width;
					var sw_margin = current_status_window_margin + status_window_width;			
				};
				
			}
			move(j_margin, sw_margin, st_margin);
		};		
	
		function move(new_margin, new_swindow_margin, new_stray_margin) {
			
			var select = ((((-1)*new_margin) / window_width)+1);
			$('.slidernumber').css({'border': '1px solid #ccc '});
			$('.slidernumber[alt="' + select + '"]').css({'border': '1px solid orange'});
			
			$('.simpleSlide-tray[rel="' + rel_no + '"]').animate({
				'marginLeft': new_margin
			}, move_speed, "swing");
			
			$('.simpleSlideStatus-window .simpleSlideStatus-tray[rel="' + rel_no + '"]').animate({
					'marginLeft': new_stray_margin				 
			}, move_speed, "swing");
			
			$('.simpleSlideStatus-tray .simpleSlideStatus-window[rel="' + rel_no + '"]').animate({
					'marginLeft': new_swindow_margin		 
			}, move_speed, "swing");		
		};
	});
};

function removeWhiteSpace(raw) {
	var cleaned_string = raw.replace(/[\r+\n+\t+]\s\s+/g, "");
	return cleaned_string;
};

 $(document).ready( function() {

    	
    });
