/*

how to instantiate:

if ($('#slideshow').length > 0){
	$('#slideshow').slideshow();
}

sample html:
			
<div id="slideshow" class="slideshow-visit">
	<div id="slide-tray">
		
		<div class="slide" id="first-slide">
			<div class="col-16 no-space-left">
				<img src="images/vista_1.jpg" alt=""  />
			</div>
			<div class="col-6 push-1 space-top">
				<h2>1Title</h2>
				<p>Description</p>
			</div>
		</div><!--slide-->	
		
		<div class="slide">
			<div class="col-16 no-space-left">
				<img src="images/vista_1.jpg" alt=""  />
			</div>
			<div class="col-6 push-1 space-top">
				<h2>2Title</h2>
				<p>Description</p>
			</div>
		</div><!--slide-->	
		
		<div class="slide">
			<div class="col-16 no-space-left">
				<img src="images/vista_1.jpg" alt=""  />
			</div>
			<div class="col-6 push-1 space-top">
				<h2>3Title</h2>
				<p>Description</p>
			</div>
		</div><!--slide-->						
		
	</div>
</div><!--slideshow-->








*/



(function($){

	
	//$.slideshow = function(element, options){
		
	var Slideshow = 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('Slideshow 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('Slideshow 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);
			
			updateText(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>';
			
			$('#slide-tray').append(circle_nav);
			
			$('<a />', { 'href':'#','id':'button-next' }).appendTo('#slideshow').hide();
			$('<a />', { 'href':'#','id':'button-prev' }).appendTo('#slideshow').hide();	
			
			
			$('#button-next').bind('click',function(e){
				
				clearInterval(slideshow_interval);
				
				next();
				return false;
			});
			$('#button-prev').bind('click',function(e){
				
				clearInterval(slideshow_interval);
				
				previous();
				return false;	
			});
			
			$('#slideshow').hover(
				function(){
					$('#button-prev').fadeIn('fast');
					$('#button-next').fadeIn('fast');
				},
				function(){
					$('#button-prev').fadeOut('fast');
					$('#button-next').fadeOut('fast');
				}
			);
			
			
			
			
			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){}

			//move old top slide down
			$('#top-slide').attr('id','').css('z-index',1);
			
			//fade out captions
			$('.slide').find('.slide-caption').fadeOut(500);
			
			//new top slide
			$('.slide').eq(index)
				.attr('id','top-slide')
				.hide();
				
			//fade in new top slide
			$('#top-slide').fadeIn(500,function(){
				
				//hide other slides
				$('.slide:not(#top-slide)').fadeOut(500);
				
				//show new caption
				$('#top-slide').find('.slide-caption').fadeIn(500);
			});
				
			
		};

		var updateText = function(index){	
			
			index = parseInt(index);
			
		};

		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.slideshow = function(options){
		
		return this.each( function(){
			var element = $(this);
						
			// pass options to plugin constructor
			var slideshow = new Slideshow(this, options);
			
			// store plugin object in this element's data
			element.data('slideshow', slideshow);
			
		});
	};


})(jQuery);
