
/*  channel player helper   */

var ChannelPlayerHelper = {
	channelPlayerName: "video_player",
	isIE : ((navigator.appVersion.indexOf("MSIE") != -1) ? true : false ) && document.all ,
	setPosition : function(videoindex){
		var swf_obj = this._getVideoObj(this.channelPlayerName);
		try{
			if(!swf_obj){
				throw Error("Element " + this.channelPlayerName + " is NULL");
			}
			swf_obj.playSelected(videoindex);
		}
		catch(e){
			if(console && console.log)console.log(e.name + "\n" + e.message);
		}
	},
	stateChangeCallback : function(state, params){
		state = (""+state).toLowerCase();
		switch(state){
			case 'play':
				PlayVideo.onstateRefresh_play(params.index, params.video_id);
				break;
			case 'stop':
			case 'finished':
				PlayVideo.onstateRefresh_stop(params.index, params.video_id);
				break;
			default: break;
		}
	},
	initPlaylist : function(length){
		var swf_obj = this._getVideoObj(this.channelPlayerName);
		if(swf_obj){
			try{
				swf_obj.initPlaylist(length);
			}catch(e){
			}
		}
	},
	addPlaylist : function (videoData,index){
		var swf_obj = this._getVideoObj(this.channelPlayerName);
		if(swf_obj){
			try{
				swf_obj.addPlaylist(videoData,index);
			}catch(e){
			}
		}
	},	
	onFlvUrl : function (strUrl, id){
		var flvid;
		if(!id){
			var swf_obj = this._getVideoObj(this.channelPlayerName);
			flvid = this.channelPlayerName;
		}
		else{	
			var swf_obj = this._getVideoObj(id);
			flvid = id;
		}
		if(swf_obj){
			try{
				swf_obj.onFlvUrl(strUrl,flvid);
			}catch(e){
			}
		}
	},		
	onFireWall : function (result, id){
		if(!id){
			var swf_obj = this._getVideoObj(this.channelPlayerName);
		}
		else{	
			var swf_obj = this._getVideoObj(id);
		}
		if(swf_obj){
			try{
				swf_obj.onFireWall(result);
			}catch(e){
			}
		}
	},		
	_getVideoObj : function (playerElementName){
		var swf_obj = null;
		if(document.getElementById){
			swf_obj = document.getElementById(playerElementName);
		}else if(document.getElementsByName){
			var objs = document.getElementsByName(playerElementName);
			if(objs.length){
				swf_obj = objs[0];
			}
		}
		return swf_obj;
	},
	addRecommendList : function (result, id){
		if(!id){
			var swf_obj = this._getVideoObj(this.channelPlayerName);
		}
		else{	
			var swf_obj = this._getVideoObj(id);
		}
		if(swf_obj){
			try{
				swf_obj.addRecommendList(result);
			}catch(e){
			}
		}
	},
	addKeywordList: function (result, id){
		if(!id){
			var swf_obj = this._getVideoObj(this.channelPlayerName);
		}
		else{	
			var swf_obj = this._getVideoObj(id);
		}
		if(swf_obj){
			try{
				swf_obj.addKeywordList(result);
			}catch(e){
			}
		}
	},
	addChannelFolderInfo: function (result, id){
		if(!id){
			var swf_obj = this._getVideoObj(this.channelPlayerName);
		}
		else{	
			var swf_obj = this._getVideoObj(id);
		}
		if(swf_obj){
			try{
				swf_obj.addChannelFolderInfo(result);
			}catch(e){
			}
		}
	}
};

window['CPH_stateChangeCallback'] = ChannelPlayerHelper.stateChangeCallback;

var PlayVideo = {
	play: function(obj, video_id, surl) {
		try{
			var video_index = ( obj.attr('videoindex') ? obj.attr('videoindex') : null );
			if(!video_index){
				return false;
			}
			ChannelPlayerHelper.setPosition(video_index);
			return false;
		}catch(e){
			return false;
		}
	},
	onstateRefresh_stop : function( video_index, video_id )	{
		window['current_playing_video_index'] = "";
		window['current_playing_video_id'] = "";
		$("#tab-content-search div.video-results-area").each(function(){
			if ($(this).hasClass("playing")) {
				$(this).removeClass("playing");
				$(this).find("a.image-bg").removeClass("play");
				var divImgPlaying = $(this).find("div.text-area img.playing");
				if (divImgPlaying)  divImgPlaying.remove();
			}
		});
	},

	onstateRefresh_play: function( video_index, video_id )	{
		var divResult = null;
		var aImgbg = null;
		divResult = $('#videolist_idx_' + video_index);
		if(divResult.length){
			window['current_playing_video_index'] = video_index;
			window['current_playing_video_id'] = video_id;
			var aImgbg = divResult.find('a.image-bg');
			divResult.addClass("playing");
			if (aImgbg) { aImgbg.addClass("play"); }
			var objSpan = divResult.find("div.text-area div.site");
			if (objSpan && objSpan.parent().find("img.playing").size() == 0) {
				objSpan.after('<img src="/images/icon_playing.gif" alt="再生中" width="62" height="14" title="再生中" class="playing" />');
			}

			//if (!jQuery.browser.msie) {
				divResult.parent().parent().parent().parent()[0].scrollTop = divResult[0].offsetTop;
			//}
		}
		return false;
	}
};
var playSelectedVideo = function() {
	$("#tab-content-search div.video-results-area div.text-area a.ttl").click(function() {
		var surl = $(this).attr("href");
		var video_id = '';
		PlayVideo.play($(this), video_id,surl);
		return false;
	});
	$("#tab-content-search div.video-results-area a.image-bg").click(function() {
		var surl = $(this).attr("href");
		var video_id = '';
		PlayVideo.play($(this), video_id,surl);
		return false;
	});
};

var gpIEMenuExt;
function InitAssistant()
{
	if (gpIEMenuExt)
		return;
	try {
		gpIEMenuExt = new ActiveXObject("WoopiePlugin.WoopieConnect");
	}catch (e) {}
	if (gpIEMenuExt)
	{
		gpIEMenuExt.onState = OnFlvUrl;
		gpIEMenuExt.OnNetState = OnFireWall;
		gpIEMenuExt.Update();
		gpIEMenuExt.CheckFireWall();
		return 1;
	}
	else
		return 0;
}

function GetFlvFromIEScript(surl)
{
	if (gpIEMenuExt)
	{
		gpIEMenuExt.GetFlvUrlFromScriptIE(1000, surl, "");
	}
}
function OnFireWall(result, id){
	ChannelPlayerHelper.onFireWall(result,id);
}
function OnFlvUrl(strUrl, id)
{
	ChannelPlayerHelper.onFlvUrl(strUrl, id)
}

//run playSelectedVideo when document ready/onload
//$(function(){
//	playSelectedVideo();
//});

