/***********************************************************************************************************
为了 jw flash 播放器
版本：	1.0
作者：	范申
qq:		85960151
时间：	最后修改2008-4-29
说明：
**********************************************************************************************************/
/*
jwFlashPlay类。该类实例化后使用，可以控制/返回当前页面上，每个播放器状态/播放内容

参数
thePlaceName：要显示播放器的所在位置的父元素名称。必有
*/

function jwFlashPlay(thePlaceName) {
	this.thePlaceName=thePlaceName
	this.theJsId=this.thePlaceName+'_js';//为js调用播放器指定唯一名称，这里以父对象名+_js来命名
};

//所有jsId，该id为了对应一个页面上每个播放器。以此为标志，存储播放器信息．作为静态变量使用
jwFlashPlay.prototype.allJsId={}

//播放器的播放状态说明．作为静态变量使用
jwFlashPlay.prototype.playState=['暂停','载入中','播放中','结束']

/*
创建播放器

arg对象参数

theFile：要播放的文件url。可以是flv/mp3/xml列表。必有

theFlashUrl：flash播放器所在位置
theWidth：flash的宽度。创建时flash为100%，实际上，这个宽度控制flash的父对象(如div)宽度，以此来限制flash宽度
theHeight：flash高度。同上
PlayHeight：视频高度。如果没有设置，默认为 theHeight-控制面板高度(20)。如果设置了小于默认，列表将显示，否则不显示
PlayWidth：视频宽度。
theImg：播放器图片
theAutoStart：是否自动播放
theShuffle：是否乱序放
theRepeat：是否连续放
theBackcolor：背景色//0x91F8F4
theFrontcolor：文本色
theLightcolor：鼠标移动上去的颜色
theScreencolor：播放器窗口背景色，可以是图片
theIcons：当控制面板无法显示(PlayHeight > theWidth)时，是否在flash上显示控制
theAutoscroll
theShowdigits
theShowvolume
*/
jwFlashPlay.prototype.createPlay=function(arg){

	var theFlashUrl=(arg['theFlashUrl'])?(arg['theFlashUrl']):('mediaplayer.swf');
	var theWidth=(arg['theWidth'])?(arg['theWidth']):(500);
	var theHeight=(arg['theHeight'])?(arg['theHeight']):(50);
	var PlayHeight=(arg['PlayHeight']>=0)?(arg['PlayHeight']):(50);
	var theRepeat=(arg['theRepeat'])?(arg['theRepeat']):('list');
	
	this.setFobj('width',theWidth)
	this.setFobj('height',theHeight)
	jwFlashPlay.prototype.allJsId[this.theJsId]={}

	var Obj = new SWFObject(theFlashUrl,this.theJsId,'100%','100%',"8");

	Obj.addParam("allowfullscreen","true");
	Obj.addParam("allowscriptaccess","always");
	Obj.addParam("wmode", "transparent");
	Obj.addVariable("file",arg['theFile']);
	//Obj.addVariable("width",100%);
	//Obj.addVariable("height",100%);
	Obj.addVariable("displayheight",PlayHeight)
	if(arg['PlayWidth']){Obj.addVariable("displaywidth",arg['PlayWidth'])}
	//Obj.addVariable("autoscroll",arg['theAutoscroll'])

	Obj.addVariable("shuffle",arg['theShuffle']);
	Obj.addVariable("repeat",theRepeat);//false/list/true
	if(arg['theAutoStart']==1 || !arg['theAutoStart'])
	{Obj.addVariable("autostart", true);}
	else
	{Obj.addVariable("autostart", false);}
	Obj.addVariable("linktarget","_blank");
	Obj.addVariable("volume","50");
	Obj.addVariable("bufferlength","3");
	Obj.addVariable("showdigits",arg['theShowdigits']);
	Obj.addVariable("showvolume",arg['theShowvolume']);//true,false,totalthe
	
	if (arg['theImg']){ Obj.addVariable("image", arg['theImg']);}
	if (arg['theBackcolor']){ Obj.addVariable("backcolor", arg['theBackcolor']);}
	if (arg['theLightcolor']){ Obj.addVariable("lightcolor", arg['theLightcolor']);}
	if (arg['theFrontcolor']){ Obj.addVariable("frontcolor", arg['theFrontcolor']);}
	if (arg['theScreencolor']){ Obj.addVariable("screencolor", arg['theScreencolor']);}
	if (arg['theIcons']){ Obj.addVariable("shownavigation", arg['theIcons']);}

	Obj.addVariable("enablejs","true");
	Obj.addVariable("javascriptid",this.theJsId);

	//Obj.addVariable("largecontrols",true);
	//Obj.addVariable("rotatetime","60"); //images only!	
	Obj.write(this.thePlaceName);
}

/*
设置父对象属性

*/
jwFlashPlay.prototype.setFobj=function(type,value)
{
	var obj=this.getWebObj(this.thePlaceName);
	if(type=='width'){obj.style.width=value+'px';}
	else if(type=='height'){obj.style.height=value+'px';}
	else if(type=='hidden'){obj.style.display='none'}
	else if(type=='show'){obj.style.display=''}
}

