/*********************************************
*	Name				:	
*	Current Version		:	0.0.1
*	Create by Date		:	
*	Last Update_Date	:	
*	Create by			:	Oh Hyun Sub
*	Description			:	
**********************************************/

var SharedObject = {
	_globalStore : null,

	/* flash version */
	_version : null,

	/* public */
	isLoaded : false,

	/* private */
	isIE : !!(window.attachEvent && !window.opera),

	_isLoaded : function(v) {
		this.isLoaded = true;
		this._version = v;
	},

	/* private */
	_create : function() {
		var tag = "";
		var codeBase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0';//9,0,115,0';
		var classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';

		switch(this.isIE) {
			case true :
//					tag +='<div style="position:absolute;top:-100px">';
					tag +='<object id="globalStorage__" codebase="'+ codeBase +'" width="100" height="100" align="middle" classid="'+classid+'">';
					tag +='<param name="movie" value="http://imgcdn.pandora.tv/gimg/so/_shared.swf?Adfas" />';
//						tag +='<param name="movie" value="http://imgcdn.pandora.tv/ptv_img/shared2.swf" />';
					tag +='<param name="quality" value="high" />';
					tag +='<param name="play" value="true" />';
					tag +='<param name="loop" value="true" />';
					tag +='<param name="scale" value="showall" />';
					tag +='<param name="wmode" value="window" />';
					tag +='<param name="devicefont" value="false" />';
					tag +='<param name="bgcolor" value="#000000" />';
					tag +='<param name="menu" value="true" />';
					tag +='<param name="allowFullScreen" value="true" />';
					tag +='<param name="allowScriptAccess" value="always" />';
					tag +='<param name="salign" value="" />';
					tag +='<embed id="globalStorage__" name="globalStorage__" width="0" height="0" src="http://imgcdn.pandora.tv/gimg/so/_shared.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" play="true" loop="true" scale="showall" wmode="window" devicefont="false" bgcolor="#000000" menu="true" allowFullScreen="true" allowScriptAccess="always" salign="" type="application/x-shockwave-flash"></embed>';
					tag +='</object>';
//					tag +='<div>';
			break;
			default:
//					tag +='<div style="position:absolute;top:-100px">';
					tag +='<embed id="globalStorage__" name="globalStorage__" width="0" height="0" src="http://imgcdn.pandora.tv/gimg/so/_shared.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" play="true" loop="true" scale="showall" wmode="window" devicefont="false" bgcolor="#000000" menu="true" allowFullScreen="true" allowScriptAccess="always" salign="" type="application/x-shockwave-flash"></embed>';
//					tag +='<div>';
			break;
		}

//		$("#sharedDiv").html(tag);
		document.getElementById("sharedDiv").innerHTML = tag;
	},


	/* public */
	setup: function(){
		/* exist Shared Object Movie */
		if(this._globalStore != null) return true;

		this._create();

		this._globalStore = (this.isIE) ? window['globalStorage__'] : document['globalStorage__'];
	},

	/** public
	* SharedObject.data[key] = value; use in Action script
	* @param {String} key
	* @param {String} value
	* @param {String} so	Shared-Object filename
	*/
	setItem : function(key, value, so) {
		var value = encodeURIComponent(value);
		return this._globalStore.setItem(key, value, so);
	},

	/** public
	 *
	 * @param {String} key
	 * @param {String} so	Shared-Object filename
	 */
	getItem : function(key, so) {
		return decodeURIComponent(this._globalStore.getItem(key, so));
	},

	/** public
	 *
	 * @param {String} key
	 * @param {String} so	Shared-Object filename
	 */
	removeItem : function(key, so) {
		return this._globalStore.removeItem(key, so);
	}
}

