//
var winsDttInstance = new Object();
var jsWinsDttReady = false;
var swfWinsDttReady = false;
var WaitWinsDtti = 0;
var winsDttActionFile="swf_save_file.asp";
var TakePhotoID = "btnTakePhoto";
var ResetTakePhotoID = "btnResetTakePhoto";



function isWinsDttReady()
{	
	return jsWinsDttReady;
}
function setWinsDttSwfIsReady()
{
	swfWinsDttReady = true;
}

//类模块开始

function winsDtt(id)
{
	jsWinsDttReady = true; 
	
	this.UpFileName = "";
	this.LastVersion = "";
	this.LastUpGradeURL = "";
	this.haveCamera = false;
	
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		this.SwfInstance = window[id];
	}
	else
	{
		this.SwfInstance = document[id];
	}
	this.UpFileName = "";
	this.isInPhoto = false;
	this.LastVersion = "";
	this.LastUpGradeURL = "";
	this.CameraMode = 1; //1:摄像头模式; 2:本地文件模式
	this.isUpLoadLocalFile = false;
	this.CheckhaveCamera = function(){return this.SwfInstance.CheckhaveCamera()};
	this.GetLastVersion = function(){this.SwfInstance.GetLastVersion();};
	this.GetUpGradeURL = function(){this.SwfInstance.GetUpGradeURL();};
	this.ResetBg = function(){this.SwfInstance.ResetBg();};
	this.ChangeBg = function(pic){this.SwfInstance.ChangeBg(pic);};
	this.LoadLocalPic = function(pic){this.SwfInstance.LoadLocalPic(pic);};
	this.SetActionFile = function (file){this.SwfInstance.SetActionFile(file);};
	this.ResetPhoto = function()
		{
			if(this.isInPhoto)
			{
				this.isInPhoto = false;
				this.SwfInstance.ResetPhoto();
			}
		};
	this.SavePhoto = function()
		{
			if(this.CameraMode == 1)
			{
				if(this.isInPhoto)
					this.SwfInstance.SavePhoto();
				else
					alert("请先点拍照按钮。");
			}
			else if(this.CameraMode == 2)
			{
				if(this.isUpLoadLocalFile)
					this.SwfInstance.SavePhoto();
				else
					alert("请先上传本地图片。");
			}
		};
	this.TakePhoto = function()
		{
			if(!this.isInPhoto)
			{
				this.isInPhoto = true;
				this.SwfInstance.TakePhoto();
			}
			else
			{
				if(confirm("要重新拍照，请按“重拍”按钮，是否现在重拍？"))
					this.ResetPhoto();
			}
		};
	this.ZoomOut = function(){this.SwfInstance.ZoomOut();};
	this.SetBright = function(num){this.SwfInstance.SetBright(num);}
	this.SetContrast = function(num){this.SwfInstance.SetContrast(num);};
	this.SetSaturation = function(num){this.SwfInstance.SetSaturation(num);};
	this.ResetFilter = function()
		{
			if(confirm("确定重设亮度、对比度、饱和度吗？"))
			{
				Bar1.setSldPoint(255);
				Bar2.setSldPoint(500);
				Bar3.setSldPoint(150);
				this.SwfInstance.ResetFilter();

			}
		};
	this.ZoomIn  = function(){this.SwfInstance.ZoomIn();};

	this.GetAuthor = function(){return this.SwfInstance.GetAuthor();};
	this.GetSite = function(){return this.SwfInstance.GetSite()}
	this.GetProductName = function(){return this.SwfInstance.GetProductName()}
	this.GetNowVersion = function(){return this.SwfInstance.GetNowVersion()}
	this.GetCameras = function(){return this.SwfInstance.GetCameras();};
}



function WinsUpDTTcompleteHandler(filename)
{
	winsDttInstance.UpFileName = filename;
}
function GetWinsDttLastVersioncompleteHandler(ver)
{
	winsDttInstance.LastVersion = ver;
}
function GetWinsDttUpGradeURLcompleteHandler(path)
{
	winsDttInstance.LastUpGradeURL = path;
}
function CheckhaveCameraHandler(v)
{
	winsDttInstance.haveCamera = v;
}




function WinsDttFlashDebug(str)
{
	alert(str);
}


function InitWinsDtt(id)
{
	winsDttInstance=new winsDtt(id);
	winsDttInstance.SetActionFile(winsDttActionFile);
	winsDttInstance.GetLastVersion();
	winsDttInstance.GetUpGradeURL();
	if(swfWinsDttReady)
	{
		SetWinsDttMode();
	}
	else
	{
		setTimeout("WaitWinsDttReady()",50); 
	}
}

function SetWinsDttMode()
{
	if(winsDttInstance.CheckhaveCamera())
		winsDttInstance.CameraMode=1;
	else
		winsDttInstance.CameraMode=2;
	ChangeWinsDttMode(winsDttInstance.CameraMode);
}


function ChangeWinsDttMode(mode)
{
	if(mode==1)
	{
		if(!winsDttInstance.haveCamera)
		{
			alert("检测到您电脑上未安装摄像头，不能启用摄像头模式！\n 要使用摄像头模式，请确认插好摄像头，并摄像头未被占用。");
			ChangeWinsDttMode(2);
			return false;
		}
		else
		{
			winsDttInstance.CameraMode=1;
			document.getElementById("rdidttSource1").checked = true;
			document.getElementById("rdidttSource2").checked = false;
			document.getElementById("trfileup").style.display = "none";
			
			document.getElementById(TakePhotoID).disabled  = false;
			document.getElementById(ResetTakePhotoID).disabled  = false;
			//这里启用Flash摄像头

		}
	}
	else
	{
		winsDttInstance.CameraMode=2;
		document.getElementById("rdidttSource1").checked = false;
		document.getElementById("rdidttSource2").checked = true;
		document.getElementById("trfileup").style.display = "";

		document.getElementById(TakePhotoID).disabled  = true;
		document.getElementById(ResetTakePhotoID).disabled  = true;
			//这里禁用Flash摄像头
	}
}



function WaitWinsDttReady()
{
	if((!swfWinsDttReady) && WaitWinsDtti<200)
	{
		WaitWinsDtti++;
		setTimeout("WaitWinsDttReady()",50); 
	}
	else
	{
		SetWinsDttMode();
	}
}


function LoadWinsDttFile(pic)
{
	winsDttInstance.LoadLocalPic(pic);
	winsDttInstance.isUpLoadLocalFile = true;
	document.frmfileup.reset();
	document.getElementById("upmsgspan").innerHTML="";

}





function BeforeUpLocalDttImg(frm)
{
	if(frm.txtfile.value=="")
	{
		alert("请选择本地图片，然后点上传按钮！");
		frm.txtfile.focus();
		return false;
	}
	var fname = frm.txtfile.value.toUpperCase();
	var   sExp   =   fname.substring(fname.lastIndexOf('.'),fname.length);   
	if(!(sExp==".JPG" || sExp==".GIF"))
	{
		alert("只支持jpg和gif格式！");
		frm.txtfile.focus();
		return false;
	}
	document.getElementById("upmsgspan").innerHTML="上传中……";
	frm.action = winsDttActionFile + "?action=upfile";
}