﻿//////////////////////////////////////////////////////////////////////////////////////
//
//	Dashboard Pet 0.3
//
var wall_imglist = new Object();
wall_imglist["010100"] = {img: "pet_wall_0.jpg"};
wall_imglist["020100"] = {img: "pet_wall_1.jpg"};
wall_imglist["020200"] = {img: "pet_wall_2.jpg"};
wall_imglist["020300"] = {img: "pet_wall_3.jpg"};
wall_imglist["020400"] = {img: "pet_wall_4.jpg"};
wall_imglist["020500"] = {img: "pet_wall_5.jpg"};
wall_imglist["020600"] = {img: "pet_wall_6.jpg"};
wall_imglist["020700"] = {img: "pet_wall_7.jpg"};

NPet.manager.setWall = function(wall_id) {
	$("_pet_dashhome").style.background = "url(/Home/app/pet/images/bg/" + wall_imglist[wall_id].img + ") no-repeat";
};

NPet.manager.setDashPetInfo = function(pet_info) {
	NPet.manager.setWall(pet_info.background); 
	this._petInfo.name = pet_info.name;
};

NPet.manager.setDashInfo = function() {
	this._pet.setRandomStatList();
}

NPet.manager.createDashPet = function(pInfo, homebox_id) {
	this._page_id = page_info.page_id;
	var pet_info = {};
	pet_info = pInfo;
	pet_info.name = pInfo.petname;
	pet_info.feed_cnt = pInfo.feedscore;
	pet_info.petme_cnt = pInfo.pettingscore;
	pet_info.pet_level = pInfo.pettotalscore;
	this._petInfo = pet_info;
	if (pet_info.petid == "000000") 
		pet_info.petid = "010100";
	if (!pet_info.background)
		pet_info.background = "010100";
	NPet.manager.setWall(pet_info.background);
	var rc = nus.dom.getObjectRect($(homebox_id)); 
	var homebox = {};
	homebox.bx = rc.left;
	homebox.by = rc.top;
	homebox.x = 0;
	homebox.y = 0;
	homebox.w = rc.getWidth();
	homebox.h = rc.getHeight();
	this._marker =  NPet.PetMarker.createInstance({
			ID:"my_pet_mark",
			pet_id: pet_info.petid,
			marker_id: pet_info.accessory
		});
	this._marker.setStarMarker(pInfo.pettotalscore);
	this._pet = NPet.DashPet.createInstance({
				ID:"my_pet",
				hbox: homebox,
				pet_id: pet_info.petid.substr(0,4),
				_pet_info: pet_info,
				width:NPetModel.Model.width, 
				height:NPetModel.Model.height,
				skinImageUrl: $URL("images/pet")
		});
	$(homebox_id).appendChild(this._marker.$);
	$(homebox_id).appendChild(this._pet.$);
	$(homebox_id).appendChild(this._pet._talktip.$);
	this._pet.run();
	if (pet_info.init)
		this._pet.helloMsg();		
	this._menu =  NPet.PetMenu.createInstance({
			ID:"my_pet_menu",
			hbox: homebox
		});
	$(homebox_id).appendChild(this._menu.$);
	this._gamemenu =  NPet.PetGameMenu.createInstance({
			ID:"my_pet_gamemenu",
			hbox: homebox
		});
	$(homebox_id).appendChild(this._gamemenu.$);
	var logmenu = NPet.PetLogMenu.createInstance({
			ID:"my_pet_logmenu",
			hbox: homebox
		});
	$(homebox_id).appendChild(logmenu.$);
	var shopmenu = NPet.PetShopMenu.createInstance({
			ID:"my_pet_shopmenu",
			hbox: homebox
		});
	$(homebox_id).appendChild(shopmenu.$);
};


