﻿function BaseObject()
{
    this._baseUrl = '';
    this._key1 = 0;
    this._key2 = 0;
    this._dialogContainer = '#dialog';
    this._dialogHeight = 520;
    this._dialogWidth = 700;
}

BaseObject.prototype.Init = function()
{
    $(this._dialogContainer).dialog({
        autoOpen: false,
        modal: true,
        height: this._dialogHeight,
        width: this._dialogWidth,
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    });
}

BaseObject.prototype.DtoRequest = function(type)
{
    var request = {};
    if (arguments.length >= 1)
        request.type = type;
    request.key1 = this._key1;
    request.key2 = this._key2;
    request.key3 = 0;
    request.data1 = '';
    request.data2 = '';
    request.data3 = '';
    return request;
}

BaseObject.prototype.DtoRender = function(request)
{
    var dto = { 'request': request };
    return JSON.stringify(dto);
}

BaseObject.prototype.Load = function(request, type, container, callback)
{
    var req = this.DtoRequest(type);

    this.LoadControl(
        this._baseUrl + request,
        this.DtoRender(req),
        container,
        container,
        callback
        );
}

BaseObject.prototype.Action = function(type, data, container, callback)
{
    var req = this.DtoRequest(type);
    req.data1 = data;

    this.Post(
        'SaveAction',
        this.DtoRender(req),
        null,
        callback,
        { type: type }
        );
}

BaseObject.prototype.ShowDialog = function(title, request, req, callbackSuccess)
{
    var $dlg = $(this._dialogContainer);
    $dlg.html('Loading...');

    $dlg.dialog('option', 'title', title);
    $dlg.show().dialog('open');

    this.LoadControl(
        this._baseUrl + request,
        this.DtoRender(req),
        this._dialogContainer,
        this._dialogContainer,
        callbackSuccess
        );
}

BaseObject.prototype.Post = function(request, data, statusEl, callbackSuccess, callbackArgs)
{
    var handle = this;
    var hasStatus = arguments.length >= 3 && statusEl != null;
    var hasCallSuccess = arguments.length >= 4 && callbackSuccess != null;
    var hideStatus = false;

    if (hasStatus)
    {
        $(statusEl).addClass('loading');
        hideStatus = $(statusEl).hasClass('dn')
        if (hideStatus)
            $(statusEl).removeClass('dn');
    }

    $.ajax({
        type: "POST",
        url: handle._baseUrl + request,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg)
        {
            if (hasStatus)
                $(statusEl).removeClass('loading');
            if (hideStatus)
                $(statusEl).addClass('dn');
            if (hasCallSuccess)
                callbackSuccess(handle, msg, callbackArgs);
        },
        error: function(xhr, status, error)
        {
            if (hasStatus)
            {
                $(statusEl).removeClass('loading');
                $(statusEl).html('An error occured, please refresh page.');
            }
            else
            {
                alert('An error occured, please refresh page.');
            }
            //alert(xhr.responseText);
        }
    });
}

BaseObject.prototype.LoadControl = function(url, data, progessEl, resultEl, callbackSuccess, callbackArgs)
{
    var handle = this;
    var hasStatus = arguments.length >= 3 && progessEl != null && progessEl.length > 0;
    var hasCallSuccess = arguments.length >= 5 && callbackSuccess != null;
    var loadClass = (progessEl == resultEl) ? 'loadingdisable' : 'loading';

    if (hasStatus)
        $(progessEl).addClass(loadClass);

    $(resultEl).show();

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg)
        {
            if (hasStatus)
                $(progessEl).removeClass(loadClass);
            $(resultEl).html(msg.d);
            $(".round").corner('10px');
            if (hasCallSuccess)
                callbackSuccess(handle, callbackArgs);
        },
        error: function(xhr, status, error)
        {
            if (hasStatus)
                $(progessEl).removeClass(loadClass);
            $(resultEl).html('An error occured, please refresh page.');
            // todo: log xhr.responseText
        }
    });
}