// 내가 담은 영상
var freeaddPlaylist = {
	vHtml : "",
	vData : "",
	vLink : "",
	mouse : "",
	data : [],
	pageLimit : 100,
	set : function(goPage, currLeftMenu) {
		try {
			this.data = eval(SharedObject.getItem("glb_bsk")).reverse();
		}
		catch (e) {
		}

		var totCnt = this.data.length;
		if (totCnt ) {
			goPage = (parseInt(goPage, 10)) ? goPage : 1;

			var totPage = Math.ceil(totCnt / this.pageLimit);

			var startNum = (goPage - 1) * this.pageLimit;
			var endNum = 0;
			if (goPage == totPage) {
				endNum = totCnt;
			} else if (goPage < totPage) {
				endNum = (goPage * this.pageLimit) + 1;
			}

			this.vData = "";
			for(var i=startNum; i<endNum; i++){
				if (this.data[i].length==5) {
					var thumb = getVodThumbnail(this.data[i][4],this.data[i][0]);
				}
				else {
					var pData = this.data[i][5];
					var thumb = getVodThumbnail(pData.chuserid, pData.prgid);
				}

				if (currLeftMenu == this.data[i][0]) {
					checkPrgid = "class=\"on\"";
					mouse = ""
				}
				else {
					checkPrgid = "";
					mouse = " onmouseover=\"StyleOver('recmenu"+i+"', 'over')\" onmouseout=\"StyleOver('recmenu"+i+"', 'out')\" ";
				}

				vLink = "onclick=\"changePrgAjaxCall('" + this.data[i][0] + "', 'img','1', 'addPlayList','"+this.data[i][4]+"')\"";

				this.vData += "					<dl id=\"recmenu"+i+"\" " + mouse + checkPrgid+" title=\""+this.data[i][1]+"\">";
				this.vData += "						<dd class=\"img\"><img class=\"prg_thumb\" src=\""+thumb+"\"  alt=\""+this.data[i][1]+"\" "+vLink+" width=60 height=45 error=\"javascript:imgError(this,'http://imgcdn.pandora.tv/static/prg_thumb.gif');\"></dd>";
				this.vData += "						<dt><span "+vLink+">" + getStrCuts(this.data[i][1],23) + "</span> (<a href=\"javascript:watchedVideo_DEL(" + this.data[i][0] + ",'glb_bsk')\">삭제</a>)</dt>";
				this.vData += "						<dd class=\"videoInfo\"><span class=\"time\">" + this.data[i][2] + "</span></dd>";
				this.vData += "					</dl>";
			}
		} else {
			this.vData = "<div id=\"contnet\"><center>내가 담은 영상이 없습니다</center></div>";
		}

		this.vHtml  = "			<div class=\"menuTitle\">";
		this.vHtml += "				<div class=\"right\"></div>";
		this.vHtml += "				<div class=\"left\">";
		this.vHtml += "					<div class=\"title\">내가 담은 영상</div>";
		this.vHtml += "				</div>";
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";
		this.vHtml += "			<div class=\"list\">";
		this.vHtml += this.vData;
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";

		$(".categoryList").hide();
		$("#ch_addPlayListDiv").html(this.vHtml);
		$("#ch_addPlayListDiv").show();
	},
	Out : function() {
		this.vHtml  = "			<div class=\"menuTitle\">";
		this.vHtml += "				<div class=\"right\"></div>";
		this.vHtml += "				<div class=\"left\">";
		this.vHtml += "					<div class=\"title\">내가 담은 영상</div>";
		this.vHtml += "				</div>";
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";
		this.vHtml += "			<div class=\"list\">";
		this.vHtml += "			<div id=\"contnet\"><center>로그인 후 서비스 이용이 가능합니다.</center></div>";
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";

		$(".categoryList").hide();
		$("#ch_addPlayListDiv").html(this.vHtml);
		$("#ch_addPlayListDiv").show();
	}
}

