// ****************************************************************************************
// ***** global variables *****

var g_tjmfile = "";					// Tjm file
var g_tpsfile = "";					// Tps file
var g_autoplay = 0;					// autoplay
var g_controlstyle = 0;				// control style
var g_textstyle = 0;				// text style
var g_borderstyle = 0;				// border style
var g_width = 100;					// player width
var g_height = 100;					// player height
var g_menustyle = 1;				// menu sytle
var g_lifemode = 1;					// life mode
var g_headlink = "";				// click head to link this page
var g_headimage = "";				// show this image when player is hidden
var g_btnOnImg = "";				// button turn on image
var g_btnOffImg = "";				// button turn off image

// ---------------------------------------------------------------------------------------
// ***** alert installing window style *****
// ***** (html, windowname, width, height, toolbar, directories, status, scrollbars, resize, menubar) *****
var g_InstallWinStyle = new Array( "InstallingPage.htm", "WindowName", "400", "300", "no", "no", "no", "yes", "no", "no" );

// ***** Talking Head linking window style *****
// ***** (html, windowname, width, height, toolbar, directories, status, scrollbars, resize, menubar) *****
var g_LinkWinStyle = new Array( "", "WindowName", "", "", "yes", "no", "yes", "yes", "yes", "yes" );

// ----------------------------------------------------------------------------------------
var g_CTClassID = "crazytalk.crazytalkplayer";			// for IE
var g_CTObjectID = "CrazyTalkPlayer_TalkingHead";		// object id
var g_CTNsClassID = "plugin/x-asgard-jspatch40";		// for netscape

var g_IsInstalled=false;
var g_ctObj=null;			// Object ID
var morphfun;
var autoflag = 0;								
var level = 0;								
var inc = 2;
var down = false;
var time = 0;
var expArray = new Array(19,4,6,10,19);			// expressions

// ----------------------------------------------------------------------------------------

// ***** determine platform *****
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var ns  = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
				&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
				&& (agt.indexOf('webtv') == -1) && (agt.indexOf('hotjava') == -1));
	
var is_win95 = ((agt.indexOf("win95") != -1) || (agt.indexOf("windows 95") != -1));
var is_win98 = ((agt.indexOf("win98") != -1) || (agt.indexOf("windows 98") != -1));
var is_winnt = ((agt.indexOf("winnt") != -1) || (agt.indexOf("windows nt") != -1));
    
var mac = (agt.indexOf("mac")!=-1);
var win = (is_win95 || is_winnt || is_win98 || 
          ((is_major >= 4) && (navigator.platform == "Win32")) ||
          (agt.indexOf("win32") != -1) || (agt.indexOf("32bit") != -1));	

// ****************************************************************************************
// Click Event handler              
if(ie && win)              
	document.write ("<script FOR=CrazyTalkPlayer_TalkingHead EVENT='Click'>CTLinkPage();</SCRIPT>");
else if(ns || !win)
	document.write ("<script language=javascript>function CrazyTalkPlayer_TalkingHead_Click() { CTLinkPage(); }</script>");


var MouseOverChk=false;

// MouseMove Event Handler
  document.write ("<script for=CrazyTalkPlayer_TalkingHead event='MouseMove(iButton,iShiftState,fX,fY)'>");
  document.write ("if (!MouseOverChk) {");
  document.write ("loadTPS('/crazytalk/data/vr-0.tps');","");
  document.write ("MouseOverChk=true;");
  document.write ("g_ctObj.Play(0);} </script>");
  
  document.write ("<script for=CrazyTalkPlayer_TalkingHead event='TpsChange(sFileName)'>");
  document.write ("if (off==1) g_ctObj.Stop();</script>");
  
// MouseOut Event Handler 
  document.write ("<script for=CrazyTalkPlayer_TalkingHead event='MouseLeave(iButton,iShiftState,fX,fY)'>");
  document.write ("MouseOverChk=false;</script>");
  
// ****************************************************************************************

function DetectCrazyTalkIE(ClassID)
{
	result=false;
	try {
		var Obj=new ActiveXObject(ClassID);
		if (Obj)  result=true;
		else result=false;
	}
	catch (e) { 
		result=false; 
	};
	
	if(Obj)
		delete Obj;
	Obj=null;
	return result;
}

// ****************************************************************************************
function detectNS(ClassID) 
{ 
	result = false; 
	if (nse.indexOf(ClassID) != -1) {
		if (navigator.mimeTypes[ClassID].enabledPlugin != null) 
			result = true; 
	}
	return result; 
}

