function Rotisserie (id, items, trackingString) {
	this.id = id;
	this.items = items;
	this.primaryItem = null;
	this.container = $('#'+this.id);
	this.rotateInterval = null;
	this.initialTimeout = null;
	this.rotateTime = 4000;
	this.firstRotateTime = 60000;
	this.trackingString = trackingString;

	// Render the rotisserie
	this.render = function () {
		var html		 = '	<div id="'+this.id+'Primary" class="primary">'
						 + '		<a id="' + this.trackingString + '" href="" class="anchor" onmousedown="setRefString(this.id);">'
						 + '			<div class="text">'
						 + '				<div class="arrow"></div>'
						 + '				<div class="title"></div>'
						 + '				<div class="description"><span class="subtitle"></span> <span class="description"></span></div>'
						 + '			</div>'
						 + '			<div class="corners"></div>'
						 + '		</a>'
						 + '	</div>'
						 + '	<div id="'+this.id+'List" class="list">';
		for (var i = 1; i <= 3; i++) {
			html		+= '		<div id="'+this.id+'Secondary'+i+'" class="secondary secondary'+i+'">'
						 + '			<a href="" class="anchor">'
						 + '				<div class="shadow-top"></div>'
						 + '				<div class="shadow-left"></div>'
						 + '			</a>'
						 + '		</div>';
		}
		html 			+= '		<div class="trc"></div><div class="brc"></div>'
						 + '	</div>';

		this.container.append(html);

		// Set items
		this.items[0].index = 0;
		this.setPrimary(this.items[0]);

		for (var n = 1; n <= 3 && n < this.items.length; n++) {
			this.items[n].index = n;
			this.setSecondary(n, this.items[n], true);
		}

		this.secondaryList = $('#'+this.id+'List');

		// Start rotation after an initial wait time
		this.initialTimeout = window.setTimeout(this.id+'.startRotation()', this.firstRotateTime);

		// Bind mouseover/out events to disable/enable rotation
		this.container.bind('mouseover', { scope: this }, function (event) {
			event.data.scope.pauseRotation();
		});
		this.container.bind('mouseout', { scope: this }, function (event) {
			event.data.scope.startRotation();
		});
	}

	// Start the rotation of the features
	this.startRotation = function() {
		this.rotateInterval = window.setInterval(this.id+'.rotate()', this.rotateTime);
		
		window.clearTimeout(this.initialTimeout);
		this.initialTimeout = null;
	}

	// Pause the rotation of the features
	this.pauseRotation = function() {
		window.clearInterval(this.rotateInterval);
		window.clearTimeout(this.initialTimeout);

		this.rotateInterval = null;
		this.initialTimeout = null;
	}

	// Set the primary slot to the specified item
	this.setPrimary = function (item) {
		var oldPrimary;
		var primary;

		if (this.primaryItem) {
			// Get the old primary
			oldPrimary = $('#'+this.id+'Primary');

			// Make a new primary (underneath the old one)
			primary = oldPrimary.clone();
			this.container.prepend(primary);

			// Animate old primary text
			$('div.text', oldPrimary).hide();
		}
		else {
			primary = $('#'+this.id+'Primary');
		}

		// Set properties of new primary
		primary.css('backgroundImage', 'url(\''+item.largeImageURL+'\')');
		$('a.anchor', primary).attr({href: item.linkURL, id: item.trackingCode});

		$('div.title', primary).text(item.title);
		if (item.subtitle) {
			$('span.subtitle', primary).text(item.subtitle+' -');
			$('span.subtitle', primary).show();
		}
		else {
			$('span.subtitle', primary).text('');
			$('span.subtitle', primary).hide();
		}
		$('span.description', primary).text(item.description);
		this.primaryItem = item;

		if (oldPrimary) {
			// Fade out old primary
			oldPrimary.addClass('rotisserie-old');
			oldPrimary.fadeOut(500, this.cleanup);
		}

		// Slide in new text
		var text = $('div.text', primary);
		text.css('top', '246px');
		text.animate({ top: '-=56' }, 500);
	}

	this.cleanup = function () {
		// Delete old (hidden) elements
		$('.rotisserie-old').remove();
	}

	// Set the secondary slot in position 'n' to the specified item
	this.setSecondary = function (n, item, noAnimation, steps) {
		var oldSecondary;
		var secondary;

		if (!noAnimation) {
			// Get the old secondary
			oldSecondary = $('#'+this.id+'Secondary'+n);
			$('a.anchor', oldSecondary).unbind('click');
			$('a.anchor', oldSecondary).bind('click', function (event) { event.preventDefault(); });

			// Make a new secondary (underneath the old one)
			secondary = oldSecondary.clone();
			this.secondaryList.prepend(secondary);
		}
		else
		{
			secondary = $('#'+this.id+'Secondary'+n);
		}

		// Set properties of new secondary
		secondary.css('backgroundImage', 'url(\''+item.smallImageURL+'\')');
		$('a.anchor', secondary).attr({href: item.linkURL, id: item.trackingCode});
		$('a.anchor', secondary).unbind('click'); // remove previous event handlers
		$('a.anchor', secondary).bind('click', { scope: this, item: item }, function (event) {
			event.data.scope.rotate.call(event.data.scope, event.data.item.index);
			window.clearInterval(event.data.scope.rotateInterval);
			event.data.scope.rotateInterval = null;
			event.preventDefault();
		});

		if (oldSecondary) {
			var top = (82*(n-1)); // the normal top position for this slot
			var distance = (82*steps); // distance to slide

			// Slide out old secondary
			oldSecondary.addClass('rotisserie-old');
			oldSecondary.animate({ top: '-='+distance}, 500, this.cleanup);

			// Slide in new secondary
			secondary.css('top', top+distance+'px');
			secondary.animate({ top: '-='+distance}, 500, this.cleanup);
		}
	}

	// Rotate the items around
	this.rotate = function (index) {
		var idx = this.primaryItem.index;
		var nidx;

		// Get index of the new primary and set it
		// If an index was supplied as an argument, use that one.
		// Else, use the next one in the list
		if (index !== undefined) {
			nidx = index;
		}
		else {
			nidx = idx + 1;
		}
		if (nidx >= this.items.length) {
			nidx = 0;
		}
		this.setPrimary(this.items[nidx]);

		// Count the steps (for animating the secondaries sliding)
		var steps = 0;
		var x = idx;
		while (x != nidx) {
			x++;
			steps++;
			if (x >= this.items.length) {
				x = 0;
			}

			// sanity check
			if (steps > 10) return false;
		}

		// Set the secondaries
		var i = nidx + 1;
		for (var n = 1; n <= 3; n++) {
			if (i >= this.items.length) {
				i = 0;
			}
			this.setSecondary(n, this.items[i], false, steps);
			i++;
		}
	}
}
