/************************************************
*  Name              :  pandora.ch.player.v1.js
*  Current Version   :  0.0.0.1
*  Create by Date    :  2011-08-24
*  Create by         :  ray.kim
*  Last Update Date  :  
*  Last Update By    :  
*  Description       :
*************************************************/

/* 플레이어 생성 START */
playerSet = {
	_playUrl : "",
	_playWidth : "100%",
	_playHeight : "100%",
	_ViewerId : "divPlayer",
	_PlayerId : "flvPlayer",
	_IcfId : "icfDiv",
	_inVideo : "",
	_Sbs : "false",
	_Prgid : "false",
	oMo : null,

	initialize : function(userid, prgid){
		this._playUrl = 'http://imgcdn.pandora.tv/gplayer/pandora_ChannelPlayer_Global.swf?userid=' + userid + '&prgid=' + prgid +'&other='+SBSis+'&lang='+ chInfoJson['clientLang'];
		if(prgid) this._Prgid = prgid;
	},
	
	//alert(this._playUrl);

	startPlay : function(){
		var Obj = this;
		var _playObj = "";

		try {
			$("icfSkipDiv").style.display = "none";
		} catch (e) {}

		try {
			$("icfDiv").style.display	= "none";
			$("icfDiv").innerHTML		= "";
		} catch (e) { }

		try {
			$("flvIcf").style.display	= "none";
			$("flvIcf").innerHTML		= "";
		} catch (e) { }

		try {
			$("endingDiv").style.display	= "none";
			$("endingDiv").innerHTML		= "";
		} catch (e) { }

		try {
			$("shakeIcf").style.display = "none";
			$("shakeIcf").innerHTML		= "";
		} catch (e) { }

		if(extIcfSet == "midroll") {
			document.flvPlayer.end_extend_ad();
		} else {
			if(Prototype.Browser.IE) _playObj = this.playerIE(this._playUrl, this._PlayerId);
			else _playObj = this.playerETC(this._playUrl, this._PlayerId);

			$(this._ViewerId).update(_playObj);
		}
		
		if(this._Prgid) {
			// 영상 힛트수 높히기
			if(hitDelay == false) {
				hitDelayInterval = setTimeout(function() {	// 채널 최초 로딩시 브라우저 죽는 현상 대처를 위해 적용 : alan - 20080822
					hitDelay = true;
					prgHitAction();
				}, 3000);
			} else {
				prgHitAction();
			}

			setTimeout( function () {
				watchedVideo();	// 최근본 동영상에 담기
			}, 1000);
		}
	},

	playerIE : function(Url, objName, type){
		var _playObj = "";
		var _playUrls = "";
		var _wmode = "transparent";

		if (!type || type == undefined) {
			_playUrls = Url;
		} else {
			_playUrls = "http://imgcdn.pandora.tv/gplayer/category/icf_player_category.swf?lang=" + chInfoJson['language_code'] + "&" + Url;
		}

		_playObj += '<object id="'+objName+'" style="visibility:visible" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,2,0,0" width="'+this._playWidth+'" height="'+this._playHeight+'" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" >';
		_playObj += '<param name="movie" value="'+_playUrls+'" />';
		_playObj += '<param name="quality" value="high" />';
		_playObj += '<param name="play" value="true" />';
		_playObj += '<param name="loop" value="true" />';
		_playObj += '<param name="scale" value="noScale" />';
		_playObj += '<param name="wmode" value="'+_wmode+'" />';
		_playObj += '<param name="devicefont" value="false" />';
		_playObj += '<param name="bgcolor" value="#FFFFFF" />';
		_playObj += '<param name="menu" value="true" />';
		_playObj += '<param name="allowFullScreen" value="true" />';
		_playObj += '<param name="allowScriptAccess" value="always" />';
		_playObj += '<param name="salign" value="T" />';
		_playObj += '</object>';

		return _playObj;
	},

	playerETC : function(Url, objName, type){
		var _playObj = "";
		var _playUrls = "";
		var _wmode = "transparent";

		if (!type || type == undefined) {
			_playUrls = Url;
		} else {
			_playUrls = "http://imgcdn.pandora.tv/gplayer/category/icf_player_category.swf?lang=" + chInfoJson['language_code'] + "&" + Url;
		}

		_playObj += '<embed name="'+objName+'" id="'+objName+'" style="visibility:visible" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+this._playWidth+'" height="'+this._playHeight+'" align="middle"';
		_playObj += 'src="'+_playUrls+'"';
		_playObj += 'quality="high"';
		_playObj += 'play="true"';
		_playObj += 'loop="true"';
		_playObj += 'scale="noScale"';
		_playObj += 'wmode="'+_wmode+'"';
		_playObj += 'devicefont="false"';
		_playObj += 'bgcolor="#FFFFFF"';
		_playObj += 'menu="true"';
		_playObj += 'allowFullScreen="true"';
		_playObj += 'allowScriptAccess="always"';
		_playObj += 'salign="T" type="application/x-shockwave-flash">';
		_playObj += '</embed>';

		return _playObj;
	},

	endding : function(Mode, ch_userid, prgid){
		
		var Obj = this;
		var endingStart = "";
		var html = flashHtml("http://imgcdn.pandora.tv/gplayer/category/pandora_Endingstill.swf?other=" + SBSis + "&lang=" + chInfoJson['clientLang']+ "&stillUrl="+escape("")+"&stillTime=8&stillReCount=1&" + endingStart + "ending=" + Mode + "&ch_userid=" + ch_userid +"&prgid=" + prgid + "&sbs=" + this._Sbs, 'enddingPlayer' );
		//var html = flashHtml("http://imgcdn.pandora.tv/gplayer/category/pandora_Endingstill.swf?other=" + SBSis + "&lang=cn&stillUrl="+escape("")+"&stillTime=8&stillReCount=1&" + endingStart + "ending=" + Mode + "&ch_userid=" + ch_userid +"&prgid=" + prgid + "&sbs=" + this._Sbs, 'enddingPlayer' );
		$(this._IcfId).show();
		$(this._IcfId).update(html);
		// 광고 강제집행 (5초 1번)2011-10-05
		try {
			loadJS("http://ads.pandora.tv/NetInsight/text/pandora_global/channel/endding@300x250");
		} catch (e) {}		

		//try { Obj.oMo.mo_stop(); } catch(e) {}
		try { 
			$("microAdDiv").hide();
			$("microAdDiv").innerHTML = "";
			$("microAdDiv1").hide();
			$("microAdDiv1").innerHTML = "";
		} catch(e) {}
	},

	endingAd : function(){
		//loadJS("http://ads.pandora.tv/NetInsight/text/pandora/pandora_channel/main@endding_01");
	},

	endingNext : function(){
		$(this._IcfId).innerHTML = "";
		$(this._IcfId).hide();
		$("endingDiv").style.display = "none";
		endPlay();
	},

	videoShare : function(ch_userid, prgid){
		LayerAction.Share(ch_userid, prgid);
	},

	videoPlay : function(){
		var Obj = this;
		//try { Obj.oMo.mo_play(); } catch(e) {}
	},

	videoPause : function(){
		var Obj = this;
		//try { Obj.oMo.mo_stop(); } catch(e) {}
	},

	videoReplay : function(){
		try { 
			clearTimeout(setstartStill);
			$("endingDiv").style.display = "none";
		} catch(e) {}

		$(this._IcfId).innerHTML = "";
		$(this._IcfId).hide();
		document.flvPlayer.reStart();
	},

	endingReplay : function(){
		try { clearTimeout(setstartStill); } catch(e) {}
		$(this._IcfId).innerHTML = "";
		$(this._IcfId).hide();
		$("endingDiv").style.display = "none";
		document.flvPlayer.reStart();
	},

	inVideo : function(Mode){
		if(Mode == "on") {
			if($("microAdDiv").style.display == "inline") {
				this._inVideo = "microAdDiv";
				$("microAdDiv").style.display = "none";
			} else if($("microAdDiv1").style.display == "inline") {
				this._inVideo = "microAdDiv1";
				$("microAdDiv1").style.display = "none";
			}
		} else {
			if(this._inVideo) $(this._inVideo).style.display = "inline";
		}
	}
};
/* 플레이어 생성 END */

