﻿var _siteDirectory = '';
var _sPostPage = '';


function failureHandler(data) {
    alert('error: ' + data.responseText);
    setTimeout(function() { stopWaitCursor() }, 500);
    $.unblockUI();
}

function successHandler(data) {
    jslog.debug('right menu button asyn return');
    jslog.debug('current browser sequence number: ' + iCurrentSeqNum);

    var d = JSON.parse(data);
    jslog.debug('packet -- controlId: ' + d._controlId);
    jslog.debug('packet -- command: ' + d._command);
    jslog.debug('packet -- commandArgument: ' + d._commandArgument);
    jslog.debug('packet -- seqNum: ' + d._seqNum);
    // do we process packet
    var iSeqNum = parseInt(d._seqNum);
    if (iSeqNum == iCurrentSeqNum) {
        // we do process
        jslog.debug('process ' + iSeqNum);

        switch (d._command) {
            case 'resource_popup':
                // keep this separate I recommend, it's different to ui blocking callbacks
                var menuCallout = $('div.rightmenucallout_content_area');
                menuCallout.css('visibility', 'hidden');
                menuCallout.css('height', 'auto');

                processDOM(d);

                $('div#rightmenucallout_description').css('color', 'white');

                var scrollHeight = 0;
                $('div#rightmenucallout_content').each(function() { scrollHeight += this.scrollHeight; });
                $('#rightmenucallout_arrow').css('top', scrollHeight + 2);

                menuCallout.css('visibility', 'visible');

                break;

            default:
                processDOM(d);

        } // end switch        
    } else {
        // we don't process
        jslog.debug('ignore ' + iSeqNum);
    }
};

function processDOM(d) {
    var pageSection = "";
    var item = "";
    var varHtml = JSON.parse(d._html);
    for (pageSection in varHtml) {
        $(convertToJQuery(pageSection)).html(varHtml[pageSection]);
    }
    var varText = JSON.parse(d._text);
    for (pageSection in varText) {
        item = convertToJQuery(pageSection);
        if ((item.length > 4) && (item.substring(0, 4) == "jcmd")) {
            eval(varText[pageSection]);
        }
        else if (item == "setMenu") {
            selectMenuButton(undefined, varText[pageSection]);
        }
        else {
            $(item).text(varText[pageSection]);
        }
    }

    var varFlash = JSON.parse(d._flash);
    for (pageSection in varFlash) {
        var varFlashProps = varFlash[pageSection];
        PlayFlashVideo(pageSection, varFlashProps.FileName, varFlashProps.Width, varFlashProps.Height);
    }

    if (d._blockingProcess == "1") {
        var stopWaitCursorDelay = 100;
        adjustBodyHeight();
        if ($.browser.msie && $.browser.version < 7) {
            $('div#contentwrapper').height($('div#mainarea').height() + 'px');
        } else {
            $('div#contentwrapper').css('height', 'auto');
            var mainHeight = $('div#mainarea').height();
            if ($('div#contentwrapper').height() < mainHeight) {
                $('div#contentwrapper').height(mainHeight + 'px');
            }
        }
        setTimeout(function() { stopWaitCursor() }, stopWaitCursorDelay);
        $.unblockUI();
        SetPageLocation();
    }
    setTimeout(function() { updateTitle() }, 100);
}

function updateTitle() {
    if (typeof (windowtitle) !== 'undefined') {
        document.title = windowtitle;
    }
}


function convertToJQuery(str) {
    return str.replace(/__period__/, ".").replace(/__hash__/, "#");
}

function doAjaxFormEx(cmd, selector, params) {
    doFormPost(cmd, '#' + selector + " *", params, true, successHandler, failureHandler);
}

function doAjaxForm(frm) {
    doFormPost(frm, '#' + frm + " *", '', true, successHandler, failureHandler);
}


function getPostPage() {
    if (_sPostPage === '') {
        if (_siteDirectory !== '') {
            _sPostPage = document.location.protocol + '//' + document.location.host + '/' + _siteDirectory + '/default.aspx'
        } else {
            _sPostPage = document.location.protocol + '//' + document.location.host + '/default.aspx'
        }
    }
    return _sPostPage;
}

function doFormPost(functionName, selector, parameters, bIsBlocking, successHandler, failureHandler) {

    // for this function to work - ALL INCLUDED FORM FIELDS MUST HAVE A NAME ATTRIBUTE       
    $.ajax({
        type: 'POST',
        url: getPostPage(),
        data: { isAsync: '1',
            method: 'ajaxFormPost',
            command: functionName,
            commandArgument: parameters,
            selectorAsJson: JSON.stringify($(selector).serializeArray()),
            seqNum: ++iCurrentSeqNum,
            isBlocking: bIsBlocking
        },
        success: successHandler,
        error: failureHandler
    });
}

function doAjaxPost(controlId, command, commandArgument, successHandler, failureHandler) {
    jslog.debug('async call [' + (iCurrentSeqNum + 1) + '] ' + command + ':' + controlId);
    $.ajax({
        type: 'POST',
        url: getPostPage(),
        data: { isAsync: '1',
            method: 'post',
            command: command,
            commandArgument: commandArgument,
            controlId: controlId,
            seqNum: ++iCurrentSeqNum
        },
        success: successHandler,
        error: failureHandler
    });
}
