﻿/// <reference name="MicrosoftAjax.js"/>

/////////////////////////////////
//Global variable declarations//
///////////////////////////////

var theForm = document.forms['frm1'];

//////////////////////////////
//Global variables for     // 
//Cross Browser and OnLoad//
///////////////////////////

var win = window,
    doc = document,
    nav = navigator,
    timer = null,
    isDomLoaded = false,
    domLoadFnArr = new Array();

///////////////////////////
//Cross Browser wrappers//
/////////////////////////

function getInnerText(ctrl) {
    if (document.all) { //IE only
        return ctrl.innerText;
    } else {
        return ctrl.textContent;
    }
}

function setInnerText(ctrl, value) {
    if (document.all) {
        ctrl.innerText = value;
    } else {
        ctrl.textContent = value
    }
}

function getEventSrcElement(event) {
    if (document.all) {
        return event.srcElement;
    } else {
        return event.target;
    }
}

function fireClickEvent(ctrl) {
    if (document.all) {
        ctrl.fireEvent('onclick');
    } else {
        var clickEvent = window.document.createEvent('MouseEvent');
        clickEvent.initEvent('click', false, true);
        ctrl.dispathEvent(clickEvent);
    }
}

function getWindowHeight() {
    if (document.all) {
        return document.body.offsetHeight;
    } else {
        var hgt = window.getBoundingClientRect();
        return hgt.height;
    }
}

function getWindowWidth() {
    if (document.all) {
        return document.body.offsetWidth;
    } else {
        var wdth = window.getBoundingClientRect();
        return wdth.width;
    }
}

////////////////////////////////
//Browser and DOM detection  //
//usage: ua.w3cdom ua.ie etc//
/////////////////////////////

var ua = function() {
    var w3cdom = typeof doc.getElementById != 'undefined' &&
                 typeof doc.getElementsByTagName != 'undefined' &&
                 typeof doc.createElement != 'undefined' &&
                 typeof doc.appendChild != 'undefined' &&
                 typeof doc.replaceChild != 'undefined' &&
                 typeof doc.removeChild != 'undefined' &&
                 typeof doc.cloneNode != 'undefined';

    var u = nav.userAgent.toLowerCase(),
        p = nav.platform.toLowerCase(),
        webkit = /webkit/.test(u) ? parseFloat(u.replace(/^.*webkit\/(d+(\.\d+)?).*$/, "$1")) : false,
        ie = false,
        windows = p ? /win/.test(p) : /win/.test(u),
        mac = p ? /mac/.test(p) : /mac/.test(u);

    /*@cc_on
    ie = true;
    @if (@_win32)
    windows = true;
    @elif (@ mac)
        mac = true;
        @end
    @*/
    return { w3cdom: w3cdom, webkit: webkit, ie: ie, win: windows, mac: mac };
} ();

//////////////////////////////
//Wait for DOM loaded event//
////////////////////////////

var onDomLoad = function() {
    if (!ua.w3cdom) {
        return;
    }
    ///////////////////////////////////
    //////////////////////////////////
    ////Add OnLoad Function Loader///
    ////////////////////////////////
    addDomLoadEvent(jwInit); ///////
    //////////////////////////////
    /////////////////////////////

    if (ua.ie && ua.win) {
        try  {
            doc.write('<scr' + 'ipt id=__ie_ondomload defer=true src=//:></scr' + 'ipt>');
            var s = doc.getElementById('__ie_ondomload');
            if (s) {
                s.onreadystatechange = function() {
                    if (this.readyState == 'complete') {
                        this.parentNode.removeChild(this);
                        callDomLoadFunctions();
                    }
                };
            }
        } catch (e) { }
    }

    if (ua.webkit && typeof doc.readyState != 'undefined') {
        timer = setInterval(function() {
            if (/loaded|complete/.test(doc.readyState)) {
                callDomLoadFunctions();
            }
        }, 10);
    }

    if (typeof doc.addEventListener != 'undefined') {
        doc.addEventListener('DOMContentLoaded', callDomLoadFunctions, null);
    }

    addLoadEvent(callDomLoadFunctions);
} ();

//////////////////////////
//Add OnLoad functions //
//to Event Daisy Chain// 
///////////////////////    

function callDomLoadFunctions() {
    if (isDomLoaded) {
        return;
    }
    if (ua.ie && ua.win) {
        var s = doc.createElement('span');
        try {
            var t = doc.getElementsByTagName('body')[0].appendChild(s);
            t.parentNode.removeChild(t);
        } catch (e) { 
            return;
        }
    }
    isDomLoaded = true;
    if (timer) {
        clearInterval(timer);
        timer = null;
    }
    var dl = domLoadFnArr.length;
    for (var i = 0; i < dl; i++) {
        domLoadFnArr[i]();
    }
}

///////////////////////////////////
//Add DOM OnLoad Event          //
//If DOM not ready add to array//
////////////////////////////////

function addDomLoadEvent(fn) {
    if (isDomLoaded) {
        fn();
    } else {
        domLoadFnArr[domLoadFnArr.length] = fn;
    }
}

/////////////////////////////////////////
//Cross Browser Attach to OnLoad Event//
///////////////////////////////////////

function addLoadEvent(fn) {
    if (typeof win.addEventListener != 'undefined') {
        win.addEventListener('load', fn, false);
    }
    else if (typeof doc.addEventListener != 'undefined') {
        doc.addEventListener('load', fn, false);
    }
    else if (typeof win.attachEvent != 'undefined') {
        win.attachEvent('onload', fn);
    }
    else if (typeof win.onload == 'function') {
        var fnOld = win.onload;
        win.onload = function() {
            fnOld();
            fn();
        };
    }
    else {
        win.onload = fn;
    }
}

