﻿/********************************/
/* jquery.GAT.Rotator.js		*/
/*	- parameter: options {		*/
/*		bindEvent				*/
/*		listSelector			*/
/*		scrollDirection			*/
/*		scrollLeftSelector		*/
/*		scrollRightSelector		*/
/*		visibleCount }			*/
/********************************/

(function($) {
	$.fn.extend({
		gat_imageRotator: function(options) {
			var defaults = {
				bindEvent: 'click',
				duration: 600,
				listSelector: '.rotatorItems',
				scrollDirection: 'Horizontal',
				scrollLeftSelector: '.rotatorScrollLeft a',
				scrollRightSelector: '.rotatorScrollRight a',
				visibleCount: 4
			};

			// Load options over defaults
			var options = $.extend(defaults, options);
			defaults = null;

			return this.each(function() {
				var rotator = $(this);

				// Apply animation for each list
				$(options.listSelector, this).each(function() {
					var container = $(this);

					var listItems = $('li', container);
					var show = null;
					var hide = null;

					// Load animations for vertical or horizontal scrolling
					if (options.scrollDirection == 'Vertical') {
						show = {
							marginTop: $(listItems).css('margin-top'),
							marginBottom: $(listItems).css('margin-bottom'),
							paddingTop: $(listItems).css('padding-top'),
							paddingBottom: $(listItems).css('padding-bottom'),
							height: $(listItems).css('height')
						};

						hide = {
							marginTop: '0px',
							marginBottom: '0px',
							paddingTop: '0px',
							paddingBottom: '0px',
							height: '0px'
						};

						// Delete null and auto effects to prevent crashing
						if (!show.marginTop || show.marginTop == 'auto') {
							delete show['marginTop'];
							delete hide['marginTop'];
						}

						if (!show.marginBottom || show.marginBottom == 'auto') {
							delete show['marginBottom'];
							delete hide['marginBottom'];
						}

						if (!show.paddingTop || show.paddingTop == 'auto') {
							delete show['paddingTop'];
							delete hide['paddingTop'];
						}

						if (!show.paddingBottom || show.paddingBottom == 'auto') {
							delete show['paddingBottom'];
							delete hide['paddingBottom'];
						}
					}
					else {
						show = {
							marginRight: $(listItems).css('margin-right'),
							marginLeft: $(listItems).css('margin-left'),
							paddingRight: $(listItems).css('padding-right'),
							paddingLeft: $(listItems).css('padding-left'),
							width: $(listItems).css('width')
						};

						hide = {
							marginRight: '0px',
							marginLeft: '0px',
							paddingRight: '0px',
							paddingLeft: '0px',
							width: '0px'
						};

						// Delete null and auto effects to prevent crashing
						if (!show.marginRight || show.marginRight == 'auto') {
							delete show['marginRight'];
							delete hide['marginRight'];
						}

						if (!show.marginLeft || show.marginLeft == 'auto') {
							delete show['marginLeft'];
							delete hide['marginLeft'];
						}

						if (!show.paddingRight || show.paddingRight == 'auto') {
							delete show['paddingRight'];
							delete hide['paddingRight'];
						}

						if (!show.paddingLeft || show.paddingLeft == 'auto') {
							delete show['paddingLeft'];
							delete hide['paddingLeft'];
						}
					}

					// Hide unused items
					$(listItems).slice(options.visibleCount).animate(hide, {
						queue: false,
						duration: 5
					});

					// Left scroll event
					$(options.scrollLeftSelector, rotator).bind(options.bindEvent, function(event) {
						event.preventDefault();

						// Refresh listItems for new order
						listItems = $('li', container);

						// Hide right item, move end item to beginning of list and show
						$(listItems).eq(options.visibleCount - 1).animate(hide, {
							queue: false,
							duration: options.duration
						});

						$(listItems).eq(listItems.length - 1).prependTo(container).animate(show, {
							queue: false,
							duration: options.duration
						});
					});

					// Right scroll event
					$(options.scrollRightSelector, rotator).bind(options.bindEvent, function(event) {
						event.preventDefault();

						// Refresh listItems for new order
						listItems = $('li', container);

						// Hide left item and move to end of list
						$(listItems).eq(0).animate(hide, {
							queue: false,
							duration: options.duration,
							complete: function() {
								$(this).appendTo(container);
							}
						});

						// Show right item
						$(listItems).eq(options.visibleCount).animate(show, {
							queue: false,
							duration: options.duration
						});
					});
				});
			});
		}
	});
})(jQuery);