Hootenanny = window.Hootenanny || {};

Hootenanny.Reels = function() {
	var _main_swf = '/swf/flvPlayback.swf'; // main flash movie
	var _artist_name_swf = '/swf/artist_name.swf';
	var _reel_div = 'reel-image'; // flash movie placement
	var _artist_name_div = 'artist-name';
	var _artist_firstname = '';
	var _artist_lastname = '';
	var _artist_title = '';
	var _current_clip = '';
	var _current_clip_index = 0;
	var _total_clips = 0;
	var _mute = false;
	var _bumper_path = null;
	var _first_run = true;
	
	function _init_clip_links() {
		$('.clips li').hover(
			// over
			function() {
				$(this).addClass('hover');
			},
			// out
			function() {
				$(this).removeClass('hover');
			}
		);
		
		$('.clips li a').each(function() {
			$(this).click(function() {
				Hootenanny.Reels.load_clip($(this).attr('file'));
				return false;
			});
		});
	}
	
	function _init_artist_name() {
		swfobject.embedSWF(_artist_name_swf, _artist_name_div, "200", "34", "8.0.0", "", { artist: _artist_firstname + ' ' + _artist_lastname, title: _artist_title }, { wmode: "transparent" }, {});
	}
	
	function _get_clip_index(clip_path) {
		var clip_index = -1;
		
		// find the index of the passed clip
		$('.clips li').each(function(index) {
			if ($(this).find('a:first').attr('file') == clip_path) {
				clip_index = index;
			}
		});
		
		return clip_index;
	}
	
	function _get_clip_path_by_index(clip_index) {
		var clips = $('.clips li');
		
		return $(clips[clip_index]).find('a:first').attr('file');
	}
	
	return {
		load_clip: function(clip_path) {
			_current_clip = clip_path;
			_current_clip_index = _get_clip_index(clip_path);
			
			swfobject.embedSWF(_main_swf, _reel_div, "482", "396", "8.0.0", "", { path: clip_path, index: (_current_clip_index + 1), clips: _total_clips, mute: _mute, first_run: _first_run, artist: _artist_firstname }, { wmode: "transparent" }, {});
			Hootenanny.Reels.highlight_clip_link(clip_path);
			
			_first_run = false;
		},
		
		play_next: function(clip_path) {
			var next_index = null;
			
			if (_current_clip_index == (_total_clips - 1) || _current_clip_index == -1) {
				next_index = 0;
			} else {
				next_index = _current_clip_index + 1;
			}
			
			//debug('the next index is ' + next_index + ' and the next path is ' + _get_clip_path_by_index(next_index));
			
			Hootenanny.Reels.load_clip(_get_clip_path_by_index(next_index));
		},
		
		play_prev: function(clip_path) {
			var prev_index = null;
			
			if (_current_clip_index <= 0) {
				prev_index = (_total_clips - 1);
			} else {
				prev_index = _current_clip_index - 1;
			}
			
			Hootenanny.Reels.load_clip(_get_clip_path_by_index(prev_index));
		},
		
		highlight_clip_link: function(clip_path) {
			var clip_link_container = null;
			
			// de-select all items
			$('.clips li').each(function() {
				$(this).attr('class', '');
				
				if ($(this).find('a:first').attr('file') == clip_path) {
					clip_link_container = this;
				}
			});
			
			// select current item
			$(clip_link_container).attr('class', 'selected');
		},
		
		toggle_mute: function(val) {
			_mute = val;
			//debug('mute = ' + _mute);
		},
		
		set_bumper_path: function(path) {
			_bumper_path = path;
		},
		
		reset: function() {
			var first_video = (_bumper_path === null) ? _get_clip_path_by_index(0) : _bumper_path;
			Hootenanny.Reels.load_clip(first_video);
		},
		
		init: function(clip_path, total_clips, artist_firstname, artist_lastname, title) {
			_total_clips = total_clips;
			_artist_firstname = artist_firstname;
			_artist_lastname = artist_lastname;
			_artist_title = title;
			
			_init_clip_links();
			_init_artist_name();
			
			Hootenanny.Reels.load_clip(clip_path);
		}
	}
}();