FirmwareFile.prototype._UnblockUpdateButton_ = function(that)
{
	$(that).closest(".Row").find(".Button").eq(2).css('pointer-events', "visible");
	$(that).closest(".Row").find(".Button").eq(2).css('opacity', 1);
};



FirmwareFile.prototype._FileTransmission_= function(that, file)
{
	var ssocket = io.connect();
	var stream = ss.createStream();
	var blobStream = ss.createBlobReadStream(file);
	var size = 0;
	var Progress;
	var Message;
	
	blobStream.on('data', function(chunk) { 
		size += chunk.length;
		Progress = Math.floor(size / file.size * 100) + "%";
		console.log(Progress);
		Message = file.name + " uploaded - " + Progress;
		FirmwareFile.prototype._SetTextareaMessage_(Message);
	});
	blobStream.on('end', function() {
		$(that).closest(".Row").find(".Loading").eq(0).hide();
		$(that).closest("div").find(":file").val("");
		FirmwareFile.prototype._UnblockUpdateButton_(that);
		FirmwareFile.prototype._UnLockAllWebUI_();
	});
	
	blobStream.pipe(stream);
	ss(ssocket).emit('UploadFirmwareFile', stream, {name: file.name});
}

FirmwareFile.prototype._CheckFirmwareFileName_ = function(that, FileName)
{
	var Return=true;
	if(!(FileName.indexOf("ua-") != -1 && FileName.indexOf("_app.tar") != -1))
	{
		$(that).closest(".Row").find(".Loading").hide();
		$(that).closest(".Row").find(".Message").eq(1).show();
		Return=false;
		return Return;
	}
	$(that).closest(".Row").find(".Message").hide();
	return Return;
};

FirmwareFile.prototype._UploadFirmwareFile_ = function(that, Event)
{
	FirmwareFile.prototype._LockAllWebUI_();
	var file = Event.target.files[0];
	if(file == null)	{return 0;}
	$("#Main_Center-Content").find(".Textarea").val("");
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Loading").eq(0).show();
	var FileName = file.name;
	$(that).closest("div").find(":text").val(file.name);
	if(FirmwareFile.prototype._CheckFirmwareFileName_(that, FileName) == false)
	{
		FirmwareFile.prototype._UnLockAllWebUI_();
		return 0;
	}

	// 掛載SD卡
	var Class = new Cgi();
	var Return = Class.RestApi("MountSdCard", new Array(), false);

	// 檢查SD卡掛載狀態
	var Class = new Cgi();
	var Return = Class.RestApi("GetSdCardFreeRate", new Array(), false);
	if(Return == false)
	{
		FirmwareFile.prototype._UnLockAllWebUI_();
		FirmwareFile.prototype._SetTextareaMessage_("Upload Fail.\nPlease, Check\n1. SD Insert\n2. SD enough space\n3. Flash enough space");
		$(that).closest(".Row").find(".Loading").eq(0).hide();
		$(that).closest(".Row").find(".Message").eq(4).show();
		$(that).closest("div").find(":text").val("");
		return 0;
	}
	else
	{
		// 建立/mnt/update folder
		var Class = new Cgi();
		var Return = Class.RestApi("MkdirUpdateFolder", new Array(), false);
		// 檔案上傳
		FirmwareFile.prototype._FileTransmission_(that, file);
	}

};
FirmwareFile.prototype._StartInstallStep_ = function(that, array)
{
	var ssocket = io.connect();
	ssocket.emit("UpdateFirmwareFile", array);
	ssocket.on("UpdateFirmwareFile", function (Return) {
	if(Return[0] == true && array[1]<4)
	{
		FirmwareFile.prototype._SetTextareaMessage_(Return[1]);
		array[1]++;
		var LogMsg1 = Return[1].indexOf('Start');
		var LogMsg2 = Return[1].indexOf('Successful');
		var LogMsg3 = Return[1].indexOf('OK');
		if(LogMsg3 > 0)
		{
			$(that).closest(".Row").find(".Loading").hide();
			$(that).closest(".Row").find(".Message").eq(3).show();
		}
		else if(LogMsg1 != -1 || LogMsg2 != -1)
		{
			FirmwareFile.prototype._StartInstallStep_(that, array);
		}
		else
		{
			$(that).closest(".Row").find(".Loading").hide();
			$(that).closest(".Row").find(".Message").eq(4).show();
			FirmwareFile.prototype._SetTextareaMessage_("Install Failed!");
		}
	}
	/*
	else
	{
		$(that).closest(".Row").find(".Loading").hide();
		$(that).closest(".Row").find(".Message").eq(4).show();
		FirmwareFile.prototype._SetTextareaMessage_("Install Failed!");
	}
	*/
	ssocket.emit("SocketClose");
	});
}