// 최근 본 영상
var freeRecent = {
	vHtml : "",
	vData : "",
	vLink : "",
	mouse : "",
	data : [],
	pageLimit : 100,
	set : function(goPage, currLeftMenu) {
		try {
			this.data = eval(SharedObject.getItem("vMovie")).reverse();
		}
		catch (e) {
		}

		var totCnt = this.data.length;
		if (totCnt ) {

			goPage = (parseInt(goPage, 10)) ? goPage : 1;

			var totPage = Math.ceil(totCnt / this.pageLimit);

			var startNum = (goPage - 1) * this.pageLimit;
			var endNum = 0;
			if (goPage == totPage) {
				endNum = totCnt;
			} else if (goPage < totPage) {
				endNum = (goPage * this.pageLimit) + 1;
			}

			this.vData = "";
			for(var i=startNum; i<endNum; i++){
				if (this.data[i].length==5) {
					var thumb = getVodThumbnail(this.data[i][2],this.data[i][1]);
				}
				else {
					var pData = this.data[i][5];
					var thumb = getVodThumbnail(pData.chuserid, pData.prgid);
				}

				if (currLeftMenu == this.data[i][1]) {
					checkPrgid = "class=\"on\"";
					mouse = ""
				}
				else {
					checkPrgid = "";
					mouse = " onmouseover=\"StyleOver('recmenu"+i+"', 'over')\" onmouseout=\"StyleOver('recmenu"+i+"', 'out')\" ";
				}

				vLink = "onclick=\"changePrgAjaxCall('" + this.data[i][1] + "', 'img','1', 'Recent','"+this.data[i][2]+"')\"";

				this.vData += "					<dl id=\"recmenu"+i+"\" " + mouse + checkPrgid+" title=\""+this.data[i][0]+"\">";
				this.vData += "						<dd class=\"img\"><img class=\"prg_thumb\" src=\""+thumb+"\"  alt=\""+this.data[i][0]+"\" "+vLink+" width=60 height=45 error=\"javascript:imgError(this,'http://imgcdn.pandora.tv/static/prg_thumb.gif');\"></dd>";
				this.vData += "						<dt><span "+vLink+">" + getStrCuts(this.data[i][0],23) + "</span> (<a href=\"javascript:watchedVideo_DEL(" + this.data[i][1] + ",'vMovie')\">삭제</a>)</dt>";
				this.vData += "						<dd class=\"videoInfo\"><span class=\"time\">" + this.data[i][3] + "</span></dd>";
				this.vData += "					</dl>";
			}

		} else {
			this.vData = "최근 본 영상이 없습니다";
		}


		this.vHtml  = "			<div class=\"menuTitle\">";
		this.vHtml += "				<div class=\"right\"></div>";
		this.vHtml += "				<div class=\"left\">";
		this.vHtml += "					<div class=\"title\">최근 본 영상</div>";
		this.vHtml += "				</div>";
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";
		this.vHtml += "			<div class=\"list\">";
		this.vHtml += this.vData;
		this.vHtml += "				<div class=\"clear\"></div>";
		this.vHtml += "			</div>";

		$(".categoryList").hide();
		$("#ch_RecentDiv").html(this.vHtml);
		$("#ch_RecentDiv").show();
	}
}

function getVodThumbnail(argUserid, argPrgid) {
	argUserid = argUserid.toLowerCase();
	var noPrgid = 30800000 ; /* 로컬 통합 정책시 적용되는 기준 프로그램 아이디...*/
	var arrSizeDir = new Array("_channel_img_sm", "_channel_img");
	var path = "http://imguser.pandora.tv/pandora/";

	if(arguments[2] != undefined && (arguments[2].toUpperCase() == "M")) {
		path += arrSizeDir[1] + "/";
	} else {
		path += arrSizeDir[0] + "/";
	}

	if(argUserid && argPrgid) {
		for(var i=0;i<2;i++) {
			path += argUserid.substr(i, 1) + "/";
		}

		path += argUserid + "/";
		if(argPrgid > noPrgid) {
			var leng = String(argPrgid).length;
			path += String(argPrgid).substring(leng, leng-2) + "/";
		}
		path += "vod_thumb_" + argPrgid + ".jpg";

	} else {
		path += "g/u/guest/guest.gif";
	}
	return path;
}
