﻿Type.registerNamespace('DDTI.Web.Map.Legend');

DDTI.Web.Map.Legend.DTLegendComponent = function(element) { 
    DDTI.Web.Map.Legend.DTLegendComponent.initializeBase(this, [element]);
    this._activeLayerIndex = null;
}

DDTI.Web.Map.Legend.DTLegendComponent.prototype = {
    initialize : function() {
        DDTI.Web.Map.Legend.DTLegendComponent.callBaseMethod(this, 'initialize');
        this._onextentChangeHandler = Function.createDelegate(this, DDTI.Web.Map.Viewer.DTViewerComponent.prototype._onLoad);
        $addHandlers ( $get('ctl00_ContentPlaceHolder1_MainMap'), { 'load' : this._onextentChange }, this );
    },

    dispose : function() {
        $clearHandlers(this.get_element());
        DDTI.Web.Map.Legend.DTLegendComponent.callBaseMethod(this, 'dispose');
    },
    
    getElementsByClassName : function (strClassName, obj) {
        var ar = arguments[2] || new Array();
        var re = new RegExp("\\b" + strClassName + "\\b", "g");

        if (re.test(obj.className)) {
            ar.push( obj );
        }
        for (var i = 0; i < obj.childNodes.length; i++)
            this.getElementsByClassName(strClassName, obj.childNodes[i], ar);
        
        return ar;
    },
    
    get_activeLayerIndex : function() {
        return this._activeLayerIndex;
    },
    
    set_activeLayerIndex : function(value) {
        if (this._activeLayerIndex !== value) {
            this._activeLayerIndex = value;
            this.raisePropertyChanged('activeLayerIndex');
        }
    },
    
    _onextentChange : function() {
        var ar = this.getElementsByClassName('layervisibility', document.body);
        var ars = new Array();
        for (var i = 0; i < ar.length; i++) {
            ars.push(ar[i].id);
        }
        PageMethods.LayerStatus(ars, this._onLayerStatusSuccess);
    },
    
    _onLayerStatusSuccess : function(result) {
        for (var i = 0; i < result.length; i++) {
            $get(result[i].Element).checked = result[i].LayerOn;
            $get(result[i].Element).disabled = !result[i].LayerWithinVisibleExtent;
        }
    },
    
    changeActiveLayer : function(element, layerIndex) {
        var ar = this.getElementsByClassName('layer', document.body);
        for (var i = 0; i < ar.length; i++) {
            ar[i].style.backgroundColor = 'transparent';
            ar[i].style.border = '1px solid transparent';
        }

        PageMethods.SetActiveLayerIndex(layerIndex)
        $get(element).style.backgroundColor = '#CED7DE';
        $get(element).style.border = '1px solid #6B8694';
        
        this.set_activeLayerIndex(layerIndex);
    }
}

DDTI.Web.Map.Legend.DTLegendComponent.registerClass('DDTI.Web.Map.Legend.DTLegendComponent', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();