// ****************************************************************************************
function SetInstallWindow()
{
	var args = SetInstallWindow.arguments;
	
	// required
	g_InstallWinStyle[0] = args[0];
	
	// optional
	if(args.length >= 2) g_InstallWinStyle[1] = args[1];
	if(args.length >= 3) g_InstallWinStyle[2] = args[2];	
	if(args.length >= 4) g_InstallWinStyle[3] = args[3];
	if(args.length >= 5) g_InstallWinStyle[4] = args[4];
	if(args.length >= 6) g_InstallWinStyle[5] = args[5];
	if(args.length >= 7) g_InstallWinStyle[6] = args[6];
	if(args.length >= 8) g_InstallWinStyle[7] = args[7];
	if(args.length >= 9) g_InstallWinStyle[8] = args[8];
	if(args.length >= 10) g_InstallWinStyle[9] = args[9];
}

// ****************************************************************************************
function SetStatusString(str)
{
	window.status=str; 
	return true;
}

// ****************************************************************************************
function LoadHeadImage(sHeadImage)
{
	document.write ("<DIV id='cthead'><IMG src="+ sHeadImage +"></DIV>");
	if(CheckInstalledCT())
	{
		g_IsInstalled = true;
		retString = "<div id='ctheadobj' style='display:none'><object id='"+ g_CTObjectID +"'" + "classid='CLSID:1CC506A7-1B8D-11D4-BDD5-0060977007E0'" + 
					"codebase='http://plug-in.reallusion.com/CrazyTalk.cab#version=3,0,0,0'" + 
					"width='"+ g_width +"'" + "height='"+ g_height +"'>" +
					"<param name='autoplay' value='"+ g_autoplay +"'>" +
					"<param name='controlstyle' value='"+ g_controlstyle +"'>" +
					"<param name='textstyle' value='"+ g_textstyle +"'>" +
					"<param name='borderstyle' value='"+ g_borderstyle +"'>" +
					"<param name='menustyle' value='"+ g_menustyle +"'>" +
					"<param name='lifemode' value='"+ g_lifemode +"'>" +
					"<param name='tjmname' value='"+ g_tjmfile +"'>" +
					"<param name='tpsname' value='"+ g_tpsfile +"'>" +
					"<param name='License' value='2390017eb7c0d6a35b5265c0'>" +
					"</object></div>";
		document.write (retString);
	}
	else
	{
		g_IsInstalled = false;
		document.write ("<div id='ctheadobj' style='display:none'></div>");
	}
}

// ****************************************************************************************
function SetButtonImage(sOnImage, sOffImage, str)
{
	if(sOnImage.length > 0) g_btnOnImg = sOnImage;
	if(sOffImage.length > 0) g_btnOffImg = sOffImage;
	document.write ("<a style='cursor:hand' onclick='javascript:TurnOnOff(TurnFlag);' onMouseOver=\"return SetStatusString('"+str+"');\" onMouseOut=\"return SetStatusString('');\"><IMG src="+ sOffImage +" border=0 name=btnimg></a>");
}
	
// ***************************************************************************************
// return true, has CT Player installed
// return false, no CT Player installed
function CheckInstalledCT()
{
	if(ie && win)
	{
		return DetectCrazyTalkIE(g_CTClassID);
	}
	else if(ns || !win)
	{
		return detectNS(g_CTNsClassID);
	}
	return false;
}

// ****************************************************************************************
function GetCookie (name) {  
	var cname = name + "=";
	var dc = document.cookie;

	if(dc.length > 0) {
		begin = dc.indexOf(cname);

		if(begin != -1) {
			begin += cname.length;
		end = dc.indexOf(";",begin);

		if(end == -1) end = dc.length;
			return unescape(dc.substring(begin,end));
		}
	}
	return null;
}

// ****************************************************************************************
function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
					((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
					((path == null) ? "" : ("; path=" + path)) +  
					((domain == null) ? "" : ("; domain=" + domain)) +    
					((secure == true) ? "; secure" : "");
}

// ****************************************************************************************
function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// ****************************************************************************************
function GetCookieState()
{
	var state = GetCookie("TalkingHeadState");
	if(state==null)	// no cookie
		return 0;
	else
		return state;
}

// ****************************************************************************************
function SetCookieState(state)
{
	var expdate = new Date();
	expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 31));
	SetCookie("TalkingHeadState", state, expdate);
}

// ****************************************************************************************
function ShowButton(state)
{
	if(state == 1)
		document.btnimg.src = g_btnOnImg;
	else if(state == 0)
		document.btnimg.src = g_btnOffImg;
}


