var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
  },

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) { return window[movieName];}
	    return document[movieName+'_em'];
	}
};

function niftyplayer(name)

{
	this.obj = FlashHelper.getMovie(name);

	if (!FlashHelper.movieIsLoaded(this.obj)) {return;}

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};

	return this;
}

function play_story(story_name){
niftyplayer('niftyPlayer1').loadAndPlay('play/'+story_name+'.mp3');
n=document.form_1.story_list.length;
for (i=0;i<n;i++){
j=document.form_1.story_list[i].value;
document.getElementById("story_style_"+j).className="play_0";

if (document.form_1.story_list[i].value==story_name)
{document.form_1.story_list[i].checked=false;
document.getElementById("story_style_"+j).className="play_1";
}
}
	url="record_story_play.php?id="+story_name;
	document.getElementById("play_times_"+story_name).innerHTML=parseInt(document.getElementById("play_times_"+story_name).innerHTML)+1;
	AJAX(url);
}

function play_loop(){
play_status=niftyplayer('niftyPlayer1').getState();
document.getElementById("play_status").value=play_status;
if (play_status=="finished"){
	play_list_1();
	}
}


function play_list_1(){
var play_list="";
n=document.form_1.story_list.length;
for (i=0;i<n;i++){
if (document.form_1.story_list[i].checked==true){play_list+=document.form_1.story_list[i].value+",";}
}
document.getElementById("play_list").value=play_list;
	play_pos=play_list.indexOf(',');
    new_id=play_list.substr(0,play_pos);
	document.getElementById("now_id").value=new_id;
	if(play_list==""){alert("没有故事可以播放!");  window.clearInterval(timerA );return false;}
	play_story(new_id);
	}
	
function MyShowA()
{
   play_list_1();
	
   try
  { 
    timerA = window.setInterval("play_loop()",5000);
	timerA=null;
   }
   catch(ex)//catch the ex 
   {
   }
   finally
   {
   }
}


 function  all_check_1(){
for (i=0;i<document.form_1.story_list.length;i++){
	if (document.form_1.story_list[i].checked==true){document.form_1.story_list[i].checked=false;}else{document.form_1.story_list[i].checked=true;}
		 }
  }
  
 function  stop_story(){ 
  story_name=document.form.now_id.value;
  niftyplayer('niftyPlayer1').stop('play/'+story_name+'.mp3');
  window.clearInterval(timerA );
 }
 
 function go_page(i)
{document.getElementById("page").value=i;
document.form.submit();}

