/**
This function should be called on the UL of stream items.
It dynamically wraps the UL in another div with the class .stream-wrapper (by default) 

The following options can be passed:
      
	horizontal: true or false (default: false) - whether the orientation of the scroller is horizontal
	count: integer - the number of items visible at a time
	easing: the easing effect to use (default: easeInOutCubic)
	speed: speed in milliseconds of the scroll animation (default: 1000)
	next: the selector for the next button (ex: 'a.nextButton') - must be a child of the main div
	back: the selector for the back button (ex: 'a.backButton') - must be a child of the main stream div
	loadCallback: function to be called after new items are pulled in
	nextClass: class name dynamically added to next button (default: 'stream-next-button')
	backClass: class name dynamically added to back button (default: 'stream-back-button')
	wrapClass: class name dynamically added to the UL wrapper
 

Sample HTML:

<div id="container">
   <a href="#" class="more">More</a>
   <a href="#" class="back">Back</a>
   <ul id="stream">
      <li>
         <h3><a href="http://www.itemlink.com/">Item Title</a></h3>
         <p>The content</p>
      </li>
   </ul>
</div>

Sample Javascript:

$('#stream').stream({
	next: '#container a.more',
	back: '#container a.back',
	horizontal: true,
	count: 6
});

**/

(function($){
	$.fn.stream = function(options) {

		// Default configuration properties.
		var defaults = {
			horizontal: false,
			count: 5, // the number of items that are visible at a time
			easing: 'easeInOutCubic',
			speed: 1000,
			next: null, // selecter for next button
			back: null, // selector for back button
			loadCallback: function() { },
			nextClass: 'stream-next-button', // this id is dynamically added to the next button
			backClass: 'stream-back-button', // this id is dynamically added to the back button
			wrapClass: 'stream-wrapper'
		};
		var options = $.extend(defaults, options);
		var loading = false;
		var getItems = function(ul, offset) {
			loading = true;
			$.post('ajax/' + window.location.search, { action: 'stream', offset: offset, count: options.count }, function(html) {
				ul.append(html);
				if($.isFunction(options['loadCallback'])) {
					options['loadCallback']();
				}
				loading = false;
			});
		};
		
		return this.each(function() {

			var ul = $(this);
			var li = $('li', ul);
			
			ul.wrap('<div class="' + options.wrapClass + '"></div>');
			var wrap = $('div.' + options.wrapClass);
			wrap.css('overflow', 'hidden').css('position', 'relative');
			
			var property, moveBy, totalPx;
			if(options.horizontal) {
				property = 'marginLeft';
				itemPx   = li.outerWidth(true);
				moveBy   = itemPx * options.count;
				wrap.width(moveBy);
				wrap.height(li.outerHeight(true));
				ul.width(30000); // set this to a big number so it can contain all the elements
			} else {
				property = 'marginTop';
				itemPx   = li.outerHeight(true);
				moveBy   = itemPx * options.count;
				wrap.height(moveBy);
				wrap.width(li.outerWidth(true));
				ul.css('height', 'auto');
			}
			
			getItems(ul, options.count);
			
			$(options.back).addClass(options.backClass);
			$(options.next).addClass(options.nextClass);

			$(options.back + ', ' + options.next).click(function() {
				if(ul.queue('fx').length > 0 || loading)
					return false;

				var currPos  = parseInt(ul.css(property));
				var nextPos;
				var itemCount = $('li', ul).length;
				var totalPx    = itemPx * itemCount;
				
				if($(this).hasClass(options.nextClass)) {
					nextPos = currPos - moveBy;
					if(totalPx + nextPos == moveBy) { // if it'll be the last ones, get some more
						getItems(ul, itemCount);
					}
				} else {
					nextPos = currPos + moveBy;
				}

				if(nextPos > 0 || nextPos < totalPx * -1)
					return false;
					
				if(options.horizontal) {
					ul.animate({ marginLeft: nextPos + 'px' }, options.speed, options.easing);
				} else {
					ul.animate({ marginTop: nextPos + 'px' }, options.speed, options.easing);
				}

				return false;
			});
		});
	};
})(jQuery);

$(document).ready(function() {
	// break out of iframes
	if(top.location != self.location) {
		try {
			if(top.location.hostname.search("magntize.") == -1) {
				top.location = self.location.href;
			}
		} catch(error) {
			top.location = self.location.href;
		}
	}
	
	// open rel="external" links in new window
	$('a[rel="external"]').live('click', function() {
		window.open( this.href );
		return false;
	});	
});