
var modeTables = new Array(idBoxes, idBoxEditor, idPlayer, idCropper)
function showMode(idMode)
{
	showObject(idMode, modeTables);
}

var controlsTable = new Array(idEditControls, idCaptureControls);

function showControls(idControls)
{
	showObject(idControls, controlsTable);
}

function showObject(id, objects)
{
	for (i = 0; i < objects.length; ++i)
	{
		var obj = objects[i];
		if (obj != id)
		{
			obj.style.display = "none";
		}
	}
	id.style.display = "";
}

function showReplaysFolder()
{
	GAMEDACTL.Explorer();
	return false;
}

function addNewBox()
{
	idBoxZIP.value = "";
	idBoxFinderID.value = "";
	idBoxPassword.value = "";

	idEditAdd.value = "Add";

	showMode(idBoxEditor);
}

function addBox(watch)
{
	var zip = idBoxZIP.value;
	if (zip.length <= 0)
	{
		alert("Please provide ZIP");
		idBoxZIP.focus();
		return;
	}
	var ID = idBoxFinderID.value;
	if (ID != "local" && ID.length < 36)
	{
		alert("Please provide correct finder ID for the box.\nIt should be in form 01234567-89AB-CDEF0123-4567-89ABCDEF");
		idBoxFinderID.focus();
		return;
	}
	var password = idBoxPassword.value;
	if (password.length <= 0)
	{
		alert("Please provide password");
		idBoxPassword.focus();
		return;
	}

	GAMEDACTL.AddBox(ID, zip, password);
	if (watch)
	{
		startBox(ID, "", password);
	}
	else
	{
		boxes();
	}
}

function watchBox(boxIndex)
{
	var box = GAMEDACTL.GetBox(boxIndex);
	startBox(box.FinderID, box.IP, box.Password);
	return false;
}

function startBox(finderID, IP, password)
{
	// ORDER: PocketIE destroys invisible ActiveX windows
	// We have to show GAMEDACTL before starting it
	showMode(idPlayer);
	GAMEDACTL.ID = finderID;
	GAMEDACTL.IP = IP;
	GAMEDACTL.Password = password;
	GAMEDACTL.StartIt();
	updateClips();
	showControls(idCaptureControls);
}

function editBox(boxIndex)
{
	var box = GAMEDACTL.GetBox(boxIndex);
	idBoxZIP.value = box.ZIP;
	idBoxFinderID.value = box.FinderID;
	idBoxPassword.value = box.Password;

	idEditAdd.value = "Save";

	showMode(idBoxEditor);
	return false;
}

function removeBox(boxIndex)
{
	var box = GAMEDACTL.GetBox(boxIndex);
	GAMEDACTL.RemoveBox(box.finderId);
	boxes();
	return false;
}

function boxes()
{
	showControls(idEditControls);
	var count = GAMEDACTL.BoxCount;
	if (count > 0)
	{
		showMode(idBoxes);
		var t = "<table align=center valign=center border=0 cellpadding=0 cellspacing=0 >"

		for (i = 0; i < count; ++i)
		{
			var box = GAMEDACTL.GetBox(i);
			t += "<tr>";
			t += "<td><input type=button style='width:100%;' value='" + box.ZIP + "' onclick='watchBox(" + i + ");'/></td>";
			t += "<td><input type=button value='Edit' onclick='editBox(" + i + ");'/></td>";
			t += "<td><input type=button value='Remove' onclick='removeBox(" + i + ");'/></td>";
			t += "</tr>";
		}
		t += "<tr align=center><td colspan=3><input type=button value='Add New Box' onclick='addNewBox();'/></table>";
		idBoxes.innerHTML = t;
	}
	else
	{
		addNewBox();
	}
}

function clearRecordProgress()
{
	idProgressLabel.Text = "";
}

function record30()
{
	idRecord30.src = "pro_files/pro-button-30-2.png";
	GAMEDACTL.Thirty();
	idRecord30.src = "pro_files/pro-button-30-1.png";
	return false;
}

function record60()
{
	idRecord60.src = "pro_files/pro-button-60-2.png";
	GAMEDACTL.Sixty();
	idRecord60.src = "pro_files/pro-button-60-1.png";
	return false;
}