/* 액션 생성 START */
var LayerAction = {
	_channelHost : 'http://channel.pandora.tv/',

	Share : function (userid, prgid) {
		window.open("http://channel.pandora.tv/php/embed.ptv?userid="+userid+"&prgid="+prgid, "share", 'width=612, height=645,resizable=yes,scrollbars=yes');
	},

	Share_Twit : function (title, userid, prg_id) {
		if(prg_id == undefined) {
			var title = VodInfo['title'];
			var userid = VodInfo['userid'];
			var prg_id = VodInfo['prg_id'];
		}

		window.open('http://twitter.com/intent/tweet?text=' + encodeURIComponent(title) + "&url=" + encodeURIComponent(this._channelHost+"channel/video.ptv?ch_userid="+userid+"&prgid="+prg_id) + "&via=pandora.tv", 'twitter', 'width=472, height=340,resizable=yes,scrollbars=yes');
	},

	Share_Me2day : function (title, userid, prg_id) {
		if(prg_id == undefined) {
			var title = VodInfo['title'];
			var userid = VodInfo['userid'];
			var prg_id = VodInfo['prg_id'];
		}

		window.open('http://me2day.net/posts/new?new_post[body]=%22' + encodeURIComponent(title) + '%22:' + encodeURIComponent(this._channelHost+"channel/video.ptv?ch_userid="+userid+"&prgid="+prg_id), 'twitter', 'width=600, height=400,resizable=yes,scrollbars=yes');
	},

	Share_Face : function (title, userid, prg_id) {
		if(prg_id == undefined) {
			var title = VodInfo['title'];
			var userid = VodInfo['userid'];
			var prg_id = VodInfo['prg_id'];
		}

		window.open('http://www.facebook.com/share.php?u=' + encodeURIComponent(this._channelHost+"channel/video.ptv?ch_userid="+userid+"&prgid="+prg_id), 'facebook', 'width=600, height=400,resizable=yes,scrollbars=yes');
	},

	Share_Cyworld : function (title, userid, prg_id) {
		if(prg_id == undefined) {
			var title = VodInfo['title'];
			var userid = VodInfo['userid'];
			var prg_id = VodInfo['prg_id'];
		}

		window.open('http://csp.cyworld.com/bi/bi_recommend_pop.php?url=' + encodeURIComponent(this._channelHost+"channel/video.ptv?ch_userid="+userid+"&prgid="+prg_id), 'cyworld', 'width=400, height=364,resizable=yes,scrollbars=no');
	},

	Embed : function (userid, prgid) {
		window.open("http://channel.pandora.tv/php/embed.ptv?userid="+userid+"&prgid="+prgid, "share");
		return;
	}
};
/* 액션 생성 START */

