/**
 * @author bjarte
 */
(function($){
	// Create constructor
	ConcertScroll = new function(){}
	
	jQuery.extend(ConcertScroll,{

		_width : 480
		, _height: 4
		, _border : {
			height: 0
			, width: 0
		}
		, _inbound : {
			height: 0
			, width: 0
		} 
		, _step : 70
		, _ready : true
		
		, init : function() {
			var _self = this;
			$('.concert-teaser').each(function(){
				_self.instance(this);
			});
		}
		, instance : function(o) {
			var _self = this;
			var ul = $(o).find(".concert-teaser-scroll ul");
			$(0).find('.concert-teaser-info').css({ opacity : 0.7 })
			
			this._border['height'] =  $(o).find(".concert-teaser-scroll").height();
			this._border['width'] =  $(o).find(".concert-teaser-scroll").width();
			
			this._inbound['height'] = ul.height();
			this._inbound['width'] =  ul.width();
			
			$(o).find(".concert-teaser-image ul").width( ul.find(">li").length * this._width );

			$(o).find('.concert-teaser-scroll-up').click(function(){
				var pTop = ul[0].offsetTop;
				if (_self._ready & pTop <= 0) {
					_self._ready = false;
					ul.animate({
						top: "+=" + ( pTop < 0 & (-pTop) > _self._step ? _self._step : -(pTop)  )
					}, 300,function(){
						_self._ready = true;
					});
				}
			});

			$(o).find('.concert-teaser-scroll-down').click(function(){
				var pBottom = _self._inbound['height'] + (ul[0].offsetTop - _self._border['height']);
				if (_self._ready & pBottom >= 0) {
					_self._ready = false;
					ul.animate({
						top: "-=" + (pBottom < _self._step ? pBottom : _self._step )
					}, 300, function(){
						_self._ready = true;
					});
				}
			});

			// activate first list item
			$(o).find('.concert-teaser-link:first')
				.addClass('concert-teaser-selected');
			
			$(o).find('.concert-teaser-scroll-link').each(function(i){
				var __self = this;
				
				
				$(this).click(function(){


					// clean selected class
					//$(o).find('.concert-teaser-selected').removeClass("concert-teaser-selected");


					// reposition the list
					$(o).find('.concert-teaser-image ul')
					.fadeOut("fast",function(){
						$(this).css({
							left: -(_self._width * i)
						});
					})
					.fadeIn("fast");




					// add selected class
					//$(this).addClass('concert-teaser-selected');

					return false;
				});

				// increase the count
				_self.count++
			});
			
			
		}

		
			
	});
	
	// make it available
	window.ConcertScroll = ConcertScroll;
	
})(jQuery)
