/**** /js/moikrug/ui/GroupSelectItem.js ****/
Lang.module('moikrug.ui.GroupSelectItem');


moikrug.ui.GroupSelectItem = Lang.createClass(Widget, {
    init: function(node, options) {
        this.baseConstructor(node, options);

        this.isSelected = false;
        this.id = '';
        this.name = '';
        
        this.noHighlight = this.options.noHighlight || false;
        this.options = options;
    },

    parse: function() {
        this.control = this.node.getElementsByTagName('input')[0];
        if(!this.control) {
            this.disabled = true;
            return this;
        }
        
        this.id = (this.node.id + '').match(/(\d+)/)[1];
        this.name = this.control.name;
        this.isSelected = !!this.control[typeof this.control.checked == 'undefined' ? 'defaultChecked' : 'checked'];
		
        if(this.isSelected != this.node.hasClassName(this.options.checkedClass)) {
            Element.Methods[this.isSelected ? 'addClassName' : 'removeClassName'](this.node, this.options.checkedClass);
        }
        this.disabled = this.control.disabled;
        return this;
    },

    setSelected: function(state, skipEvent) {
        if(this.isSelected == state) {
            return;
        }
        this.isSelected = state;
        if (!this.nHighlight) {
    		Element.Methods[state ? 'removeClassName' : 'addClassName'](this.node, this.options.uncheckedClass);
    		Element.Methods[state ? 'addClassName': 'removeClassName'](this.node, this.options.checkedClass);
        }
        this.control.checked = state;
        if (!skipEvent) {
        	this.onUpdate(this);
        }
    },

    bindEvents: function() {
        if(!this.control) {
        	return this;
        }
        Event.observe(this.control, 'click', this.onControlClick.bindAsEventListener(this));
		return this;    
    },

    onControlClick: function(event) {
        var state = this.control.checked;
        this.setSelected(state);
    },

    setDisabled: function(state) {
        this.control.disabled = state;
    },

    onUpdate: function(self) {}


});
;

/**** /js/moikrug/ui/GroupSelect.js ****/
Lang.module('moikrug.ui.GroupSelect');


Lang.include('moikrug.ui.GroupSelectItem');

moikrug.ui.GroupSelect = Lang.createClass(moikrug.Widget, {
    init: function(node, options, name) {
        this.baseConstructor(node, options, 'GroupSelect', name);
        this.hash = options.hash;
        
        this.selector = options.selector || '.item';
        this.items = [];
        this.map = [];

        this.selected = [];
        this.prevSelectedCount = 0;
        this.currSelectedCount = 0;
        this.activeItemsCount = 0;
        this.isCookieUpdate = true;
        this.options = options;
    },

    parse: function() {
        var nodes = this.node.select(this.selector);

        this.onItemUpdateObserver = this.onItemUpdate.bind(this);
        for(var i = 0, l = nodes.length; i < l; i++) {
            var item = this.addItem(new moikrug.ui.GroupSelectItem(nodes[i], this.options).parse().bindEvents());
        }
        return this;
    },

    addItem: function(item) {
        if(item.disabled) {
        	return item;
        }

        item.observe('update', this.onItemUpdateObserver);
        this.selected[item.id] = item.isSelected;
        this.currSelectedCount += (item.isSelected ? 1 : 0);
        this.items.push(item);
        this.map[item.id] = item;
        this.activeItemsCount++;
        return item;
    },

    onItemUpdate: function(item, skipEvent) {
        this.selected[item.id] = item.isSelected;
        this.currSelectedCount += (item.isSelected ? 1 : -1);
        if(this.isCookieUpdate) {
        	this.store();
        }
        if (!skipEvent) {
        	this.onUpdate({source: item});
        }
    },

    onUpdate: function(event) {
    	
    },
    
    setSelectedAll: function(state) {
        var _this = this;
        this.currSelectedCount = 0;
        this.items.each(function(item){
            if (!item.disabled) {
                item.setSelected(state, true);
                _this.selected[item.id] = item.isSelected;
                if (item.isSelected) {
                	_this.currSelectedCount++;
                }
            }
        });
        this.store();
        this.onUpdate();
    },

    restore: function() {
        if(!this.hash) {
        	return;
        }
        var ids = Cookie.get('p_uid' + this.hash);
        if(ids) {
            this.isCookieUpdate = false;
            ids = ids.split('|');
            var tmp = [];
            for(var i = 0, l = ids.length; i < l; i++) {
                if(!this.map[ids[i]]) {
                    this.prevSelectedCount++;
                    this.selected[ids[i]] = true;
                } else {
                    this.map[ids[i]].setSelected(true);
                    tmp[ids[i]] = true;
                }
            }
            this.items.each(function(item){
                if(!tmp[item.id]) {
                    item.setSelected(false);
                }
            });
            this.isCookieUpdate = true;
        }
        return this;
    },

    store: function() {
        if(!this.hash) {
        	return;
        }
        var res = [];
        for(var i in this.selected) {
            if(this.selected.hasOwnProperty(i) && this.selected[i]) {
                res.push(i);
            }
        }
        Cookie.set('p_uid' + this.hash, res.join('|'), null, '/');
    },

    clearState: function() {
        if(!this.hash) {
        	return;
        }
        Cookie.set('p_uid' + this.hash, '', new Date(), '/');    
    },

    getSelected: function() {
        var res = [];
        for(var i in this.selected) {
            if(this.selected.hasOwnProperty(i) && this.selected[i] && this.map[i]) {
                res.push(i);
            }
        }
        return res;
    }
});