/* MO 생성 START*/
function MO(parent) {
	this._super = parent;
	this.panset = "on";

	this._res       = null;
	this._Imgres       = null;

	this.mi         = 0;
	this.mj         = 0;
	this.mk         = 0;
	this.snum       = "";
	this.csnum      = "";
	this.oldsum     = "";

	// mo 기본 설정
	this.dt         = new Date();
	this.delaytime  = 21000;
	this.gettime    = 10100;
	this.inittime   = 1000;
	this.xsize      = 11;
	this.xborder    = 1;
	this.xdraw      = 1;

	this.tid1       = null;
	this.tid2       = null;
	this.tid3       = null;
	this.tid4       = null;
	this.tid5       = null;
	this.msc        = null;

	this.adMsg		= new Array();

	this.imgpath	= "http://imgcdn.pandora.tv/static/";
	this.currentShowLogo = 0;

	this.setIni = function(isMO) {	// 0 / 1
		switch(true) {
			case (this.panset == "on" && isMO == 1) : return;
			case (this.panset == "off" && isMO == 0) : return;
		}

		this.panset = (isMO == 1) ? "on" : "off";
		if(this._super.isPlay() == true) {
			if(isMO == 0) this.mo_stop();
			else {
				if(isMO == 1) { this.mo_play(); }
			}
		}
	}

	this.initMsg = function () {	// 메세지 초기화
		try { 
			document.flvPlayer.setMo("msg", '{"m_clickURL":"","m_message":"","m_fontColor":"","m_font":"","m_fontSize":""}');
		} catch(e) {return;}
	}

	this.writeHtml = function () {	// 메세지 출력
		try {
			var obj = this;
			this.mj++;
			this.csnum = oCookie.get("snum");
			this.msc = this.dt.getSeconds() + "" + this.dt.getMilliseconds();

			this.getMo("snum");

			if ((this.snum != this.oldsnum || this.mj < 2) && (this.snum != "" && this.csnum != this.snum)) {
				this.getMo("msg");
			} else if(this.mj < 2) {
				this.initMsg();
			}

			if(this.mj < 2) {
				this.tid1 = setTimeout( function () { obj.writeHtml(); }, this.inittime);
			} else {
				clearTimeout(this.tid1);
				this.tid2 = setTimeout( function () { obj.writeHtml(); }, this.gettime);
			}
		} catch (e) {}
	}

	this.mo_play = function () {
		this.mo_stop();
		if(this.panset == "off") {
			this.show_logo(0);
		} else {
			if( parseInt(chInfoJson['mychannel'], 10) == 1)	return;		// my channel 
			this.writeHtml();
			this.show_logo(1);
		}
	}

	this.mo_stop = function () {
		var obj = this;
		try {
			this.clearCheck();
		} catch (e) {
			this.tid2 = setTimeout( function () { obj.mo_stop(); }, 2000);
		}
	}

	this.clearCheck = function () {
		try {
			clearTimeout(this.tid1);
			clearTimeout(this.tid2);
			clearTimeout(this.tid3);
			clearTimeout(this.tid4);
			document.flvPlayer.stopMo("all");
		} catch (e) {}
	}

	this.clearSet = function () {
		this.mo_stop();
	}

	this.show_pan = function () {
		try {
			if(this.currentShowLogo==1) {
				this.show_logo(0);
				this.clearSet();
			} else {
				this.show_logo(1);
				this.writeHtml();

				this.panset = "on";
			}
		} catch (e) {}
	}

	this.show_logo = function (num) {
		if(num == 1) {
			this.currentShowLogo = 1;
		} else {
			this.currentShowLogo = 0;
		}
	}

	this.displayMsg = function (args) {
		var obj = this;
		var strHTML = "";
		var size    = new Array();
		var size_w  = "";
		var size_h  = "";
		var vmsg    = "";

		if(this.panset != "off") {
			if(args != undefined) this._res = args;
			if((this._res != undefined)) {
				if(this._res.length > this.mi) {
					if (this.mk < 1 && parseInt(this.adMsg.length, 10) > 0) {  //페이지 로딩후 최초1회한 2007-09-12 10:13오전 by royes
						var rn = Math.floor(Math.random()* parseInt(this.adMsg.length, 10));
						strHTML = this.adMsg[rn];
						this.gettime=10100;
						this.mk++;
					}
					else {
						strHTML = this._res[this.mi];
						strHTML = strHTML.replace("\n", "");  //개행문자변경.. only for p3player
						this.gettime=10100;
						this.mi++;
					}
					var vcolor = "";
					var vvmsg = strHTML.substring(0, strHTML.length-2);

					var msgAr = new Array();

					var m_message = "";
					var m_clickURL = "";
					var m_screen = "true";
					var m_font = "Arial,Tahoma, Helvetica, sans-serif";
					var m_fontColor = "#ffffff";
					var m_fontSize = "12";
					var m_rollingTime = "10";
					var m_setting = "";

					// 메시지|URL|새창여부(popup,new)|창 사이즈”_”가 구분자|폰트 색|폰트 종류
					//   0   | 1 |    2              |         3           |   4   |    5
					var is_admin = strHTML.substring(strHTML.length-2,strHTML.length);	//관리자추가(a1:판PD, a2:도라PD)
					msgAr = vvmsg.split("|");

					// 폰트색 정의
					if (is_admin == ")a" || is_admin == "a1" || is_admin == "a2" || is_admin == "ad") {
						if (is_admin == "ad") {
							msgAr[4] = "0x00" + "ff74d0";
							m_fontColor = "#FFFF00";
						} else if (is_admin == "a2") {
							msgAr[4] = "0x00" + "ff74d0";
							m_fontColor = "#febbfe";
						} else {
							msgAr[4] = "0x00" + "00ffff";
							m_fontColor = "#A851FF";
						}
					} else {
						if (msgAr[4] == "" || msgAr[4] == undefined) {
							msgAr[4] = "0x00" + "bbbbbb";
						} else {
							msgAr[4] = "0x00" + msgAr[4].substring(-2) + msgAr[4].substring(2, 2) + msgAr[4].substring(2);
						}
						m_fontColor = "#ffffff";
					}
					// 폰트 정의
					if (msgAr[5] == "" || msgAr[5] == undefined) {
						msgAr[5] = "돋움";
					}

					// 링크 정의
					msgAr_2 = "0";
					// 윈도우 창 크기
					if (msgAr[2] == "p" && (msgAr[3] && msgAr[3] != undefined) ) {
						size = msgAr[3].split("_");
						size_w = size[0];
						size_h = size[1];
						msgAr_2 = "2";
					} else {
						if (msgAr[2] == "n") {
							msgAr_2 = "1";
						}
						size_w = "0";
						size_h = "0";
					}

					// 메세지 정의
					if (msgAr[1] == "" || msgAr[1] == undefined) {
						if (msgAr.length > 1) {
							vmsg = msgAr[0];
						} else {
							vmsg = vvmsg;
						}
						msgAr[1] = " ";
					} else {
						if (msgAr_2 == "0") {
							msgAr_2 = "1";
						}
						vmsg = "[클릭]"+msgAr[0];
						m_clickURL = msgAr[1];
					}
					m_message = vmsg;

					//관리자가 아닌 경우 mo 리스트 경로로 이동(도미노피자 이벤트)
					//if ((is_admin != "a") && (msgAr[1] == "" || msgAr[1] == undefined)) {
					//if (is_admin != "a") {
					if (is_admin != ")a" && is_admin != "a1" && is_admin != "a2" && is_admin != "ad") {
						msgAr_2 = "1";
						m_clickURL = "http://mobile.pandora.tv/mo/r.ptv";
					} else {
						if(m_clickURL == "")	{
							msgAr_2 = "1";
							m_clickURL = "http://mobile.pandora.tv/mo/r.ptv";
						}
					}

					m_setting = '{"m_message":"'+m_message+'","m_clickURL":"'+m_clickURL+'","m_screen":"'+m_screen+'","m_font":"'+m_font+'","m_fontColor":"'+m_fontColor+'","m_fontSize":"'+m_fontSize+'","m_rollingTime":"'+m_rollingTime+'"}';
					try {
						document.flvPlayer.setMo("msg", m_setting);
					} catch (e) {}

					clearTimeout(this.tid2);
					if(this._res.length > this.mi) {
						this.tid3 = setTimeout( function () { obj.displayMsg(); }, this.gettime);
					} else {
						this.tid3 = setTimeout( function () { obj.displayMsg(); }, this.delaytime);
					}

				} else {
					this.mi = 0;
					this.initMsg();
					clearTimeout(this.tid2);
					this.tid2 = setTimeout( function () { obj.writeHtml(); }, this.inittime);
				}
			}
		}
	}

	this.getMo = function (res) {
		var targetUrl   = "";
		var dummy = new Date().getTime();

		if (res == "msg" || res == null) {
			targetUrl   = "/data/mo/msg.htm?"+ dummy;
			this.oldsnum = this.snum;
			oCookie.set("snum", this.snum);
		} else {
			targetUrl   = "/data/mo/snum.htm?"+ dummy;
		}

		new Ajax.Request(targetUrl, {
			method : 'get',
			onSuccess : function(res){
				this.ajaxCallback(res);
			}.bind(this),
			onException : function(res){
				throw new Error("Exception : mo list json fail");
			}
		});
	}

	this.ajaxCallback = function (res) {
		var ajax = "";

		if(res.responseText != "") {
			try {
				eval("ajax = " + res.responseText);
			} catch (e) {}

			if (res.responseText.indexOf(this.imgpath) == -1) {

				if (parseInt(res.responseText, 10) > 20070000000000) {
					this.snum = parseInt(res.responseText, 10);
				} else {
					//alert("responseText:"+res);
					this.displayMsg(ajax[0]);
				}
			}
		}
	}
}
/* MO 생성 END*/
