//----------------------------------------------------------------------------
// constants
//----------------------------------------------------------------------------
var NTSC = 0;
var PAL = 1;
var ANY = 2;
var PROG = 3;

//----------------------------------------------------------------------------
// Class Device
//----------------------------------------------------------------------------
function Device(model, format, maxCamera, maxAudio,
        resolutionList, qualityList,
        ipsBase, sizePAL, sizeResolution, sizeQuality, sizeAudio,
        frameRateListNTSC, frameRateListPAL,
        maxIPSinTotal, maxIPSperChannel, hddList)
{
    // member variables
    this.model = model;
    this.format = format;
    this.maxCamera = maxCamera;
    this.maxAudio = maxAudio;
    this.resolutionList = resolutionList;
    this.qualityList = qualityList;
    this.ipsBase = ipsBase;
    this.sizePAL = sizePAL;
    this.sizeResolution = sizeResolution;
    this.sizeQuality = sizeQuality;
    this.sizeAudio = sizeAudio;
    this.frameRateList = [frameRateListNTSC, frameRateListPAL];
    this.maxIPSinTotal = maxIPSinTotal;
    this.maxIPSperChannel = maxIPSperChannel;
    this.hddList = hddList;
}

//----------------------------------------------------------------------------
// Units
//----------------------------------------------------------------------------
function Units()
{
    this.nameList = new Array("bps", "Kbps", "Mbps", "Bps", "KBps", "MBps");
    this.scaleList = new Array(1, 1000, 1000000, 8, 8000, 8000000);
}

var g_units = new Units();

function addUnitSelection()
{
    setListOptions("selectUnit", g_units.nameList);
}

function addCustomDayHourSelection()
{
    setListOptions("selectCustomDayHour", ["Days", "Hours"]);
}

//----------------------------------------------------------------------------
// Class Choice
//----------------------------------------------------------------------------
function Choice()
{
    this.dev = 0;

    this.numCamera = 0;
    this.numAudio = 0;
    this.format = 0;
    this.resolution = 0;
    this.quality = 0;
    this.allQuality = false;
    this.frameRate = 0;
    this.unit = 0;
    this.useCustomSize = false;
    this.customSize = 1000;
    this.useCustomDays = false;
    this.customDays = 10;
    this.customDayHour = 0;

    this.calculateBitsPerSecond = calculateBitsPerSecond;
}

function calculateBitsPerSecond(quality)
{
    var kilo = 1000;
    var dev = this.dev;
    var bitsPerImage = dev.ipsBase * kilo * 8;
    if (this.format == PAL) {
        bitsPerImage *= dev.sizePAL;
    }
    bitsPerImage *= dev.sizeResolution[this.resolution];

	if (dev.sizeResolution[this.resolution] == 16 ) { // 1920*1280  
		if ( quality == 2 )  // high&vhigh x2.5 x3.5
			bitsPerImage *= 2.5;
		else if ( quality == 3 )  // vhigh x3.5
			bitsPerImage *= 3;
		else bitsPerImage *= dev.sizeQuality[quality];
	}
    else
    bitsPerImage *= dev.sizeQuality[quality];

    var imagesPerSec = this.frameRate;
    imagesPerSec *= this.numCamera;
    var maxFrameRate = dev.maxIPSinTotal[this.resolution];
    if (this.format == PAL) {
	maxFrameRate = maxFrameRate * 25 / 30;
    }
    if (imagesPerSec > maxFrameRate) {
	imagesPerSec = maxFrameRate;
    }

    var bpsAudio = dev.sizeAudio * this.numAudio * kilo;

    var bps = bitsPerImage * imagesPerSec + bpsAudio;
    return bps;
}

//----------------------------------------------------------------------------
// global variables
//----------------------------------------------------------------------------