moikrug.ui.GroupSelect.getInstance = function(node, options) {
    return Widget.getInstance(node) || Widget.registerInstance(node, new moikrug.ui.GroupSelect(node, options));
};
;

/**** /js/moikrug/ui/SelectableList.js ****/
Lang.module('moikrug.ui.SelectableList');

/**
 * @todo: move action button to a separte class
 */

Lang.include('moikrug.ui.GroupSelect');

moikrug.ui.SelectableList = Lang.createClass(moikrug.ui.GroupSelect, {
    init: function(node, options, name) {
        options.selector = options.selector || '.item';
        this.controls = options.controls || [];
        this.baseConstructor(node, options, name);
    },

    parse: function() {
        var actions = [];
        var selectAllControls = [];
        this.controls.each(function(item) {
            var actionContainers = item.select('.actions .action');
            actionContainers.each(function(container) {
                var matches = container.className.match(/(^| )limit-(\d+)/);
                actions.push({
                    limit: (matches && matches[2]) || 1e6,
                    message: container.down('.message-error'),
                    input: container.down('.input')
                }); 
            });
            selectAllControls = selectAllControls.concat(item.select('.select-all'));
        });
        this.actions = actions;
        this.selectAllControls = selectAllControls;

        if(!this.node) {
        	return;
        }

        this.form = this.node;
        this.hiddenInput = this.form.down('.groupSelect-hidden-input');
        this.hiddenHashInput = this.form.down('.groupSelect-hidden-hash-input');
        if(this.hiddenInput) {
        	this.hiddenInput.value = '';
        }
        moikrug.ui.GroupSelect.prototype.parse.call(this, null);
        this.updateEvents();
        return this;
    },
    
    restore: function() {
        moikrug.ui.GroupSelect.prototype.restore.call(this, null);
        if(this.selectAllControls) {
            this.selectAllControls.each(function(control) {
                control.disabled = this.activeItemsCount === 0;
            });
        }
        this.updateAllSelected();
        
        if(this.actions) {
            var selected = this.currSelectedCount;
	        this._updateActions(selected);
        }
        this.updateEvents();
        return this;    
    },

    bindEvents: function() {
        var observer = this.onSelectAllControllClick.bindAsEventListener(this);
        this.selectAllControls.each(function(item) {
            Event.observe(item, 'click', observer);
        });
        for(var i = 0, l = this.actions.length; i < l; i++) {
            if (this.actions[i].input) {
                Event.observe(this.actions[i].input, 'click', this.onActionClick.bindAsEventListener(this, i));
            } 
        }
        return this;
    },
    
    onActionClick: function(event, index) {
        if(this.actions[index].input && this.actions[index].input.disabled) {
        	return;
        }
        
        var name = this.items[0].name;
        var target = this.form;
       	if (this.hiddenHashInput) {
       		this.hiddenHashInput.value = this.hash;
       	}
    
    },

    onSelectAllControllClick: function(event) {
        var state = Event.element(event).checked;
        this.setSelectedAll(state);
        this.selectAllControls.each(function(control) {
            control.checked = state;
        });
    },
    
    updateEvents: function() {
        var selected = (this.currSelectedCount);
        this.updateAllSelected();
        this._updateActions(selected);
    },

    updateAllSelected: function() {
        var state = (this.currSelectedCount == this.activeItemsCount) && this.activeItemsCount;
        this.selectAllControls.each(function(control) {
            control.checked = state;
        });
    },
    
    onItemUpdate: function(item) {
        moikrug.ui.GroupSelect.prototype.onItemUpdate.call(this, item);
        this.updateEvents();
    },
    
    setSelectedAll: function(state) {
        moikrug.ui.GroupSelect.prototype.setSelectedAll.call(this, state);
        this.updateEvents();
    },

	/**
	 * @event
	 */
	onActionsUpdate: function(selected) {},
    
    _updateActions: function(selected) {
        this.actions.each(function(item){
        	if (item.input) {
	        	if (item.input.hasClassName('item_counter')) {
					item.input.value = item.input.value.replace(/ +\(\d+\)$/, '') + (selected > 0 ? ' (' + selected + ')' : '');
	        	}
				item.input.disabled = (selected === 0) || (selected > item.limit);
				if(item.message) {
					item.message.style.display = (selected > item.limit) ? 'block' : 'none';
				}
        	}
        });
		this.onActionsUpdate(selected);
    }
});

