/* Audio/Video javascript  */
var aVideoSize = new Array(5);
aVideoSize[0] = new Array(true, "Half", 1);
aVideoSize[1] = new Array(true, "Half x 2", 2);
aVideoSize[2] = new Array(true, "Normal", 3);
aVideoSize[3] = new Array(true, "Normal x 2", 4);
aVideoSize[4] = new Array(true, "Double", 5);

var aVideoRes = new Array(4);
aVideoRes[0] = new Array(true, "160x120", "160x120");
aVideoRes[1] = new Array(true, "176x144", "176x144");
aVideoRes[2] = new Array(true, "320x240", "320x240");
aVideoRes[3] = new Array(true, "640x480", "640x480");

var aVideoBitrate = new Array(14);
aVideoBitrate[0] = new Array(true, "20 Kbps",    20000);
aVideoBitrate[1] = new Array(true, "30 Kbps",    30000);
aVideoBitrate[2] = new Array(true, "40 Kbps",    40000);
aVideoBitrate[3] = new Array(true, "50 Kbps",    50000);
aVideoBitrate[4] = new Array(true, "64 Kbps",    64000);
aVideoBitrate[5] = new Array(true, "128 Kbps",   128000);
aVideoBitrate[6] = new Array(true, "256 Kbps",   256000);
aVideoBitrate[7] = new Array(true, "512 Kbps",   512000);
aVideoBitrate[8] = new Array(true, "768 Kbps",   768000);
aVideoBitrate[9] = new Array(true, "1 Mbps",   1000000);
aVideoBitrate[10] = new Array(true, "1.5 Mbps", 1500000);
aVideoBitrate[11] = new Array(true, "2 Mbps",  2000000);
aVideoBitrate[12] = new Array(true, "3 Mbps",  3000000);
aVideoBitrate[13] = new Array(true, "4 Mbps",  4000000);

var aFramerateTbl = new Array(1, 2, 3, 5, 10, 15, 20, 25, 30);
var aVideoMaxframe = new Array(aFramerateTbl.length);
for (i = 0; i < aVideoMaxframe.length; i ++)
{
	aVideoMaxframe[i] = new Array(true, aFramerateTbl[i], aFramerateTbl[i])
}

function changeToComputerModeMenu()
{
	var avForm = document.AVOption;
	for (i = 0; i < aVideoRes.length; i ++)
		aVideoRes[i][0] = true;
	updateDynSelOpt(avForm.video_resolution, aVideoRes);
	/* 
	//this is a better manipulation, but ONLY IE DONNOT support this
	var selVideoSize = avForm.video_resolution;
	for (i = 0; i < selVideoSize.length; i ++)
		selVideoSize.options[i].disabled = false;
	*/
	avForm.video_quality[1].disabled = false;
	for (i = 0; i < aVideoBitrate.length; i ++)
			aVideoBitrate[i][0] = true;
	updateDynSelOpt(avForm.video_bitrate, aVideoBitrate);
	/*
	//this is a better manipulation, but ONLY IE DONNOT support this
	var selVideoBitrate = avForm.video_bitrate;
	for (i = 0; i < selVideoBitrate.length; i ++)
		selVideoBitrate.options[i].disabled = false;
	*/
	avForm.audio_type[0].disabled = false;
}

function changeToMobileModeMenu()
{
	var avForm = document.AVOption;
	for (i = 0; i < aVideoRes.length; i ++)
	{
		if (aVideoRes[i][2] != "176x144")
			aVideoRes[i][0] = false;
	}
	updateDynSelOpt(avForm.video_resolution, aVideoRes);
	/*
	//this is a better manipulation, but ONLY IE not support this
	var selVideoSize = avForm.video_resolution;
	for (i = 0; i < selVideoSize.length; i ++)
	{	// 1 is half, and disable others
		if (selVideoSize.options[i].value != 1)
			selVideoSize.options[i].disabled = true;
	}
	*/
	/* disable fixed quality setting */
	avForm.video_quality[1].disabled = true;
	for (i = 0; i < aVideoBitrate.length; i ++)
	{
		if (aVideoBitrate[i][2] > 512000)
			aVideoBitrate[i][0] = false;
	}
	updateDynSelOpt(avForm.video_bitrate, aVideoBitrate);
	/*
	//this is a better manipulation, but ONLY IE not support this
	var selVideoBitrate = avForm.video_bitrate;
	for (i = 0; i < selVideoBitrate.length; i ++)
	{	// if bitrate larger than 512000, diable it 
		if (selVideoBitrate.options[i].value > 512000)
			selVideoBitrate.options[i].disabled = true;
	}
	*/
	/* disable AAC4 setting */
	avForm.audio_type[0].disabled = true;
}

