/////////////////////////////////////////////////////////
document.onmousemove = function(evt){
	FC_onMouseMove(evt);
}
document.onmousedown = function(evt){
	FC_onMouseDown(evt);
}
document.onmouseup = function(evt){
	FC_onMouseUp(evt);
}
/////////////////////////////////////////////////////////
var FC_InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var content_url = webpath + "content.html";
var currentInviteUID = "";
var startInviteUID = "";
var init_nickname = "";

var ONLINE_STATUS = 1;
var OFFLINE_STATUS = 0;
var FC_windowObject;
var FC_openedWindow = false;
var FC_userStatus = OFFLINE_STATUS;
var popWinCount = 100;
var currentX;
var currentY;
var onPressX;
var onPressY;
var dragName = "";
//
//	DC js start here
function FC_myFlash_DoFSCommand(command, args){
	var FC_myFlashObj = FC_InternetExplorer ? FC_myFlash : document.FC_myFlash;
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
 	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub FC_myFlash_FSCommand(ByVal command, ByVal args)\n');
	document.write(' call FC_myFlash_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}

init();
function init(){
	createDCContainer();
}

function FC_invite_1to1_chat(userid){
	//window.document.FC_myFlash.SetVariable("inviteUID", userid);
	currentInviteUID = userid;
	startInviteUID = userid;
	popupInviteWindow();
	//popupInviteWindow(newurl, "_123flashchat_" + init_user);
	//onCloseInvite();
}
function popupInviteWindow(){
	var winID = String(Math.round(Math.random() * 1E14));
	var vars = "?u=" + init_user + "&k=" + init_password + "&n=" + startInviteUID;
	FC_windowObject = window.open(content_url + vars, winID, "resizable=1,width=600,height=383,status=0");
	if (!FC_windowObject){
		window.location.href = content_url + vars;
		FC_openedWindow = true
	}else{
		FC_windowObject.focus();
		FC_openedWindow = true;
	}	
}

function set_userinfo(my_uid, my_pwd, u_status, my_nick){
	init_user = my_uid;
	init_password = my_pwd;	
	FC_userStatus = u_status;
	init_nickname = my_nick;
}

function response_invite(result){
	
	var INVITE_SUCCESS = 0;
	var INVITE_NOT_ONLINE = 1;
	
	if (result == INVITE_NOT_ONLINE){
		alert(currentInviteUID + " is not online now!");	
	}
}

function set_userstatus(u_status){
	FC_userStatus = u_status;	
	if (u_status == "0"){
		setWindowClosed();
	}
}

function FC_onGetNewInvite(inviteName){
	window.focus();
	this.focus();
	//
	popupNewInvite(inviteName);
}

function FC_onGetCloseInvite(userName){
	removeInviteWin(userName);
}

function createDCContainer(){
	var divName = "FC_DC_container";
	var swfWidth = 1;
	var swfHeight = 1;
	//
	var FC_swf_url = webpath + dameonFilename;
	FC_swf_url += "?init_user=" + init_user;
	FC_swf_url += "&init_surl=" + init_surl;
	FC_swf_url += "&init_host=" + init_host;
	FC_swf_url += "&init_port=" + init_port;
	FC_swf_url += "&init_password=" + init_password;
	FC_swf_url += "&checkInterval=" + checkInterval;
	FC_swf_url += "&init_language=" + init_language;
	
	var FC_swfhtmlcode = "";
	FC_swfhtmlcode += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,0,0\" width=\"" + swfWidth + "\" height=\"" + swfHeight + "\" id=\"FC_listenerSwf\">";
	FC_swfhtmlcode += " <param name=\"movie\" value=\"" + FC_swf_url + "\" \/> ";
	FC_swfhtmlcode += " <param name=\"wmode\" value=\"transparent\" \/>";
	FC_swfhtmlcode += " <param name=\"quality\" value=\"high\" \/>";
	FC_swfhtmlcode += " <embed src=\"" + FC_swf_url + "\" quality=\"high\"  width=\"" + swfWidth + "\" height=\"" + swfHeight + "\" name=\"FC_listenerSwf\" id=\"FC_listenerSwf\" swLiveConnect=\"true\" type=\"application\/x-shockwave-flash\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\"><\/embed>";
	FC_swfhtmlcode += " <\/object>";	
	FC_swfhtmlcode = getDivCode(divName, popWinCount++, FC_swfhtmlcode);
	FC_swfhtmlcode += getDivCode("FC_windows", popWinCount++, "");

	document.write(FC_swfhtmlcode);
	var setSwfPositionCyc = setInterval(function(){
		changeDivPosition("FC_windows", 0, 0);
	}, 50);
	
}

function getDivCode(divName, divZ, html, pos){
	var str = "";	
	str += "<div id=\"" + divName + "\" style=\"";
	str += "left:0px; ";
	str += "top:0px; ";
	if(pos == null){
		str += "position:absolute; ";
	}else{
		str += "position:" + pos + "; "
	}
	str += "z-index:" + divZ + "; ";
	//str += "border:solid thin red; ";
	str += "\">";
	str += html;
	str += "<\/div>";
	return str;
}

function popupNewInvite(inviteName){
	var popWinWidth = 202;
	var popWinTitleHeight = 15;
	var popWinLogoHeight = 35;
	var popWinBodyHeight = 66;
	var totalHeight = popWinTitleHeight + popWinLogoHeight + popWinBodyHeight;
	//
	if(getElement("inviteWin_" + inviteName) == null){
		if(getElement("container_" + inviteName) == null){
			var conStr = getDivCode("container_" + inviteName, popWinCount++);
			getElement("FC_windows").innerHTML += conStr;
		}
		var str = "<table width=\"" + popWinWidth + "\" height=\"" + totalHeight + "\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
		str += "<tr height=\"" + popWinTitleHeight + "\">";
		str += "<td id=\"winTitle_" + inviteName + "\" width=\"100%\" background=\"" + webpath + "\/images\/inviteBoxTitle.png\">";
		str += "</td></tr>"
		str += "<tr height=\"" + popWinLogoHeight + "\"><td><img src=\"" + webpath + "\/images\/inviteLogo.png\"></td></tr>"
		str += "<tr height=\"" + popWinBodyHeight + "\">";
		str += "<td background=\"" + webpath + "\/images\/inviteBox.png\" style=\"background-repeat:no-repeat\" align=\"center\">";
		str += "<span style=\"font-size:12px;\">" + getReplaceString(inviteLangage, "%USER%", inviteName) + "</span>";
		str += "<br />"
		str += "<a href=\"#\" onclick=\"onClickAccept('" + inviteName + "');\" style=\"font-size:12px\"><img style=\"border:none\" src=\"" + webpath + "\/images\/acceptBtn.png\"></a>";
		str += "&nbsp;&nbsp;&nbsp;"
		str += "<a href=\"#\" onclick=\"onClickDeny('" + inviteName + "');\" style=\"font-size:12px\"><img style=\"border:none\" src=\"" + webpath + "\/images\/denyBtn.png\"></a>";
		str += "</td></tr>";
		str += "</table>";
		str = getDivCode("inviteWin_" + inviteName, popWinCount++, str);
		getElement("container_" + inviteName).innerHTML = str;
		changeStaticPosition("inviteWin_" + inviteName, 0, -totalHeight);
		showInviteWindowOut(inviteName);
	}
}

function showInviteWindowOut(userName){
	var winY = getStaticY("inviteWin_" + userName);
	if(winY < 0){
		changeStaticPosition("inviteWin_" + userName, 0, winY + 10);
		setTimeout("showInviteWindowOut('" + userName + "');", 50);
	}else{
		changeStaticPosition("inviteWin_" + userName, 0, 0);
	}
}

function FC_onMouseMove(evt){
	if(FC_IE4){
		currentX = window.event.clientX;
		currentY = window.event.clientY;
	}else{
		currentX = evt.clientX;
		currentY = evt.clientY;
	}
	if(dragName != ""){
		var x1 = Number(currentX) - Number(onPressX);
		var y1 = Number(currentY) - Number(onPressY);
		if(FC_IE4){
			changeDivPosition("inviteWin_" + dragName, x1, y1);	
		}else{
			changeStaticPosition("inviteWin_" + dragName, x1, y1);
		}
	}
}

function FC_onMouseDown(evt){
	var id;
	if(FC_IE4){
		id = window.event.srcElement.id;
	}else{
		id = evt.target.id;
	}
	if(id.indexOf("winTitle") != -1){
		dragName = id.substring(id.indexOf("_") + 1);
		onPressX = currentX - getElementX("inviteWin_" + dragName);
		onPressY = currentY - getElementY("inviteWin_" + dragName);
	}
}

function FC_onMouseUp(evt){
	dragName = "";
}

function onClickAccept(userName){
	removeInviteWin(userName);
	window.document.FC_listenerSwf.SetVariable("acceptName", userName);
	FC_invite_1to1_chat(userName);
}

function onClickDeny(userName){
	removeInviteWin(userName);
	window.document.FC_listenerSwf.SetVariable("denyName", userName);
}

function removeInviteWin(userName){
	getElement("container_" + userName).innerHTML = "";
}

function getElement(winName){
	if (FC_NS4){
		return document.layers[winName];
	}else if(FC_NS6){
		return document.getElementById(winName);
	}else if(FC_IE4){
		return document.all[winName];
	}
}
function getReplaceString(oldStr, tarStr, newStr){
	if(oldStr.indexOf(tarStr) != -1){
		var s1 = oldStr.substring(0, oldStr.indexOf(tarStr));
		var s2 = oldStr.substring(oldStr.indexOf(tarStr) + tarStr.length);
		s1 = getReplaceString(s1, tarStr, newStr);
		s2 = getReplaceString(s2, tarStr, newStr);
		return s1 + newStr + s2;
	}else{
		return oldStr;
	}
}