var g_deviceList = new Array();
var g_choice = new Choice();
var g_formatList = [["NTSC"], ["PAL"], ["NTSC", "PAL"], ["PROG"]]
var g_preFormat = 99;
//----------------------------------------------------------------------------
// reset selections
//----------------------------------------------------------------------------
function resetFormat()
{
    var list = g_formatList[g_choice.dev.format];
    var index = replaceList("inputVideoFormat", list);
    if (g_choice.dev.format == PAL) {
        g_choice.format = PAL;
    }
    else if (g_choice.dev.format == PROG) {
        g_choice.format = PROG;
    }
    else {
        g_choice.format = index;
    }
	// PROG·Î °¬´Ù°¡ ¿À¸é ÀÌÀüÀÇ NTSC/PAL·Î µ¹·ÁÁØ´Ù.
	if ( g_preFormat == 99 ) 
		g_preFormat = g_choice.format;
	if ( g_choice.dev.format != PROG ) {
		selectIndex("inputVideoFormat", g_preFormat);
		g_choice.format = g_preFormat;
	}
}

function resetResolution()
{
    g_choice.resolution = replaceList("inputVideoResolution",
	    g_choice.dev.resolutionList);
}

function resetQuality()
{
    g_choice.quality = replaceList("inputVideoQuality",
	    g_choice.dev.qualityList);
    var input = document.getElementById("allQuality");
    input.checked = g_choice.allQuality;
}

function resetFrameRate()
{
    var format = g_choice.format;
	if (format == PROG) {
        format = NTSC;
    }
    var res = g_choice.resolution;
    var quality = g_choice.quality;
    var fList = g_choice.dev.frameRateList[format];
    var list = [];
    var max = g_choice.dev.maxIPSperChannel[res];
    var maxTotal = g_choice.dev.maxIPSinTotal[res] / g_choice.numCamera;
    if (max > maxTotal) {
        max = maxTotal;
    }
    if (format == PAL) {
        max = max * 25 / 30;
    }
    // ¼Ò¼öÁ¡ µÎÀÚ¸®±îÁö
    max = Math.round(max*100)/100;

    var i;
    var needMax = true;
    for (i = 0; i < fList.length; i++) {
        if (fList[i] <= max) {
            list.push(fList[i]);
            if (fList[i] == max) {
                needMax = false;
                break;
            }
        }
        else {
            break;
        }
    }
    if (needMax) {
        list.push(max);
    }
    g_choice.frameRate = list[replaceList("inputFrameRate", list)];
}

function resetNumCamera()
{
    var list = newNumList(1, g_choice.dev.maxCamera);
    g_choice.numCamera = replaceList("inputNumCamera", list) + 1;
}

function resetNumAudio()
{
    var list = newNumList(0, g_choice.dev.maxAudio);
    g_choice.numAudio = replaceList("inputNumAudio", list);
}

//----------------------------------------------------------------------------
// calback functions for selection changed
//----------------------------------------------------------------------------
function modelChanged()
{
	if ( g_choice.dev.format == ANY ) {
		if ( g_preFormat != 99 ) 
			g_preFormat = g_choice.format;
	}

    var input = document.getElementById("inputModel");
    g_choice.dev = g_deviceList[input.selectedIndex];

    resetNumCamera();
    resetNumAudio();
    resetFormat();
    resetResolution();
    resetQuality();
    resetFrameRate();

    refreshOutput();
}

function formatChanged()
{
    var input = document.getElementById("inputVideoFormat");
    if (g_choice.dev.format == PAL) {
        g_choice.format = PAL;
    }
    else if (g_choice.dev.format == PROG) {
        g_choice.format = PROG;
    }
    else {
        g_choice.format = input.selectedIndex;
    }

    resetResolution();
    resetQuality();
    resetFrameRate();

    refreshOutput();
}

function resolutionChanged()
{
    var input = document.getElementById("inputVideoResolution");
    g_choice.resolution = input.selectedIndex;

    resetQuality();
    resetFrameRate();

    refreshOutput();
}

function qualityChanged()
{
    var input = document.getElementById("inputVideoQuality");
    g_choice.quality = input.selectedIndex;
    input = document.getElementById("allQuality");
    g_choice.allQuality = input.checked;

    refreshOutput();
}

