
jQuery.fn.slideshow = function(thumbnails_div_id, settings) {
	
	var defaults = {
		slides: '', //array location of big images
		slideshow_type: '', //'internal' or 'external'
		duration: 6, //seconds delay before next slide
		fadespeed: 1,	//seconds duration of fade
		current_slide: 0 //initially active slide
	};

	settings = $.extend(defaults, settings);

	//return  this.each(function(){
	
	var current_slide = settings.current_slide;
	
	//var obj = $(this);
	//var _self = this;

	var image_viewer = this;
	var thumbnails_ul = $("#"+thumbnails_div_id+" ul");
	var slides_array = settings.slides || new Array();

	var is_playing = true;//autostart

	var interval;//interval iterator
	var total_slides;


	init = function(){
		
		clearInterval(interval);
		//makeSlideshow();

		current_slide = 0;
		total_slides = thumbnails_ul.find("li").length -1;

		build_slideshow_controls();
		set_thumbnail_highlights();
		build_slide_array();
		
		//prep first slide
		$('#imageviewer_portfolio').find('img').addClass('new-slide');
		
		//resize outer container
		var new_height = parseInt( $('#imageviewer_portfolio').find('img').height() );
		$('#imageviewer_portfolio').height(new_height);
		
		//load_slide( slides_array[2] );
		
		play();
	};
	
	//choose active slide
	select = function(index){
		
		
		current_slide = index;
		load_slide();
//alert("select "+index);
	
	};
		
	//start slideshow
	play = function(){
		
		is_playing = true;
		
		interval = setInterval(next, settings.duration * 1000);
	};
	
	//stop slideshow
	stop = function(){
		is_playing = false;
		
		clearInterval(interval);	
	};
	
	//next slide
	next = function(){
//alert(current_slide+" "+total_slides);
		current_slide++;
		if (current_slide > total_slides){
			current_slide = 0;
		}
		load_slide();
	};
	
	//previous slide
	previous = function(){
		
		current_slide--;
		if (current_slide < 0){
			current_slide = total_slides;
		}
		load_slide();
	};
	
	build_slide_array = function(){
	
		//$("#"+setttings.thumbnails+" a").each( function(){
		obj = this;
//alert(thumbnails_div_id);

		$("#"+thumbnails_div_id+" ul li a").each(function(i){
												   
			slides_array.push($(this).attr("href"));
			
			$(this).attr("rel",i);
			
			$(this).click(function(e){
		//alert("click");
				obj.select( $(this).attr("rel") );
				obj.stop();
				e.preventDefault(); 
				//return false;
			});
		});
		
		//alert(slides_array);
	};
	
	//load an image
	load_slide = function(){
		
		clearInterval(interval);	
		
		img_src = slides_array[current_slide];
		img_src += '?random=' + (new Date()).getTime() +'';//ie caching problem fix
		
//alert("load_slide("+img_src+") "+current_slide);
		var new_height = $('#imageviewer_portfolio').find('img.new-slide').height();
		$('#imageviewer_portfolio').height(new_height);

		//prep current slide
		$('#imageviewer_portfolio').find('img.new-slide')
			.removeClass('new-slide')
			.addClass('remove-me');
			//.fadeOut('fast',function(){ //$(this).remove(); });

		
		$('<img />')
			.attr('src', img_src)
			.one('load', function(){

				var init_height = this.height;
				var init_width = this.width;

				var new_img = $('<img />')
					.attr('src', img_src)
					.attr('alt', '')
					.addClass('new-slide')
					.hide();
					
				$('#imageviewer_portfolio').append(new_img);
				
				var new_height = parseInt( $('#imageviewer_portfolio').find('img.new-slide').height() );
				//var new_width = $('#imageviewer_portfolio').find('img').width();
				
				//var height_ratio = init_height/new_height;
				//var width_ratio = init_width/new_width;
				
				//height_ratio = height_ratio.toFixed(3);
				//width_ratio = width_ratio.toFixed(3);
				
//try{ console.log(init_height+"/"+init_width+"  "+new_height+"/"+new_width+"  "+height_ratio+"  "+width_ratio )} catch(e){};
				
				/*if (height_ratio != width_ratio){
					new_height = init_height/width_ratio;
					$('#imageviewer_portfolio').find('img').height(new_height);
					
//try{ console.log(adj_height )} catch(e){};
				}*/
				
				//size height of outer container
				$('#imageviewer_portfolio').stop().animate({height:new_height}, 500, function(){
					
					//fade in new slide
					$('img.new-slide')
						.fadeIn("slow",function(){

							//fade out old slide
							$('img.remove-me')
								.fadeOut('fast',function(){
									
									//remove old slide
									$('img.remove-me').remove();
								})
						});
					
				});
			})
			.each(function(){
			//Cache fix for browsers that don't trigger .load()
				if (this.complete){
					$(this).trigger('load');
				}
			});		
		
		set_thumbnail_highlights();
	
	
		if (is_playing){
			
			interval = setInterval(next, settings.duration * 1000);
			
		}
		
	};
	
	//set thumbnail classes
	set_thumbnail_highlights = function(){
		
		$("#"+thumbnails_div_id+" ul li a").each(function(i){
				
			$(this).toggleClass("active", current_slide == i);

		});	
	};
	
	//slideshow controls
	build_slideshow_controls = function(){
	
		$("#"+thumbnails_div_id).prepend("<a href='#' id='slideshow-controls' class='stop'>Stop</a>");

		$("a#slideshow-controls").bind('click',function(e){
		
//try { console.log('clicky '+$(this).attr("class")); } catch(e){}
		
			if ( $(this).hasClass("stop") ){
				
				$(this).removeClass("stop").addClass("play");
				$(this).html("Play");
				
				stop();
				
			} else {
				
				$(this).removeClass("play").addClass("stop");
				$(this).html("Stop");
				
				play();
				
			}
			
			return false;
		});
		
	};
	
	//kick start
	init();
	//});
};