/*
获得播放器当前信息

参数
stateName

stime:当前时间
etime:剩余时间
atime:一共时间
volume:音量大小
item:播放
state:播放状态
stateText:播放状态解释
load:载入的文件地址
sizeX:宽度
sizeY:高度
*/
jwFlashPlay.prototype.getPlayInfo=function(stateName){
	if(!stateName){return jwFlashPlay.prototype.allJsId[this.theJsId]}
	else if(stateName=='atime'){return parseInt(jwFlashPlay.prototype.allJsId[this.theJsId]['stime'])+parseInt(jwFlashPlay.prototype.allJsId[this.theJsId]['etime'])}
	else{return jwFlashPlay.prototype.allJsId[this.theJsId][stateName]}
}

/*
获得当前播放列表长度
*/
jwFlashPlay.prototype.getPlayLength=function() { 
  return this.thisMovie(this.theJsId).getLength();
};

/*
获得列表信息
已生成在xml的列表中信息，通过这个方法读取

参数
ThItem。当前播放/指定某一首：空/num
infoName。所有信息/指定某一个：空/信息名

file
title
link
type
id
image
author
captions
audio
start
category
description
latitude
longitude
city
date
*/
jwFlashPlay.prototype.getListInfo=function(ThItem,infoName){
		ThItem=(ThItem)?(ThItem):(this.getPlayInfo('item'))
		if(typeof(ThItem)=='number'){
			var obj = this.thisMovie(this.theJsId).itemData(ThItem);
			if(infoName){return obj[infoName]}
			else{return obj}
		}
}

/*
控制播放器

参数
prev：上一首
next：下一首
stop：停止
playpause：播放暂停
scrubAdd：增加进度
scrubSub：减少进度
volumeAdd：增加音量
volumeSub：减少音量
playitem：播放第几首
*/
jwFlashPlay.prototype.sendPlay=function(typ,prm) {

	var go_to;
	if(typ=='scrubAdd' ||typ=='scrubSub')
	{
		go_to=(typ=='scrubAdd')?(this.getPlayInfo('stime')+prm):(this.getPlayInfo('stime')-prm)

		this.thisMovie(this.theJsId).sendEvent('scrub',go_to);
	}
	else if(typ=='volumeAdd' ||typ=='volumeSub')
	{
		go_to=(typ=='volumeAdd')?(this.getPlayInfo('volume')+prm):(this.getPlayInfo('volume')-prm)
		
		this.thisMovie(this.theJsId).sendEvent('volume',go_to);
	}
	else
	{
		go_to=(prm)?(prm):('');
		this.thisMovie(this.theJsId).sendEvent(typ,go_to); 
	}
}

jwFlashPlay.prototype.thisMovie=function(movieName){

	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

jwFlashPlay.prototype.getWebObj=function(objName){
	if(document.getElementById(objName)){return document.getElementById(objName)}
	else{throw(new Error("objErr||"+objName))}
}

/*
jw播放器自带的回调函数。这个函数将回调页面上所有jw播放器的状态，以播放器唯一js名称进行区分
参数
typ：播放器状态名
pr1：状态值1
pr2：状态值2。大部分只有一个值，但比如 size 会有第2个值
prm：播放器唯一js名称
*/
function getUpdate(typ,pr1,pr2,prm) { 
	if(prm != "null"){

			if(typ == "time") { 
				jwFlashPlay.prototype.allJsId[prm]['stime'] = pr1;
				pr2 == undefined ? null: jwFlashPlay.prototype.allJsId[prm]['etime'] = Math.round(pr2);
				//jwFlashPlay.prototype.allJsId[prm]['atime']
			}
			else if(typ == "size") { 
					jwFlashPlay.prototype.allJsId[prm]['sizeX'] =  pr1; 
					pr2 == undefined ? null: jwFlashPlay.prototype.allJsId[prm]['sizeY'] =  Math.round(pr2); 
				}
			else if(typ == "volume") { jwFlashPlay.prototype.allJsId[prm]['volume'] = pr1; } 
			else if(typ == "item") { jwFlashPlay.prototype.allJsId[prm]['item'] = pr1;}
			else if(typ == "state") {
				jwFlashPlay.prototype.allJsId[prm]['state'] = pr1; 
				jwFlashPlay.prototype.allJsId[prm]['stateText']=jwFlashPlay.prototype.playState[pr1];
			}
			else if(typ == "load") { jwFlashPlay.prototype.allJsId[prm]['load'] = pr1; }
	}
};

/*
增加列表/移除列表/装载。待扩展
function addItem(swf,obj,idx) { 
  thisMovie(swf).addItem(obj,idx); 
};
function removeItem(swf,idx) { 
  thisMovie(swf).removeItem(idx); 
};
function loadFile(obj) { thisMovie("flvPlay").loadFile(obj); };
};
*/
