// A123 FEATURED SHOWROOM


jQuery.debug = function(text,type) {
	if (!window.console) return;
	if (type == 'info' && window.console.info) {
		window.console.info(text);
	}
	else if (type == 'warn' && window.console.warn) {
		window.console.warn(text);
	}
	else if (window.console.log) {
		window.console.log(text);
	}
};


(function($) { // FUNCTION CLOSURE
	
	$.fn.scrollerator = function(o) {
		
		var s = $(this);
		
		var d = { // DEFAULTS
		
			autoplay:			true,		// IF IT PLAYS AT THE BEGINNING
			continueTime:		30,			// SECONDS BEFORE STARTING TO PLAY AGAIN, IF AUTOPLAY IS ON
			timeout:			6,
			showPager:			true,
			itemsPerPage:		6,
			width:				0,
			repeat:				true,
			even:				true,		// WHETHER OR NOT ALL ITEMS ARE THE SAME SIZE
			
			container:			s,
			items:				".scrollerator-item",
			
			scrollInside:		".scrollerator-inside",
			
			debugging:			false
		};
		
		var o = $.extend(d, o);
		
		var follow = function(u) {
			window.open(u, '_blank', "scrollbars=1,resizable=yes,width=100%,height=100%");
			return false;
		};
		
		// PRIVATE
		var p = {
			unitSize: 0,
			width: 0,
			height: 0,
			intrans: false, // IF IN TRANSITION
			ctnOffset: 0, // CONTAINER OFFSET
			playing: false,
			playInterval: null,
			continueTimer: null,
			itemWidth: 0,
			pageWidth: 0,
			total: 0,
			pages: 0,
			pageCurrent: 0
		};
		
		// GENERAL FUNCTIONS
		var debug = function(msg) {
			if (o.debugging) jQuery.debug(msg);
		}
		
		var display = function(n) {
			if (n > p.pages) n = p.pages;
			else if (n < 0) n = 0;
			
			debug("DISPLAYING: " + n);
			
			// jQuery.debug("CTN OFFSET: " + o.container.offset().left + " - ITEMS: " + o.items.eq(n).offset().left + " - DIFFERENCE: " + (o.container.offset().left - o.items.eq(n).offset().left));
			
			o.scrollInside.animate(
				{
					left: p.pageWidth * n * -1
				},
				{
					queue: false,
					duration: 500
				}
			);
			
			var pager = o.itemsPerPage * n;
			/*
			for (i = 0; i < o.itemsPerPage; i++) {
				
				jQuery.debug("ITEM: " + (i + pager));
				jQuery.debug(p.itemWidth * i);
				
				anim(o.items.eq(i + pager), {
					left: p.itemWidth * i
				});
				
			}
			*/
		};
		
		var moveOutLeft = function(it) {
			var w = it.width();
			anim(
				it,
				{
					left: -w
				}
			);			
		};
		var moveOutRight = function(it) {
			anim(
				it,
				{
					left: d.container.width()
				}
			);
		};
		
		var anim = function(it, css) {
			it.animate(
				css,
				{
					queue: false,
					duration: 1000
				}
			);	
		}
		
		var next = function() {
			if (p.pageCurrent < (p.pages - 1)) {
				p.pageCurrent++;
			} else if (o.repeat) {
				p.pageCurrent = 0;
			}
			display(p.pageCurrent);
		};
		var previous = function() {
			if (p.pageCurrent > 0) {
				p.pageCurrent--;
			} else if (o.repeat) {
				p.pageCurrent = (p.pages - 1);
			}
			display(p.pageCurrent);
		};
		
		var play = function() {
			stop();
			p.playInterval = setInterval(next, o.timeout * 1000);
			p.playing = true;
		};
		
		var stop = function() {
			clearInterval(p.playInterval);
			p.playing = false;
		};
		
		var continueTimer = {
			set: function() {
				this.clear();
				p.continueTimer = setTimeout(function() { play(); }, o.continueTime * 1000);
			},
			clear: function() {
				clearTimeout(p.continueTimer);
			}
		};
		
		var init = function() {
			
			// CONTROLS
			o.container = $(o.container);
			o.items = o.container.find(o.items);
			p.total = o.items.length;
			
			o.container.addClass("ui-scrollerator");
			if (o.debugging) o.container.addClass("scrollerator-debugging");
			
			o.scrollOutside = o.container.find(o.scrollOutside);
			if (!(o.scrollOutside.length > 0)) {
				o.scrollOutside = $("<div>").addClass("scrollerator-outside");
				o.container.wrapInner(o.scrollOutside);
				o.scrollOutside = o.container.children("div.scrollerator-outside");
			}
			
			o.scrollInside = o.scrollOutside.find(o.scrollInside);
			if (!(o.scrollInside.length > 0)) {
				o.scrollInside = $("<div>").addClass("scrollerator-inside");
				o.scrollOutside.wrapInner(o.scrollInside);
				o.scrollInside = o.scrollOutside.children("div.scrollerator-inside");
			}
			
			o.previous = o.container.find("scrollerator-previous");
			if (!(o.previous.length > 0)) {
				o.previous = $("<a>").addClass("scrollerator-previous");
				o.container.prepend(o.previous);
				o.previous.append("<b>Previous</b>");
			}
			
			o.next = o.container.find("scrollerator-next");
			if (!(o.next.length > 0)) {
				o.next = $("<a>").addClass("scrollerator-next");
				o.container.prepend(o.next);
				o.next.append("<b>Next</b>");
			}
			
			// AVAILABLE WIDTH
			var availWidth = (parseFloat(o.scrollOutside.width())) - parseFloat(o.next.width()) - parseFloat(o.previous.width());
			
			p.pageWidth = availWidth / o.itemsPerPage;
			p.itemWidth = o.width || o.items.first().width();
			
			o.scrollOutside.css({
				overflow: "hidden",
				position: "relative",
				height: o.items.first().height()
			});
			
			o.scrollInside.css({
				position: "absolute",
				width: Math.ceil(p.total * p.pageWidth * 1.1)
			});
			
			
			var margin = (p.pageWidth - p.itemWidth) / 2;
			
			var maxHeight = 0;
			var maxWidth = 0;
			
			o.items.each(function() {
				
				var t = $(this);
				
				//t.width(p.itemWidth);
				t.css({
					"margin-left": margin,
					"margin-right": margin
				});
				
				var it = { // ITEM
					a: t.find("a")
				};
				
				var url = it.a.attr("href");
				
				it.a.click(function() {
					follow(url);
					return false;
				});
				
				it.a.attr("href", "");
				
				var h = t.height();
				if (h > maxHeight) maxHeight = h;
				var w = t.width();
				if (w > maxWidth) maxWidth = w;
				
			});
			
			if (maxHeight) o.scrollOutside.height(maxHeight);
			if (o.even && maxWidth) {
				o.items.width(maxWidth);
				p.itemWidth = maxWidth;
				
				//RECALCULATE MARGIN
				margin = (p.pageWidth - p.itemWidth) / 2;
				o.items.css({
					"margin-left": margin,
					"margin-right": margin
				});
			}
			
			p.pageWidth = p.itemWidth + margin * 2;
			
			p.pages = p.total - o.itemsPerPage + 1;
			
			
			///
			
			if (o.autoplay) {
				o.container.hover(stop, play);
			}
			
			o.previous.click(previous);
			o.next.click(next);
			
			display(0);
			
			play();
			//next();
			
			return {
				items: o.items	
			}
			
		};
			
		return init();
		
		
		////////////////////////////
	};
	
})(jQuery);