moikrug.ui.SelectableList.getInstance = function(node, options) {
    return Widget.getInstance(node) || Widget.registerInstance(node, new moikrug.ui.SelectableList(node, options));
};
;

/**** /js/moikrug/SearchResultsItem.js ****/
Lang.module('moikrug.SearchResultsItem');

moikrug.SearchResultsItem = Lang.createClass(Widget, {
	init: function(node, options) {
		this.baseConstructor(node, options);
		
		return this;
	},
	
	parse: function() {
		this.row = this.node.up();
		this.addToFirstCircleLink = this.node.select(".addUserToFirstCircle");
		
		return this;
	},
	
	bindEvents: function() {
 		Event.observe(this.row, "mouseover", this.onMouseover.bind(this));
 		Event.observe(this.row, "mouseout", this.onMouseout.bind(this));
        
        return this;
	},
	
	onMouseover: function() {
		this.row.addClassName("hover");
	},
	
	onMouseout: function() {
		this.row.removeClassName("hover");
	}
});

;

/**** /js/moikrug/widget/favorite/Manager.js ****/
Lang.module('moikrug.widget.favorite.Manager');

moikrug.widget.favorite.Manager = {
	items: [],
	
	url: '/widgets/ajax/tags/set',
	
	updating: null,
	
	add: function(favorite) {
		var objId = favorite.getObjId();
		if (!this.items[objId]) {
			this.items[objId] = [];
		}
		this.items[objId].push(favorite);
		favorite.observe('update', this._onFavoriteUpdate.bind(this, favorite));
	},
	
	_onFavoriteUpdate: function(favorite, state) {
		var objId = favorite.getObjId();
		if (this.updating == objId) {
			return;
		}
		this.updating = objId;
		
		var items = this.items[objId];
		selected = favorite.isSelected();
		for (var i = 0, l = items.length; i < l; i++) {
			items[i].setSelected(favorite.isSelected());
			items[i].setUpdating(true);
		}
		this._updateServer(objId, selected, function() {
			for (var i = 0, l = items.length; i < l; i++) {
				items[i].setUpdating(false);
			}
		});
		this.updating = null;
	},
	
	_updateServer: function(objId, state, callback) {
		var request = new JsHttpRequest();
	    request.onreadystatechange = function() {
	    	if (request.readyState == 4) {
	    		callback();
    		}
    	};
	    request.open('get', this.url, true);
	    request.send({
				objId: objId,
				state: state * 1
		});
	}
	

	
	
};
;

/**** /js/moikrug/widget/Favorite.js ****/
Lang.module('moikrug.widget.Favorite');

Lang.include('moikrug.Widget');
Lang.include('moikrug.widget.favorite.Manager');