function resetComputerModeSetting()
{
	var avForm = document.AVOption;
	selectTheDropdownEnt(avForm.video_resolution, "640x480");
	var selVMode = avForm.video_mode
	if (selVMode.options[selVMode.selectedIndex].value == 50)
		selectTheDropdownEnt(avForm.video_maxframe, 25);
	else if (selVMode.options[selVMode.selectedIndex].value == 60)
		selectTheDropdownEnt(avForm.video_maxframe, 30);
	selectTheRadioButton(avForm.video_quality, 2, 1);
	selectTheDropdownEnt(avForm.video_quant, 5);
	selectTheRadioButton(avForm.audio_type, 2, "AAC4");
	selectTheDropdownEnt(avForm.audio_aacbitrate, 128000);
}

function resetMobileModeSetting()
{
	var avForm = document.AVOption;
	selectTheDropdownEnt(avForm.video_resolution, "176x144");
	selectTheDropdownEnt(avForm.video_maxframe, 5);
	selectTheRadioButton(avForm.video_quality, 2, 0);
	selectTheDropdownEnt(avForm.video_bitrate, 40000);
	selectTheRadioButton(avForm.audio_type, 2, "GAMR");
	selectTheDropdownEnt(avForm.audio_aacbitrate, 12200);
}

function VideoSubmit()
{
	var avForm = document.AVOption;
	//if (!checkInString(avForm.video_text))
	//	return -1;
	if (avForm.flip.checked == true)	avForm.video_flip.value = 1;
	else								avForm.video_flip.value = 0;
	if (avForm.mirror.checked == true)	avForm.video_mirror.value = 1;
	else								avForm.video_mirror.value = 0;
	if (avForm.imprinttimestamp.checked == true)	avForm.video_imprinttimestamp.value = 1;
	else								            avForm.video_imprinttimestamp.value = 0;
	if (avForm.audiomute.checked == true)	avForm.audio_transfermode.value = 4;
	else				     	            avForm.audio_transfermode.value = 3;
	avForm.submit();
}

function checkViewMode(resetDefaultSetting)
{
	var avForm = document.AVOption;
	if (avForm.system_viewmode[0].checked)
	{
		changeToComputerModeMenu();
		if (resetDefaultSetting)	resetComputerModeSetting();
	}
	else if (avForm.system_viewmode[1].checked)
	{
		changeToMobileModeMenu();
		if (resetDefaultSetting)	resetMobileModeSetting();
	}
}

function updateMaxFrameDropdownList(targetSel, maxframerate)
{
	var oldSelect = targetSel.selectedIndex;
	for (i = 0; i < aVideoMaxframe.length; i ++)
	{
		if (aVideoMaxframe[i][2] > maxframerate)
			aVideoMaxframe[i][0] = false;
		else
			aVideoMaxframe[i][0] = true;
	}
	updateDynSelOpt(targetSel, aVideoMaxframe);
	if (oldSelect >= targetSel.length)
		targetSel.selectedIndex = targetSel.length - 1;
	else
		targetSel.selectedIndex = oldSelect;
}

function checkPowerLineFreq()
{
	var avForm = document.AVOption;
	var selVMode = avForm.video_mode
	if (selVMode.options[selVMode.selectedIndex].value == 50)
	{
		updateMaxFrameDropdownList(avForm.video_maxframe, 25);
	}
	else if (selVMode.options[selVMode.selectedIndex].value == 60)
	{
		updateMaxFrameDropdownList(avForm.video_maxframe, 30);
	}
}