NPet.manager.createProfilePet = function(pInfo, homebox_id) {
	this._page_id = page_info.page_id;
	var pet_info = {};
	pet_info = pInfo;
	pet_info.name = pInfo.petname;
	pet_info.feed_cnt = pInfo.feedscore;
	pet_info.petme_cnt = pInfo.pettingscore;
	pet_info.pet_level = pInfo.pettotalscore;
	this._petInfo = pet_info;
	if (pet_info.petid == "000000") 
		pet_info.petid = "010100";
	if (!pet_info.background)
		pet_info.background = "010100";
	NPet.manager.setWall(pet_info.background);
	var rc = nus.dom.getObjectRect($(homebox_id)); 
	var homebox = {};
	homebox.bx = rc.left;
	homebox.by = rc.top;
	homebox.x = 0;
	homebox.y = 0;
	homebox.w = rc.getWidth();
	homebox.h = rc.getHeight();
	this._marker =  NPet.PetMarker.createInstance({
			ID:"my_pet_mark",
			pet_id: pet_info.petid,
			marker_id: pet_info.accessory
		});
	this._marker.setStarMarker(pInfo.pettotalscore);
	this._pet = NPet.ProfilePet.createInstance({
				ID:"my_pet",
				hbox: homebox,
				pet_id: pet_info.petid.substr(0,4),
				_pet_info: pet_info,
				width:NPetModel.Model.width, 
				height:NPetModel.Model.height,
				skinImageUrl: $URL("images/pet")
		});
	$(homebox_id).appendChild(this._marker.$);
	$(homebox_id).appendChild(this._pet.$);
	$(homebox_id).appendChild(this._pet._talktip.$);
	this._pet.run();

	this._menu =  NPet.PetMenu.createInstance({
			ID:"my_pet_menu",
			hbox: homebox
		});
	$(homebox_id).appendChild(this._menu.$);

	var fPet = null;
	if (page_info.is_my_friend)
		fPet = {msrl:this._page_id, name: page_info.name, petid:pet_info.petid, petname:pet_info.name};
	this._gamemenu =  NPet.PetGameMenu.createInstance({
			ID:"my_pet_gamemenu",
			hbox: homebox,
			_event: "Profile/Pet",
			_bFirstPet: fPet
		});

	$(homebox_id).appendChild(this._gamemenu.$);

	if (nasc.User.isOwner()) {
		var logmenu = NPet.PetLogMenu.createInstance({
				ID:"my_pet_logmenu",
				hbox: homebox
			});
		$(homebox_id).appendChild(logmenu.$);
		var shopmenu = NPet.PetShopMenu.createInstance({
				ID:"my_pet_shopmenu",
				hbox: homebox
			});
		$(homebox_id).appendChild(shopmenu.$);
	}
};


NPet.manager.displayPetInfo = function(infobox_id, status) {
	if (status)
		this._petInfo = status;
	var pet_info = this._petInfo;
	var petInfo_html = [
		"<div style='font-weight:bold'>",
			"<div style='margin:2px 0px 2px 2px'>{name} {level}</div>",
			"<div><img src='/Home/app/pet/images/petme_cnt_icon.gif' align='absmiddle' style='margin-right:2px'/> Petted<span style='margin-left:10px;color:#000;font-weight:normal'>{petme_cnt}</span>", 
			"<img src='/Home/app/pet/images/feedcnt_icon.gif' align='absmiddle' style='margin-left:60px; margin-right:2px'/> Fed<span style='margin-left:10px;color:#000;font-weight:normal'>{feed_cnt}</span> </div>",
		"</div>"
	];
	var stars = "";
	if (pet_info.pet_level == 0)
		stars = "<img src='/Home/app/pet/images/pet_b_star.gif' align='absmiddle' />";
	for (var i = 1; i < NPet.starLevels.length; i++) {
		if ( NPet.starLevels[i] <= pet_info.pet_level ) 
			stars += "<img src='/Home/app/pet/images/pet_i_star.gif' align='absmiddle' />";
	}
	pet_info.level = stars;
	$(infobox_id).innerHTML = petInfo_html.toHTML(pet_info);
};


NPet.OwnerDashStatus = [
"<span class='petname'>My Name is {name}</span> <br />",
"<div class='oddpet'>",
"<div><span class='pettype'>Type</span>{type}</div><div class='clear_bar'></div>",
"</div><div class='petlabel'>In your Pagii Pocket</div>",
"<div class='oddpet'>",
/*
"<div> You have <span style='font-weight:bold';'> {left_petme}/10</span> petting and <span style='font-weight:bold';'> {left_feed}/30</span> biscuits left!</div>",
*/
"<div> You have <span style='font-weight:bold';'>{left_feed}</span> Pet Money and <span style='font-weight:bold';'>{left_petme}/10</span> petting left!</div>",
"</div>"
];


NPet.DashMenuWakeup = [
"<img src='/Home/app/pet/images/pet_i_wakeup.gif' align='absmiddle'  /><a href='javascript:void(0)' class='pet_ws_btn'><span id='wake_up'>Wake Up</span></a><div class='clear_bar'></div>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_setup'><div id='setting'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_status'><div id='status'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_feedme'><div id='feed_me'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_petme'><div id='pet_me'></div></a><div class='clear_bar'></div><br />"
].join("");

