/*
 * Based on wideslide by Andrey Petrov (shazow.net) and Ryan Feeley (ryanfeeley.com).
 * http://github.com/shazow/wideslide
 */
(function($) {
	function MuSlide(_target, slides, options) {
		this.pos    = 0;
		this.slides = slides;
		this.$next  = options.controlNext ? $(options.controlNext) : null;
		this.$prev  = options.controlPrev ? $(options.controlPrev) : null;
		this.css    = { enabled: options.enabledClass, disabled: options.disabledClass };

		//
		// IE7 needs the "position: relative" bit or it makes its usual mess of things.
		// Thanks: http://www.webproworld.com/graphics-design-discussion-forum/63488-ie7-css-overflow-hidden.html
		//
		var target = $('<div></div>').css({
			height:   options.height + 'px',
			width:    options.width  + 'px',
			overflow: 'hidden',
			display:  'block',
			position: 'relative'
		});
		$(_target).replaceWith(target);
		var container = $('<div></div>').css('width', options.maxWidth + 'px');
		container.offset = 0;
		target.append(container);
		var parentLeft = target.offset().left;

		if(this.$next) {
			var self = this;
			this.$next.click(function() {
				if(!self.hasNext())
					return;
				self.next();
				self.toggleControls();
			});
		}
		if(this.$prev) {
			var self = this;
			this.$prev.click(function() {
				if(!self.hasPrev())
					return;
				self.prev();
				self.toggleControls();
			});
		}

		slides.each(function(i) {
			this.num = i;
			container.append($(this).css('float', 'left').css('margin', '0 ' + options.margin + 'px').css('position', 'relative'));
			this.view = function() {
				var $this = $(this);
				var left  = -parentLeft + $this.offset().left - options.margin;
				var width = $this.width() + 2 * options.margin;

				var elemOffset = -left;
				if(this.num == slides.length - 1) {
					elemOffset = -left + (options.width - width);
				}
				else if(this.num == 0) {
					container.offset = 0;
					elemOffset       = 0;
				}

				container.offset += elemOffset;
				container.animate({
					marginLeft: container.offset + 'px'
				}, options.speed);
			}
		});

		//
		// We assume they're all the same size (but I'm trying to issolate the
		// assumption in case I change my mind...)
		//
		this.visible = options.width / ($(slides[0]).width() + 2 * options.margin);

		this.toggleControls();
	}

	MuSlide.prototype.hasNext = function() {
		return this.pos < this.slides.length - this.visible;
	}

	MuSlide.prototype.next = function() {
		if(this.hasNext())
			this.slides[++this.pos].view();
	}

	MuSlide.prototype.hasPrev = function() {
		return this.pos > 0;
	}

	MuSlide.prototype.prev = function() {
		if(this.hasPrev())
			this.slides[--this.pos].view();
	}

	MuSlide.prototype.toggleControls = function() {
		if(this.$next) {
			if(this.hasNext()) {
				this.$next.removeClass(this.css.disabled);
				this.$next.addClass(this.css.enabled);
			}
			else {
				this.$next.addClass(this.css.disabled);
				this.$next.removeClass(this.css.enabled);
			}
		}
		if(this.$prev) {
			if(this.hasPrev()) {
				this.$prev.removeClass(this.css.disabled);
				this.$prev.addClass(this.css.enabled);
			}
			else {
				this.$prev.addClass(this.css.disabled);
				this.$prev.removeClass(this.css.enabled);
			}
		}
		return;
	}

	$.fn.muSlide = function(options) {
		options = jQuery.extend($.fn.muSlide.defaults, options);
		$(this).each(function() {
			new MuSlide(this, $(options.selector, this).remove(), options);
		});
	};

	$.fn.muSlide.defaults = {
		width:         0,			// Width of the container (required)
		height:        0,			// Height of the container (required)
		maxWidth:      10000,		// Any number greater than the sum of all slides' widths
		speed:         300,			// Speed of the switching animation
		margin:        0,			// Right/left margin between slides
		selector:     'img',		// Standard selector for the slides
		controlNext:   null,		// The selector for the "next slide" control
		controlPrev:   null,		// The selector for the "previous slide" control
		enabledClass:  null,		// CSS class to add to enabled controls (if any)
		disabledClass: null			// CSS class to add to disabled controls (if any)
	};
})(jQuery);

// $HeadURL: http://localhost/svn/isos/tmpl/static/jquery/muSlide.js $
// $Id: muSlide.js 6229 2010-09-22 20:30:19Z mu $

