/* huperLab */
/* Test script for remote record playback */

var cmdSnapshot = "WebConfig/RSetCGICamera.cgi?Action=Get&CamNumber=CAMERA_ID&GetRecordSnapshot=Time=TIME";
var cmdVideo = "WebConfig/RSetCGICamera.cgi?Action=Get&CamNumber=CAMERA_ID&GetRecordVideo=StartTime=START_TIME||EndTime=END_TIME||Format=";
var htmlImgSrc = "<img src=\"IMG_SRC\">";

var htmlVideoSrc = " <video controls poster=\"IMG_SRC\">\
        <source src=\"MP4_SRC\"  type='video/mp4'>\n\
        <source src=\"WEBM_SRC\" type='video/webm'>\n\
        <source src=\"OGG_SRC\"  type='video/ogg'>\n\
         <p> Your browser does not support HTML5 video<p>\n\
         </video>";

function getRecord() {
    'use strict';
    var camID = document.getElementById('CamNumber')
        .value;
    var dateTime = document.getElementById('datetime')
        .value;
    var startTime = dateTime.replace(/[-: ]/g, "") + "00000";

    var theHTML;
    var imgSrc = cmdSnapshot.replace("CAMERA_ID", camID)
                 .replace("TIME", startTime);

    if (document.getElementsByName('recordType')[0].checked) {
        // Snapshot mode
        theHTML = htmlImgSrc.replace("IMG_SRC", imgSrc);

    } else {
        // Video Mode

        var endTime = dateTime.replace(/[-: ]/g, "") + "59000";
        var videoSrc = cmdVideo.replace("CAMERA_ID", camID)
            .replace("START_TIME", startTime)
            .replace("END_TIME", endTime);
        var videoSrcMP4 = videoSrc + "mp4";
        var videoSrcWebM = videoSrc + "webm";
        var videoSrcOgg = videoSrc + "ogg";
        theHTML = htmlVideoSrc.replace("IMG_SRC", imgSrc)
            .replace("MP4_SRC", videoSrcMP4)
            .replace("WEBM_SRC", videoSrcWebM)
            .replace("OGG_SRC", videoSrcOgg);
    }
    var result = document.getElementById('record');
    result.innerHTML = theHTML;
    return false;
}

function init() {
    'use strict';
    document.getElementById('theForm')
        .onsubmit = getRecord;
}
window.onload = init;