moikrug.widget.Favorite = Lang.createClass(moikrug.Widget, {
	init: function(node, options, name) {
		this.baseConstructor(node, options, 'Favorite', name);
		moikrug.widget.favorite.Manager.add(this);
	},
	
	parse: function() {
		return this;
	},
	
	bindEvents: function() {
		this.node.observe('click', this._onNodeClick.bindAsEventListener(this));
	},
	
	isSelected: function() {
		return this.node.hasClassName('star_selected');
	},
	
	isUpdating: function() {
		return this.node.hasClassName('star_updating');
	},
	
	getObjId: function() {
		var matches = this.node.className.match(/objId-(\d+)/);
		return matches ? matches[1] : null;
	},
	
	setSelected: function(state) {
		if (state == this.isSelected()) {
			return;
		}
		this.node[state ? 'addClassName' : 'removeClassName']('star_selected');
		this.onUpdate();
	},
	
	setUpdating: function(state) {
		if (state == this.isUpdating()) {
			return;
		}
		this.node[state ? 'addClassName' : 'removeClassName']('star_updating');
	},
	
	onUpdate: function(state) {
	},
	
	_onNodeClick: function(event) {
		if (!this.isUpdating()) {
			this.setSelected(!this.isSelected());
		}
		event.stop();
		return false;
	}
});

moikrug.widget.Favorite.attachItems = function(nodes, nameGenrator) {
	nameGenrator = nameGenrator || function(node) {
		return 'star_' + WidgetRegistry.nextUniqId();
	};
	for (var i = 0, l = nodes.length; i < l; i++) {
		(new moikrug.widget.Favorite(nodes[i], {}, nameGenrator(nodes[i]))).parse().bindEvents();
	}
};
;

/**** /js/moikrug/widget/PersonGroup.js ****/
Lang.module('moikrug.widget.PersonGroup');
Lang.include('moikrug.Widget');

moikrug.widget.PersonGroup = Lang.createClass(moikrug.Widget, {
	init: function(node, options, name) {
		this.baseConstructor(node, options, 'Favorite', name);
		this.url = options.url;
		this.obj_id = options.obj_id;
		this.label_id = options.label_id;
	},
	
	parse: function() {
		this.delNode = this.node.down('.group_del');
		return this;
	},
	
	bindEvents: function() {
		this.delNode.observe('click', this._onDelClick.bindAsEventListener(this));
		this.delNode.observe('mouseover', this._onHover.bindAsEventListener(this));
		this.delNode.observe('mouseout', this._onHoverout.bindAsEventListener(this));
	},
	
	_onDelClick: function() {
		this._removeLabel();
	},
	
	_onHover: function() {
		this.delNode.removeClassName('transparent');	
	},
	
	_onHoverout: function() {
		this.delNode.addClassName('transparent');	
	},
	
	_removeLabel: function() {
		this.node.hide();
		this._query({
			action: 'remove',
			obj_id: this.obj_id,
			label_id: this.label_id
		}, function() {
			this.node.remove();					
		}, function() {
			this.node.show();
		});
		
	},
	
	_query: function(params, succFunc, errorFunc) {
		var request = new JsHttpRequest();
	    request.onreadystatechange = (function() {
	    	if (request.readyState == 4) {
	    		if (request.status || (request.status >= 200 && request.status < 300)) {
		    		(succFunc.bind(this))(request);
		    	} else {
		    		if (errorFunc) {
		    			(errorFunc.bind(this))(request);
		    		}
		    	}
    		}
    	}).bind(this);
	    request.open('get', this.url, true);
	    request.send(params);
	}
	
});
;

