var NowPlaying_Instances = new Array();
var NowPlaying_Panels = new Array();
var NowPlaying_Containers = new Array();
var NowPlaying_Scroll = new Array();

function NowPlaying_RegisterInstance(id, rotate, seconds, horizontal)
{
	NowPlaying_Instances[id] = new Array();
	NowPlaying_Instances[id]['id'] = id;
	NowPlaying_Instances[id]['rotate'] = rotate;
	NowPlaying_Instances[id]['seconds'] = seconds;
	NowPlaying_Instances[id]['horizontal'] = horizontal;
}

// when the DOM is ready...
jQuery(document).ready(function ($) {

	for(instance in NowPlaying_Instances)
	{
		NowPlaying_SetupInstances(NowPlaying_Instances[instance]['id'], NowPlaying_Instances[instance]['horizontal'],NowPlaying_Instances[instance]['seconds'],NowPlaying_Instances[instance]['rotate']);
	}
	
	function NowPlaying_SetupInstances(instance, horizontal, interval, rotate){

		if(rotate == 0) {
			interval = 0;
		}
		
		NowPlaying_Panels[instance] = $('#'+instance+' .NowPlaying_ScrollContainer > .NowPlaying_Panel');
		NowPlaying_Containers[instance] = $('#'+instance+' .NowPlaying_ScrollContainer');
		
		// float the NowPlaying_Panels[instance] left if we're going horizontal
		if (horizontal) {
		  NowPlaying_Panels[instance].css({
		    'float' : 'left',
		    'position' : 'relative' // IE fix to ensure overflow is hidden
		  });
		  
		  // calculate a new width for the NowPlaying_Containers[instance] (so it holds all NowPlaying_Panels[instance])
		  NowPlaying_Containers[instance].css('width', NowPlaying_Panels[instance][0].offsetWidth * NowPlaying_Panels[instance].length);
		}
		
		// collect the NowPlaying_Scroll[instance] object, at the same time apply the hidden overflow
		// to remove the default NowPlaying_Scroll[instance]bars that will appear
		NowPlaying_Scroll[instance] = $('#'+instance+' .NowPlaying_Scroll').css('overflow', 'hidden');
		
		// handle nav selection
		function selectNav() {
		  $(this)
		    .parents('ul:first')
		      .find('a')
		        .removeClass('selected')
		      .end()
		    .end()
		    .addClass('selected');
		}
		
		$('#'+instance+'_Navigation').find('a').click(selectNav);
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		  var el = $('#'+instance+'_Navigation').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('#'+instance+'_Navigation a:first').click();
		}
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? 
		  NowPlaying_Containers[instance].css('paddingTop') : 
		  NowPlaying_Containers[instance].css('paddingLeft')) 
		  || 0) * -1;
		
		
		var scrollOptions = {
		  target: NowPlaying_Scroll[instance], // the element that has the overflow
		  
		  // can be a selector which will be relative to the target
		  items: NowPlaying_Panels[instance],
		  
		  navigation: '#'+instance+'_Navigation a',
		  
		  // selectors are NOT relative to document, i.e. make sure they're unique
		  prev: 'img.left', 
		  next: 'img.right',
		  
		  // allow the NowPlaying_Scroll[instance] effect to run both directions
		  axis: 'xy',
		  
		  onAfter: trigger, // our final callback
		  
		  interval: interval,
		  
		  offset: offset,
		  
		  // duration of the sliding effect
		  duration: 500,
		  
		  // easing - can be used with the easing plugin: 
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports// the indexed next and previous NowPlaying_Scroll[instance] along with hooking 
		// in to our navigation.
		$('#'+instance).serialScroll(scrollOptions);
		
		if(rotate == 1)
		{
			setTimeout(function() { NowPlaying_Scroll[instance].trigger( 'next' ) },interval);
			$('#'+instance+' .NowPlaying_Autoplay_Control').toggle(
				function() { 
					NowPlaying_Scroll[instance].trigger( 'stop' ); 
					$(this).html(NowPlaying.text_start); 
					return false;
				},
				function() { 
					NowPlaying_Scroll[instance].trigger( 'start' ); 
					$(this).html(NowPlaying.text_stop); 
					return false;
				}
			);
		}
		
	}


});