(function($){

	
	//$.slideshow = function(element, options){
		
	var Slideshow = function(element, options){
	
		var elem = $(element);
		var obj = this;
		
		var default_options = {
			slide_width:746,
			slide_height:360
		};
				
		var settings = {};
		settings = $.extend({}, default_options, options); 
		
		//element.data('slideshow', this);
		
		var current_index = 0;
		var last_index = 0;
		var total_slides = 0;
		var slideshow_interval;
		
		
		var init = function(element, options){         


//try{ console.log('Slideshow init'); }catch(e){}		

			if ( $('body').hasClass('home') ){
				//add title
				var tmp_title = $('<h1 id="slideshow-title"></h1>');
				$('#container_show').append(tmp_title);
				tmp_title.hide();
			}
			
			$('#first-slide').eq(0).attr('id','bottom-slide');
			
			reset();
		};

		var reset = function(){
			
			if (slideshow_interval){ clearInterval(slideshow_interval); }
			
			
			removeHeading();
			
			//delete controls
			var circle_nav = $('#slideshow').find('#slideshow-circle-nav');
			if ( circle_nav.length > 0 ){
				circle_nav.remove();
			}
			
			//bail out if no slides
			total_slides = $('#slideshow').find('.slide').length;
			if (total_slides < 1){ return; }
			
			//set up view
			//$('#slideshow').find('.slide').hide();
			$('h1#slideshow-title').hide();
			
			$('#slideshow').find('.slide').each(function(i){
				$(this).attr('rel',i);
			});
			
			//arrangeSlides();
			
			//rebuild controls
			buildControls();
			
			show();
			
			setCaption(0);
			
			slideshow_interval = setInterval ( function(){ next(); }, 10000 );			
		};
		
		var arrangeSlides = function(){
			
			//horizontal
			//$('#slide-tray').css('width', total_slides * settings.slide_width);
			//vertical
			//$('#slide-tray').css('height', total_slides * settings.slide_height);
			
			/*$('#slide-tray').find('.slide').each(function(i){
				$(this).show().css('top', i * settings.slide_height);	
			});*/
			
		};
		
		var show = function(){
			
			//$('#slideshow').find('.slide:first-child').show().addClass('active');
			//var o = this;
			$('#slideshow').find('.slide').show();
			//$('#slideshow').slideDown(2000,function(){
				showHeading();
			//});
			
		};
		
		/*notify = function(name){
			//we have a new slideshow loaded
try{ console.log('slideshow notify'); }catch(e){}

			reset();

		
		};*/

		var select = function (index){
						
			//do nothing if already there
			if (index == current_index){
				return;	
			}
			
			//stash index
			last_index = current_index;
			current_index = parseInt(index);
			
			removeHeading();
			
			setHighlight(index);
			
			setCaption(index);
			
			doTransition(index);
		};

		var buildControls = function(){			
			
			//bottom area
			var bottom_html = '<div id="slideshow-nav"><h3></h3><p></p>';
			
			//circles
			bottom_html += '<div id="slideshow-circle-nav"><ul>';
			for (i = 0; i < total_slides; i++){
				bottom_html+= '<li><a href="#" rel="'+i+'"></a></li>';
			}
			bottom_html += '</ul></div></div>';
			
			$('#slideshow').after(bottom_html);
			$('#slideshow-nav').hide().fadeIn();
			
			//prev next
			var prev_next_html = '<div id="slideshow-step-nav">'+
				'<a href="#" title="Previous" id="slideshow-previous"></a>'+
				'<a href="#" title="Next" id="slideshow-next"></a></div>';
			$('#slideshow-nav').append(prev_next_html);
			$('#slideshow-step-nav').hide().fadeIn();
			
			setHighlight(0);
			
			//click events
			$('#slideshow-circle-nav').find('a')
				.bind('click',function(e){
					
//try{ console.log('slideshow click '+$(this).attr('rel')); }catch(e){}
					
					//stop slide show
					if (slideshow_interval){ clearInterval(slideshow_interval); }
					
					var new_index = $(this).attr('rel');
					
					select( new_index );
					
					return false;
				})
				.hover(
				
					function(e){
						$(this).parent().animate({opacity:1},250);
					},
					function(e){
						
						var this_index = $(this).attr('rel');

						if (this_index == current_index){
							$(this).parent().animate({opacity:1},250);
						} else {
							
							$(this).parent().animate({opacity:.5},250);	
						}
					}
				);
				
			$('#slideshow-previous').bind('click',function(e){
					if (slideshow_interval){ clearInterval(slideshow_interval); }
					previous();
					return false;
				});
			$('#slideshow-next').bind('click',function(e){
					if (slideshow_interval){ clearInterval(slideshow_interval); }
					next();
					return false;
				});
			if ( $('body').hasClass('single-work') ){
			//clicking slide image goes to next
				$('.slide').bind('click',function(e){
						if (slideshow_interval){ clearInterval(slideshow_interval); }
						next();
						return false;
					});
				
			}
		};
		
		var next = function(){
			
			var new_index = current_index + 1;
//try{ console.log('next '+new_index+' '+current_index); }catch(e){}

			if (new_index >= total_slides){ new_index = 0; }
			
			select(new_index);
		};
		
		var previous = function(){

			var new_index = current_index - 1;
//try{ console.log('previous '+new_index+' '+current_index); }catch(e){}

			if (new_index < 0){ new_index = total_slides -1; }
			
			select(new_index);
		};
	
		var doTransition = function(index){	
					
			//crossfade
			$('.slide').eq(index)
					.hide()
					.css({'z-index':110})
					.fadeIn('slow',function(){
						$('#bottom-slide').hide().attr('id','');
						$(this).css('z-index',101).attr('id','bottom-slide');
						showHeading(); 
					})
			
			/*
			//vertical slide transition
			var new_y = -settings.slide_height * index;
			//var speed = Math.abs(last_index - index) * 500;
			var speed = 1000;
			if (!speed){ speed = 500; }			
			$('#slide-tray').animate({'top':new_y}, speed, function(){ 
				showHeading(); 
			});
			*/
			
			/*//slide out last slide
			$('#slideshow')
				.find('div.active')
				.removeClass('active')
				.addClass('inactive')
				.animate({left:-950, opacity:0},1000)
				//.fadeOut();*/
			
			
			//next slide
			//$('#slideshow').find('.slide:eq('+ index  +')').hide();			
			
			
			/*$('#slideshow').find('.slide:eq('+ index  +')')
				.addClass('active')
				.hide()
				.css({'z-index':100, left:950, display:'block', opacity:0})//top:425
				.animate({left:0, opacity:1},1000)//top:0
				//.css({left:0})
				.fadeIn(1000, function(){  
					//fade out last image
					
//try{ console.log('moveanim done '+ index +' '+ $(this).attr('rel') ); }catch(e){}
					
					
					$('#slideshow').find('div.inactive')
						.fadeOut('fast')
						.removeClass('inactive')
						.css({'z-index':99});
					
					showHeading();
					
				//	
				});*/
		};
		var setCaption = function(index){
			
			if ( $('body').hasClass('single-work') ){
				return;
			}
			
			var new_h3 = $('#slideshow').find('div.slide:eq('+index+') h3').html();
			var new_p = $('#slideshow').find('div.slide:eq('+index+') p').html();
				
			$('#slideshow-nav').find('h3').fadeOut(500,function(){
				$(this).html(new_h3).fadeIn(500);
			});
			$('#slideshow-nav').find('p').fadeOut(500,function(){
				$(this).html(new_p).fadeIn(500);
			});
		};
		var setHighlight = function(index){
			
			$('#slideshow-circle-nav').find('li').each(function(i){
				
				if (i == index){
					$(this).animate({opacity:1},250);
				} else {
					
					$(this).animate({opacity:.5},250);	
				}
			});
		};
		var removeHeading = function(){
			$('h1#slideshow-title').clearQueue().fadeOut(500);
		};
		var showHeading = function(){
			
			var new_heading = $('#slideshow').find('.slide:eq('+ current_index  +') h2').html();
			
//try{ console.log( new_heading  ); }catch(e){}
			
			$('h1#slideshow-title')
				.show()
				.css({opacity:0})
				.html(new_heading)
				.delay(1000)
				.animate({opacity:1},500);
				//.fadeIn('fast');
		};
		
		this.notify = function(){
		
//try{ console.log('slideshow notify  '+$('#slideshow').find('.slide').length); }catch(e){}

			reset();
		};
		
		this.clear = function(){
			
			if (slideshow_interval){ clearInterval(slideshow_interval); }
			
//try{ console.log('slideshow clear'); }catch(e){}
			removeHeading();
		};


		init(element, options);	
	};

	/*$.fn.slideshow = function(options){                   
		return this.each( function(){
			(new $.slideshow($(this), options));              
		});        
	};*/
	
	
	$.fn.slideshow = function(options){
		
		return this.each( function(){
			var element = $(this);
			
			// return early if this element already has a plugin instance
			//if (element.data('slideshow')){ return; }
			
			// pass options to plugin constructor
			var slideshow = new Slideshow(this, options);
			
			// store plugin object in this element's data
			element.data('slideshow', slideshow);
			
			
		});
	};


	/*$.fn.slideshow.notify = function(name){
		
	//function notify(){
			//we have a new slideshow loaded
try{ console.log('slideshow notify'); }catch(e){}

		this.reset();
	};*/



})(jQuery);