/**** /js/helpPopup.js ****/
var helpPopup = {
    TIP_TIME:       500,
    OUT_TIME:       100,
    divTip:         null,
    html:           '<div class="help_tips" style="text-align: left;"></div>',

    scheduleShowTip: function(elt, text, coord_shift) {
        if (this.timeoutTip) {
        	this.timeoutTip = clearTimeout(this.timeoutTip);
        }
        if (this.timeoutMouseOut) {
        	this.timeoutMouseOut = clearTimeout(this.timeoutMouseOut);
        }
        if (this.divTip && this.divTip.elt == elt) {
        	return; // already shown
        }
        this.timeoutTip = setTimeout(function() { 
        	this.timeoutTip = null; 
        	elt.title = ''; 
        	this.showTip(elt, text, coord_shift);
      	}.bind(this), this.TIP_TIME);
    },
    
    scheduleShowTip2: function(elt, coord_shift, waitTime) {
    	elt = $(elt);
        var title = elt.title;
        waitTime = waitTime || this.TIP_TIME;
        elt.setAttribute('title', '');
        elt.title = '';

        if (this.timeoutTip) {
        	this.timeoutTip = clearTimeout(this.timeoutTip);
        }
        if (this.timeoutMouseOut) {
        	this.timeoutMouseOut = clearTimeout(this.timeoutMouseOut);
        }
        if (this.divTip && this.divTip.elt == elt) {
        	return; // already shown
        }
		if (elt.tagName.toLowerCase() != 'span' || elt.hasClassName('tip-hide-on-click')){
			// hack for ie
			this.clickHandler = function(){ 
	            this.hideTip(); 
	            if (this.timeoutTip) {
	            	this.timeoutTip = clearTimeout(this.timeoutTip);
	            }
	            if (this.timeoutMouseOut) {
	            	this.timeoutMouseOut = clearTimeout(this.timeoutMouseOut);
	            }
	            Event.stopObserving(elt, 'click', this.clickHandler);
	            if (elt.title.strip() === '') {
	            	elt.title = title;
	            }
	        }.bindAsEventListener(this);
	        
			setTimeout(function(){
		        Event.observe(elt, 'click', this.clickHandler);
		    }.bind(this),1);    
		}
        Event.observe(elt, 'mouseout', function(){ 
            if (elt.title.strip() === '') {
            	elt.title = title;
            }
            if (this.timeoutTip) {
            	this.timeoutTip = clearTimeout(this.timeoutTip);
            }
	        if(this.clickHandler) {
	        	Event.stopObserving(elt, 'click', this.clickHandler);
	        }
            this.scheduleHideTip();
        }.bindAsEventListener(this));

        this.timeoutTip = setTimeout(function() { 
            this.timeoutTip = null; 
            this.showTip(elt,title,coord_shift);
        }.bind(this), waitTime);
        return false;
    },

    scheduleHideTip: function() {
        if (this.timeoutTip) {
        	this.timeoutTip = clearTimeout(this.timeoutTip);
        }
        if (this.timeoutMouseOut) {
        	this.timeoutMouseOut = clearTimeout(this.timeoutMouseOut);
        }
        this.timeoutMouseOut = setTimeout(function() { 
        	this.timeoutMouseOut = null; 
        	this.hideTip();
        }.bind(this), this.OUT_TIME);
    },
    
    clearTimeouts: function() {
        if (this.timeoutTip) {
        	this.timeoutTip = clearTimeout(this.timeoutTip);
        }
        if (this.timeoutMouseOut) {
        	this.timeoutMouseOut = clearTimeout(this.timeoutMouseOut);
        }
    },

    showTip: function(elt, text, coord_shift) {
        this.locked = false;
        this.hideTip();
        // Create tip.
        var div = document.createElement("div");
        div.innerHTML = this.html;
        this.divTip = div.childNodes[0];
        this.divTip.innerHTML = text;
        this.divTip.elt = elt;

        // Position tip.
        document.body.appendChild(this.divTip);
        this.coord = this.getAbsPos(elt);
        this.coord.x += coord_shift.x;
        this.coord.y += coord_shift.y;
        var width = coord_shift.width;
 		
        var docWidth = document.body.scrollWidth;
        if (this.coord.x + width > docWidth) {
        	this.coord.x = docWidth - width - 14;
        }
        if (this.coord.x < 0) {
        	this.coord.x = 0;
        }
        this.divTip.style.left = this.coord.x + "px";
        this.divTip.style.top = this.coord.y + "px";
        if (width)  {
        	this.divTip.style.width = width + "px";
        }
        this.divTip.style.display = "block";
        // Enents for tip.
        Event.observe(this.divTip, 'mouseover', function() {
            this.scheduleShowTip(elt);
            return true;
        }.bindAsEventListener(this));
        Event.observe(this.divTip, 'mouseout', function() {
            if (this.inConfirm) {
            	return;
            }
            this.scheduleHideTip();
            return true;
        }.bindAsEventListener(this));

    },

    hideTip: function() {
        if (!this.divTip) {
        	return;
        }
        this.divTip.parentNode.removeChild(this.divTip);
        this.divTip = null;
    },

    getAbsPos: function(p) {
        var s = { x: 0, y: 0 };
        while (p.offsetParent) {
            s.x += p.offsetLeft;
            s.y += p.offsetTop;
            p = p.offsetParent;
        }
        return s;
    }
};

function helpPopupAdd(element) {
    var offsetX = element.className.match(/\bhelpPopupX_(-?\d+)\b/);
    var offsetY = element.className.match(/\bhelpPopupY_(\d+)\b/);
    var width = element.className.match(/\bhelpPopupWidth_(\d+)\b/);
    helpPopup.scheduleShowTip2(element, { 
        x: offsetX ? parseInt(offsetX[1], 10) : 0 , // default offsetX
        y: offsetY ? parseInt(offsetY[1], 10) : 16,  // default offsetY
        width: width ? parseInt(width[1], 10) : 160 // default width
    });
}
;

