// Modified Highlights - jQuery Plugin
// $Date: 2011-09-14 (Wed, 14 Sep 2011) $
// $version: 1.0
(function ($) {
	$.fn.highlights = function (options) {

		//Set identifier
		$(this).attr('class') ? Wrapper = $(this).attr('class') : Wrapper = $(this).attr('id');
		Wrapper = '.' + $.trim(Wrapper).replace(' ', '.');

		//Setting Defaults
		var defaults = {
			HighlightOverflow: Wrapper + ' .HighlightOverflow',
			HighlightWrapper: Wrapper + ' .HighlightWrapper',
			HighlightElement: Wrapper + ' .Highlight',
			ArrowElement: Wrapper + ' .Arrow',
			ArrowLeftElement: Wrapper + ' .Arrow.slider_leftBtn',
			ArrowRightElement: Wrapper + ' .Arrow.slider_rightBtn',
			NavigationElement: Wrapper + ' .Circles a',
			DisabledClass: 'Disabled',
			Delay: 3000,
			Speed: 1000,
			AddCircles: true
		}

		// Setting Variables
		var o = $.extend(defaults, options);
		var $this = $(this);
		var Timer;
		var CurrentIndex = 0;
		var Width = $(o.HighlightElement + ":first").width();
		var Total = $(o.HighlightElement).size();
		var Animating = false;

		// Styling DOM Elements
		$(o.HighlightWrapper).css('width', Width * Total);

		// Loop
		return this.each(function () {
			function Init() {
				if (IsApple() && $.fn.swipe) AddSwipe();
				AddCircles();
				SetCircles();
				CheckArrows();
				StartTimer();
				WindowResize();
			}

			function AnimationIsSafe() { return $(o.HighlightWrapper).width() + parseInt(CurrentIndex * -Width) > $(o.HighlightOverflow).width(); }
			function RequiresReset() { return $(o.HighlightWrapper).width() + parseInt((CurrentIndex - 1) * -Width) <= $(o.HighlightOverflow).width(); }
			function StartTimer() { if (NeedsArrows() && AnimationIsSafe()) Timer = setInterval(ChangeWithTimer, o.Delay); }
			function StopTimer() { clearInterval(Timer); }
			function Increment(Direction) { CheckChange(CurrentIndex + Direction); }
			function CheckChange(NewIndex) { NewIndex >= 0 && NewIndex < Total ? Change(NewIndex) : StopTimer(); }
			function ChangeWithTimer() { AnimationIsSafe() ? Increment(1) : StopTimer(); }

			function Change(Index) {
				if (!Animating && Index != CurrentIndex) {
					Animating = true;
					CurrentIndex = Index;
					CheckArrows();
					SetCircles();
					$(o.HighlightWrapper).animate({ 'margin-left': Index * -Width + 'px' }, o.Speed, "easeInOutExpo", function () {
						Animating = false;
						CheckReset();
					});
				}
			}

			function CheckArrows() {
				$(o.ArrowElement).unbind("click", ArrowClick);
				$(o.ArrowElement).addClass(o.DisabledClass);
				if (NeedsArrows()) {
					if (CurrentIndex > 0) $(o.ArrowLeftElement).bind("click", function (Event) { ArrowClick(Event, this) }).removeClass(o.DisabledClass);
					if (CurrentIndex < Total - 1 && AnimationIsSafe()) {
						$(o.ArrowRightElement).bind("click", function (Event) { ArrowClick(Event, this) }).removeClass(o.DisabledClass);
					}
				}
			}

			function CheckReset() {
				if (RequiresReset()) {
					StopTimer();
					Change(0);
				}
			}

			function AddSwipe() {
				$(o.HighlightWrapper).swipe({
					threshold: { x: 100, y: 50 },
					swipeLeft: function () { $(o.ArrowRightElement).trigger('click', o.ArrowRightElement); },
					swipeRight: function () { $(o.ArrowLeftElement).trigger('click', o.ArrowLeftElement); }
				});
			}

			function AddCircles() {
				if (!o.AddCircles) return false;
				$(o.HighlightElement).each(function () { $(o.NavigationElement + ':first').show().clone().appendTo($(o.NavigationElement).parent()).removeClass('Active'); });
				$(o.NavigationElement + ':last').remove();
				$(o.NavigationElement).click(function (event) { CircleClick(event, this) });
			}

			function SetCircles() {
				if (!o.AddCircles) return false;
				$(o.NavigationElement).removeClass("Active");
				$(o.NavigationElement).eq(CurrentIndex).addClass("Active");
			}

			function CircleClick(Event, Element) {
				if (!o.AddCircles) return false;
				Event.preventDefault();
				StopTimer();
				CheckChange($(o.NavigationElement).index($(Element)));
			}

			function ArrowClick(Event, Element) {
				Event.preventDefault();
				StopTimer();
				var Direction = -1;
				if ($(Element).get(0) === $(o.ArrowRightElement).get(0)) Direction = 1;
				Increment(Direction);
			}

			function NeedsArrows() {
				if ($(o.HighlightOverflow).width() < ((Total - 1) * Width)) return true;
				return false;
			}

			function WindowResize() {
				$(window).resize(function () {
					CheckArrows();
					CheckReset();
				});
			}

			function IsApple() {
				if (navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null) return true;
				return false;
			}

			Init();
		});
	};
})(jQuery);
