﻿/// <reference name="MicrosoftAjax.js" />
var filterToggles=[[],[],[],[],[],[]];
function toggleFilterValue(index, valueindex) {
    if (Array.contains(filterToggles[index], valueindex)) {
        Array.remove(filterToggles[index], valueindex);
        Sys.UI.DomElement.removeCssClass($get("cond" + valueindex, $get("listFilterFilter" + index)), "selectedValue");
    }
    else {
        Array.add(filterToggles[index], valueindex);
        Sys.UI.DomElement.addCssClass($get("cond" + valueindex, $get("listFilterFilter" + index)), "selectedValue");
    }
    updateFilter();
    return false;
}

function updateFilter(instantUpdate, map) {
    if (instantUpdate == null)
        instantUpdate = true;
    var values = [];
    var whereClause="";
    var isFirst=true;
    var filters = ListFilterModule.filters;
    var filterEmpty = true;
    for (var i = 0; i < filterToggles.length; i++) {
        if (filterToggles[i].length > 0)
            filterEmpty = false;
        values.push(filterToggles[i].join(","));
        if (filterToggles[i].length > 0) {
            if (!isFirst) { whereClause += " AND "; }
            isFirst = false;
            whereClause += "(";
            var isFirstVal = true;
            for (var j = 0; j < filterToggles[i].length; j++) {
                if (!isFirstVal) { whereClause += " OR "; }
                isFirstVal = false;
                whereClause += String.format(filters[i].attributeName, filters[i].values[filterToggles[i][j]]);
            }
            whereClause+=")"
        }
    }
    if (!filterEmpty) {
        document.location.hash = values.join(";");
    }
    else {
        document.location.hash = "";
    }
    setMapLoading(true);
    if (map == null) {
        map = Nukleus && Nukleus.Profile && Nukleus.Profile.Current ? Nukleus.Profile.Current.map : null;
    }
    if (map != null) {
        map.addSetLayerWhereClause("lay_proj_pnt_view", whereClause);
        map.addSetLayerWhereClause("lay_proj_lin_view", whereClause);
        map.addSetLayerWhereClause("lay_proj_plg_view", whereClause);
        if (whereClause != "") {
            map.addZoomToFilter("lay_proj_pnt_view", null);
            map.addZoomToFilter("lay_proj_lin_view", null);
            map.addZoomToFilter("lay_proj_plg_view", null);
        }
        else {
            map.addZoomToLayer("lay_proj_pnt_view");
            map.addZoomToLayer("lay_proj_lin_view");
            map.addZoomToLayer("lay_proj_plg_view");
        }
        if (instantUpdate)
            map.executeCommandQueue(false);
    }
    showFilteredProjects(genericListModuleId, 0, ListFilterModule.id, values[0], values[1], values[2], values[3], values[4], values[5]);
}
if (Nukleus!=null && Nukleus.Map!=null) {
    Nukleus.Map.addOnBeforeFirstRequest(function (map) {
        if (document.location.hash != null && document.location.hash.length > 1) {
            var hash = document.location.hash.substring(1);
            var arr = hash.split(';');
            if (arr.length == filterToggles.length) {
                for (var i = 0; i < arr.length; i++) {
                    var vals = arr[i].split(',');
                    if (vals == null || (vals.length == 1 && vals[0] == "")) {
                        continue;
                    }
                    for (var j = 0; j < vals.length; j++) {
                        vals[j] = parseInt(vals[j]);
                        Sys.UI.DomElement.addCssClass($get("cond" + vals[j], $get("listFilterFilter" + i)), "selectedValue");
                    }
                    filterToggles[i] = vals;
                }
            }
        }
        map.addClearSelection();
        updateFilter(false, map);
    });
}
else {
    updateFilter(false);
}

