﻿var glb_help = {call_tid: null, menu_tid: null};
var glb_room = {id: "", type: "off", master_id: "", rnd_num: "", online: false, action_end: true, check_tid: null, check_cnt: 0, check_url: "/global/chatcheckonline.aspx", check_data: {MemberID: 100000}};
var glb_flat_ad_chat = {id: "id_flat_ad_chat", x: 40, y: 40, xin: true, yin: true, tid: null, home: false, on: false};

(function(){
	$(window).bind("load", function()
	{
		var p=glb_flat_ad_chat;
		var o=$("#id_Live_Help img");
		o.attr({"alt": glb_label.call.title, "title": glb_label.call.title});
		
		if ($.browser.mozilla) o.attr("onclick", "HelpClick(this, event)");
		else o.bind("click", function(){HelpClick(this)});
		
		$("<span>", {id: "idCallPhone"}).appendTo("body");
		
		if (p.home)
		{
			$("<div>", {
				id: p.id,
				mouseover: function(){glb_flat_ad_chat.on = true},
				mouseout: function(){glb_flat_ad_chat.on = false},
				click: function(){OpenChatRoom(glb_room.type)},
				css: {width: 140, height: 63, border: "1px solid gray", display: "none", "background-color": "#eee"},
				html: "<img src='/files/images/common/server.gif' width='140' height='63' border='0' alt='" + glb_label.menu["chatonline"] + "' />"
			}).appendTo("body")
		}
		
		CheckChatOnline()
	});
	
	CallPhone=function(s) 
	{
		var o=$("#idCallPhone");
		if (o.size() > 0)
		{
			o[0].innerHTML="<object ID='idNetMeeting' classid='CLSID:3E9BAF2D-7A79-11d2-9334-0000F875AE17' style='display:none' codebase='http://download.microsoft.com/msdownload/netmeeting/3.0/x86/en/NM30.EXE'><param name='mode' value='DataOnly'></object>";
			o=document.getElementById("idNetMeeting");
			if (o)
			{
				try
				{
					if (o.IsInConference() == 0) 
					{
						if (glb_help.call_tid) {alert(glb_label.call.alert)}
						else 
						{
							var sCallTo = "";

							if (s == "hk") sCallTo = "85223431933+gateway=210.6.229.234+type=phone+conference=ad-web-hk";
							if (s == "sz") sCallTo = "8675527983034+gateway=mic.slyip.net+type=phone+conference=ad-web-sz";
							if (s == "sh") sCallTo = "8602162361456+gateway=sh-voip.slyip.net+type=phone+conference=ad-web-sh";

							if (sCallTo != "") o.CallTo(sCallTo);

							glb_help.call_tid = setTimeout(function(){if (glb_help.call_tid) clearTimeout(glb_help.call_tid); glb_help.call_tid = null}, 60000)
						}
					}
				} catch (e) {alert("Not Supported !")}
			}
		}
	};
	
	OpenChatRoom=function(s)
	{
		var r=glb_room;
		if (s == "plugin") {ShowWindow("/chatroom/plugin/room.client.asp?RoomID=" + r.id + "&MasterID=" + r.master_id + "&RndNum=" + r.rnd_num, 600, 450, "")}
		else if (s == "asp") {ShowWindow("/chatroom/asp/room.client.asp?RoomID=" + r.id + "&MasterID=" + r.master_id + "&RndNum=" + r.rnd_num, 600, 420, "", "no")}
	};
	
	HelpClick=function(o, e) 
	{
		var rm, oMenu=$("#idMenu_Help");
		if (oMenu.size() == 0)
		{ 
			var sMenu = "<table cellspacing='1' cellpadding='1' style='cursor: default'>\n" +
				"<tr>" +
				"   <td style='width:20px' class='logo'>&nbsp;</td>\n" +
				"   <td><table width='110px' cellspacing='0' cellpadding='2'>" +
				"   <tr class='m' m='hk' onclick='ClickHelpMenu(this)'><td class='img phone'>&nbsp;</td><td>" + glb_label.menu["hk"] + "</td></tr>\n" +
				"   <tr class='m' m='sz' onclick='ClickHelpMenu(this)'><td class='img phone'>&nbsp;</td><td>" + glb_label.menu["sz"] + "</td></tr>\n" +
				"   <tr class='m' m='sh' onclick='ClickHelpMenu(this)'><td class='img phone'>&nbsp;</td><td>" + glb_label.menu["sh"] + "</td></tr>\n" +
				"   <tr class='m' m='rm' onclick='ClickHelpMenu(this)'><td class='img offline'>&nbsp;</td><td>" + glb_label.room["business"] + "</td></tr>\n" +
				"   <tr><td height='6' colspan='2' disabled><img src='/files/images/common/blank.gif' class='sep' /></td></tr>\n" +
				"   <tr class='m' m='hp' onclick='ClickHelpMenu(this)'><td class='img help'>&nbsp;</td><td>" + glb_label.menu["question"] + "</td></tr>\n" +
				"   </table></td>"
				"</tr></table>";
	               
			oMenu = $("<div>", {
				id: "idMenu_Help",
				mouseover: function(){clearTimeout(glb_help.menu_tid)},
				mouseout: function(){glb_help.menu_tid = setTimeout("HideHelpMenu()", 200)},
				selectstart: function(){return false},
				contextmenu: function(){return false},
				html: sMenu}).appendTo("body");
		}
		
		oMenu.find("tr").bind({selectstart: function(){return false}, contextmenu: function(){return false}});
	    oMenu.find("tr .m").bind({
			mouseover: function(){$(this).css({"color": "white", "background-color": "blue"})},
			mouseout: function(){var o=$(this); o.css({"color": (o.attr("m")=="rm" && !glb_room.online ? "gray":"black"), "background-color": ""})}
	    });
	    
	    rm=oMenu.find("tr .m[m='rm']").attr("t", glb_room.type);
	    rm.css("color", glb_room.online ? "black" : "gray");
	    rm.children().first().attr("class", "img " + (glb_room.online ? "on":"off") + "line");
	    
		e = e || window.event;
		
		oMenu.css({
			top: ($.browser.mozilla ? e.pageY : $(o).offset().top + e.offsetY), 
			left: ($.browser.mozilla ? e.pageX : $(o).offset().left + e.offsetX)
		});
		oMenu.fadeIn();
		
		glb_help.menu_tid = setTimeout("HideHelpMenu()", 4000)
	};
	
	HideHelpMenu=function(){$("#idMenu_Help").hide()};
	ClickHelpMenu=function(o)
	{
		var m=$(o).attr("m"); 
		if (m=="hk" || m=="sz" || m=="sh") {CallPhone(m)} 
		else if (m=="rm") {var t=$(o).attr("t"); if (t=="plugin" || t=="asp") OpenChatRoom(t)}
		else if (m=="hp") {}
		
		HideHelpMenu()
	};

	FloatAdChat=function() 
	{
		var r=glb_room, p=glb_flat_ad_chat;
		if (p.home)
		{
			var o=$("#" + p.id);
			if (r.online && r.type != "my")
			{
				o.show();
				
				if (!p.on) 
				{
					var step = 2, l=0, t=0, d=$(window);
					var w = d.width() - o.width() - 2, h = d.height() - o.height() - 2;
			        
					p.x = p.x + step * (p.xin ? 1:-1);
					if (p.x < l) {p.xin = true; p.x = l}
					if (p.x > w) {p.xin = false; p.x = w}

					p.y = p.y + step * (p.yin ? 1:-1);
					if (p.y < t) {p.yin = true; p.y = t}
					if (p.y > h) {p.yin = false; p.y = h}
					
					o.offset({left: p.x + d.scrollLeft(), top: p.y + d.scrollTop()})
				}
			}
			else
			{
				o.hide();
				if (p.tid) {clearInterval(p.tid); p.tid=null}
			}
		}
	};

	CheckChatOnline=function() 
	{
		var r=glb_room, p=glb_flat_ad_chat;
		
		if (r.action_end) 
		{
			r.check_cnt = 0;
			r.action_end = false
		}
		else
		{
			r.check_cnt++;
		}

		if (r.check_cnt == 3) 
		{
			// if the chat online not be got in 5 x 3 seconds, then recheck again!
			r.check_cnt = 0;
			r.online = false
		}
		
		if (r.check_cnt == 0)
		{
			$.ajax({
				type: "post",
				url: r.check_url,
				data: r.check_data,
				success: function(data) {(new Function(data))()},
				error: function(){$("#idCheckChatOnline").attr("src", glb_room.check_url + "&?MemberID=" + glb_room.check_data.MemberID)}
			})
		}
		
		if (!r.check_tid) r.check_tid = setInterval(CheckChatOnline, 5000);
		if (p.home && r.online && !p.tid) p.tid = setInterval(FloatAdChat, 100)
	};
	
})();

