(function($){

	
	//$.slideshow = function(element, options){
		
	var GiantSlideshow = function(element, options){
	
		var elem = $(element);
		var obj = this;
		
		var default_options = {
			slide_width:812,
			slide_height:542
		};
				
		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){         

			if ($('#slideshow').find('.slide').length <= 1){
				return false;
			}

//try{ console.log('GiantSlideshow init '+$('#slideshow').find('.slide').length); }catch(e){}		
			
			$('#first-slide').eq(0).attr('id','top-slide').show();
			
			reset();
		};

		var reset = function(){
			
			if (slideshow_interval){ clearInterval(slideshow_interval); }
			
//try{ console.log('GiantSlideshow reset '); }catch(e){}
			
			
			//bail out if no slides
			total_slides = $('#slideshow').find('.slide').length;
			if (total_slides < 1){ return; }
			
			
			$('#slideshow').find('.slide').each(function(i){
				$(this).attr('rel',i);
			});
			
			
			//build controls
			buildControls();
						
			slideshow_interval = setInterval ( function(){ next(); }, 10000 );			
		};
		
		
		var show = function(){
			
			//$('#slideshow').find('.slide:first-child').show().addClass('active');
			//var o = this;
			$('#slideshow').find('.slide').show();
			
		};

		var select = function (index){
			
//try{ console.log('slideshow select '+index); }catch(e){}
			
			//do nothing 
			if (index == current_index){
				return;	
			}
			
			//stash index
			last_index = current_index;
			current_index = index;
						
			setHighlight(index);
						
			doTransition(index);
		};

		var buildControls = function(){
			
			var circle_nav = '<div id="circle-nav"><ul>';
			
//try{ console.log('buildControls '+total_slides); }catch(e){}

			
			for (i = 0; i < total_slides; i++){
				circle_nav += '<li><a href="#" rel="'+i+'"></a></li>';
			}
			
			circle_nav += '</ul></div>';
			
			$('#header').append(circle_nav);
			
			
			setHighlight(0);
			
			$('#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);	
						}
					}
				);
		};
		
		var next = function(){
			
			var new_index = current_index + 1;
			
			if (new_index >= total_slides){ new_index = 0; }
			
			select(new_index);
		};
		
		var previous = function(){
			
			var new_index = current_index - 1;
			
			if (new_index < 0){ new_index = total_slides -1; }
			
			select(new_index);
		};
		
		var doTransition = function(index){	
			
			index = parseInt(index);
//try{ console.log('doTransition  '+index+' '+$('.slide').eq(index).length); }catch(e){}


			$('#top-slide').attr('id','');
			
			$('.slide').eq(index)
				.attr('id','top-slide')
				.hide()
				.fadeIn('fast',function(){
					$('.slide').not('#top-slide').hide();
				})

		};

		var setHighlight = function(index){
			
			$('#circle-nav').find('li').each(function(i){
				
				if (i == index){
					$(this).animate({opacity:1},250);
				} else {
					
					$(this).animate({opacity:.5},250);	
				}
			});
		};



		init(element, options);	
	};	
	
	$.fn.giantSlideshow = function(options){
		
		return this.each( function(){
			var element = $(this);
						
			// pass options to plugin constructor
			var giant_slideshow = new GiantSlideshow(this, options);
			
			// store plugin object in this element's data
			element.data('slideshow', giant_slideshow);
			
		});
	};


})(jQuery);
