/*****************************************************
* Poster Slider
* Scott Wespi (scott@darkstardesign.com)
*****************************************************/
var PS = {
	posterWidth: 133 			// includes padding/margins
	, posterHeight: 93			// used to center images
	, wrap: false 				// when we get to the end of one side, wrap posters from the other side
	, visiblePosters: 3			// number of posters visible at a time
	, slideBy: 3 				// number of posters to slide in on each slide
	, speed: .2					// time (in seconds) for per item scrolled
	, _moving: false
	, _curLeft: 0
	, _posterCount: 0
	, _containerWidth: 0
	, _pc: false
	, _sl: false
	, _sr: false
	, _generated: false
	
	, generate: function(container, data) {
		for (var i = 0, j = data.length; i < j; ++i) {
			var img = new Image();
			img.onload = function() {
				var h = this.height;
				$(this).setStyle('padding-top:' + ((PS.posterHeight - h) / 2) + 'px;');
			};
			
			img.src = data[i].src;
			img.alt = data[i].alt;
			
			var div = new Element('div');
			div.addClassName('client-slide');
			div.insert({bottom: img});
			
			container.insert({bottom: div});
		}
		PS._pc = container;
	}
	
	, init: function(slideLeft, slideRight) {
		// check poster numbers - if greater than visiblePosters, add functionality to both arrows, otherwise hide arrows
		PS._sl = slideLeft;
		PS._sr = slideRight;
		
		if (PS._pc && PS._sl && PS._sr) {		
			PS._posterCount = PS._pc.childElements().length;
			
			PS._pc.select('img').each(function(s) {
				// center all images vertically
				var h = s.getDimensions().height;
				if (h > 0) {
					s.setStyle('padding-top:'+((PS.posterHeight - h) / 2) + 'px;');
				} else {
					s.onload = function() {
						$(this).setStyle('padding-top:' + ((PS.posterHeight - $(this).getDimensions().height) / 2) + 'px;');
					};
				}
			});
			
			if (PS._posterCount > PS.visiblePosters) {
				PS._sl.onclick = PS.slide_left;
				PS._sr.onclick = PS.slide_right;
				
				PS._containerWidth = PS._posterCount * PS.posterWidth;
				PS._pc.setStyle('width:' + PS._containerWidth + 'px;');
			} else {
				PS._sl.hide();
				PS._sr.hide();
			}
		} else { 
			// fail
			alert('postersContainer, slideLeft, and slideRight must all exist!');
		}
	}, slide_left: function() {
		if (!PS._moving) {
			// update curLeft and set moving - the strip is moving to the left, new posters are appearing on the right
					
			// how many posters are hidden?
			var buffer = PS._posterCount - PS.visiblePosters;
			
			// how many are hidden on the right?
			var curRight = PS._posterCount - PS.visiblePosters - PS._curLeft;
			
			// how many do we want to slide by?
			sb = Math.min(buffer, PS.slideBy);
			
			PS._moving = true;
			if (curRight < sb) {
				// not enough room to slide by sb - if we're wrapping move some posters and slide by sb, otherwise slide by curRight
				if (PS.wrap) {
					// move just enough posters to the end of the list and shift our list
					var shiftedPosters = sb - curRight;
					for (var i = 0, j = shiftedPosters; i < j; ++i) {
						PS._pc.insert({bottom:PS._pc.childElements()[0]});
					}
					PS._curLeft -= shiftedPosters;
					PS._pc.setStyle('left: -' + ((PS._curLeft) * PS.posterWidth) + 'px;'); // we can put this in the loop if it looks fucked up	
				} else {
					if (curRight > 0) {
						sb = curRight;
					} else {
						// can't slide
						PS._moving = false;
						return;
					}
				}
			}
			PS._curLeft += sb;
			PS._pc.visualEffect('move', {x: (-1 * (sb * PS.posterWidth)), y:0, mode:'relative', duration:PS.speed * sb, afterFinish: function() { PS._moving = false; } });			
		}
	}, slide_right: function() {
		if (!PS._moving) {
			// update curLeft and moving - the strip is moving right and new posters are appearing on the left
					
			// how many posters are hidden?
			var buffer = PS._posterCount - PS.visiblePosters;
			
			// how many do we want to slide by?
			sb = Math.min(buffer, PS.slideBy);
			
			PS._moving = true;
			if (PS._curLeft < sb) {
				// no room to slide - move posters around if we're wrapping, otherwise adjust our sb or cancel the slide
				if (PS.wrap) {
					// move just enough posters to the beginning of the list and shift our list
					var shiftedPosters = sb - PS._curLeft;
					for (var i = 0, j = shiftedPosters; i < j; ++i) {
						PS._pc.insert({top:PS._pc.childElements()[PS._posterCount - 1]});
					}
					PS._curLeft += shiftedPosters;
					PS._pc.setStyle('left: -' + ((PS._curLeft) * PS.posterWidth) + 'px;'); // we can put this in the loop if it looks fucked up					
				} else {
					if (PS._curLeft > 0) {
						sb = PS._curLeft;
					} else {
						PS._moving = false;
						return;
					}
				}
			}
			PS._curLeft -= sb;
			PS._pc.visualEffect('move', {x: (sb * PS.posterWidth), y:0, mode:'relative', duration:PS.speed * sb, afterFinish: function() { PS._moving = false; } });		
		}
	}
};