NPet.DashMenuSleep = [
"<img src='/Home/app/pet/images/pet_i_sleep.gif' align='absmiddle' /><a href='javascript:void(0)' class='pet_ws_btn'><span id='sleep'>Sleep</span></a><div class='clear_bar'></div>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_setup'><div id='setting'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_status'><div id='status'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_feedme'><div id='feed_me'></div></a>",
"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_petme'><div id='pet_me'></div></a><div class='clear_bar'></div><br />"
].join("");

NPet.DashHello = [
"<br />",
"<div class='petmsg' style='margin-top:-10px'> {pet_icon} Hey, I'm {petname}. </div>",
"<div class='petcontent' align='center'>I am your pet, click me!</div><div class='clear_bar'></div>"
]; // .join("");

NPet.DashFeedMe = [
"<div class='petmsg'>{pet_icon} {random_msg}</div><br />",
"<div style='padding-left:10px'>",
"<a href='javascript:void(0)' class='pet_df_btn pet_f_btn1'><div id='one_feed'> </div></a>",
"<a href='javascript:void(0)' class='pet_df_btn pet_f_btn2'><div id='two_feed'> </div></a>",
"<a href='javascript:void(0)' class='pet_df_btn pet_f_btn3'><div id='three_feed'> </div></a><div class='clear_bar'></div>",
// "<div align='center' style='font-size:10px'><img src='/Home/app/pet/images/feedone_icon.gif' align='absmiddle'  /> = 1 Pet Money</div>",
"</div>",
]; // .join("");


NPet.DashRandomMsg = new Object();
NPet.DashRandomMsg["browser_photo"] = [
"Check this out, cool photos on Pagii!",
"Wanna see popular photos now?",
"Get your photo on the list!",
"Find out hot photos online!",
"Looking for cool photos? Click!"
];

NPet.DashRandomMsg["browser_search"] = [
"Ready to meet new people?",  
"Add more friends, build up a network!",
"Search for new friends now!",
"Check out what others are doing!",
"Hey, wanna find out friends on Pagii?"
];

NPet.DashRandomMsg["photo_upload"] = [
"Hey, why don’t you upload your pic?",
"Get photos to for your friends!",
"Add your photo to find you easily!"
];

NPet.DashRandomMsg["new_page"] = [
"Hey, create your own page now!",
"Make you pages for others to see!",
"Get your page, tell me about you!"
];

NPet.DashRandomMsg["new_inbox"] = [
"You’ve got new messages!",
"Your friends sent you messages!",
"New messages in your inbox!"
];

NPet.DashRandomMsg["invite_friends"] = [
"Bring your friends to Pagii!",
"Find your friends already on Pagii!",
"Invite your friends now!",
"Ask your friends to join you!",
"Get your friends & build your network!"
];

NPet.DashRandomMsg["request_friend"] = [
"Request your friends now!"
];

NPet.DashRandomMsg["game_petfeed"] = [
"Ready to play game with me?",
"Wanna get some Pet Money? Click!",
"Got bored? Let’s play game!",
"Play game and earn Pet Money!"
];

NPet.DashRandomList = [
"browser_photo", "browser_search", "invite_friends", "game_petfeed"
];



NPet.DashRandomForm = [
"<div>{pet_icon}</div><div class='petlink' align='center'><a href='#' onclick='return false'><span class='auto_msg' id={id}>{link_msg}</span></a></div><br />"
];

NPet.MenuPos = [
{},
{x:  10, y: 180},
{x: 220, y: 180},
{x: 220, y: 10},
{x:  10, y: 10}
];

NPet.Net.sendDashPetInit =  function() {
	var params = { 
			pet_used : 1,
			method: "pet.setpetinfo",
			callback: "NPet.Net.dummy"		
		};
	nus.io.ScriptRequest.sendRequest(nasc.API_URL, params, null, null); 
};