var recording = false;
function startRecording()
{
	if (!recording)
	{
		idRecordButton.src = "pro_files/pro-button-record-2.png";
		GAMEDACTL.AssignLabelCtrl(idProgressLabel.HWND);
		GAMEDACTL.Record();
		recording = true;
	}
	return false;
}

function queryStopRecording()
{
	if (recording)
	{
		idThumbnails.style.display = "none";
		idRecordConfirm.style.display = "";
		idStopRecordButton.focus();
	}
	return false;
}

function stopRecording()
{
	if (recording)
	{
		// TODO: update saving time
		GAMEDACTL.AssignLabelCtrl(0);
		GAMEDACTL.Stop();
		idRecordLink.focus();

		idThumbnails.style.display = "";
		idRecordConfirm.style.display = "none";
		recording = false;

		var rec = GAMEDACTL.RecordString;
		var idx = rec.indexOf("|");
		if (idx >= 0)
		{
			rec = "Saved" + rec.substr(idx, rec.length - idx);
			idProgressLabel.Text = rec;
			setTimeout(clearRecordProgress, 5000);
		}
		else
		{
			clearRecordProgress();
		}
		idRecordButton.src = "pro_files/pro-button-record-1.png";
	}
	return false;
}

function continueRecording()
{
	idThumbnails.style.display = "";
	idRecordConfirm.style.display = "none";
	return false;
}

var clipPosition = 0;
var clips = new Array();
var thumbnails = new Array();

function addClip(dir, clipName)
{
	var filename = dir + clipName.substr(0, clipName.length - 3) + "gif";
	thumbnails.unshift(filename);
	clips.unshift(clipName);
	updateClips();
}

function updateClips()
{
	var txtPosition = "0/0";

	if (thumbnails.length > 0)
	{
		txtPosition = (clipPosition + 1) + "/" + thumbnails.length;
		idClipImg.src = "file://" + thumbnails[clipPosition];
	}
	else
	{
		idClipImg.src = "pro_files/space.png";
	}
	idClipPosition.innerHTML = txtPosition;
	
	//idPrevClip.disabled = clipPosition <= 0;
	//idNextClip.disabled = (clipPosition + 1) >= thumbnails.length;
	//idEditClip.disabled = clipPosition >= thumbnails.length;
	//idPlayClip.disabled = clipPosition >= thumbnails.length;
	if (clipPosition >= thumbnails.length)
	{
		idEditClip.src = "pro_files/edit-clip-4.gif";
		idPlayClip.src = "pro_files/play-clip-4.gif";
	}
	else
	{
		idEditClip.src = "pro_files/edit-clip-1.gif";
		idPlayClip.src = "pro_files/play-clip-1.gif";
	}
}

function prevClip()
{
	if (clipPosition > 0)
	{
		--clipPosition;
		updateClips();
	}
	return false;
}

function nextClip()
{
	if (clipPosition + 1 < thumbnails.length)
	{
		++clipPosition;
		updateClips();
	}
	return false;
}

function playClip()
{
	if (clipPosition < clips.length)
	{
		GAMEDACTL.FileName = clips[clipPosition];
		GAMEDACTL.PlayFile();
	}
	return false;
}

var cropMax = 0;
var cropStart = 0;
var cropEnd = 0;
// we mau decide to use another control for crop length later
//var idCropLabel = idProgressLabel;

function editClip()
{
	if (clipPosition < clips.length)
	{
		showMode(idCropper);
		CROPPER.FileName = clips[clipPosition];
		CROPPER.StartIt();
		cropMax = 0;
		cropStart = 0;
		cropEnd = 0;
		updateCropLength(0, 1000);
	}
	return false;
}

function checkCropMax()
{
	if (cropMax == 0)
	{
		cropMax = CROPPER.ClipLength;
		cropEnd = cropMax;
	}
}

function getCropStart()
{
	if (cropMax == 0)
	{
		return 0;
	}
	else
	{
		return Math.floor(1000 * cropStart / cropMax);
	}
}

function getCropEnd()
{
	if (cropMax == 0)
	{
		return 1000;
	}
	else
	{
		return Math.floor(1000 * cropEnd / cropMax);
	}
}