FirmwareFile.prototype._UpdateFirmwareFile_ = function(that)
{
	var TimeoutSec = 240;
	FirmwareFile.prototype._LockAllWebUI_();
	$(that).closest(".Row").find(".Button").eq(2).css('pointer-events', "none");
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Loading").eq(1).show();
	var FileName = $(that).closest("div").find(":text").val();
	var array = new Array(FileName);
	$(that).closest("div").find(":text").val("");
	FirmwareFile.prototype._SetTextareaMessage_("Start Install...");
	var ssocket = io.connect();
	console.log(TimeoutSec);
	ssocket.emit("UpdateFirmwareFile", array);
	ssocket.on("UpdateFirmwareFile", function (Return) {
	if(Return == true)
	{
		var Class = new Cgi();
		var LogString = {Input:null};
		var LogStringSetInterval = setInterval(function()
		{
			Class.RestApi("GetWebLogMessage", new Array(), true, LogString);
		},2000);
		var SetInterval = setInterval(function()
		{
			FirmwareFile.prototype._SetTextareaMessage_("Updating, wait " + TimeoutSec + " seconds");
			if(TimeoutSec < 0)
			{
				clearInterval(LogStringSetInterval);
				clearInterval(SetInterval);
				$(that).closest(".Row").find(".Loading").hide();
				$(that).closest(".Row").find(".Message").eq(3).show();
				FirmwareFile.prototype._RebootSecondLeft_();
			}
			TimeoutSec = TimeoutSec - 1;
		},1000);
	}
	else
	{
		$(that).closest(".Row").find(".Loading").hide();
		$(that).closest(".Row").find(".Message").eq(4).show();
		FirmwareFile.prototype._UnLockAllWebUI_();
	}
	ssocket.emit("SocketClose");
	});
};
FirmwareFile.prototype._RebootSecondLeft_ = function(that)
{
	var Time = 5, BootTime = 60;
	var SetInterval = setInterval(function()
	{
		if(Time > 0)
		{
			FirmwareFile.prototype._SetTextareaMessage_("Reboot after " + Time + " seconds");
		}
		else
		{
			FirmwareFile.prototype._SetTextareaMessage_("UA reboot ...");
			clearInterval(SetInterval);
			//var ssocket = io.connect();
			//FirmwareFile.prototype._Reboot_(ssocket);
		}
		Time--;
	},1000);
	var BootSetInterval = setInterval(function()
	{
		if(BootTime <= 75)
		{
			FirmwareFile.prototype._SetTextareaMessage_("Booting, wait " + BootTime + " seconds");
			if(BootTime < 1)
			{
				clearInterval(BootSetInterval);
				var Local_IP = document.location.hostname; 
				document.location.href = document.location.protocol + "//" + Local_IP;
			}
		}
		BootTime--;
	},1000);
};


FirmwareFile.prototype._RestoreFirmware_ = function(that)
{

};

FirmwareFile.prototype._SetTextareaMessage_ = function(message)
{
	var OldMessage = $("#Main_Center-Content").find(".Textarea").val();
	if(OldMessage == '')
	{
		$("#Main_Center-Content").find(".Textarea").val(message);
	}
	else
	{
		if(OldMessage.indexOf(message) >= 0)
		{
			$("#Main_Center-Content").find(".Textarea").val(OldMessage);
		}
		else
		{
			$("#Main_Center-Content").find(".Textarea").val(message + "\n" + OldMessage);
		}
	}
};

FirmwareFile.prototype._Reboot_ = function(ssocket)
{
	ssocket.emit('DeviceReboot');
};

FirmwareFile.prototype._LockAllWebUI_ = function(ssocket)
{
	$("#Main_Base").css('pointer-events', "none");
	$("#Main_Left-List").find("div").css('pointer-events', "none");
	$("#Main_Center-Content").find("div").css('pointer-events', "none");
	$(".Textarea").css('pointer-events', "visible");
};

FirmwareFile.prototype._UnLockAllWebUI_ = function(ssocket)
{
	$("#Main_Base").css('pointer-events', "visible");
	$("#Main_Left-List").find("div").css('pointer-events', "visible");
	$("#Main_Center-Content").find("div").css('pointer-events', "visible");
	$(".Textarea").css('pointer-events', "visible");
};