////////////////////////////////////////
//
NPet.DashPet = NPetModel.extend();
NPet.DashPet.addPrototypes({
	_pMe : 10,
	_fMe : 30,
	_talktip:0,
	_tip_html:null,
	_menuHTML:null,
	_pet_info:null,
	canDrag:true,
	_petIcons:null,
	_petEvent: "Dashboard/Pet",
	_popup_time: 20,
	_fast_cnt : 6,
	_randomList: null,
	onDrag:function(ev) {
		var pos = nus.event.getEventPosition(ev);
		if ((pos.x < this.hbox.bx + 50) || (pos.x > this.hbox.bx + this.hbox.w - 50) )
			return;
		if ((pos.y < this.hbox.by + 50) || (pos.y > this.hbox.by + this.hbox.h - 50) )
			return;
		NPet.DashPet.superCall(this, "onDrag", arguments);
	},
	petmove:function(x,y) {
		var size = nus.dom.getPageSize();
		var dp = nus.dom.getScrollPos();
		var x1 = this.hbox.x; 
		var y1 = this.hbox.y;
		var x2 = x1 + this.hbox.w - this.width;
		var y2 = y1 + this.hbox.h - this.height;
		if (y1 == 0) {
			if (this._pet_info.pettotalscore > 0) 
				y1 = 18;
		}
		if (x1 > x) 
			x = x1;
		else if (x2 < x)
			x = x2; 
		if (y1 > y) 
			y = y1;
		else if (y2 < y)
			y = y2; 
		this.move(x,y);
	},
	setRandomStatList: function() {
	    if (!PageManager.page_list || PageManager.page_list.length == 0 || (PageManager.page_list.length == 1 && PageManager.page_list[0].file_id == "0")) 
			this._randomList.push("new_page");
		if ($("user_thumbnail").style.display == "none")
			this._randomList.push("photo_upload");
		if ((Dashboard.MyInfo.msgcount.newmsg > 0) || (Dashboard.MyInfo.msgcount.newinvite > 0))
			this._randomList.push("new_inbox");
	},
	onCreate: function() {
		NPet.DashPet.superCall(this, "onCreate", arguments);
		this._initPos();
		this._initTip();
		this.setStyle({cursor:"pointer"});
		this._menuHTML = NPet.DashMenuSleep;
		this._randomList = NPet.DashRandomList;
	},
	onDestroy: function() {
		NPet.DashPet.superCall(this, "onDestroy", arguments);
	},
	_tiptime_callback: function() {
		window.clearInterval(this._tipTimer);
		this._tipTimer = 0;
		if (this.bTipOpen) {
			this._tipClose();
		}
	},
	tipAutoTimer: function(sec) {
		if (this._tipTimer) 
			window.clearInterval(this._tipTimer);
		this._tipTimer = window.setInterval(this.ID+"._tiptime_callback()", 1000 * sec);
	},
	helloMsg: function() {
		this._talktip.setTipWidth(220);
		this._showTip(NPet.DashHello.toHTML({pet_icon:this._petIcon, petname: this._pet_info.name}));
		// this.tipAutoTimer(20);
		NPet.Net.sendDashPetInit();
	},
	getRandomTips: function() {
		if (!this._randomList)
			return;
		var idx = Math.floor(Math.random()* this._randomList.length);
		var id_str = this._randomList[idx];
		var tmpMsg = this.getRandomMsg(NPet.DashRandomMsg[id_str]);
		return NPet.DashRandomForm.toHTML({pet_icon: this._petIcon, id:id_str, link_msg:tmpMsg});
	},
	petAutoTips: function() {
		this._talktip.setTipWidth(245);				
		this._showTip(this.getRandomTips());
		this.tipAutoTimer(10);
	},
	_popuptime_callback: function() {
		if (this.bTipOpen || NPet.Net.is_waitNew() || (this._petState == NPetModel.STAT_DRAG)) 
			return;
		if (this._fast_cnt != 0) {
			this._fast_cnt--;
			if (this._fast_cnt == 0) {
				this._popup_time = 30;
				this.runRandomMessage();
			}
		}
		this.petAutoTips();
	},
	runRandomMessage: function() {
		if (this._popupTimer) 
			window.clearInterval(this._popupTimer);
		if (this._petState != NPetModel.STAT_SLEEP)  
			this._popupTimer = window.setInterval(this.ID+"._popuptime_callback()", 1000 * this._popup_time);
	},
	run: function() {
		this._petIcon = NPet.PetIcons[NPetModel.PetList[this.pet_id].type];
		this.petRestart();
		this.runRandomMessage();
	},
	Show: function() {},
	resetTipNew: function() {},
	change: function(petInfo) {
		this.pet_id = petInfo.petid.substr(0,4);
		NPet.manager.setDashPetInfo(petInfo);
		NPet.manager.displayPetInfo("_pet_dashinfo");
		this.petStop();
		this.petChange(this.pet_id);
		this.petStart();
		this._petIcon = NPet.PetIcons[NPetModel.PetList[this.pet_id].type];
	}, 
	_getBigDir:function() {
		var x = this.left + this.width / 2;
		var y = this.top + this.height / 2;
		var x1 = this.hbox.x; var y1 = this.hbox.y;
		var x2 = this.hbox.x + this.hbox.w;
		var y2 = this.hbox.y + this.hbox.h;
		var cx = this.hbox.x + this.hbox.w / 2;
		var cy = this.hbox.y + this.hbox.h / 2;
		var dir = 1;
		if ((x1 <= x && x <= cx) && (y1 <= y && y <= cy))
			dir = 4;
		else if ((cx <= x && x <= x2) && (y1 <= y && y <= cy))
			dir = 3;
		else if ((x1 <= x && x <= cx) && (cy <= y && y <= y2))
			dir = 1;
		else if ((cx <= x && x <= x2) && (cy <= y && y <= y2))
			dir = 2;
		return dir;
	},
	_isEnableMenuPos: function(x1, y1, x2, y2) {
		var bIn = false;
		var mx1 = this.hbox.x; var my1 = this.hbox.y;
		var mx2 = mx1 + this.hbox.w;
		var my2 = my1 + this.hbox.h;
		if ((mx1 < x1 && x1 < mx2) && (my1 < y1 && y1 < my2) &&
			(mx1 < x2 && x2 < mx2) && (my1 < y2 && y2 < my2))
			bIn = true;
		return bIn;
	},
	_setMenuTipPos: function(bPass) {
		var x, y; 
		var w = this._talktip.width;
		var h = this._talktip.height;
		var dir = this._getBigDir();
		// var dir = this._getDir(w, h); 
		switch(dir) {
		case 1 : 
			x = this.left + 10;
			y = this.top - h - 30;
			break;
		case 2 :
			x = this.left - w + 40;
			y = this.top - h - 30;
			// y = this.top - h - 50;
			break;
		case 3 :
			x = this.left - w + 40;
			y = this.top + 60 + 20;
			break;
		case 4 : 
			x = this.left + 10;
			y = this.top + 60 + 20;
			break;
		}
		if (!bPass && !this._isEnableMenuPos(x-8,y-8, x+w+8, y+h+8)) {
			this.move(this.hbox.x + NPet.MenuPos[dir].x, this.hbox.y + NPet.MenuPos[dir].y);
			this._setMenuTipPos(true);
		}
		else {
			this._talktip.move(x, y);
			this._talktip.setTail(dir);
		}
	},
	_showTip:function(text) {
		this.bTipOpen = true;
		this.petSetState(NPetModel.STAT_NOTIFY);
		this._talktip.setVisible(true);
		this._talktip.show(0, 0, text); 
		this._setMenuTipPos();
	},
	_tipClose:function() {
		this._talktip.setVisible(false);
		this.bTipOpen = false;
		this.petSetState(NPetModel.STAT_FREE);
	},
	_showMenu:function() {
		this._talktip.setTipWidth(245);
		this._showTip(this._menuHTML);
		this._setMenuTipPos(); 
		nus.Tracker.register(this._petEvent, "PetMenu");
	},
	onMenu: function() {
		if (!this.bTipOpen) {
			if (this._tipTimer) 
				window.clearInterval(this._tipTimer);
			this._tipClose();
		}
		this._showMenu();
	},
	onClick: function() {
		this.petAutoTips();
		nus.Tracker.register(this._petEvent, "PetClick");
	},
	_initPos: function() {
		NPet.MenuPos[1].y  = this.hbox.h - 60;
		NPet.MenuPos[2].x  = this.hbox.w - 60;
		NPet.MenuPos[2].y  = this.hbox.h - 72;
		NPet.MenuPos[3].x  = this.hbox.w - 60;
		var y = this.hbox.y + this.hbox.h - 80; 
		var x = this.hbox.x + 20;
		this.move(x,y);
	},
	_initTip: function() {
		this._talktip = NPet.MsgTip.createInstance({
			ID:"dash_pettip",
			width:"auto",
			zIndex:2000001,
			isVisible:false,
			parent: this, 
			onClick: function(ev) {
				if ((ev.srcElement.nodeName == "DIV") || (ev.srcElement.nodeName == "IMG") || (ev.srcElement.nodeName == "SPAN")) {
					if (ev.srcElement.id == "")
						return;
					var g_event = ev.srcElement.id;
					switch(ev.srcElement.id) {
					case "game_petfeed":
						NPet.manager.onRunGame();
						break;
					case "browser_photo":
						document.location.href = nasc.photologURL+"/Photo/most_popular.php";
						break;
					case "browser_search":
						document.location.href = nasc.appURL+"/Browse/";
						break;
					case "invite_friends":
						document.location.href = nasc.appURL+"/Member/my_friend_invite.php?from=dashboard";
						break;
					case "photo_upload":
						document.location.href = nasc.appURL+"/Member/signup_upload_photo.php";
						break;
					case "new_page":
						// nasc.gotoPage(NPet.manager._page_id, {"editmode":"new"});
						PageManager.gotoHome();
						break;
					case "new_inbox":
						document.location.href = nasc.msgURL+"/Inbox/";
						break;
					case "request_friend" : 
						nasc.message.showMessageBox("request_friend", page_info.name, page_info.page_id);
						break;
					case "pet_menu":
						if (this.parent._tipTimer) 
							window.parent.clearInterval(this.parent._tipTimer);
						this.parent._showMenu();
						break;
					case "close":
						if (this.parent._tipTimer) 
							window.parent.clearInterval(this.parent._tipTimer);
						this.parent._tipClose();
						break;
					case "setting":
						this.parent._tipClose();
						NPet.manager.settingPet(nasc.User.getSSN());
						break;
					case "status" :
						NPet.Net.getPetInfo(nasc.User.getSSN(), "NPet.manager.onGetPet_Status");
						break;
					case "sleep":
						this.parent._sleep();
						break;
					case "wake_up":
						this.parent._wakeUp();
						break;
					case "feed_me" :
						if (!this.parent.checkLogin())
							return;
						if (this.parent._fMe <= 0) 
							this.parent.openTipMsg(NPet.FeedMe_OOPS, null, 240, {pet_icon:this.parent._petIcon, count: "0/30"}, 5);	
						else
							NPet.Net.getPetMyPocket(nasc.User.getSSN(), this.parent.ID+".petFeedMe");
						break;
					case "one_feed" :
						g_event = "/feedme/" + ev.srcElement.id;
						this.parent.petFeedThank(1);
						break;
					case "two_feed" :
						g_event = "/feedme/" + ev.srcElement.id;
						this.parent.petFeedThank(2);
						break;
					case "three_feed" :
						g_event = "/feedme/" + ev.srcElement.id;
						this.parent.petFeedThank(3);
						break;
					case "pet_me" :
						if (!this.parent.checkLogin())
							return;
						if (this.parent._pMe <= 0) 
							this.parent.openTipMsg(NPet.PetMe_OOPS, null, 240, {pet_icon:this.parent._petIcon, count: "0/10"}, 5);	
						else
							NPet.Net.getPetMyPocket(nasc.User.getSSN(), this.parent.ID+".petMe");
						break;
					case "pet_me_btn" :
						g_event = "/pet_me/" + ev.srcElement.id;
						this.parent.petMeThank();
						break;
					default:
						g_event = null;
						break;
					}
					if (g_event)
						nus.Tracker.register(this.parent._petEvent, g_event);
				}
			}
		});	
	},
	Sleep:function() {
		this.petSetState(NPetModel.STAT_SLEEP);
		this._menuHTML = NPet.DashMenuWakeup;
	},
	_sleep: function() {
		this._tipClose();
		this.Sleep(); 	
	},
	_wakeUp: function() {
		this._tipClose();
		this.petRestart();
		this._menuHTML = NPet.DashMenuSleep;
	},
	resetStatus: function(rsp) {
		var statusData = {};
		statusData.name = NPet.manager._petInfo.name;
		statusData.feed_cnt = rsp.owner_feedscore;
		statusData.petme_cnt = rsp.owner_pettingscore;
		statusData.pet_level = rsp.owner_pettotalscore;
		NPet.manager.displayPetInfo("_pet_dashinfo", statusData);
	},
	petStatus: function(statusData) {
		statusData.type = NPetModel.PetList[this.pet_id].type;
		this._talktip.setTipWidth(240);
		this._showTip(NPet.OwnerDashStatus.toHTML(statusData));
		NPet.manager.displayPetInfo("_pet_dashinfo", statusData);
	},
	checkLogin : function() {
		return nasc.User.checkLogin();
	},
	petFeedMe: function(rsp) {
		if (rsp.statusCode != 'ok')
			return;
		this._fMe = rsp.owner_biscuits;
		if (this._fMe <= 0) 
			this.openTipMsg(NPet.FeedMe_OOPS, null, 240, {pet_icon:this._petIcon, count: "0/30"}, 5);	
		else
			this.openTipMsg(NPet.DashFeedMe, NPet.Feed_GuideMsg, 240, {pet_icon:this._petIcon, count: this._fMe + "/30"}, 0);
	},
	petFeedThank: function(mode) {
		this._fMe -= mode;
		if (this._fMe < 0)
			this._fMe = 0;
		NPet.Net.setPetMyPocket(NPet.manager._page_id, 0, mode, "NPet.manager.onSetPet_MyPocket");
		this.openTipMsg(NPet.FeedMe_Thank, NPet.Feed_ThankMsg[mode], 240, {pet_icon:this._petIcon, remain_count: this._fMe}, 4);
	},
	petMe: function(rsp) {
		if (rsp.statusCode != 'ok')
			return;
		this._pMe = rsp.owner_petting;
		if (this._pMe <= 0) 
			this.openTipMsg(NPet.PetMe_OOPS, null, 240, {pet_icon:this._petIcon, count: "0/10"}, 5);	
		else
			this.openTipMsg(NPet.PetMe, NPet.PetMe_GuideMsg, 240, {pet_icon:this._petIcon, count: this._pMe + "/10"}, 0);
	},
	petMeThank: function() {
		this._pMe -= 1;
		if (this._pMe < 0)
			this._pMe = 0;
		NPet.Net.setPetMyPocket(NPet.manager._page_id, 1, 1, "NPet.manager.onSetPet_MyPocket");
		this.openTipMsg(NPet.PetMe_Thank, NPet.PetMe_ThankMsg, 240, {pet_icon:this._petIcon, remain_count: this._pMe}, 4);
	},
	getRandomMsg: function(arr_msg) {
		return arr_msg[Math.floor(Math.random()* arr_msg.length)]
	},
	openTipMsg: function(arr_html, arr_msg, w, data, tAuto) {
		this._talktip.setTipWidth(w);
		if (arr_msg) 
			data.random_msg = this.getRandomMsg(arr_msg); 
		this._showTip(arr_html.toHTML(data));
		if (tAuto)
			this.tipAutoTimer(tAuto);
	}
});


