(function($) {
  

  
  $.fn.nasSlideshow = function(o){
    
    o = $.extend({
        speed: 800, 
        percentage: 100,
        margin: 32,
        startPosition: 0, 
        maxHeight: 480,
        pager: '#pagination a'
    }, o || {});
     
        
    var cw = 0; // current width
    var tw = 0; // total width
    var ci = 0; // current index
    var pr = 100 - o.percentage; // percentage remainder

	if(location.hash) {	
		
		
		var id = location.hash.substr(3), 
			id = id-1;
			
		$('#nas-slideshow li div').removeClass('enlarged');
		var theImage = $('#nas-slideshow li div:eq(' + id + ')');
		theImage.addClass('enlarged');
		
		$('#nas-slideshow li div').each(function(e){
		
			if($(this).parents('li').find('.vimeo-player').length) {
				$(this).parents('li').find('img').show();
				$(this).parents('li').find('.vimeo-player').hide();	
			}
		});

		if(theImage.parents('li').find('.vimeo-player').length) {

			theImage.parents('li').find('img').hide();	
			theImage.parents('li').find('.vimeo-player').show();
		}

	} else {
		$('#nas-slideshow li div:first').addClass('enlarged');
	}
      
    
      
    $(this).parent().css('float','left');
    $(this).parent().css('margin-right', o.margin);
      
    this.each(function() {  
      
      // $(this).show().css('opacity', 0.5);
      
      if(!$(this).hasClass('enlarged')) $(this).fadeTo(0, 0.5); // fade all items except the initial item
      if($(this).hasClass('enlarged')) $(this).fadeTo(0, 1); // fade all items except the initial item
      
      if ($(this).height() > o.maxHeight) o.maxHeight = $(this).height(); // override the max height with the tallest item
      
      var w = $(this).width(); // get the image width with margin
      var h = $(this).height(); // get the image height
      
      // set the height of the parent li item
      $(this).parent().css('height', o.maxHeight);
        
      if(!$(this).hasClass('.enlarged')) {
        
        // reduce the width and height based on the percentage option
        w = Math.round((w / 100) * o.percentage);
        h = Math.round((h / 100) * o.percentage); 
          
      }
        
      // set the width of the parent li item
      $(this).parent().css('width', w);
    
      // set the width and height of the image
      $(this).css('width', w);
      $(this).css('height', h);
 		  
 		  // tally up the total width required for the images
 		  tw += w + o.margin;
 		       
    });

	$(this).parent().parent().parent().css('height', o.maxHeight);

	if(location.hash) {	
		var id = location.hash.substr(3), 
			id = id-1;
		cw = 0;
		var ti = id; // click items index
		
		$('#nas-slideshow li div').each(function(){
	        if ($("#nas-slideshow li div").index(this) < ti) {
	          cw += $(this).width();
	          cw += o.margin;
	        }
      	});
		
		$("#nas-slideshow").css({ left: "-" + cw + 'px'});
		
	} else {
	    $(this).parent().parent().parent().css('left', o.startPosition);
	}
       
    
    // add a few pixels to stop it from dropping down when it animates
    $(this).parent().parent().parent().css('width',tw + 200); 
    
    
       
    return this.click(function() {  
      
       cw = 0;
       
       // get the width and height of the currently selected item
       var prev_width = $("#nas-slideshow li div.enlarged").width();
       var prev_height = $("#nas-slideshow li div.enlarged").height();
       
       // reduce the width and height based on the percentage option
       var small_width = Math.round((prev_width / 100) * o.percentage);
       var small_height = Math.round((prev_height / 100) * o.percentage);
       
       // get the width and height of the clicked item
       var this_width = $(this).width();
       var this_height = $(this).height();
      
       // increase the width and height based on the percentage option
       var full_width = this_width + Math.round((this_width / o.percentage) * pr);
       var full_height = this_height + Math.round((this_height / o.percentage) * pr);
      
       // adjust the total width based on the 2 items animating
       tw = (tw - prev_width - this_width) + small_width + full_width;
      
       var ti = $('#nas-slideshow li div').index(this); // click items index
       
        
       $('#nas-slideshow li div').each(function(){
        if ($("#nas-slideshow li div").index(this) < ti) {
          cw += $(this).width();
          cw += o.margin;
        }
      });

	  // if(!$('#nas-slideshow li div').hasClass('enlarged')) $(this).fadeTo(0, 0.5); // fade all items except the initial item
	  //      if($('#nas-slideshow li div').hasClass('enlarged')) $(this).fadeTo(0, 1); // fade all items except the initial item
      
      // adjust the width after resize 
      if(ci < ti){
        var actualWidth = (cw - (prev_width - small_width)) - o.startPosition;
      } else {
        var actualWidth = cw - o.startPosition;
      }
      
      // set current index to value of clicked index
      ci = ti;
      
		// alert(ti);
      
      var theImage = $('#nas-slideshow li div:eq(' + (ti) + ')');

// #TODO
	$('#nas-slideshow li div').each(function(e){
		
		if($(this).parents('li').find('.vimeo-player').length) {
			$(this).parents('li').find('img').show();
			
			var videoElm = $(this).parents('li').find('.vimeo-player');
				videoElm.hide();
	
				var froogaloop = $f(videoElm[0].id);
					froogaloop.api('unload');	
		}
	});
	
	if(theImage.parents('li').find('.vimeo-player').length) {
		theImage.parents('li').find('img').hide();	
		var videoElm = theImage.parents('li').find('.vimeo-player');
			videoElm.show();
			
			var froogaloop = $f(videoElm[0].id);
				froogaloop.api("seekTo", 0);
				froogaloop.api('play');
	}
      
	
	
	
	
		// $('#nas-slideshow li div').each(function(e){
			// if($(this).parents('li').find('.vimeo-player').length) {
			// 	$(this).parents('li').find('img').show();
			// 	
			// 	var videoElm = $(this).parents('li').find('.vimeo-player');
			// 		videoElm.hide();
			// 
			// 	var froogaloop = $f(videoElm[0].id);
			// 	froogaloop.api("seekTo", 0);
			// 	froogaloop.api('stop');
			// }
		// });
		


		// if($(this).parents('li').find('.vimeo-player').length) {
		// 	$(this).parents('li').find('img').hide();
		// 	$(this).parents('li').find('.vimeo-player').show();	
		// 
		// }
		
		//       if(!$(this).hasClass('.enlarged')) {
		//         $('.captions p').each(function() {
		//            $(this).fadeTo("normal", 0, function(){$(this).hide()});
		//          });
		// 
		// // if($(this).parents('li').find('.vimeo-player').length) {
		// // 	$(this).parents('li').find('img').show();
		// // 	
		// // 	var videoElm = $(this).parents('li').find('.vimeo-player');
		// // 		videoElm.hide();
		// // 	
		// // 	alert('not-' + videoElm[0].id);
		// // 	
		// // 	// var froogaloop = $f(videoElm[0].id);
		// // 	// froogaloop.api("seekTo", 0);
		// // 	// froogaloop.api('stop');
		// // 	
		// // }
		// 
		//       }
      
      
      // if the index is 0 reset to the start position
      // otherwise carry one with animation
      if (ti == 0) {
        $("#nas-slideshow").animate({ left: o.startPosition }, o.speed, null,function(){
          $('.captions p:eq(' + ci + ')').show().fadeTo("normal", 1);
        });
      } else {
        $("#nas-slideshow").animate({ left: "-" + actualWidth}, o.speed, null, function(){
          $('.captions p:eq(' + ci + ')').show().fadeTo("normal", 1);
        });
      }
      
      

      
      
        // animate (shrink) the enlarged image parent li elem
        $("#nas-slideshow li div.enlarged").parent().animate(
    		  { 
      		  width: small_width, 
      			height: o.maxHeight 
      		}, o.speed
    		);
    		
    		
  			// animate (shrink) the enlarged image
        $("#nas-slideshow li div.enlarged").animate(
          { 
            width: small_width, 
            height: small_height,
            opacity: 0.5
          }, o.speed
        ).removeClass("enlarged");
  	
       
        // add the enlarged class to the click elem
        $(this).addClass('enlarged');
		
		
		if(!$(this).hasClass('.enlarged')) {
		// 
		// if($(this).parents('li').find('.vimeo-player').length) {
		// 	$(this).parents('li').find('img').hide();
		// 
		// 	var videoElm = $(this).parents('li').find('.vimeo-player');
		// 		videoElm.show();
		// 
		// 	alert(videoElm[0].id);
		// 
		// 	// var froogaloop = $f(videoElm[0].id);
		// 	// 		froogaloop.api("seekTo", 0);
		// 	// 		froogaloop.api('play');
		// 
		// 	// 	videoElm.show();
		// 	// 	
		// 	// var player = $f(videoElm);
		// 	// player.api('play');
		// }
      
        
        /////////////
       
        // animate (enlarge) the enlarged image
        $(this).animate(
  			  { 
    			  width: full_width, 
    				height: full_height,
    				opacity: 1 
    			}, o.speed
  			);
			
  			// animate (enlarge) the enlarged image parent li elem
        $(this).parent().animate(
          { 
            width: full_width, 
            height: o.maxHeight 
          }, o.speed
        );
        
        $(o.pager).each(function() {
            $(o.pager + '.selected').removeClass('selected');
        });
        
        $('#pagination li a:eq(' + ci + ')').addClass('selected');
 			 
 		  } // end if
				
				return false;
    });
    
    
					
  }; // eof
  
  

	$.fn.nasPager = function(pager, obj) {
		
		if(location.hash) {	
			var id = location.hash.substr(3), 
				id = id-1;
			$('#pagination.nas-pagination li a').removeClass('selected');
			$('#pagination.nas-pagination li a:eq(' + id + ')').addClass('selected');

		} else {
			$('#pagination.nas-pagination li a:eq(0)').addClass('selected');
		}
		
		$(this).click(function() {
			
			var leftColSize = 0;
            var currentWidth = 0;

            $(pager).each(function() {
                $(pager).removeClass('selected');
            });

			var thisIndex = $(pager).index(this),
				loc = thisIndex+1;
			
			location.hash = '!/' + loc;
			
			if ($(this).attr('class') != "selected") {
				
                $('#nas-slideshow li div').each(function() {
					// 
					// if($(this).parents('li').find('.vimeo-player').length) {
					// 	$(this).parents('li').find('img').show();
					// 
					// 	var videoElm = $(this).parents('li').find('.vimeo-player');
					// 		videoElm.hide();
					// 
					// 		var froogaloop = $f(videoElm[0].id);
					// 			froogaloop.api('unload');	
					// }

					
					// if($(this).parents('li').find('.video').length) {
					// 	$(this).parents('li').find('img').hide();
					// 	$(this).parents('li').find('.video').show();	
					// }
					
					// alert($(this).width());
                    if ($("#nas-slideshow li div").index(this) < thisIndex) {
                        currentWidth += $(this).width();
                        currentWidth += 32;
                    }
                });
            }

            var actualWidth = currentWidth - leftColSize;
            currentIndex = thisIndex;

			var theImage = $(obj + ':eq(' + thisIndex + ')');
			
			// #TODO
			$('#nas-slideshow li div').each(function(e){
			
				if($(this).parents('li').find('.vimeo-player').length) {
					$(this).parents('li').find('img').show();
			
					var videoElm = $(this).parents('li').find('.vimeo-player');
						videoElm.hide();
			
						var froogaloop = $f(videoElm[0].id);
							froogaloop.api('unload');	
				}
			});
			
			if(theImage.parents('li').find('.vimeo-player').length) {
				theImage.parents('li').find('img').hide();	
				var videoElm = theImage.parents('li').find('.vimeo-player');
					videoElm.show();
			
					var froogaloop = $f(videoElm[0].id);
						froogaloop.api("seekTo", 0);
						froogaloop.api('play');
			}
			
			$(obj + ':eq(' + thisIndex + ')').fadeTo("normal", 0.5); // fade all items except the initial item
			
			if (thisIndex == 0) {
                $("#nas-slideshow").animate({
                    "left": 0
                },
                800, null,
                function() {
                    theImage.fadeTo("normal", 1);
                    // $('#ss_captions p:eq(' + currentIndex + ')').show().fadeTo(0, 1);
                });
            } else {
                $("#nas-slideshow").animate({
                    "left": '-' + actualWidth
                },
                800, null,
                function() {
                    theImage.fadeTo("normal", 1);
                    // $('#ss_captions p:eq(' + currentIndex + ')').show().fadeTo(0, 1);
                });
            }
			
			// theImage
			$(this).addClass('selected');	
			
			
			return false;
		});
	};  // eof
  
})(jQuery);

$(document).ready(function(){
  

	
	// $('#nas-slideshow').before('<p id="loading">Loading images...</p>');
  	$('#nas-slideshow div').hover(function(){
    	$(this).css('cursor', 'pointer');
  	});
	
});

$(window).load(function(){
	

	// var thisIndex = $(pager).index();
	
  $('#loading').fadeOut('slow');
  // $('.captions p').each(function() { $(this).fadeTo(0, 0).hide(); });
  //   $('.captions p:first').show().fadeTo(1500, 1);
   
   
  $('#nas-slideshow li div').nasSlideshow();
  // $('#pagination.nas-pagination li a:first').addClass('selected');
  $('#pagination.nas-pagination li a').nasPager('#pagination.nas-pagination a', '#nas-slideshow li div');



  $('#nas-slideshow li img').show();

	
  //    
    // $('#pagination li a').show().css('opacity', 0);
    //    $('#pagination li a').fadeTo(1500, 1); 
   
});
