﻿if(typeof(EdgControl) == 'undefined')
{
    var EdgControl = window.EdgControl = Class.extend({
        //properties
        data: new Object(),
        fields: new Object(),
        watchedFields: new Object(),
        container: new Object(),
        containerId: new String(),
        errorDivId: "",

        //methods
        init: function(containerId) {
            this.containerId = containerId;
            this.container = jQuery('#' + this.containerId);
            this.Page = jQuery(document).data('pageController');

            if (!(this.Page) || (typeof (this.Page) == 'undefined')) {
                this.Page = new EdgPage();
            }

            if ((this.Page) && (typeof (this.Page.ComponentAdd) != 'undefined')) {
                this.Page.ComponentAdd(this);
            }
        },
        log: function(data) {
            this.Page.log(arguments);
        },

        // Be nice! Tell everyone what they can listen for here.
        // Accepts a string or array of events provided.
        ProvidesEvent: function(events) {
            if (typeof (events) == 'object') {
                for (var evt in events) {
                    this.Page.AddTrigger(events[evt]);
                }
            }
            else if (typeof (events) == 'string') {
                this.Page.AddTrigger(events);
            }
        },

        // So your component can call this.Trigger
        // rather than having a reference to the Page itself.
        Trigger: function(evt, args) {
            this.log("Triggering " + evt);
            this.Page.Trigger(evt, args);
        },
        isEmpty: function(item) {
            return ((typeof (item) == "undefined") || (item == '') || (item == null));
        },

        // Expects the return of an AjaxResultHelper
        // and maybe a callback function if you want
        // to process the result.Data
        ReDraw: function(data, callback) {
            var result = data.d;

            if (result.HTMLResult) {
                if (result.DivToReplace) {
                    $('#' + result.DivToReplace).get(0).innerHTML = result.HTMLResult;
                }
                else {
                    this.container.get(0).innerHTML = (result.HTMLResult);
                }
            }

            if (result.JSEval) {
                eval(result.JSEval);
            }

            if (result.Data)
                if (typeof (callback == 'function'))
                callback.apply(result.Data);

        },
        IsValid: function() {
            return true;
        },
        ErrorDiv: function() {
            var errDiv;
            if (typeof (this.errorDivId) != 'undefined') {
                errDiv = $('[id$=' + this.errorDivId + ']');
            }
            return $(errDiv);
        },
        SetError: function(msg) {
            var foundDiv = this.ErrorDiv();
            if (foundDiv.length > 0) {
                foundDiv.html(msg).addClass('error').show();
                $.scrollTo(foundDiv);
            }
            else {
                this.Page.SetError(msg);
            }
            $.unblockUI();
        },
        HideError: function() {
            var foundDiv = this.ErrorDiv();
            if (foundDiv.length > 0) {
                foundDiv.hide();
            }
            else {
                this.Page.HideError();
            }
        },
        BlockPage: function(wrapMsg) {
            this.Page.BlockPage.apply(this.Page, [wrapMsg]);
        },
        Alert: function(msg, showButton) {
            this.Page.Alert.apply(this.Page, [msg, showButton]);
        }
    });
}
