/**
 * Карта с выбором округов Москвы
 *
 * @property jQuery[] nodesLinks
 *           ноды с надписями-ссылками округов
 * @property jQuery[] nodesAreas
 *           ноды Area (на них ловится наведение мыши)
 * @property jQuery[] nodesImages
 *           ноды изображений с подсветкой региона
 * @property ItemClass{} items
 *           объекты округов. Название => объект
 * @property ItemClass current
 *           текущий округ
 * @property function[] listeners
 *           слушатели события выбора округа
 */
CalcClass.MapClass = go.Class({
    
    /**
     * Конструктор
     *
     * @param mixed nodesLinks
     * @param mixed nodesAreas
     * @param mixed nodesImages
     */
    '__construct': (function(nodesLinks, nodesAreas, nodesImages) {
        this.nodesLinks  = $(nodesLinks);
        this.nodesAreas  = $(nodesAreas);
        this.nodesImages = $(nodesImages);
        this.loadItems();
        this.current   = null;
        this.listeners = [];
    }),
    
    /**
     * Создание объектов округов
     */
    'loadItems': (function() {
        var secs = {};
        var len  = this.nodesLinks.length;
        for (var i = 0; i < len; i++) {
            var link = this.nodesLinks.eq(i);
            secs[link.attr("rel")] = {"link": link};
        }
        for (var i = 0; i < len; i++) {
            var area = this.nodesAreas.eq(i);
            secs[area.attr("rel")]["area"] = area;
        }
        for (var i = 0; i < len; i++) {
            var image = this.nodesImages.eq(i);
            secs[image.attr("class")]["image"] = image;
        }
        
        this.items = {};
        for (var name in secs) {
            var sec = secs[name];            
            this.items[name] = new this.ItemClass(this, name, sec.link, sec.area, sec.image);
        }
    }),
    
    /**
     * Событие: выбран округ
     */
    'onSelect': (function(item) {
        if (this.current) {
            if (this.current == item) {
                return;
            }
            this.current.unselect();
        }
        this.current = item;
        this.runListeners();
    }),
    
    /**
     * Добавить слушателя события выбора округа
     *
     * @param function listener
     */
    'addEventListener': (function(listener) {
        this.listeners.push(listener);
    }),
    
    /**
     * Запуск всех слушателей события
     */
    'runListeners': (function() {
        var name      = this.current.name;
		
		if(name == "co2")
			var inttk =  1;
		else if(name == "co")
			var inttk =  2;
		else
			var inttk =  0;
			
        var listeners = this.listeners;
        var len       = listeners.length;
        for (var i = 0; i < len; i++) {
            listeners[i](name, inttk);
        }
    }),
    
    /**
     * Класс отдельного округа
     *
     * @property ClassMap map
     *           карта которой принадлежит
     * @property string name
     *           название округа
     * @property jQuery nodeLink
     *           нода ссылки
     * @property jQuery nodeArea
     *           нода area
     * @property jQuery nodeImage
     *           нода изображения подсветки
     * @property bool selected
     *           округ выбран
     * @property bool highlighted
     *           округ подсвечивается
     */
    'ItemClass': go.Class({
    
        /**
         * Конструктор
         *
         * @param ClassMap map
         *        карта которой принадлежит
         * @param string name
         *        название округа
         * @param jQuery nodeLink
         *        нода ссылки
         * @param jQuery nodeArea
         *        нода AREA
         * @param jQuery nodeImage
         *        нода изображения подсветки
         */
        '__construct': (function(map, name, nodeLink, nodeArea, nodeImage) {
            this.map         = map;
            this.name        = name;
            this.nodeLink    = $(nodeLink);
            this.nodeArea    = $(nodeArea);
            this.nodeImage   = $(nodeImage);
            this.selected    = false;
            this.hidhlighted = false;
            this.colored     = false;
            this.nodeLink.click(this.onClick);
            this.nodeImage.click(this.onClick);
            this.nodeArea.mouseenter(this.onMouseEnter);
            this.nodeLink.mouseenter(this.onMouseEnter);
            this.nodeImage.mouseenter(this.onMouseEnter);            
            this.nodeArea.mouseleave(this.onMouseLeave);
            this.nodeLink.mouseleave(this.onMouseLeave);
            this.nodeImage.mouseleave(this.onMouseLeave);            
        }),
        
        /**
         * Выбрать текущий округ
         */
        'select': (function() {
            if (!this.selected) {
                this.selected = true;
                this.draw();
            }
        }),
        
        /**
         * Отменить выбор текущего округа
         */
        'unselect': (function() {
            if (this.selected) {
                this.selected = false;
                this.draw();
            }
        }),
        
        /**
         * Подсветить округ
         */
        'highlight': (function() {
            if (!this.highlighted) {
                this.highlighted = true;
                this.draw();
            }
        }),
        
        /**
         * Снять подсветку округа
         */
        'unhighlight': (function() {
            if (this.highlighted) {
                this.highlighted = false;
                this.draw();
            }
        }),
        
        /**
         * Отрисовать всё что нужно в связи с данным округом
         */
        'draw': (function() {
            var colored = (this.selected || this.highlighted);
            if (colored == this.colored) {
                return;
            }
            this.colored = colored;
            if (colored) {
                this.nodeLink.addClass("current");
                this.nodeImage.show();
            } else {
                setTimeout(this.onUncolored, 50);
            }
        }),
        
        /**
         * Обработка наведения мыши на округ
         */
        'onMouseEnter_bind': (function(e) {
            this.highlight();
        }),
        
        /**
         * Обработка увода мыши с округа
         */
        'onMouseLeave_bind': (function(e) {
            this.unhighlight();
        }),
        
        /**
         * Обработка щелчка на округе
         */
        'onClick_bind': (function(e) {
            if (!this.selected) {
                this.select();
                this.map.onSelect(this);                
            }
            e.preventDefault();
        }),
    
        'onUncolored_bind': (function(e) {
            if (!this.colored) {
                this.nodeLink.removeClass("current");
                this.nodeImage.hide();            
            }
        }),
        
        'eoc': null
    }),
    
    'eoc': null
});

