﻿	/************************************************
	*  Name              :  gnb.navigation.js
	*  Current Version   :  0.0.0.1
	*  Create by Date    :  2010-11-30
	*  Create by         :  ray.kim
	*  Last Update Date  :  2010-11-30
	*  Last Update By    :  ray.kim
	*  Description       :
	*************************************************/

	gnbNavigation = {
		_html : '',
		_infoHost : 'http://www.pandora.tv/',
		_channelHost : 'http://channel.pandora.tv/',
		_uploadHost : 'http://up.pandora.tv/',
		_apiHost : 'http://interface.pandora.tv/',
		_naviMemLang : null,
		_profileLang : null,
		_clientLang : 'ko',
		_userid : '',
		_ch_userid : '',
		_chstate : '',
		_mychannel : '',
		_setLang : function () {
			if(this.getCookie('clientLang')) this._clientLang = this.getCookie('clientLang');
			if(this.getCookie('glb_mem[userid]')) this._userid = this.getCookie('glb_mem[userid]');
			if(chInfoJson.ch_userid) this._ch_userid = chInfoJson.ch_userid;
			if(chInfoJson.chstate) this._chstate = chInfoJson.chstate;
			if(chInfoJson.mychannel) this._mychannel = chInfoJson.mychannel;

			switch (this._clientLang) {
				case "ko" :
					this._naviMemLang = ["가입시청자","우수시청자","특별시청자"];
					this._profileLang = ["프로필 보기","쪽지 보내기","홈페이지 바로가기","즐겨찾기에 추가","위젯 만들기","RSS 구독하기","선물 보내기", "채널정보 더보기","프로필","일반 채널"//0 ~ 9
										,"브랜드 채널","이름순정렬","추가순정렬","가입채널관리 바로가기","가입하기","가입회원관리","프로그램","플레이리스트","게시판","방명록"//10 ~ 19
										,"관리자","영상업로드","채널","닫기","대표이미지","채널 소개말","가입된 채널이 없습니다.","자주가는 채널을 추가해 보세요.", "받은 쪽지함","최근답글"
										,"채널 홈","쪽지","선물","이 채널에 가입하시겠습니까?", "채널을 탈퇴 하시겠습니까?", "로그인이 필요합니다. 지금 로그인 하시겠습니까?"];
				break;
				case "cn" :
					this._naviMemLang = ["登入收视会员","优秀收视会员","特别收视会员"];
					this._profileLang = ["查看简历","发送小纸条","主页","加入收藏","制造wizhe","订阅RSS","发礼物","浏览频道信息","简介（在这里交）","一般频道"//0 ~ 9
										,"品牌频道","根据名字排列","根据添加顺序排列","马上进行企业频道管理","加入","加入会员管理","节目","播放清单","公告","留言"//10 ~ 19
										,"チャンネル管理","上传视频","频道","关闭","代表形象","频道介绍","没有加入的频道请添加你经常去的频道","", "便条箱","最近的评论"
										,"我的主頁","便条","Gift", "要加入这频道吗?", "想要退出这个频道", "您需要先登录才能在目录里进行保存。现在要登录吗?"];
				break;
				case "jp" :
					this._naviMemLang = ["参加チャンネル","優秀視聴者","特別視聴者"];
					this._profileLang = ["See Profile","Send Message","Go to Homepage","Add to Favorites","Create Widgets","Subscribe to RSS Feeds","Send Presents","More Information on the Channel","Profile","General Channel"//0 ~ 9
										,"ブランドチャンネル","名前順整列","追加順整列","加入チャンネルへ","加入する","加入会員管理","プログラム","プレイリスト","掲示板","足あと"//10 ~ 19
										,"チャンネル管理","動画アップロード","チャンネル","閉じる","代表イメージ","チャンネル紹介","加入されたチャンネルがありません。","よく訪れるチャンネルを追加してみてください。", "メッセージ受信箱","最新のコメント"
										,"Home","メッセージ","Gift","上のチャンネルに加入しますか。", "チャンネルを脱会しますか?", "ログインが必要なサービスです。\nログインページへ移動しますか？"];
				break;
				default :
					this._naviMemLang = ["Subscribers","Silver Members","Gold Members"];
					this._profileLang = ["See Profile","Send Message","Go to Homepage","Add to Favorites","Create Widgets","Subscribe to RSS Feeds","Send Presents","More Information on the Channel","Profile","General Channel" //0 ~ 9
										,"Brand Channel","Sort by Title","Sort by Date","Manage My Subscribed Channels","Subscribe","Manage Viewers","Program","Playlist","Board","Guestbook" //10 ~ 19
										,"Admin","Upload Video","Channel","Close","My channel image","My channel introduction","No data found.","You can add favorite channel.", "Message Inbox","Most Recent Comments"
										,"Home","Message","Gift","Subscribe this channel?", "Unsubscribe this channel?", "You have to login to subscribe, login now?"];
				break;
			}
		},

		/* set cookie value */
		setCookie : function (sName, sValue, expireSeconds) {
			var sDomain = ".pandora.tv";
			var todayDate = new Date();
			var ExpireTime = new Date(todayDate.getTime()+expireSeconds*1000);
			document.cookie = sName + "=" + sValue + ";" + ((expireSeconds) ? "expires=" + ExpireTime.toGMTString() + ";" : "") + "domain=" + sDomain + ";path=/;";
		},

		/* get cookie value */
		getCookie : function (sName) {
			var aCookie = document.cookie.split("; ");

			for (var i=0; i<aCookie.length; i++) {
				var aCrumb = aCookie[i].split("=");
				if (sName == aCrumb[0]) {
					return aCrumb[1];
				}
			}
			return null;
		},

		// 채널가입정보
		chstate : function () {
			var join_mem = '';

			switch(this._chstate){
				case "10001":
					join_mem = '<a><span class="ch_level">'+this._naviMemLang[0]+'</span></a>';		//"가입시청자";
					break;

				case "10002":
					join_mem = "<a><span class='ch_level'>"+this._naviMemLang[1]+"</span></a>";		//"우수시청자";
					break;

				case "10003":
					join_mem = "<a><span class='ch_level'>"+this._naviMemLang[2]+"</span></a>";		//"특별시청자";
					break;

				default:
					join_mem = '<a onclick="chJoin();"><span class="ch_level">'+this._profileLang[14]+'</span></a>';
					break;
			}

			if(this._mychannel == 1) join_mem = "<a href='"+this._channelHost+"/manager/?ch_userid="+this._userid+"&menu=audience'><span class='ch_level'>"+this._profileLang[15]+"</span></a>";

			return join_mem;
		},

		getMusician : function (obj) {
			switch(obj) {
				case "20001":
					musician = 'chTendency_Producer';
					break;
				case "20002":
					musician = 'chTendency_Musician';
					break;
				case "20003":
					musician = 'chTendency_Comedian';
					break;
				case "20004":
					musician = 'chTendency_Dancer';
					break;
				case "20005":
					musician = 'chTendency_Gamer';
					break;
				case "20006":
					musician = 'chTendency_Animania';
					break;
				case "20007":
					musician = 'chTendency_Sportsman';
					break;
				case "20008":
					musician = 'chTendency_Reporter';
					break;
				case "20009":
					musician = 'chTendency_Livecaster';
					break;
				case "20010":
					musician = 'chTendency_Premium';
					break;
				case "20011":
					musician = 'chTendency_SBS';
					break;
			}

			return musician;
		},

		// 즐겨찾기 추가
		favorite : function () {
			var addthis_title = chInfoJson.chname + " " + chInfoJson.meta_title;
			addthis_title = addthis_title.replace(/\^/g, " ");
			var addthis_url = "http://www.pandora.tv/my." + this._ch_userid;

			addthis_url = decodeURIComponent(addthis_url);
			addthis_title = decodeURIComponent(addthis_title);
			if (document.all) window.external.AddFavorite(addthis_url,addthis_title);
			else window.sidebar.addPanel(addthis_title,addthis_url,'');
		},

		draw : function (target, menu) {
			this._setLang();

			if(chInfoJson.hompyUrl){
				var urlHtml = chInfoJson.hompyUrl.split("http://");

				if(!urlHtml[1]){
					var hompyUrl = "http://" + chInfoJson.hompyUrl;
				} else {
					var hompyUrl = chInfoJson.hompyUrl;
				}

				var newhomeURL = hompyUrl;
			}

			this._html+= '<div class="channel-container-header">';
			this._html+= '	<dl class="main-header">';
			this._html+= '		<dt>';
			this._html+= '			<img src="'+chInfoJson.logobase+'" width="40" height="40" onerror="javascript:imgError(this,\'http://imgcdn.pandora.tv/static/prg_thumb.gif\');" />';
			this._html+= '			<a onclick="profileEvent(event)" class="ico">';
			this._html+= '				<img src="http://imgcdn.pandora.tv/ptv_img/ch/ch2010/icon_titleimg.gif" id="profileICon" title="'+this._profileLang[0]+','+this._profileLang[1]+','+this._profileLang[2]+','+this._profileLang[3]+','+this._profileLang[4]+','+this._profileLang[5]+','+this._profileLang[6]+'" />';
			this._html+= '			</a>';
			this._html+= '			<div id="profileLayer" class="infobox" style="z-index:1;display:none;">';
			this._html+= '				<ul>';
			this._html+= '					<li><a onclick="$(\'layerProfile\').style.display=\'block\';">'+this._profileLang[0]+'</a></li>';
			this._html+= '					<li><a onclick="gMemoFunc(event, \'' + this._ch_userid + '\');profileEvent(event);">'+this._profileLang[1]+'</a></li>';

			if(chInfoJson.hompyUrl){
				this._html+= '					<li><a href="' + newhomeURL + '" target="_blank">'+this._profileLang[2]+'</a></li>';
			}

			this._html+= '					<li><a onclick="gnbNavigation.favorite();">'+this._profileLang[3]+'</a></li>';
			this._html+= '					<li><a href="'+this._apiHost+'?p=widget&mode=rss&rurl=' + escape(this._channelHost + "json/v003/rss.dll?userid=" + this._ch_userid) + '" target="_blank">'+this._profileLang[4]+'</a></li>';
			this._html+= '					<li><a href="'+this._channelHost+'json/v003/rss.dll?userid='+this._ch_userid+'" target="_blank">'+this._profileLang[5]+'</a></li>';
			this._html+= '					<li><a onclick="goGift(\'' + chInfoJson.chno + '\')">'+this._profileLang[6]+'</a></li>';
			this._html+= '				</ul>';
			this._html+= '			</div>';
			this._html+= '			<div id="layerProfile" class="profile" style="z-index:2;display:none;">';
			this._html+= '				<div class="profile-header">';
			this._html+= '					<h6>'+chInfoJson.nickname+'</h6>';

			if(this._mychannel != 1) {
				this._html+= '					<a onclick="gMemoFunc(event, \'' + this._ch_userid + '\')" class="areaL">'+this._profileLang[31]+'</a>';
			}

			if(this._clientLang == "ko") {
				this._html+= '					<a onclick="goGift(\'' + chInfoJson.chno + '\')" class="areaL">'+this._profileLang[32]+'</a>';
			}
			this._html+= '					<a onclick="$(\'layerProfile\').style.display=\'none\';" class="areaR"><img src="http://imgcdn.pandora.tv/ptv_img/ch/ch2010/btn_close3.gif" alt="'+this._profileLang[23]+'"></a>';
			this._html+= '				</div>';
			this._html+= '				<p class="profile-content">'+chInfoJson.chname+'</p>';
			this._html+= '			</div>';
			this._html+= '		</dt>';
			this._html+= '		<dd>';
			this._html+= '			<h3 class="ch"><span class="channel" title="'+this._profileLang[22]+'">CH.'+chInfoJson.chno+'</span> <span class="name">'+chInfoJson.nickname+'</span> <a onclick="profileEvent(event)" class="info-more">['+this._profileLang[7]+']</a></h3>';

			this._html+= '			<ul>';
			this._html+= '				<li>';
			this._html+= '					<h3 class="name areaL"><a href="/channel/video.ptv?ch_userid='+this._ch_userid+'">'+chInfoJson.chname+'</a></h3>';

			if(chInfoJson.chChType != 0) {
				this._html+= '					<span class="' + this.getMusician(chInfoJson.chChType) + ' png24 areaL" title="musician"></span>';
			}

			this._html+= '				</li>';
			this._html+= '			</ul>';

			var Status_Day=new Array();
			Status_Day[0]="1";
			Status_Day[1]="0";
			Status_Day[2]="2";
			Status_Day[3]="2";
			Status_Day[4]="2";
			Status_Day[5]="2";
			Status_Day[6]="1";

			var newDate = new Date();
			var newTime = leadingZeros(newDate.getHours(), 2) + "" + leadingZeros(newDate.getMinutes(), 2);

			var strImg = 1;
			if(newDate.getDay() != 1 && ((Status_Day[newDate.getDay()] == 2 && newTime > 1810 && newTime < 2100) || (Status_Day[newDate.getDay()] == 1 && newTime > 1640 && newTime < 2000))){
				strImg = (Math.floor(Math.random() * 3) + 1);
			}

			var adShow = "";
			if(this._clientLang != "ko" && this._clientLang != "jp") adShow = 'display:none;';

			this._html+= '		</dd>';
			this._html+= '	</dl>';
			this._html+= '	<div class="main-header-right" id="mainHeaderRight" style="'+adShow+'" aling="right">';

			if(this._clientLang == "jp") {
				//scr.src = "http://ads.pandora.tv/NetInsight/text/pandora_jp/jp_channel/main@menu_topR";
				//document.write('<script type="text/javascript" src="http://ads.pandora.tv/NetInsight/text/pandora_jp/jp_channel/main@menu_topR">');
				//<a onfocus="blur()" target="_blank" href="http://ads.pandora.tv/NetInsight/click/pandora_jp/jp_channel/main@menu_topR?cmpnno%3d414%26adsno%3d2%26ctvno%3d1682%26locno%3d173%26clkno%3d1595%26">
				this._html+= '<a href="http://www.pandora.tv/sign/complete.ptv?step=faq "><img border="0" src="http://imgcdn.pandora.tv/ptv_img/japan/ch/ch_banner_02_01.png"></a> <a href="http://www.pandora.tv/sign/complete.ptv?step=cmain"><img border="0" src="http://imgcdn.pandora.tv/ptv_img/japan/ch/ch_banner_02_02.png"></a> <a href="http://www.pandora.tv/game"><img border="0" src="http://imgcdn.pandora.tv/ptv_img/japan/ch/ch_banner_02_03.png"></a> <a href="http://www.pandora.tv/shop"><img border="0" src="http://imgcdn.pandora.tv/ptv_img/japan/ch/ch_banner_02_04.png"></a>';
			} else {
				if(strImg == 3) 
					this._html+= '';//'		<a href="http://fancast.pandora.tv/"><img src="http://imgcdn.pandora.tv/ptv_img/promotion/fan_images/chad_fan_03.gif" width="320" height="80" /></a>';
			}

			this._html+= '	</div>';
			this._html+= '</div>';

			this._html+= '<div class="gnb Bgcolor1">';
			this._html+= '	<div class="inset">';
			this._html+= '		<div class="gnb_menu">';
			this._html+= '			<ul>';
			this._html+= '				<li class="m1"><a href="/channel/?ch_userid=' + this._ch_userid + '"><span class="first-child ' + ( ( menu == 'home' )?'active':'' ) + '">'+this._profileLang[30]+'</span></a></li>';
			this._html+= '				<li class="m2"><a href="/channel/video.ptv?ch_userid=' + this._ch_userid + '"><span ' + ( ( menu == 'program' )?'class="active"':'' ) + '>'+this._profileLang[16]+'</span></a></li>';
			this._html+= '				<li class="m3"><a href="/channel/playlist.ptv?ch_userid=' + this._ch_userid + '"><span ' + ( ( menu == 'playlist' )?'class="active"':'' ) + '>'+this._profileLang[17]+'</span></a></li>';
			this._html+= '				<li class="m4"><a href="/channel/board.ptv?ch_userid=' + this._ch_userid + '"><span ' + ( ( menu == 'board' )?'class="active"':'' ) + '>'+this._profileLang[18]+'</span></a></li>';
			this._html+= '				<li class="m5"><a href="/channel/guest.ptv?ch_userid=' + this._ch_userid + '"><span class="last-child ' + ( ( menu == 'guest' )?'active':'' ) + '">'+this._profileLang[19]+'</span></a></li>';
			this._html+= '			</ul>';
			this._html+= '		</div>';
			this._html+= '		<div class="gnb_menu_aside" >';

			if(this._mychannel == 1) {
				this._html+= '			<ul>';
				this._html+= '				<li class="m1"><a href="'+this._uploadHost+'channel/upload.ptv?ch_userid='+this._userid+'"><span>'+this._profileLang[21]+'</span></a></li>';
				this._html+= '				<li class="m2"><a href="'+this._channelHost+'manager/?ch_userid='+this._userid+'"><span>'+this._profileLang[20]+'</span></a></li>';
				this._html+= '			</ul>';
			}

			this._html+= '			<ul class="gnb_level ">';
			this._html+= '				<li>'+this.chstate()+'</li>';
			this._html+= '			</ul>';
			this._html+= '		</div>';
			this._html+= '		<span class="gradient"></span>';
			this._html+= '	</div>';
			this._html+= '</div>';

			try {
				$(target).innerHTML = $(target).innerHTML + this._html;
			}
			catch (e) {}
		}
	}

	function leadingZeros(n, digits) {
		var zero = '';
		n = n.toString();

		if (n.length < digits) {
		for (i = 0; i < digits - n.length; i++)
			zero += '0';
		}

		return zero + n;
	}

	function profileEvent(obj){
		var el = Event.element(obj);

		if( $("profileLayer").style.display == "none" )	{
			$("profileLayer").style.display = "block";
			$("profileICon").src = "http://imgcdn.pandora.tv/ptv_img/ch/ch2010/icon_titleimg_1.gif";

			Event.observe("profileLayer", "mouseout", chInfoOut);
			Event.observe("profileLayer", "mouseover", chInfoOver);
		} else {
			$("profileICon").src = "http://imgcdn.pandora.tv/ptv_img/ch/ch2010/icon_titleimg.gif";
			$("profileLayer").style.display = "none";
		}
	}

	function chInfoOut() {
		$("profileLayer").style.display = 'none';
		$("profileICon").src = "http://imgcdn.pandora.tv/ptv_img/ch/ch2010/icon_titleimg.gif";
		Event.stopObserving("profileLayer", "mouseout");
		Event.stopObserving("profileLayer", "mouseover");
	}

	function chInfoOver() {
		$("profileICon").src = "http://imgcdn.pandora.tv/ptv_img/ch/ch2010/icon_titleimg_1.gif";
		$("profileLayer").style.display = 'block';
	}

	/* 채널 가입 및 탈퇴 Start */
	function chJoin() {
		if(gnbNavigation.getCookie('glb_mem[userid]')) {
			if (confirm(gnbNavigation._profileLang[33])) {
				var obj = Object.toJSON({'ch_userid':chInfoJson.ch_userid, 'login_userid':gnbNavigation.getCookie('glb_mem[userid]')})
				new Ajax.Request(variable.getChild("pathChIsapi") + "GAccount" + variable.getChild("isapiExt") + "/Join",{
					method : 'post',
					parameters : {'json':obj},
					onSuccess : function(res) {
						var msg = "";
						var ajax = res.responseText.evalJSON();
						//success
						if (ajax.IsRes == 1) {
							location.reload();
						// fail
						} else {
							switch (ajax.IsCode) {
								case 1 :
									msg = "";//"로그인을 하셔야 합니다.";joinLang[4]
									break;
								case 2 :
									msg = "";//"이미 가입된 채널 입니다.";joinLang[5]
									break;
								default :
									msg = "";//"다시 한번더 시도 해 주십시요";joinLang[6]
									break;
							}
						}

					}.bind(this),
					onException : function(res){
						throw new Error("Exception : channel join fail");
					}
				});
			}
		} else {
			if (confirm(gnbNavigation._profileLang[35])) { //joinLang[4]); // '로그인을 먼저 하셔야 합니다. 지금 로그인 하시겠습니까?');
				gnbTop.ptvLogin('login');
			}
		}
	}

	function chOut() {
		// login check
		if(gnbNavigation.getCookie('glb_mem[userid]')) {
			if (confirm(gnbNavigation._profileLang[34])) {
				var obj = Object.toJSON({'ch_userid':chInfoJson.ch_userid, 'login_userid':gnbNavigation.getCookie('glb_mem[userid]')});

				new Ajax.Request(variable.getChild("pathChIsapi") + "GAccount" + variable.getChild("isapiExt") + "/Out",{
					method : 'post',
					parameters : {'json':obj},
					onSuccess : function(res) {
						var msg = "";
						var ajax = res.responseText.evalJSON();
						//success
						if (ajax.IsRes == 1) {
							location.reload();
						// fail
						} else {
							switch (ajax.IsCode) {
								case 1 :
									msg = ""; // "로그인을 하신후 탈퇴가 가능 합니다.";outLang[1]
									break;
								case 2 :
									msg = ""; // "이미 가입된 채널 입니다.";outLang[2]
									break;
								default :
									msg = ""; // "다시 한번더 시도 해 주십시요";outLang[3]
									break;
							}
						}

					}.bind(this),
					onException : function(res){
						throw new Error("Exception : channel join fail");
					}
				});
			}
		}
	}
	/* 채널 가입 및 탈퇴 End */