/////////////////////////////////////
//
NPet.ProfilePet = NPet.DashPet.extend();
NPet.ProfilePet.addPrototypes({
	setRandomStatList: function() {
		if (!nasc.User.isOwner()) {
			this._randomList.pop();
			if (nasc.User.isLogin() && page_info.is_my_friend != undefined && !page_info.is_my_friend) {
				this._randomList.push("request_friend");
			}
		}
	},
	onCreate: function() {
		if (!nasc.User.isOwner()) {
			NPet.DashMenuWakeup = [
			"<img src='/Home/app/pet/images/pet_i_wakeup.gif' align='absmiddle'  /><a href='javascript:void(0)' class='pet_ws_btn'><span id='wake_up'>Wake Up</span></a><div class='clear_bar'></div>",
			"<div style='padding-left:10px'>",
			"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_feedme'><div id='feed_me'></div></a>",
			"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_petme'><div id='pet_me'></div></a><div class='clear_bar'></div></div><br />"

			].join("");
			NPet.DashMenuSleep = [
			"<img src='/Home/app/pet/images/pet_i_sleep.gif' align='absmiddle' /><a href='javascript:void(0)' class='pet_ws_btn'><span id='sleep'>Sleep</span></a><div class='clear_bar'></div>",
			"<div style='padding-left:10px'>",
			"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_feedme'><div id='feed_me'></div></a>",
			"<a href='javascript:void(0)' class='pet_dm_btn pet_dm_petme'><div id='pet_me'></div></a><div class='clear_bar'></div></div><br />"
			].join("");
		}
		NPet.ProfilePet.superCall(this, "onCreate", arguments);
		this._petEvent = "Profile/Pet";
		this.setRandomStatList();
	},
	_showMenu:function() {
		if (!nasc.User.isOwner()) 
			this._talktip.setTipWidth(140);
		else
			this._talktip.setTipWidth(245);	
		this._showTip(this._menuHTML);
		this._setMenuTipPos(); 
		nus.Tracker.register(this._petEvent, "PetMenu");
	}
});