////////////////////////////////
//JANWebs Page Initialization//
//////////////////////////////
function jwInit() {
    if (!theForm) {
        theForm = document.frm1;
    }
    focusOnFirstTextBox();
    fixTargetBlanks();
    initVars();
}

///////////////////////////////////////
//OnLoad Set Focus To First Text Box//
/////////////////////////////////////

function focusOnFirstTextBox() {
    if (document.getElementById('noFirstFocus') == null) {
        for (f = 0; f < document.forms.length; f++) {
            for (i = 0; i < document.forms[f].length; i++) {
                if (document.forms[f][i].type == 'text') {
                    if (document.forms[f][i].type.disabled != true) {
                        if (document.forms[f][i].readOnly != true) {
                            try {
                                document.forms[f][i].focus();
                                break;
                            } catch (e) { }
                        }
                    }
                }
            }
        }
    } 
}

///////////////////////////////////////////////////////////////////
//OnLoad - Initialize Xhtml1.1 Strict external links            //
//Usage: html - replace all target="_blank" with rel="external"// 
////////////////////////////////////////////////////////////////

function fixTargetBlanks() {
    if (!document.getElementsByTagName) {
        return;
    } else {
        var anchors = document.getElementsByTagName('a');
        for (var i=0; i < anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') {
                anchor.target = '_blank';
            }
        }
    }
}

/////////////////////////////////////////////
//OnLoad - Initialize all global variables//
///////////////////////////////////////////

function initVars() {
    //Page Initialization
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// MAIN PAGE PROCESSING /////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// SUPPORTING FUNCTIONS ////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Alert Timeouts
function onAlertTimerDone() {
    //Alert Timeouts
}

//Timer for Alert Messages to turn off
function startAlertTimer() {
    setTimeout(onAlertTimerDone, 5000);
}

//General ClientSide Functions
function getTimeStamp() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var day = now.getDate();
    var hour = now.getHours();
    var min = now.getMinutes();
    var sec = now.getSeconds();
    var msec = now.getMilliseconds();
    month += 1;
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    if (hour < 10) {
        hour = '0' + hour;
    }
    if (min < 10) {
        min = '0' + min;
    }
    if (sec < 10) {
        sec = '0' + sec;
    }
    if (msec < 100) {
        if (msec < 10) {
            msec = '00' + msec;
        } else {
            msec = '0' + msec;
        }
    }
    return year.toString() + month.toString() + day.toString() + hour.toString() + min.toString() + sec.toString() + msec.toString();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// OBOUT VIDEO GRID //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function onBeforeClientUpdate(record) {
    var already = document.getElementById("tempInputFile");
    if (already != null) {
        already.parentNode.removeChild(already);
    } else {
        var el = document.getElementById(GetClientId("userFilePath"));
        if (el != null) {
            if (el.value.length > 0) {
                var ext = "";
                var str = el.value;
                var dotPos = str.lastIndexOf(".");
                var slashPos = str.lastIndexOf("/");
                var backSlashPos = str.lastIndexOf("\\");

                if (dotPos > slashPos && dotPos > backSlashPos) {
                    ext = str.substr(dotPos + 1).toLowerCase();
                }

                if (ext == "flv") {
                    var dt = new Date(record.date.toString());
                    var m = dt.getMonth() + 1;
                    m = (m < 10) ? '0' + m : m;
                    var d = dt.getDate();
                    d = (d < 10) ? '0' + d : d;
                    var y = dt.getFullYear().toString().substring(2, 4);
                    var fName = m + d + y + ' ' + record.setting.toLowerCase() + '.flv';

                    var recType = '';
                    var recId = '0';
                    if (record.id == undefined) {
                        recType = 'UploadNew - ';
                    } else {
                        recType = 'UploadExisting - ';
                        recId = record.id;
                    }
                    document.getElementById(GetClientId("serverResponse")).value = recType + 'ß' +
									   recId + 'ß' +
									   record.title + 'ß' +
									   record.setting + 'ß' +
									   record.caption + 'ß' +
									   record.date + 'ß' +
									   fName;
                } else {
                    alert("Invalid Flash (.flv) Video File");
                    return false;
                }
            } else {
                var updateType = '';
                var updateId = '0';
                if (record.id == undefined) {
                    updateType = 'UpdateNew - ';
                } else {
                    updateType = 'UpdateExisting - ';
                    updateId = record.id;
                }
                document.getElementById(GetClientId("serverResponse")).value = updateType + 'ß' +
                                    updateId + 'ß' +
									record.title + 'ß' +
									record.setting + 'ß' +
									record.caption + 'ß' +
									record.date + 'ß';
            }
        }
    }
    return true;
}

function onRecordSelect(arySelectedRecords) {
    for (var i = 0; i < arySelectedRecords.length; i++) {
        var record = arySelectedRecords[i];
        ASPNetMedia.FlashVideo("flvServices").StopMedia();
        ASPNetMedia.FlashVideo("flvServices").PlayMedia(record.videoUrl);
        var newVideoCaption = (record['caption'] != null) ? record['caption'].toString() : '';
        setInnerText(document.getElementById('videoCaption'), newVideoCaption);
    }
}

function onClientDelete(record) {
    document.getElementById(GetClientId("serverResponse")).value = 'Delete - ' + record.id;
    return true;
}

function onClientCancel(record) {
    var el = document.getElementById(GetClientId("userFilePath"));
    if (el != null) {
        if (el.value.length > 0) {
            //alert("Video Upload Cancel");
        }
    }
    document.getElementById(GetClientId("serverResponse")).value = 'Cancel - ' + record.id;
    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Must always be at the end of each .js file to notify server that the script load completed.//////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
