;(function($) {
	
	$.fn.liteAccordion = function(options) {
				
		// DEFAULT
		var defaults = {
			containerWidth : 922,
			containerHeight : 360,
			headerWidth : 30,
			firstSlide : 11, 
			onActivate : function() {},
			slideSpeed : 800,
			slideCallback : function() {},			
			autoPlay : false,
			pauseOnHover : false, 
			clickStop : true, 
			cycleSpeed : 6000,
			theme : 'basic', // NEED TO CREATE THEMES
			rounded : false,
			enumerateSlides : false
		},
		
		// MERGE DEFAULTS WITH CUSTOM SETTINGS				
			settings = $.extend({}, defaults, options),
	
		// KEY VARIABLES
			$accordion = this,
			$slides = $accordion.find('li'),
			slideLen = $slides.length,
			slideWidth = settings.containerWidth - (slideLen * settings.headerWidth),
			$header = $slides.children('h2'),
			
		// UTILITY / ANIMATION STUFF
			utils = {
				getGroup : function(pos, index) {		
					if (this.offsetLeft === pos.left) {
						return $header.slice(index + 1, slideLen).filter(function() { return this.offsetLeft === $header.index(this) * settings.headerWidth });
					} else if (this.offsetLeft === pos.right) {
						return $header.slice(0, index + 1).filter(function() { return this.offsetLeft === slideWidth + ($header.index(this) * settings.headerWidth) });	
					} 					
				},
				nextSlide : function(slideIndex) {
					var slide = slideIndex + 1 || settings.firstSlide;

					return function() {
						return slide++ % slideLen;
					}
				},
				play : function(slideIndex) {
					var getNext = utils.nextSlide((slideIndex) ? slideIndex : ''), // create closure
						start = function() {
							$header.eq(getNext()).click();
						};
					
					utils.playing = setInterval(start, settings.cycleSpeed);			
				},
				pause : function() {
					clearInterval(utils.playing);
				},
				playing : 0,
				sentinel : false
			};		
		
		// CONTAINER HEIGHT / WIDTH, THEME, CORNERS
		$accordion
			.height(settings.containerHeight)
			.width(settings.containerWidth)
			.addClass(settings.theme)
			.addClass(settings.rounded && 'rounded');
		
		// TAB WIDTH, HEIGHT, SELECTED CLASS
		$header
			.width(settings.containerHeight)
			.height(settings.headerWidth)
			.eq(settings.firstSlide - 1).addClass('selected');
		
		// IE CRAP ;(
		if ($.browser.msie) {
			if ($.browser.version.substr(0,1) > 8) {
				$header.css('filter', 'none');
			} else if ($.browser.version.substr(0,1) < 7) {
				return false;
			}
		}
		
		// SLIDE INITIAL POS
		$header.each(function(index) {
			var $this = $(this),
				left = index * settings.headerWidth;
				
			if (index >= settings.firstSlide) left += slideWidth;
			
			$this
				.css('left', left)
				.next()
					.width(slideWidth)
					.css({ left : left, paddingLeft : settings.headerWidth });
			
			// ADD NUMBER
			settings.enumerateSlides && $this.append('<b>' + (index + 1) + '</b>');			

		});	
				
		// EVENT HANDLER
		$header.click(function(e) {
			var $this = $(this),
				index = $header.index($this),
				$next = $this.next(),
				pos = {
					left : index * settings.headerWidth,
					right : index * settings.headerWidth + slideWidth,
					newPos : 0
				}, 
				$group = utils.getGroup.call(this, pos, index);
								
			// SLIDE DIRECTION
			if (this.offsetLeft === pos.left) {
				pos.newPos = slideWidth;
			} else if (this.offsetLeft === pos.right) {
				pos.newPos = -slideWidth;
			}
			
			// CHECK FOR ANIMATION
			if (!$header.is(':animated')) {

				// CALLBACK	
				if (e.originalEvent) {
					if (utils.sentinel === this) return false;
					settings.onActivate.call($next);
					utils.sentinel = this;
				} else {
					settings.onActivate.call($next);
					utils.sentinel = false;
				}

				// HANDLE SELECTED CLASS
				$header.removeClass('selected').filter($this).addClass('selected');
			
				
				// ANIMATE OTHER TABS

				if ($group == undefined) {
				}
				else {
				$group
					.animate({ left : '+=' + pos.newPos }, settings.slideSpeed, function() { settings.slideCallback.call($next) })
					.next()
					.animate({ left : '+=' + pos.newPos }, settings.slideSpeed);
				}
										
			}
		});
			
		// PAUSE ON HOVER			
		if (settings.pauseOnHover) {
			$accordion.hover(function() {
				utils.pause();
			}, function() {
				utils.play($header.index($header.filter('.selected')));
			});
		}
		
		if (settings.clickStop) {
			$('.staff_profile_trigger').click(function() {
				utils.pause();
			});
			$('.staff_profile_back').click(function(){
				utils.play($header.index($header.filter('.selected')));
			});
		}
	
		// LITTLE HACK TO FIX SKIPPING
		
		function init_start(){
			utils.play($header.index($header.filter('.selected')));
		}
	
		setTimeout(init_start,500);		
		
		
		// START AUTOPLAY, CALL UTILS WITH NO ARGS = START FROM FIRSTSLIDE
		settings.autoPlay && utils.play(); 
		
		return $accordion;
		
		
	};
	
})(jQuery);
