/**
 * Author: Bjarte Bore, Jon Inge Andersen
 * Email: bjarte@inbusiness.no, joninge@inbusiness.no
 */

timer = true;
(function($){
	// Create constructor
	ConcertTeaser = new function(){}
	
	jQuery.extend(ConcertTeaser,{
		
		_width : 480
		, _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(){
					x = setTimeout(function(){ 
						_self._autotoggle = true
					}, 5000);
				});
				
			});
			
	
			$(".concert-teaser-scroll").each(function(){				
				$(this).mouseenter(function(){
					timer = false;
				});
				
				$(this).mouseleave(function(){
					x = setTimeout(function(){ 
						timer = true;
					}, 5000);
					
				});
				
			});
		}
		
		/**
		 * 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-scroll li:first')
				.addClass('concert-teaser-selected');
			
			$(o).find('.concert-teaser-scroll 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");
					_self._autotoggle = false;	
					timer = false;
					x = setTimeout(function(){ 
						_self._autotoggle = true
						timer = true;
					}, 12000);
					
					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-scroll li');
		
				_self._timekey = setTimeout(function(){
					var selected = $(o).find('.concert-teaser-scroll li.concert-teaser-selected');
					if (timer) {
						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-scroll 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);
			}
			
		
	
	

	   , wait: function(option, options) {
	        milli = 1000; 
	        if (option && (typeof option == 'function' || isNaN(option)) ) { 
	            options = option;
	        } else if (option) { 
	            milli = option;
	        }
	        // set defaults
	        var defaults = {
	            msec: milli,
	            onEnd: options
	        },
	        settings = $.extend({},defaults, options);
	
	        if(typeof settings.onEnd == 'function') {
	            this.each(function() {
	                setTimeout(settings.onEnd, settings.msec);
	            });
	            return this;
	        } else {
	            return this.queue('fx',
	            function() {
	                var self = this;
	                setTimeout(function() { $.dequeue(self); },settings.msec);
	            });
	        }
	
	    }
		
	});
	
	// make it available
	window.ConcertTeaser = ConcertTeaser;
	
})(jQuery)
