jQuery(function($) {
	$('.rotator_view').each(function(i) {
		var clickTimeout = null;
		var view = $(this);
		var items = view.find('li');
		// new rotator
		var rotator = new ListRotator( items.length );
		var mouseOver = false;
		// interface
		// build navigation
		var navhtml = '<div class="rotator_nav"><div class="padding-box">';
		if( view.hasClass('autoplay') ) navhtml += '<a class="playpause">pause</a> <span class="separator">|</span> ';
		navhtml += '<a class="prev">&laquo; prev</a>';
		if( view.hasClass('skipnav') ) {
			navhtml += '<span class="skipnav">';
			for( var j=1 ; j<=items.length ; j++ ) {
				navhtml += '<a class="rotator_nav_item" href="#'+view.attr('id')+'" id="'+view.attr('id')+'-'+j+'">'+j+'</a> ';
			}
			navhtml += '</span>';
		}
		navhtml += '<a class="next">next &raquo;</a></div></div>';
		view.append(navhtml)
		var navs = $('.rotator_nav_item[href="#'+view.attr('id')+'"]');
		navs.each(function(i){
			$(this).click(function(){
				if(rotator.playing) rotator.stop();
				window.clearTimeout(clickTimeout);
				clickTimeout = window.setTimeout(function(){ items.filter('.current').find('.entry').slideUp(250); }, 2000);
				rotator.selectIndex(i);
				return false;
			});
		});
		// fix for empties we don't want to show up
		items.find('.entry').each(function(i) {
			if ( !$.trim( $(this).html() ) ) $(this).remove();
		});
		view.hover(function() {
			window.clearTimeout(clickTimeout);
			items.filter('.current').find('.entry').slideDown(250);
			view.find('.rotator_nav').slideDown(250);
			mouseOver = true;
		}, function() {
			items.filter('.current').find('.entry').slideUp(250);
			view.find('.rotator_nav').slideUp(250);
			mouseOver = false;
		});
		// rotator_nav
		view.find('.rotator_nav').find('a.playpause').click(function(){
			if(rotator.playing) {
				rotator.stop();
			} else {
				rotator.play();
			}
			return false;
		}).end().find("a.next").click(function() {
			rotator.next();
			rotator.stop();
			return false;
		}).end().find("a.prev").click(function() {
			rotator.previous();
			rotator.stop();
			return false;
		});
		// the rotator bound events
		$(rotator).bind('change',function() {
			items.css({position:'absolute',top:0,left:0}).fadeOut(500).removeClass('current').find('.entry').hide();
			items.eq(this.currentIndex).css({position:'relative'}).addClass('current').fadeIn(500);
			var e = items.eq(this.currentIndex).find('.entry');
			var h = e.height();
			e.css({backgroundPositionX:'-1500px'})
				.animate({backgroundPositionX:'0px'},{queue:false,duration:500}, 'easeOutQuad')
				.slideDown(250);
			navs.removeClass('current');
			navs.eq(this.currentIndex).addClass('current');
			var current = items.eq(this.currentIndex);
			if(view.hasClass('liquid')) view.animate({width:current.width(),height:current.height()}, 500, 'easeOutQuad');
		}).bind('interval',function() { 
			if(this.currentSecond == 2 && !mouseOver) items.filter('.current').find('.entry').slideUp(250);
		}).bind('play',function() { 
			view.addClass('playing').find('.rotator_nav a.playpause').html('pause');
		}).bind('stop',function() { 
			view.removeClass('playing').find('.rotator_nav a.playpause').html('play');
		});
		// misc init settings
		// initial display
		items.hide().css({position:'absolute',top:0,left:0})
			.find('.entry').hide().css('opacity', 0.8);
		items.filter('.current')
			.find('.entry').slideDown(250);
		if(view.hasClass('liquid')) view.css({width:view.width(),height:view.height()});
		view.css({overflow:'hidden',position:'relative'})
			.find('.rotator_nav').hide().css('opacity', 0.8).end()
			.find('embed, object').click( function() { rotator.stop(); } );
		$(window).load(function(){
			rotator.init();
			// start auto timer
			if( view.hasClass('autoplay') ) rotator.play();
		});
	});
});