﻿(function($){
$.fn.sscarousel = function(visible,init) {
  var $this = $(this),
    thisId = (function(){
        if ($($this).attr('id').length>0) {
          return $($this).attr('id');
        } else {
          alert ('Each carousel must have a unique ID. Plug-in terminating.');
          return false;
        }
      })(),
    clickcounter = {thisId: 0},
    slides = $('#'+thisId+' li'),
    totalSlides = $(slides).length,
    carouselId = thisId+'_wrapper',
    jumpId = thisId+'_jump',
    btnsId = thisId+'_btns',
    carouselWrapper = document.createElement('div'),

    totalslides = parseInt(totalSlides,10),
    visibleslides = parseInt(visible,10);
    
  $(slides).css('float','left');
  var slideWidth = parseInt($(slides).outerWidth(),10)+parseInt($(slides).css('marginLeft'),10)+parseInt($(slides).css('marginRight'),10);
  $(carouselWrapper).css({'position':'relative','width':slideWidth*visible,'overflow':'hidden'}).attr('id',carouselId).insertBefore($this).append($this);

  $('<div id="'+btnsId+'" style="position:relative; float:left">').insertBefore(carouselWrapper).append(carouselWrapper).append('<a href="#" class="ssc_prev disabled">Previous</a><a href="#" class="ssc_next">Next</a>');
  
  $($this).css({
    'float':'left',
    'list-style':'none',
    'margin':'0',
    'padding':'0',
    'position':'relative',
    'width':'900000px'
  });

  var thumbindex = 0;

  function setBtnState () {
    var prev = $('#'+btnsId+' .ssc_prev'),
    	next = $('#'+btnsId+' .ssc_next');
    	
    var currSlide = slides.eq(clickcounter.thisId);
    
    slides.removeClass('viewing');
    currSlide.addClass('viewing');
    
    if ($('#aboutCCarousel').length>0 && thisId == 'carousel1') {
    	var swfcode = $('.swfinfo','.viewing').html();
    	$('.swfinfo').each(function(index) {
    		var vidholder = $(this).attr('holder');
    		swfobject.removeSWF(vidholder);
    		if ($('#'+vidholder).length <1 ) {
    			$(this).parent().append('<div id="'+vidholder+'"></div>');
    		}
    	});
    	eval(swfcode);
    }//if on Company About page
    if ($('#'+jumpId).length>0) {
    	var jump = $('#'+jumpId),
    	viewing = $('li.viewing'),
    	index = $('#'+thisId+' li').index(viewing),
    	thumb = jump.find('li').eq(index);
    	
    	jump.find('li').removeClass('video_active');
    	thumb.addClass('video_active');
    	
    	var thumbPos = thumb.offset();
    	
    	if (parseInt(index,10) > parseInt(thumbindex,10)) {
    		$('#'+jumpId+'_btns a.ssc_next').click();
    	} else {
    		$('#'+jumpId+'_btns a.ssc_prev').click();
    	}
    	thumbindex = index;
    	
    }//If there are thumbs
    
    
    switch (clickcounter.thisId) {
      case 0:
        prev.addClass('disabled');
        if (next.hasClass('disabled')) {
          next.removeClass('disabled');
        }
        break;
      case 1:
        prev.removeClass('disabled');
        if (totalslides-visibleslides === 1) {
          next.addClass('disabled');
        } else {
          if (next.hasClass('disabled')) {
            next.removeClass('disabled');
          }
        }
        break;
      case (totalslides-visibleslides-1):
        next.removeClass('disabled');
        break;
      case (totalslides-visibleslides):
        prev.removeClass('disabled');
        next.addClass('disabled');
        break;
      default:
        prev.removeClass('disabled');
        next.removeClass('disabled');
      break;
    }
  }
  
  if (totalslides <= visibleslides) {
    $('#'+btnsId+' .ssc_next').addClass('disabled');
  }
  
  function moveSlides(left,callback) {
    $($this).animate({
      left: left
    },250,callback);  
  }
  
  $('#'+btnsId+' a').click(function(e){
    var tg = e.target;
    
	if ($(tg).hasClass('disabled')) {
		//do nothing
	} else {
	
		if ($(tg).hasClass('ssc_next') || $(tg).hasClass('ssc_prev')) {
		  
		  if ($(tg).hasClass('ssc_next') && $(tg).hasClass('disabled')) {
			//if jump div exists
			target = $("a[rel='0']");
			if (target.length > 0) {
			  target.click();
			}
			return false;
		  }
		  
		  var left = '';
			
		  if($(tg).hasClass('ssc_next') && clickcounter.thisId < (totalslides-visibleslides)) {
			left = '-='+slideWidth;
			clickcounter.thisId++;
		  } else if($(tg).hasClass('ssc_prev') && clickcounter.thisId > 0) {
			left = '+='+slideWidth;
			clickcounter.thisId--;
		  }
		  moveSlides(left);
		  setBtnState();
		}
	}//if not disabled
	return false;
  });
  $('#'+jumpId+' a').click(function(){
    var target = $(this).attr('rel'),
      curr = clickcounter.thisId,
      diff = curr-target;
      
    left = '+='+(slideWidth*diff);
    clickcounter.thisId = parseInt(target,10);
    moveSlides(left);
    setBtnState();
    return false;
  });
  
  if (init) {
    target = $("a[rel='"+init+"']");
    target.click();
  }
  
	$('#'+thisId+' a').click(function(e){
		var target = $(this).attr('href');
		window.location = target;
		//console.dir($(this));
		//e.stopPropagation();
		return true;
	});
  
  
};

})(jQuery);
