/*
 * axCarousel - jQuery Plugin
 *
 * $Date: 2010-04-20 (Tue, 20 Apr 2010) $
 * $version: 0.1
 *
 * Updated Infinite Carousel Plugin for AX OpenForum
 * 
 * Usage:
 * Apply to any 'new style' carousel markup. Gallery container MUST HAVE UNIQUE ID!
 * 
 * $('#Carousel1').axCarousel({
 * 		visible: INT - Setting for carousel size !! REQUIRED !!
 * 		speed: INT - value for transition speed, default is 500 ms
 * 		start: INT - start position offset, default is 0
 * });
 * 
 * Features:
 * 	- Infinite Right / Left navigation
 * 	- Multiple galleries per page
 * 	- Adjustable transition speed
 * 
 *
*/
(function($) {

	$.fn.contactcarousel = function(options, beforeSend, callback) {

		var opts = $.extend({}, $.fn.contactcarousel.defaults, options);

		return this.each(function() {
			$this = $(this);
			$('ul', $this).wrap('<div class="carousel_wrapper"></div>');

			var liCount = $('li',$this).size();

				var div = $('.carousel_wrapper', $this);
				var o = $.metadata ? $.extend({}, opts, div.metadata()) : opts;

				if(!o.visible) {
					o.visible = 6;
				} 

				o.scroll = o.visible;

			if(liCount > o.visible) {

				if(liCount < o.circularThreshold && liCount % o.visible != 0 && liCount > o.visible) {
					var i = o.visible - liCount % o.visible;
					while(i-- > 0) {
						$('ul', $this).append('<li class="carouselItemEmpty">&nbsp;</li>');
					}
				}

				var running = false;
				var ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

				$(".carousel_prev, .carousel_next").removeClass("hidden");
				if(o.circular) {
					ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());
					o.start += v;
				}

				var li = $("li", ul), itemLength = li.size(), curr = o.start;

				li.css({ overflow: "hidden", 'float': "left" });
				ul.css({ margin: "0", padding: "0", position: "relative", listStyleType: "none", zIndex: 1 });
				div.css({ overflow: "hidden", position: "relative", zIndex: 2, left: "0px" });				

				if(liCount < o.visible) {
					div.css({left: "8px"});
				}

				var liSize = width(li);   
				var ulSize = liSize * itemLength;
				var divSize = ulSize * v;
				if(jQuery.browser.version == "6.0"){
					ulSize = ulSize + (5 * itemLength);
				}
				ul.css("width", ulSize+"px").css("left", -(curr*liSize));

				$('.carousel_prev', $this.parent()).click(function(e) {
					go(curr-o.scroll, beforeSend, callback);
					return false;
				});

				$('.carousel_next', $this.parent()).click(function(e) {
					go(curr+o.scroll, beforeSend, callback);
					return false;
				});

				function vis() {
					return li.slice(curr).slice(0,v);
				};

				function go(to, beforeSend, callback) {
					if(callback && typeof(callback) == 'function')
						beforeSend();

					if(!running) {

						if(o.beforeStart)
							o.beforeStart.call(this, vis());

						if(o.circular) {
							if(to<=o.start-v-1) {
								ul.css("left", -((itemLength-(v*2))*liSize)+"px");
								curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
							} else if(to>=itemLength-v+1) { 
								ul.css("left", -( (v) * liSize ) + "px" );
								curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
							} else curr = to;
						} else {
							if(to<0 || to>itemLength-v) return;
							else curr = to;
						}

						running = true;

						ul.animate(
							{ left: -(curr*liSize) }, o.speed, o.easing,
							function() {
								if(o.afterEnd)
									o.afterEnd.call(this, vis());
									running = false;
							}
						);

						if(!o.circular) {
							$(o.btnPrev + "," + o.btnNext).removeClass("disabled");
							$( (curr-o.scroll<0 && o.btnPrev) || (curr+o.scroll > itemLength-v && o.btnNext) || []).addClass("disabled");
						}
					}

					if(callback && typeof(callback) == 'function')
						callback();

					return false;
				};

			} else {
				$this.find('.carousel_wrapper').addClass('inactive_carousel');
			}

		$this.hide(1, function() { $(this).show(); });

		});

	};

	function css(el, prop) {
		return parseInt($.css(el[0], prop), 10) || 0;
	};
	function width(el) {
		return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
	};
	function height(el) {
		return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
	};

	$.fn.contactcarousel.defaults = {
		btnPrev: null,
		btnNext: null,
		btnGo: null,
		mouseWheel: false,
		auto: null,
		speed: 500,
		easing: null,
		vertical: false,
		circular: true,
		visible: null,
		start: 0,
		scroll: null,
		beforeStart: null,
		afterEnd: null,
		circularThreshold: 60
	};


}) (jQuery);

