var player    =  null;
var playlist  =  null;
try
{
	if(video2interactive == undefined)
	{
		var video2interactive = null;
	}
}
catch(err)
{
	var video2interactive = null;
}
try 
{
	if(siteurl == undefined)
	{
		var siteurl = "http://www.rossiterandco.com";
	}
}
catch(err)
{
	var siteurl = "http://www.rossiterandco.com";
}


function playerReady(obj)
{
	player = gid(obj.id);
	addListeners();
};

var isInteractive = false;
function interactiveReady(rdy)
{
	isInteractive = rdy;
};


function addListeners()
{
	playlist = player.getPlaylist();

	if((playlist !== null) && (playlist.length > 0))
	{
		// ie7 bug fix additional line, also autostart is made false (ie7 can't add object to DOM b4 video starts loading)
		player.sendEvent("PLAY");
		player.addModelListener('STATE', 'stateMonitor');
	}
	else
	{
		setTimeout("addListeners();", 100);
	}
};


function stateMonitor(obj)
{
	if(obj.newstate == 'COMPLETED')
	{
		if(currentDivContents != '' && !isInteractive)
		{
			gid(currentPlayerID + 'o').innerHTML = currentDivContents;
		}
	}
};

 function getInteractive4Video(video)
 {
	try
	{
		if(video2interactive[video] != undefined && video2interactive[video] != null)
		{
			if(video2interactive[video] == '')
			{
				// don't do anything
			}
			else
			{
				player.loadInteractive(siteurl + '/cms/interactive/get.php?interactive_id=' + video2interactive[video]);
			}
		}
		else if(video2interactive['default'] != undefined && video2interactive['default'] != null && video2interactive['default'] != '')
		{
			player.loadInteractive(siteurl + '/cms/interactive/get.php?interactive_id=' + video2interactive['default']);
		}
	}
	catch(err) {}
 }

function gid(name)
{
	return document.getElementById(name);
};

var currentDivContents = '';
var currentPlayerID    = '';

function loadPlayer(videofile, imagefile, width, height, playerid)
{
	// if a player is already loaded, remove the player and restore it's div contents
	if(currentDivContents != '')
	{
		swfobject.removeSWF('playerID');
		gid(currentPlayerID + 'o').innerHTML = currentDivContents;
	}

	//...save current div contents
	currentDivContents = eval(document.getElementById(playerid + 'o')).innerHTML;

	//...save current player id
	currentPlayerID = playerid;

	var flashvars =
	{
		'file':                     encodeURIComponent(videofile),
		'type':                    'video',
		'image':                    encodeURIComponent(imagefile),
		'backcolor':               '333333',
		'frontcolor':              'CCCCCC',
		'lightcolor':              '62A9F0',
		'screencolor':             'FFFFFF',
		'id':                      'playerID',
		'autostart':               'false'
	};

	var params =
	{
		'allowscriptaccess':       'always',
		'allowfullscreen':         'true'
	};

	var attributes =
	{
		'id':                      'playerID',
		'name':                    'playerID'
	};
	swfobject.embedSWF(siteurl + '/videoplayer/player_pro.swf', playerid, width, height, '9.0.124', false, flashvars, params, attributes);
};

// list player content
var listPlayerContent = '';
function loadPlaylist(videofile, imagefile, width, height)
{
	// player id is fixed for playlist, since we have only one player for a playlist
	playerid = 'playerid1';
	// default width & height is: 550, 309
	if(width == undefined || width == null)
	{
		width = 550;
	}
	if(height == undefined || height == null)
	{
		height = 309;
	}

	if(listPlayerContent == '')
	{
		listPlayerContent = eval(document.getElementById(playerid + 'o')).innerHTML;
	}
	jumpTo(playerid + 'o');

	// if a player is already loaded, remove the player and restore it's div contents
	// @todo: if player is loaded, don't replace the content. just stop the current playback and load the new file
	if(currentDivContents != '')
	{
		swfobject.removeSWF('playerID');
		gid(currentPlayerID + 'o').innerHTML = currentDivContents;
	}
	// save current div contents
	currentDivContents = listPlayerContent;
	// save current player id
	currentPlayerID = playerid;

	var flashvars =
	{
		'file':                     encodeURIComponent(videofile),
		'type':                    'video',
		'image':                    encodeURIComponent(imagefile),
		'backcolor':               '333333',
		'frontcolor':              'CCCCCC',
		'lightcolor':              '62A9F0',
		'screencolor':             'FFFFFF',
		'id':                      'playerID',
		'autostart':               'false'
	};

	if(video2interactive != undefined && video2interactive != null)
	{
		if(video2interactive[videofile] != undefined && video2interactive[videofile] != null)
		{
			if(video2interactive[videofile] != '')
			{
				flashvars.plugins = siteurl + '/videoplayer/interactive_plugin';
				flashvars.interactive = siteurl + '/cms/interactive/get.php?interactive_id=' + video2interactive[videofile];
			}
			else
			{
				isInteractive = false;
			}
		}
		else if(video2interactive['default'] != undefined && video2interactive['default'] != null && video2interactive['default'] != '')
		{
			flashvars.plugins = siteurl + '/videoplayer/interactive_plugin';
			flashvars.interactive = siteurl + '/cms/interactive/get.php?interactive_id=' + video2interactive['default'];
		}
		else
		{
			isInteractive = false;
		}
	}
	else
	{
		isInteractive = false;
	}

	var params =
	{
		'allowscriptaccess':       'always',
		'allowfullscreen':         'true'
	};

	var attributes =
	{
		'id':                      'playerID',
		'name':                    'playerID'
	};

	swfobject.embedSWF(siteurl + '/videoplayer/player_pro.swf', playerid, width, height, '9.0.124', false, flashvars, params, attributes);
};

function jumpTo(id)
{
	var scrl_obj = document.getElementById(id);
	if(scrl_obj != undefined && scrl_obj != null)
	{
		window.scrollTo(0, scrl_obj.offsetTop);
	}
};