/**** /js/moikrug/ChatNotificationManager.js ****/
Lang.module("moikrug.ChatNofiticationManager");

moikrug.ChatNotificationManager = Lang.createClass(Widget, {

	frameSize: null,
	msgCount: null,
	widgets: [],
	queen: null,
	msgContainer: null,

	closeNode: null,
	CLOSE_COOKIE: 'ChatNotificationManager_closed',

    init: function(node, options, limit, count) {
        this.baseConstructor(node, options);
        this.frameSize = limit;
        this.msgCount = count;
    },

    parse: function() {
        this.widgets = [];
        
        // Save sample (prototype) node and remove it from the container.
        this.queen = WidgetRegistry.get(this.node.down('.chat_notifier_item.hidden'));
        this.msgContainer = this.queen.node.parentNode;
        this.msgContainer.removeChild(this.queen.node);
        
        this.node.select('.chat_notifier_item').each((function(item) {
            this.widgets.push(WidgetRegistry.get(item));
        }).bind(this));
        
        this.closeNode = this.node.down('.chat_notifier_close');
        if (!this.isClosed()) {
        	this.setClosed(false);
        }

        this.limitFrame();
        return this;
    },

    bindEvents: function() {
        var markReadAndMaybeHide = (function(notification) {
            moikrug.Multiplexor.removeKey("Message", notification.getMessageId());
        }).bind(this);

        this.widgets.each((function(notification) {
            notification.observe("textClick", markReadAndMaybeHide.curry(notification));
        }).bind(this));
        
        Event.observe(this.closeNode, "click", (function(e) {
            e.stop();
            this.setClosed(true);
        }).bindAsEventListener(this));

        moikrug.Multiplexor.subscribe("Message", (function(messageData) {
            if (moikrug.ThreadPage && moikrug.ThreadPage.instance.threadId == messageData.threadId) {
                moikrug.Multiplexor.removeKey("Message", messageData.messageId);
                return;
            }
            var newNotification = this.queen.cloneCurrent();
            if (this.msgContainer.firstChild) {
		        this.msgContainer.insertBefore(newNotification.node, this.msgContainer.firstChild);
            } else {
		        this.msgContainer.appendChild(newNotification.node);
            }
            newNotification.setMessageId(messageData.messageId);
            newNotification.setPersonName(messageData.personName, messageData.personNameFull);
            newNotification.setPersonLink(messageData.personLink);
            newNotification.setMessageText(messageData.inlineText);
            newNotification.setMessageLink(messageData.url);
            newNotification.node.removeClassName("hidden");

            newNotification.observe("close", markReadAndMaybeHide.curry(newNotification));
            newNotification.observe("textClick", markReadAndMaybeHide.curry(newNotification));
            this.widgets.push(newNotification);
            this.msgCount++;
            this.limitFrame();

            this.setClosed(false);
            newNotification.blink();
            moikrug.TitleNotification.startBlink(messageData.personName + ' - ' + DIC.new_message_from);
            
        }).bind(this));
        return this;
    },
    
    limitFrame: function() {
		var f = $(this.msgContainer.parentNode).select('.chat_notifier_f')[0];
		var items = $(this.msgContainer).select('.chat_notifier_item');
		var nShown = 0;
		for (var i = 0; i < items.length; i++) {
			var item = items[i];
			var widget = WidgetRegistry.get(item);
			if (nShown < this.frameSize) {
	        	item.removeClassName("hidden");
				nShown++;
			} else {
	        	item.addClassName("hidden");
			}
        }
        if (this.msgCount <= nShown) {
        	f.style.display = 'none';
        } else {
        	f.style.display = '';
			$(f).select('a')[0].innerHTML = common.TextUtils.formatNumberTemplate(
				this.msgCount - nShown,
				DIC.chat_notifier_n_new_messages
			);
        }
    },
    
    setClosed: function(flag) {
        Cookie.set(this.CLOSE_COOKIE, flag? 1 : 0, null, "/");
        var allHidden = this.widgets.all(function(widget) {return widget.node.hasClassName("hidden");});
        if (!flag && !allHidden) {
        	this.node.removeClassName("hidden");
        } else {
        	this.node.addClassName("hidden");
        }
    },
    
    isClosed: function() {
        return parseInt(Cookie.get(this.CLOSE_COOKIE), 10);
    }
});
;