// ****************************************************************************************
function SetTalkingHead(HeadImage,TjmFile, TpsFile, PlayerWidth, PlayerHeight, AutoPlay, ControlStyle, TextStyle, BorderStyle, MenuStyle, LifeMode, LinkUrl)
{
	if(HeadImage.length > 0){
		g_headimage = HeadImage;
		LoadHeadImage(HeadImage);
	}
	
	if(TjmFile.length > 0) g_tjmfile=TjmFile;
			
	if(TpsFile.length > 0) g_tpsfile=TpsFile;

	if(PlayerWidth > 0) g_width=PlayerWidth;
		
	if(PlayerHeight > 0) g_height=PlayerHeight;
		
	if(AutoPlay >= 0) g_autoplay=AutoPlay;
		
	if(ControlStyle >= 0) g_controlstyle=ControlStyle;
		
	if(TextStyle >= 0) g_textstyle=TextStyle;
		
	if(BorderStyle >= 0) g_borderstyle=BorderStyle;
		
	if(MenuStyle >= 0) g_menustyle=MenuStyle;
		
	if(LifeMode >= 0) g_lifemode=LifeMode;
	
	g_headlink = LinkUrl;
}

// ****************************************************************************************
function ShowTalkingHead()
{
	// check cookie
	if(GetCookieState()==0)
	{
		// Turn off, pass 1 into function
		TurnOnOff(1);
	}
	else{
		if(CheckInstalledCT()){
			// Turn on pass 0 into function
			TurnOnOff(0);
		}
	}
}

// ****************************************************************************************
function ShowHead(mode)
{
	if(mode==1){	// show player
		document.getElementById("cthead").style.display = "none";
		if(g_IsInstalled)
		{
			document.getElementById(g_CTObjectID).width = g_width;
			document.getElementById(g_CTObjectID).height = g_height;
			document.getElementById(g_CTObjectID).autoplay = g_autoplay;
			document.getElementById(g_CTObjectID).controlstyle = g_controlstyle;
			document.getElementById(g_CTObjectID).textstyle = g_textstyle;
			document.getElementById(g_CTObjectID).borderstyle = g_borderstyle;
			document.getElementById(g_CTObjectID).menustyle = g_menustyle;
			document.getElementById(g_CTObjectID).lifemode = g_lifemode;
			document.getElementById(g_CTObjectID).tjmname = g_tjmfile;
			document.getElementById(g_CTObjectID).tpsname = g_tpsfile;
		}
		else
		{
			retString = "<object id='"+ g_CTObjectID +"'" + "classid='CLSID:1CC506A7-1B8D-11D4-BDD5-0060977007E0'" + 
					"codebase='http://plug-in.reallusion.com/CrazyTalk.cab#version=4,5,615,1'" + 
					"width='"+ g_width +"'" + "height='"+ g_height +"'>" +
					"<param name='autoplay' value='"+ g_autoplay +"'>" +
					"<param name='controlstyle' value='"+ g_controlstyle +"'>" +
					"<param name='textstyle' value='"+ g_textstyle +"'>" +
					"<param name='borderstyle' value='"+ g_borderstyle +"'>" +
					"<param name='menustyle' value='"+ g_menustyle +"'>" +
					"<param name='lifemode' value='"+ g_lifemode +"'>" +
					"<param name='tjmname' value='"+ g_tjmfile +"'>" +
					"<param name='tpsname' value='"+ g_tpsfile +"'>" +
					"<param name='License' value='2390017eb7c0d6a35b5265c0'>" +
					"</object>";
			document.getElementById("ctheadobj").innerHTML = retString;
		}
		document.getElementById("ctheadobj").style.display = "";
		
		try{
			g_ctObj = eval("document." + g_CTObjectID);
		}catch(e) { g_ctObj = null;}		
	}
	else if(mode == 0){	// show image
		document.getElementById("ctheadobj").style.display = "none";
		document.getElementById("cthead").innerHTML = "<IMG src='"+ g_headimage +"'>";
		document.getElementById("cthead").style.display = "";
	}
}

// ****************************************************************************************
var TurnFlag = 0;		// Turn on / off
var off=0;

