$.fn.artistWidget = function(venue_id){
	$(this).each(function(){
		var o = $(this);

		o.v = {
			listcontainer: $('div.list-container', o),
			list: $('div.list-container ul.list', o),
			venue: venue_id,
			autoscroll: true,
			autoscroll_direction: 'down'
		};
		
		o.resetTopBottom = function(){
			$('li', o.v.listcontainer).removeClass('bottom').removeClass('top');
			$('li:visible:eq(0)', o.v.listcontainer).addClass('top');
			$('li:visible:eq(2)', o.v.listcontainer).addClass('bottom');
		};

		o.getNext = function(current){
			var next = current.next('li');
			if(next.length === 0){
				return next;
			}else if(next.is(':visible')){
				return next;
			}else{
				return o.getNext(next);
			}
		};

		o.getPrev = function(current){
			var prev = current.prev('li');
			if(prev.length === 0){
				return prev;
			}else if(prev.is(':visible')){
				return prev;
			}else{
				return o.getPrev(prev);
			}
		};

		o.startDelay = function(){
			setTimeout(function(){
				if(o.v.autoscroll){
					var next = o.getNext($('li.bottom', o.v.listcontainer));
					var prev = o.getPrev($('li.top', o.v.listcontainer));

					if(next.length === 0 && o.v.autoscroll_direction === 'down'){
						o.v.autoscroll_direction = 'up';
					}else if(prev.length === 0 && o.v.autoscroll_direction === 'up'){
						o.v.autoscroll_direction = 'down';
					}

					if(next.length > 0 && o.v.autoscroll_direction === 'down'){
						$('.arrow-down', o).trigger('click');
					}else if(prev.length > 0 && o.v.autoscroll_direction === 'up'){
						$('.arrow-up', o).trigger('click');
					}
				}
				o.startDelay();
			}, 2000);
		};

		o.v.listcontainer.live('mouseleave',function(){
			$('a.arrow-up, a.arrow-down', o.v.listcontainer).fadeTo(500, 0);
		});

		$('li.top', o.v.listcontainer).live('mouseenter', function(){
			$('a.arrow-down', o.v.listcontainer).fadeTo(500, 0);
			$('a.arrow-up', o.v.listcontainer).fadeTo(500, 0.6);
		});

		$('li.bottom', o.v.listcontainer).live('mouseenter', function(){
			$('a.arrow-up', o.v.listcontainer).fadeTo(500, 0);
			$('a.arrow-down', o.v.listcontainer).fadeTo(500, 0.6);
		});

		o.bind('mouseenter', function(){ o.v.autoscroll = false; });
		o.bind('mouseleave', function(){ o.v.autoscroll = true; });

		$('.arrow', o)
			.bind('click', function(){
				var next = o.getNext($('li.bottom', o.v.listcontainer));
				var prev = o.getPrev($('li.top', o.v.listcontainer));

				if($(this).is('.arrow-up') && (prev.length > 0)){
					var top_prev = o.getPrev($('li.top', o.v.listcontainer));
					var bottom_prev = o.getPrev($('li.bottom', o.v.listcontainer));
					$('li.top', o.v.listcontainer).removeClass('top');
					top_prev.addClass('top');
					$('li.bottom', o.v.listcontainer).removeClass('bottom');
					bottom_prev.addClass('bottom');
					o.v.list.animate({
						'margin-top': '+=120px'
					});
				}else if($(this).is('.arrow-down') && (next.length > 0)){
					var top_next = o.getNext($('li.top', o.v.listcontainer));
					var bottom_next = o.getNext($('li.bottom', o.v.listcontainer));
					$('li.top', o.v.listcontainer).removeClass('top');
					top_next.addClass('top');
					$('li.bottom', o.v.listcontainer).removeClass('bottom');
					bottom_next.addClass('bottom');
					o.v.list.animate({
						'margin-top': '-=120px'
					});
				}
				return false;
			})
			.bind('mouseenter', function(){
				$(this).fadeTo(500, 1);
			});

		/*$('.switcher.top .button a', o).bind('click', function(){
			var target = $(this);
			var id = target.attr('id');

			$('li.button', o).removeClass('selected');
			target.closest('li').addClass('selected');

			o.v.list.animate({
				'margin-top': 0
			});

			if(id){
				$('ul.list li.'+target.attr('id'), o).slideDown(500, function(){
					$('ul.list li:not(.'+target.attr('id')+')', o).slideUp(500);
				});
			}else{
				$('ul.list li', o).slideDown(500);
			}

			setTimeout(function(){
				o.resetTopBottom();
			}, 1100);

			return false;
		});*/
		
		o.resetTopBottom();
		o.startDelay();

		if(o.v.venue){
			$('a#venue-'+o.v.venue).trigger('click');
		}
		
	});
};

$(document).bind('ready', function(){
	$('ul.gallery a').lightBox({
		overlayBgColor: '#d71478',
		overlayOpacity: 0.7
	});
	
	var venue_id = parseInt($('input#venue_id').val());

	$('.artist-widget').artistWidget(venue_id);
	
	$('#navigation li').bind({
		mouseenter: function(){
			$('#navigation li').removeClass('selected');
			$(this).addClass('selected');
		},
		mouseleave: function(){
			$(this).removeClass('selected');
		}
	});

	$('a[href^="http://"]:not(a[href^="http://www.seetickets.com"])')
		.attr({
			target: "_blank"
		});
});