function frameRateChanged()
{
    var format = g_choice.format;
	if (format == PROG) {
        format = NTSC;
    }
    var res = g_choice.resolution;
    var list = g_choice.dev.frameRateList[format];
    var input = document.getElementById("inputFrameRate");
    g_choice.frameRate = list[input.selectedIndex];

    refreshOutput();
}

function numCameraChanged()
{
    var input = document.getElementById("inputNumCamera");
    g_choice.numCamera = input.selectedIndex + 1;

    resetFrameRate();
    refreshOutput();
}

function numAudioChanged()
{
    var input = document.getElementById("inputNumAudio");
    g_choice.numAudio = input.selectedIndex;

    refreshOutput();
}

function unitChanged()
{
    var input = document.getElementById("selectUnit");
    g_choice.unit = input.selectedIndex;

    refreshOutput();
}

function hddChanged()
{
    var input = document.getElementById("useCustomSize");
    g_choice.useCustomSize = input.checked;

    input = document.getElementById("customSize");
    g_choice.customSize = parseInt(input.value, 10);

    refreshOutput();
}

function daysChanged()
{
    var input = document.getElementById("useCustomDays");
    g_choice.useCustomDays = input.checked;

    input = document.getElementById("customDays");
    g_choice.customDays = parseInt(input.value, 10);

    refreshOutput();
}

function customDayHourChanged()
{
    var input = document.getElementById("selectCustomDayHour");
    g_choice.customDayHour = input.selectedIndex;

    refreshOutput();
}

var TAB_DISK_CAPACITY = 0;
var TAB_BANDWIDTH = 1;
var TAB_TIME_LENGTH = 2;

function tabChanged(args)
{
    var tab = args.index;

    var obj = document.getElementById("selectCustomDayHour");
    obj.disabled = (tab != TAB_DISK_CAPACITY);
    obj = document.getElementById("customDays");
    obj.disabled = (tab != TAB_DISK_CAPACITY);
    obj = document.getElementById("useCustomDays");
    obj.disabled = (tab != TAB_DISK_CAPACITY);

    obj = document.getElementById("useCustomSize");
    obj.disabled = (tab != TAB_TIME_LENGTH);
    obj = document.getElementById("customSize");
    obj.disabled = (tab != TAB_TIME_LENGTH);

    obj = document.getElementById("allQuality");
    obj.disabled = (tab == TAB_BANDWIDTH);

    obj = document.getElementById("selectUnit");
    obj.disabled = (tab != TAB_BANDWIDTH);
}

//----------------------------------------------------------------------------
// output
//----------------------------------------------------------------------------
function refreshOutput()
{
    document.getElementById("useCustomSize").checked
	= g_choice.useCustomSize;
    document.getElementById("customSize").value
	= g_choice.customSize;

    document.getElementById("useCustomDays").checked
	= g_choice.useCustomDays;
    document.getElementById("customDays").value
	= g_choice.customDays;

    redrawRecDurationTable();
    redrawDiskCapacityTable();
    redrawBandwidthTable();
}

//----------------------------------------------------------------------------
// initialize
//----------------------------------------------------------------------------
function initialize()
{
    pushDevices();

    var list = document.getElementById("inputModel");
    var i;
    for (i = 0; i < g_deviceList.length; i++) {
        var option = document.createElement('option');
        option.text = g_deviceList[i].model;
        if (getBrowser() == "ie") {
            list.add(option);   // IE only
        }
        else {
            list.add(option, null);
        }
    }
    list.selectedIndex = 0;

    initializeDiskCapacityTable();
    initializeRecDurationTable();
    initializeBandwidthTable();
    addUnitSelection();
    addCustomDayHourSelection();

    tabberAutomatic({'onClick':tabChanged});

    modelChanged();
    tabChanged({index:TAB_DISK_CAPACITY});
}

var pos = 0;
function keypressed(event)
{
    var keycode;
    var word = [118, 101, 114];

    if (window.event) {
        keycode = window.event.keyCode;
    }
    else if (event) {
        keycode = event.which;
    }
    if (keycode == word[pos]) {
        pos++;
        if (pos == word.length) {
            alert("Version: " + VERSION);
            pos = 0;
        }
    }
}