function adjustCropStart(offset)
{
	checkCropMax();
	cropStart += offset;
	if (cropStart < 0)
	{
		cropStart = 0;
	}
	else if (cropStart > cropEnd)
	{
		cropStart = cropEnd;
	}

	//idStartDecrement.disabled = cropStart < 1;
	//idStartIncrement.disabled = cropStart + 1 > cropEnd;
	//idEndDecrement.disabled = idStartIncrement.disabled;

	var start = getCropStart();
	var end = getCropEnd();
	CROPPER.DragPlay(start);
	updateCropLength(start, end);

	return false;
}

function adjustCropEnd(offset)
{
	checkCropMax();
	cropEnd += offset;
	if (cropStart > cropMax)
	{
		cropStart = cropMax;
	}
	else if (cropStart > cropEnd)
	{
		cropEnd = cropStart;
	}

	//idEndDecrement.disabled = cropEnd - 1 < cropStart;
	//idEndIncrement.disabled = cropEnd + 1 > cropMax;
	//idStartIncrement.disabled = idEndDecrement.disabled;

	var start = getCropStart();
	var end = getCropEnd();
	CROPPER.DragEnd(end);
	updateCropLength(start, end);
	
	return false;
}

function updateCropLength(start, end)
{
	var t = "<table width=100% height=100% valign=top border=0 cellpadding=0 cellspacing=0 ><tr>";
	t += "<td width=" + start + "></td>"
	t += "<td><img width=16 height=15 src=pro_files/slider-left-default.gif></td>";
	t += "<td width=" + (end - start) + "></td>"
	t += "<td><img width=16 height=15 src=pro_files/slider-right-default.gif></td>";
	t += "<td width=" + (1000 - end) + "></td></tr></table>";
	idCropIndicators.innerHTML = t;

	if (0 != cropMax)
	{
		var duration = Math.floor(cropEnd - cropStart);
		var minutes = Math.floor(duration / 60);
		var seconds = duration % 60;
		t = "";
		if (minutes < 10)
		{
			t = "0";
		}
		t += minutes.toString() + ":";
		if (seconds < 10)
		{
			t += "0";
		}
		t += seconds.toString();

 		idCropLabel.Text = "Crop to " + t;
	}
	else
	{
		idCropLabel.Text = "";
	}
}

function saveClip()
{
	CROPPER.SaveCropped();
	idCropLabel.Text = "You edit has been saved";
	showMode(idPlayer);
	updateClips();
	return false;
}

function closeClip()
{
	idCropLabel.Text = "";
	showMode(idPlayer);
	updateClips();
	return false;
}

function deleteClip()
{
	// adjust clips and thumbnails list
	var filename = CROPPER.FileName;
	var newClips = new Array();
	var newThumbnails = new Array();
	while (clips.length > 0)
	{
		var c = clips.shift();
		var t = thumbnails.shift();
		if (c != filename)
		{
			newClips.push(c);
			newThumbnails.push(t);
		}
	}
	clips = newClips;
	thumbnails = newThumbnails;
	if (clipPosition >= clips.length)
	{
		clipPosition = clips.length - 1;
		if (clipPosition < 0)
		{
			clipPosition = 0;
		}
	}

	idCropLabel.Text = "";
	updateClips();
	CROPPER.DeleteFile();
	showMode(idPlayer);
	return false;
}

function channelDown()
{
	GAMEDACTL.ChanDown();
	return false;
}

function channelUp()
{
	GAMEDACTL.ChanUp();
	return false;
}

function logOff()
{
	GAMEDACTL.LogOff();
	boxes();
	return false;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function onLoad()
{
	var logged = false;
	var UID = "";
	var EMAIL = "";
	
	try
	{
		UID = getCookie("UIDc");
		EMAIL = getCookie("EMAILc");
		logged = true;
	}    
	catch (ex)
	{
		return;
	}
	/*
	if (!logged ||
		 UID == undefined ||
		 EMAIL == undefined)
	{
		window.navigate("/login.php");
		return;
	}
*/
	try
	{
		/*
		GAMEDACTL.UID = UID;
		GAMEDACTL.EMAIL = EMAIL;*/
		
		GAMEDACTL.UID = '1';
		GAMEDACTL.EMAIL = 'zxcjason@yahoo.com';
		GAMEDACTL.StartIt();
	}    
	catch (ex)
	{
		window.navigate("download.htm")
	}
}

