﻿var core = {
    init: function() {
        this.ua = navigator.userAgent.toLowerCase();
        this.ws = "wonderbutton.asmx";
        this.isDebug = true;
    },
    getData: function(targetUrl, method, params, format, handler, elem, msg) {
        $.ajax({
            type: method,
            url: targetUrl,
            data: params,
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                handler(data);
            },
            error: function(request, status, error) {
                //alert(handler + ' ' + request.responseText);
            }
        });
    },
    getObjectData: function(result, test) {
        var data = $.parseJSON(result.d);
        switch (test) {
            case 'length':
                if (data.length > 0) { return data; }
                break;
            case 'null':
                if (data != null) { return data; }
                break;
            default:
        }
    },

    buildModal: function(id, innertext) {
        try {
            var s = '<div id="' + id + 'modalscreen" class="modalscreen" onclick="gmh.closeWin(\'' + id + '\');"></div>';
            s += '<div id="' + id + 'modal" class="modalcontainer modalrounded"></div>';
            s += '<div id="' + id + 'modalmain" class="modalmain modalrounded">';
            s += '<div id="' + id + 'modalheader" class="modalheader modalrounded">';
            s += '<span id="' + id + 'modaltitle" class="modaltitle"></span>';
            s += '<span id="' + id + 'modalquit" class="modalquit" onclick="gmh.closeWin(\'' + id + '\');">';
            s += '<img alt="quit" src="res/img/quit.png" /></span></div>';
            s += '<div id="' + id + 'modalcontent" class="modalcontent modalrounded">';
            s += '<div id="' + id + 'modalhelp"></div><div id="' + id + 'modalinner">';
            s += '<span style="color:#3f3f3f;width:100%;display:block;">';
            s += innertext;
            s += '</span></div></div></div>';
            var d = document.createElement('div');
            d.setAttribute('id', id);
            var p = document.getElementById('popups');
            p.appendChild(d);
            d.innerHTML = s;
        } catch (e) {
            this.showError(e);
        }
    },
    loadModal: function(w, h, t, te,s) {
        try {
            var hscroll = (document.all ? document.scrollLeft : window.pageXOffset);
            var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
            var sh = screen.height; //this.getBrowserHeight();
            var sw = screen.width; //this.getBrowserWidth();
            $('#' + t + 'modal').css("width", (parseFloat(w) + 5) + "px");
            $('#' + t + 'modal').css("height", (parseFloat(h) + 5) + "px");
            $('#' + t + 'modal').css("top", (vscroll + (sh - parseFloat(h))-s) + "px");
            $('#' + t + 'modalmain').css("width", (parseFloat(w) - 5) + "px");
            $('#' + t + 'modalmain').css("height", (parseFloat(h) - 5) + "px");
            $('#' + t + 'modalmain').css("top", (vscroll + (sh - parseFloat(h))-s) + "px");
            $('#' + t + 'modalheader').css("width", (parseFloat(w) - 5) + "px");
            $('#' + t + 'modal').css("marginLeft", (-Math.round((parseFloat(w) + 10) / 2)) + "px");
            $('#' + t + 'modal').css("marginTop", (-Math.round((parseFloat(h) + 10) / 2)) + "px");
            $('#' + t + 'modalmain').css("marginLeft", (-Math.round(parseFloat(w) / 2)) + "px");
            $('#' + t + 'modalmain').css("marginTop", (-Math.round(parseFloat(h) / 2)) + "px");
            $('#' + t + 'modalcontent').css("height", (parseFloat(h) - 50) + "px");
            $('#' + t + 'modalinner').css("height", (parseFloat(h) - 50) + "px");
            if (te.length > 0) { $('#' + t + 'modaltitle').html(te); } else { $('#' + t + 'modaltitle').html(t); }
            $('#' + t + 'modalscreen').css("top", vscroll + "px");
            $('#' + t + 'modalscreen').show();
            $('#' + t + 'modal').show();
            $('#' + t + 'modalmain').show();
        } catch (e) {
            this.showError(e);
        }
    },
    getBrowserHeight: function() {
        var winH = 0;
        if (document.body && document.body.offsetWidth) {
            winH = document.body.offsetHeight;
        }
        if (document.compatMode == 'CSS1Compat' &&
            document.documentElement &&
            document.documentElement.offsetWidth) {
            winH = document.documentElement.offsetHeight;
        }
        if (window.innerWidth && window.innerHeight) {
            winH = window.innerHeight;
        }
        return winH;
    },
    getBrowserWidth: function() {
        var winW = 0;
        if (document.body && document.body.offsetWidth) {
            winW = document.body.offsetWidth;
        }
        if (document.compatMode == 'CSS1Compat' &&
            document.documentElement &&
            document.documentElement.offsetWidth) {
            winW = document.documentElement.offsetWidth;
        }
        if (window.innerWidth && window.innerHeight) {
            winW = window.innerWidth;
        }
        return winW;
    },
    closeWin: function(s) {
        try {
            $('#' + s + 'modalscreen').hide();
            $('#' + s + 'modal').hide();
            $('#' + s + 'modalmain').hide();
            $('#' + s).remove();
            $('.tooltip').hide();
        } catch (e) {
            this.showError(e);
        }
    },
    showError: function(e) {
        alert(e);
    }
};