/////////////////////////////////////////////////////
//
NPet.PetMenu = nus.dom.DivClass.extend();
NPet.PetMenu.addPrototypes({
	width:55,
	height:18,
	onCreate:function() {
		NPet.PetMenu.superCall(this, "onCreate", arguments);
//		var html = ["<a class='pet_menu_btn'><div id='petbox_menu'></div></a>"];
		var html = ["<a href='javascript:void(0)' class='pet_menu_btn  petmenu_btn'><div id='petbox_menu'></div></a>"];
		this.$.innerHTML = html.toHTML();
		this.setZIndex(1900000);
		var x = this.hbox.w - this.width;
		var y = this.hbox.h - this.height;
		this.move(x, y);
	},
	onClick: function() {	
		//NPet.manager._pet.onMenu();
		window.setTimeout("NPet.manager._pet.onMenu();", 100);
	}
});


//////////////////////////////////////////////////////////////////////
//
//  Pet Game : Pet Feeding Game 
//
function onInviteGameFriend(name, msrl) {
	nasc.message.showMessageBox("request_petgame", name, msrl);
}

NPet.manager.onRunGame = function(FirstPet) {
	var sendbox = nus.dom.IframeDialog.createInstance({
				width:600, // height:480,
				hasVisualEffect:false,
				contentURL: $URL("game/gamefeed/game_box.html"),
				_firstPet: FirstPet,
				onInitDialog: function() {
					var env = {};
					env.first = this._firstPet; 
					env.msrl = nasc.User.getSSN();
					env.API_URL = nasc.API_URL;
					env.appURL = nasc.appURL;
					env.InviteBox = onInviteGameFriend;
					this.getContentWindow().init(env);
				},
				onDestroy: function() {
					nus.dom.IframeDialog.superCall(this, "onDestroy", arguments);
				},
				onCommand: function(cmd, result) {
					switch(cmd) {
					case "save":
					case "cancel":
						this.destroy();
						break;
					}
				}
	});
}