/**** /js/moikrug/TitleNotification.js ****/
Lang.module("moikrug.TitleNotification");

moikrug.TitleNotification = {

    focusStatus: false,
    blinkStatus: false,
    oldTitle: false,
    timeout: null,
    
    bindEvents: function() {
        if (Prototype.Browser.IE) {
            document.observe('focusin', this._setFocus.bindAsEventListener(this));
            document.observe('focusout', this._clearFocus.bindAsEventListener(this));
        } else {
	        document.observe('focus', this._setFocus.bindAsEventListener(this));
	        document.observe('blur', this._clearFocus.bindAsEventListener(this));
        }
        return this;
    },
    
    _setFocus: function() {
        this.focusStatus = true;
    },
    
    _clearFocus: function() {
        this.focusStatus = false;
    },
    
    startBlink: function(msg) {
        this.oldTitle = document.title;
        msg = msg.stripTags();
        msg = msg.replace(/\&.*?;/,'');
        this._blinkTitle(msg);
    },
    
    stopBlink: function() {
        this._clearFocus();
    },
    
    _blinkTitle: function(msg) {
        
        if (this.blinkStatus) {
            document.title = msg;
        } else {
            document.title = this.oldTitle;
        }
        this.blinkStatus = !this.blinkStatus;
        if (!this.focusStatus) {
            this.timeout = setTimeout(function() { this._blinkTitle(msg); }.bind(this), 1500);
        } else {
            document.title = this.oldTitle;
        }
        
    }
    
};
moikrug.TitleNotification.bindEvents();

;

/**** /js/moikrug/ui/messaging/ChatNotification.js ****/
Lang.module('moikrug.ui.messaging.ChatNotification');

Lang.include('moikrug.widget.Clonable');

moikrug.ui.messaging.ChatNotification = Lang.createClass(moikrug.widget.Clonable, {
    init: function(node, options, name, className) {
        this.baseConstructor(node, options, name, className || "ChatNotification");
    },

    _createClonedWidget: function(node, options, name, className) {
        return new moikrug.ui.messaging.ChatNotification(node, options, name, className).parse().bindEvents();
    },
    
    parse: function() {
        this.personNameNode = this.node.down(".chat_notifier_person");
        this.messageTextNode = this.node.down(".chat_notifier_text");
        return this;
    },
    
    bindEvents: function() {
        Event.observe(this.node, "click", (function() {
            this.onTextClick();
        }).bind(this));
        return this;
    },

    blink: function() {
        var node = this.node;
        var delay = 400;
        node.addClassName("chat_notifier_item_dark");
        window.setTimeout(function() {node.removeClassName("chat_notifier_item_dark");}, delay);
        window.setTimeout(function() {node.addClassName("chat_notifier_item_dark");}, delay*2);
        window.setTimeout(function() {node.removeClassName("chat_notifier_item_dark");}, delay*3);
        window.setTimeout(function() {node.addClassName("chat_notifier_item_dark");}, delay*4);
        window.setTimeout(function() {node.removeClassName("chat_notifier_item_dark");}, delay*5);
        window.setTimeout(function() {node.addClassName("chat_notifier_item_dark");}, delay*6);
        window.setTimeout(function() {node.removeClassName("chat_notifier_item_dark");}, delay*7);
    },

    close: function() {
        this.node.addClassName("hidden");
    },

    getMessageId: function() {
        return this.options.messageId;
    },

    setMessageId: function(id) {
        this.options.messageId = id; 
    },

    /**
     * Setters
     */
    setPersonName: function(name, fullName) {
        this.personNameNode.title = fullName;
        this.personNameNode.innerHTML = common.TextUtils.highlightFirstLetterInName(name);
    },

    setPersonLink: function(link) {
        // No separated link for a person. 
        //this.personNameNode.href = link;
    },

    setMessageLink: function(link) {
    	// Note that yo CANNOT do up('a'), because this.messageTextNode
    	// is not in DOMDocument yet at this time. :-(
    	node = $(this.messageTextNode).up(); 
        node.href = link;
    },

    setMessageText: function(text) {
        this.messageTextNode.down("span").innerHTML = text;
    },

    /**
     * Events
     */
    onClose: function(){},
    onTextClick: function(){}

});
;
