/*
 * Copyright (c) 2009 Night Agency LLC
 *
 */

/**
 * @requires jQuery 1.3
 * @requires swfobject v2.1
 *
 */
if( typeof( PUREX ) === 'undefined') {
	PUREX = {};
}
/**
 * @param {Array} videos - an array of object literals with two properties: 'flv'
 *                       the flv file, and 'preview_img' - the image for the preview
 * @param {String} video_player_id
 * @param {Number} player_width
 * @param {Number} player_height
 */
PUREX.media_center = function( videos, video_player_id, player_width, player_height ) {


var CURRENT_VIDEO_ID = 'now-playing',
	LIST_ID = 'movie-thumb-list',
	list_items = $( '#' + LIST_ID + ' li'),
	//gets the first anchor of the first video and sets its 'rel' attribute
	//to 'false' to prevent automatically playing on page load'
	first_video_anchor = $('a:first', list_items[0] ).attr('rel', 'false');

	jQuery.map( list_items, function( item, index ) {

		$('a', item ).click( function() {

			var autoload,
				video = function( i ) {
					
					return videos[ i ];
				}( index ),
				player = swfobject.getObjectById( video_player_id );


			// adds check for first anchor, so that it does autoload on the
			// addLoadEvent called on the first anchor
			if( $(this).attr('rel') === 'false'  ) {
				$(this).removeAttr( 'rel' );
				autoload = false;
			} else {
				autoload = true;
			}

			if (video.bumper) {
				player.loadVideo( video.movie, video.preview_img, autoload, video.bumper, video.bumpLink );
			} else {
				player.loadVideo( video.movie, video.preview_img, autoload);
			}
			
			player.setSize( player_width, player_height );

			$( '#' + CURRENT_VIDEO_ID ).attr('id','');

			$(this).closest('.thumb-content').attr('id', CURRENT_VIDEO_ID );

			return false;
		});
	});

	first_video_anchor.click();

};