NPet.manager.onPetLog = function() {
	var sendbox = nus.dom.IframeDialog.createInstance({
				width: 575, 
				hasVisualEffect:false,
				contentURL: $URL("app/pet/petlog_box.html"),
				parent: this, 
				onInitDialog: function() {
					var env = {};
					env.msrl = nasc.User.getSSN();
					env.API_URL = nasc.API_URL;
					env.appURL = nasc.appURL;
					env.petname = this.parent._petInfo.name;
					this.getContentWindow().init(env);
				},
				onDestroy: function() {
					nus.dom.IframeDialog.superCall(this, "onDestroy", arguments);
				},
				onCommand: function(cmd, result) {
					if (cmd == "close") {
						this.destroy();
					}
				}
	});
}


NPet.PetGameMenu = nus.dom.DivClass.extend();
NPet.PetGameMenu.addPrototypes({
	width:57,
	height:18,
	_event: "Dashboard/Pet",
	_bFirstPet: null,
	onCreate:function() {
		NPet.PetMenu.superCall(this, "onCreate", arguments);
		var html = ["<a href='javascript:void(0)' class='pet_game_btn  petmenu_btn'><div id='petgame_menu'></div></a>"];
		this.$.innerHTML = html.toHTML();
		this.setZIndex(1900000);
		var x = this.hbox.w - this.width * 2 + 2;
		var y = this.hbox.h - this.height;
		this.move(x, y);
	},
	onClick: function() {	
		nus.Tracker.register(this._event, "game");
		if (!nasc.User.checkLogin()) 
			return;
		NPet.manager.onRunGame(this._bFirstPet);
	}
});


