
// Prevent text bullets on slideshows from being selected
$(function(){
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('#features li').disableTextSelect();
});

function rotate_slides() {

  // Get active image and label
  var $active_image = jQuery('#screenshot IMG.active');
  var $active_label = jQuery('#features li.active');

  // Get next image and label
  var $next_image =  $active_image.next().length ? $active_image.next()
      : jQuery('#screenshot IMG:first');
  var $next_label =  $active_label.next().length ? $active_label.next()
      : jQuery('#features li:first');
             
  // Animate between images
  $next_image.css({opacity: 0.0})
    .addClass('active')
    .animate({opacity: 1.0}, 700, function() {
        $active_image.removeClass('active');
    });
  
  // Animate between labels
  $active_label.removeClass('active');
  $next_label.addClass('active');
  $active_label.slideUp(300, function() {
    $active_label.appendTo("#features").show();
  });
  
}


function reshuffle_slides(selected_label) {
  
  // Reshuffle (only if new label is clicked)
  if (jQuery('#features li.active').attr("id") != selected_label.attr("id")) {
    
    // IMAGES

      // Grab active image
      var $active_image = jQuery('#screenshot IMG.active');
  
      // Grab next image
      if ($("img[src*='wildcard']")) {
        next = "#image" + selected_label.attr("id").substring(7,8);
      } else {
        next = "#image" + selected_label.attr("id").substring(9,10);
      }
      var $next_image = jQuery(next);       
                  
      // Animate between images
      $next_image.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 300, function() {
            $active_image.removeClass('active');
        });
      
    // LABELS
    
      // Grab active label
      var $active_label = jQuery('#features li.active');
        
      // Remove active class
      $active_label.removeClass('active');
      
      // Set active class to selected label
      selected_label.addClass('active');
      
      // Move previous labels to bottom of stack
      jQuery(jQuery.makeArray(selected_label.prevAll()).reverse()).each(function() {
        jQuery(this).slideUp(200, function() {
          jQuery(this).appendTo("#features").show();
        });
      });
  
  }

}

// Let's go
$(document).ready(function() {

  // Tabs
  $("#tabs").tabs();
  
  // Slideshow (if slides are present)
  if (jQuery('#features').length) {
  
    // Rotate slides
    slides = setInterval("rotate_slides()", 9000);  
  
    // Jump to slide
    jQuery("#features li").click(function () { 
      
      // Stop slideshow
      clearInterval(slides);

      // Reshuffle slides
      reshuffle_slides(jQuery(this));
                
      // Restart slideshow at current position
      slides =  setInterval("rotate_slides()", 9000);
      
    });
  
  }
  
});
