/**
 * Author: Bjarte Bore
 * Email: bjarte@inbusiness.no
 */

_timer = true;
(function($){
	// Create constructor
	ConcertTeaser = new function(){}
	
	jQuery.extend(ConcertTeaser,{
		
		_width : 425
		, _count : 0
		, _autotoggle: true
		, _timer : 3000
		
		/**
		 * I initiate the whole damn thing
		 */
		, init: function(){
			var _self = this;
			$(".concert-teaser").each(function(){
				_self.instance(this);
				_self.toggle(this);
				
				$(this).mouseover(function(){
					_self._autotoggle = false;
				});
				
				$(this).mouseout(function(){
					_self._autotoggle = true;
				});
				
			});

		}
		
		/**
		 * I start the instance and I should create a unique pool of data for each instance
		 * @param {Object} o
		 */
		, instance : function(o) {
			var _self = this;
			
			// do the transparent stuff
			$(o).find('.concert-teaser-info').css({ opacity : 0.7 })
			
			// activate first list item
			$(o).find('.concert-teaser-list li:first')
				.addClass('concert-teaser-selected');
			
			$(o).find('.concert-teaser-list li').each(function(i){
				var __self = this;
				
				$(this).click(function(){

					// clean selected class
					$(o).find('.concert-teaser-selected')
						.removeClass("concert-teaser-selected");

					// add selected class
					$(this).addClass('concert-teaser-selected');

					// reposition the list
					$(o).find('.concert-teaser-image ul')
						.fadeOut("fast",function(){
							$(this).css({
								left: -(_self._width * i)
							});
						})
						.fadeIn("fast");
					_timer = false; //stop the timer
					return false;
				});

				// increase the count
				_self.count++
			});
		}
		/**
		 * I do the times toggle
		 * @param {Object} o
		 */
		, toggle : function(o){
			var _self = this;
			var list = $(o).find('.concert-teaser-list li');
			
			
			if (_timer) {
				_self._timekey = setTimeout(function(){
					var selected = $(o).find('.concert-teaser-list li.concert-teaser-selected');
					if (_self._autotoggle) {
						selected.removeClass('concert-teaser-selected');
						selected = selected.next();
						if (selected.length > 0) {
							selected.addClass('concert-teaser-selected');
						}
						else {
							selected = $(o).find('.concert-teaser-list li:first');
							selected.addClass('concert-teaser-selected');
						}
						
						$(o).find('.concert-teaser-image ul').fadeOut("fast", function(){
							$(this).css({
								left: -(_self._width * $.inArray(selected[0], list))
							});
						}).fadeIn("fast");
						
					}
					_self.toggle(o);
				}, _self._timer);
			}

		}
	});
	
	
	// make it available
	window.ConcertTeaser = ConcertTeaser;
	
})(jQuery)