NPet.PetLogMenu = nus.dom.DivClass.extend();
NPet.PetLogMenu.addPrototypes({
	width:56,
	height:18,
	_event: "Dashboard/Pet",
	onCreate:function() {
		NPet.PetMenu.superCall(this, "onCreate", arguments);
		var html = ["<a href='javascript:void(0)' class='pet_log_btn petmenu_btn'><div id='petlog_menu'></div></a>"];
		//var html = ["<a href='javascript:void(0)' class='pet_game_btn'><div id='petgame_menu'></div></a>"];
		this.$.innerHTML = html.toHTML();
		this.setZIndex(1900000);
		var x = this.hbox.w - (this.width + 1) * 3 + 2;
		var y = this.hbox.h - this.height;
		this.move(x, y);
	},
	onClick: function() {	
		nus.Tracker.register(this._event, "log");
		if (!nasc.User.checkLogin()) 
			return;
		 NPet.manager.onPetLog();
	}
});




NPet.PetShopMenu = nus.dom.DivClass.extend();
NPet.PetShopMenu.addPrototypes({
	width:57,
	height:18,
	_event: "Dashboard/Pet",
	onCreate:function() {
		NPet.PetMenu.superCall(this, "onCreate", arguments);
		var html = ["<a href='javascript:void(0)' class='pet_shop_btn petmenu_btn'><div id='petshop_menu'></div></a>"];
		//var html = ["<a href='javascript:void(0)' class='pet_game_btn'><div id='petgame_menu'></div></a>"];
		this.$.innerHTML = html.toHTML();
		this.setZIndex(1900000);
		var x = this.hbox.w - this.width * 4 + 2;
		var y = this.hbox.h - this.height;
		this.move(x, y);
	},
	onClick: function() {	
		nus.Tracker.register(this._event, "shop");
		if (!nasc.User.checkLogin()) 
			return;
		 NPet.manager.onPetShop();
	}
});