dojo.provide("Nukleus.Controls.NListScroller");
//dojo.require("dijit._Widget");
//dojo.require("dijit._Templated");
//dojo.require("dojo.fx");
dojo.declare("Nukleus.Controls.NListScroller", [dijit._Widget, dijit._Templated], {
    templateString: "<div tabIndex=\"0\" dojoAttachEvent=\"onkeypress: _handleKey, onfocus: _a11yStyle, onblur:_a11yStyle\" class=\"${orientation}PagerContainer\">\n"
                        + "<div tabIndex=\"0\" dojoAttachPoint=\"pagerPrevious\" class=\"listScrollerPrevious\" dojoAttachEvent=\"onmousedown: _scrollBack\"><img dojoAttachPoint=\"pagerIconPrevious\" src=\"${iconPrevious}\" alt=\"Previous\" /></div>\n\t\t"
                        + "<div class=\"listScrollerContainer\" dojoAttachPoint=\"listScrollerContainer\">\n\t\t"
                            + "<div dojoAttachPoint=\"listScrollerContainerView\" class=\"${orientation}LSView\">\n\t\t"
                                + "<div dojoAttachPoint=\"listScrollerItemContainer\"><ul dojoAttachPoint=\"containerNode\" class=\"listScrollerItems\"></ul></div>\n\t\t"
                            + "</div>\n\t\t"
                        + "</div>\n\t"
                        + "<div tabIndex=\"0\" dojoAttachPoint=\"pagerNext\" class=\"listScrollerNext\" dojoAttachEvent=\"onmousedown: _scrollForward\"><img dojoAttachPoint=\"pagerIconNext\" src=\"${iconNext}\" alt=\"Next\" /></div>\n\t\t\t"
                    + "</div>\n",
    store: null,
    orientation: "horizontal",
    pagerPos: "center",
    duration: 500,
    scrollStep: 200,
    resizeChildren: true,
    postMixInProperties: function () {
        var h = (this.orientation == "horizontal");
        dojo.mixin(this, {
            _currentOffset: 0,
            dirClass: "pager" + (h ? "Horizontal" : "Vertical"),
            iconNext: Nukleus.NApplicationUrl + "/Scripts/Nukleus/Controls/images/NListScroller/" + (h ? "h" : "v") + "Next.png",
            iconPrevious: Nukleus.NApplicationUrl + "/Scripts/Nukleus/Controls/images/NListScroller/" + (h ? "h" : "v") + "Previous.png"
        });
    },
    postCreate: function () {
        this.inherited(arguments);
        this._init();
    },
    _a11yStyle: function (e) {
        dojo[(e.type == "focus" ? "addClass" : "removeClass")](e.target, "pagerFocus");
    },
    _handleKey: function (e) {
        var dk = dojo.keys;
        var _1 = (e.charCode == dk.SPACE ? dk.SPACE : e.keyCode);
        switch (_1) {
            case dk.UP_ARROW:
            case dk.RIGHT_ARROW:
            case 110:
            case 78:
                e.preventDefault();
                this._pagerNext();
                break;
            case dk.DOWN_ARROW:
            case dk.LEFT_ARROW:
            case 112:
            case 80:
                e.preventDefault();
                this._pagerPrevious();
                break;
            case dk.ENTER:
                switch (e.target) {
                    case this.pagerNext:
                        this._pagerNext();
                        break;
                    case this.pagerPrevious:
                        this._pagerPrevious();
                        break;
                }
                break;
        }
    },
    _init: function () {
        this._renderView();
        //this._renderPager();
    },
    _renderView: function () {
        var _container = this.listScrollerContainer;
        var _isHor = (this.orientation == "horizontal");
        if (_isHor) {
            var _h = dojo.style(this.listScrollerContainerView, "height");
            var _w = dojo.style(this.domNode, "width") - 28;
            dojo.style(_container, "height", _h + "px");
            dojo.style(_container, "width", _w + "px");
            var _viewWidth = dojo.style(this.listScrollerContainerView, "width");
            if (_w >= _viewWidth) {
                this.pagerIconPrevious.style.display = "none";
                this.pagerIconNext.style.display = "none";
            }
        }
        dojo.parser.parse(this.containerNode);
    },

    _scrollForward: function () {
        var _viewWidth = dojo.style(this.listScrollerContainerView, "width");
        var _containerWidth = dojo.style(this.listScrollerContainer, "width");
        if (_viewWidth - this._currentOffset - _containerWidth >= this.scrollStep)
            this._scroll(this.scrollStep);
        else if (_viewWidth - this._currentOffset - _containerWidth > 0)
            this._scroll(_viewWidth - this._currentOffset - _containerWidth);

    },
    _scrollBack: function () {
        if (this._currentOffset >= this.scrollStep)
            this._scroll(this.scrollStep * -1);
        else if (this._currentOffset > 0)
            this._scroll(this._currentOffset * -1);
    },

    _scroll: function (offset) {
        if (this._anim) {
            return;
        }
        this._currentOffset += offset;
        this._anim = dojo.fx.slideTo({
            node: this.listScrollerContainerView,
            left: -1 * this._currentOffset,
            duration: this.duration
        });
        var _33 = this.connect(this._anim, "onEnd", function () {
            delete this._anim;
            this.onScrollEnd();
            this.disconnect(_33);
        });
        this._anim.play();
    },
    onScrollEnd: function () { }
});




