var player = null;
var clicked = null;

function playerReady(obj) {
	player = document.getElementById(obj.id);
	var currentPlaylist = player.getPlaylist();
	for(var i in currentPlaylist){ 
		var lst = new Array({file:currentPlaylist[i].file});
	}
	player.sendEvent("LOAD",lst);
	player.sendEvent("PLAY","true");
	$('.cycle_slides').cycle('pause');
	player.addModelListener('STATE', 'stateMonitor');
}

function stateMonitor(obj){
	if(obj.newstate == 'COMPLETED'){
		$('#placeholder').html("<div id=\"sting\"><\/div>");
		player = null;
		$('.cycle_slides').cycle('resume', true);
	}
};


function createPlayer(placeholder){
	var flashvars = {
		file:"/images/slide/hippo_slide.flv", 
		autostart:"false",
		controlbar:"none",
		dock:"false",
		displayclick:"none",
		icons:"false",
		screencolor:"000000",
		backcolor:"000000",
		frontcolor:"000000",
		lightcolor:"000000",
		width:"956",
		height:"268",
		stretching: "none"
	}

	var params = {
		allowfullscreen:"false", 
		allowscriptaccess:"always",
		bgcolor:"#180000",
		wmode:"transparent"
	}

	var attributes = {
		id:"player1",  
		name:"player1"
	}

	swfobject.embedSWF("/media_players/jw_flv_player/player.swf", placeholder, "938", "481", "8", false, flashvars, params, attributes);
}

function onPrevNext(curr, next, opts) {
	clicked = next;
}

function onBefore(curr, next, opts) {
	// alert($('#cycle').cycle().nextSlide);
	if(opts.nextSlide != 0 && clicked != null){
		if(opts.nextSlide == clicked){
			opts.timeout = 8000; 
			clicked = null;
		} else {
			opts.timeout = 4000; 
		}
		$('#placeholder').html("<div id=\"sting\"><\/div>");
		player = null;
	} else {
		opts.timeout = 4000; 
	}
	$('.cycle_slides').cycle('resume');
} 

function onAfter(curr, next, opts) { 
	if(opts.currSlide == 0){
		createPlayer("sting");
	}
}
$(document).ready(function(){
	$('.cycle_slides').cycle({ 
		fx: 'fade',
		speed:1000, 
		timeout:4000,
		sync:1,
		before:onBefore, 
		after:onAfter,
		prevNextClick:onPrevNext
	});
});