// ****************************************************************************************
// turn = 1 : turn off
// turn = 0 : turn on
function TurnOnOff(turn)
{
	stopPlayer(0);
	if(turn==1)	// off
	{
		
		TurnFlag = 0;
		ShowHead(0);		// head image
		ShowButton(0);		// off button
		SetCookieState(0);	// set off state to Cookie
		off=1;
	}
	else if(turn==0) // on
	{
		// Check Installed CT
		if(CheckInstalledCT()==true)
		{
			g_IsInstalled = true;
			TurnFlag = 1;
			ShowHead(1);		// head player
			ShowButton(1);		// on button
			SetCookieState(1);	// set on state to Cookie
			if (off==1)
			{
				loadTPS("/crazytalk/data/vr-0.tps","");
				g_ctObj.Play(0);
				off=0;
			}
			
		}
		else
		{
			// popup alert window
			var winstyle = "width=" + g_InstallWinStyle[2] + 
							", height=" + g_InstallWinStyle[3] +
							", toolbar=" + g_InstallWinStyle[4] + 
							", directories=" + g_InstallWinStyle[5] + 
							", status=" + g_InstallWinStyle[6] + 
							", scrollbars=" + g_InstallWinStyle[7] + 
							", resize=" + g_InstallWinStyle[8] +
							", menubar=" + g_InstallWinStyle[9];
			open(g_InstallWinStyle[0], g_InstallWinStyle[1], winstyle);
		}
	}
}

// ****************************************************************************************
function CTLinkPage()
{
	if(g_headlink.length > 0){
		/*var winstyle = "width=" + g_LinkWinStyle[2] + 
				", height=" + g_LinkWinStyle[3] +
				", toolbar=" + g_LinkWinStyle[4] + 
				", directories=" + g_LinkWinStyle[5] + 
				", status=" + g_LinkWinStyle[6] + 
				", scrollbars=" + g_LinkWinStyle[7] + 
				", resize=" + g_LinkWinStyle[8] +
				", menubar=" + g_LinkWinStyle[9];*/
		//window.open(g_headlink, g_LinkWinStyle[1], winstyle);
		window.location = g_headlink;
	}
}

// ****************************************************************************************
function loadTJM(sFileName, sUrl) {
	// work when turn on
	if(TurnFlag==1)
	{
		if (navigator.appName=="Netscape") {
			if(g_ctObj){
				g_tjmfile = sFileName;
  				g_ctObj.LoadTJM(sFileName);
  			}
		}
		else {
			g_tjmfile = sFileName;
			g_ctObj.TjmName = sFileName;
		}
		g_headlink = sUrl;
	}
}

// ****************************************************************************************
function loadTPS(sFileName, sUrl) {
	// work when turn on
	if(TurnFlag==0)
		TurnOnOff(0);
		
	if(TurnFlag==1)
	{
		if (navigator.appName=="Netscape") {
			g_tpsfile = sFileName;
			g_ctObj.LoadTPS(sFileName);
		}
		else {
			g_tpsfile = sFileName;
			g_ctObj.TpsName=sFileName;
		}
		g_headlink = sUrl;
	}
}

// ****************************************************************************************
// delay ms milli-second then stop player
function stopPlayer(ms)
{
	// work when turn on
	if(TurnFlag==1)
	{
		if(g_ctObj)
   			setTimeout("g_ctObj.Stop()", 200);
   			//g_ctObj.Stop();
   	}
}


// ****************************************************************************************
function setLevel(iLevel) {
  if (navigator.appName=="Netscape") {
    	g_ctObj.setLevel(iLevel);
  }
  else
  {
    	g_ctObj.Level=iLevel;
  }
}

// ****************************************************************************************
function set_Expression(iExpression)
 { 
   if (navigator.appName=="Netscape") {
     	g_ctObj.setLevel(100);
     	g_ctObj.setExpression(iExpression);
     	g_ctObj.setTagMode(0); 
     }
   else
    {
    	if (autoflag==1){
    		autoflag=0;
    		g_ctObj.Level=100;   
      		g_ctObj.Expression = iExpression; 
      		g_ctObj.TagMode=0;  
      		g_ctObj.Play(0);
    	}
    	else{
      		g_ctObj.Level=100;   
      		g_ctObj.Expression = iExpression; 
      		g_ctObj.TagMode=0;  
      	}
    }
 }

// ****************************************************************************************
function morph() {
	level=level+inc;
	setLevel(level);
	if (level>=120) {
		inc=-2;
		down=true;
	}
	if ((down) && (level<=0)){
		down=false;
		level=0;
		inc=2;
		time=time+1;
		set_Expression(expArray[time]);
	
		if (time>4) {
			time=0;
			clearInterval(morphfun);
			setLevel(level);
			g_ctObj.Lifemode=1;
			g_ctObj.TagMode=255;
		}
	}
}

// ****************************************************************************************
function expression_morph(iExpression) {
	level=0;
	inc=2;
	down=false;
	time=0;
	expArray=new Array(19,4,6,10,19);
	g_ctObj.TagMode=0;
	g_ctObj.Lifemode=0;
	clearInterval(morphfun);
	set_Expression(expArray[0]);
	morphfun=setInterval("morph()",1);
}	
