var video_player = null;
var bufferingStarted = false;

var pl = new Array();
var plc = 6;
var cpli = 0;

function onModelState(state)
{
	oldstate = state['oldstate'];
	newstate = state['newstate'];
//alert('state: '+oldstate+'->'+newstate);

	if(newstate == 'COMPLETED')
	{
//		$('#info').text($('#info').text()+'completed with #'+pl[cpli]+'<br/>\r\n');
//		video_player.sendEvent('SEEK', 0);

		cpli++;
		if(cpli >= plc)
			cpli = 0;
		video_player.sendEvent('LOAD', pl[cpli]);
	}
	else if(oldstate == 'BUFFERING' && newstate == 'PLAYING' && bufferingStarted)
	{
		showCam();
	}
}
//function video_playerReady(thePlayer) 
function playerReady(thePlayer)
{
//alert('player ready');
//	video_player = window.document[thePlayer.id];
	video_player = document.getElementById('div_camera');
	video_player.addModelListener('STATE', 'onModelState');

	// enable connect button
	$('#div_buffering').css('display', 'none');
	$('#div_start').css('display', 'block');
}

function clickListener(obj) 
{
//alert('click!');
}

function createPlayer() 
{
	for(i = 0; i < plc; i++)
		pl[i] = 'sb_0'+(i+1)+'.flv';

	for(var j, x, i = pl.length; i; j = parseInt(Math.random() * i), x = pl[--i], pl[i] = pl[j], pl[j] = x);

//	$('#info').text(pl);

	var video_flashvars = 
	{
		allowscriptaccess: 'always', 
		plugins: 'clickproxy', 
		playerready: 'video_playerReady', 
		file: pl[cpli], 
		controlbar: 'none', 
		repeat: 'always', 
		displayclick: 'none', 
		icons: 'false', 
		autostart: 'false', 
		mute: 'true', 
		stretching: 'exactfit', 
//		bufferlength: 10, 
		type: 'http', 
		start: Math.floor(Math.random()*140+1), 
		streamer: 'http://kirieshki.ru/streamer.php'
	};
	var video_params = 
	{
		allowfullscreen:'false', 
		allowscriptaccess:'always'
	};
	var video_attributes = 
	{
		id:'div_camera', name:'div_camera'
	};
	swfobject.embedSWF('/player.swf?'+Math.random(), 'div_camera', '100%', '100%', '9.0.0', 'expressInstall.swf', video_flashvars, video_params, video_attributes);

	$('#div_buffering').css('display', 'block');
}

function showCam()
{
//alert('show cam');
	$('#div_buffering').css('display', 'none');
	$('#div_start').css('display', 'none');
//	$('#div_wrapper').css('width: 320px');
//	$('#div_wrapper').css('height: 240px');
}

function startBuffering()
{
//alert('start buffering');
	$('#div_buffering').css('display', 'block');
	$('#div_start').css('display', 'none');
	video_player.sendEvent('PLAY', 'true');
	bufferingStarted = true;
}

