function ListAlert(params) {
    if(params) {
	for(var i in params) this[i] = params[i];
    } else { throw('Error in ListAlert!!!') }
    this.title = document.title;
    this.alertDelete.observe('click', this.deleteAlert.bindAsEventListener(this));
    window.onfocus = window.onFocus = this.ifFocus.bindAsEventListener(this);
    $(document).observe('focus', this.ifFocus.bindAsEventListener(this));
    this.chTitle = 0;
    this.interval = setInterval(this.listen.bind(this, null), this.listenInterval);
}

ListAlert.prototype.ifFocus = function(){
    if(this.chtitleInterval) clearInterval(this.chtitleInterval);
    document.title = this.title;
}

ListAlert.prototype.listen = function(data){
    if(data) {
		if(data.Alert_1_AlertID == this.alertID) return;
		if(data.Alert_1_Type){
			if(!this.AlertTemplater) {
				if(window.Booster) if(window.Booster.wait) window.Booster.wait();
				this.forTemplate = data;
				var tmpls = document.createElement('script');
				tmpls.src = this.tmplSrc;
				document.body.appendChild(tmpls);
				return;
			}
			this.data = this.prepareData(data);
			var Alert = new Template(unescape(this.AlertTemplater(data['Alert_1_Type'])));
			this.alertContainer.innerHTML = Alert.evaluate(this.data);
			this.alertContainer.select('a').each(function(el) { if(el.readAttribute('phref')) el.href = el.readAttribute('phref') });
			this.massObserve('[name="DeleteThis"]','click', this.deleteAlert.bindAsEventListener(this) );
			this.boosterObserve();
			this.alertContainer.parentNode.style.display = '';
			if(this.chtitleInterval) clearInterval(this.chtitleInterval);
			this.chtitleInterval = setInterval(this.setTitle.bindAsEventListener(this), this.titleInterval);
		} else {
			this.alertContainer.parentNode.style.display = 'none';
			this.ifFocus();
		}
    } else this.newAlert();
}

ListAlert.prototype.newAlert = function(del) {
	if(!del) del = '';
	var sc = document.getElementById('alertScript');
	if(sc) document.body.removeChild(sc);
	sc = document.body.appendChild(document.createElement('script'));
	sc.id = 'alertScript';
	sc.src = this.statusRequestURL + '&var=' + this.objName + del + '&' + (new Date()).getTime();
}

ListAlert.prototype.prepareData = function(data){
    for(i in this.myvars) data[i] = this.myvars[i];
    data.photohost = this.photohost;
    data.AvatarChangeTime = (new Date()).getTime();
    data.sex = (data.Alert_1_Sex == 2 ? 'à' : '');
    data.classmatesSex = (data.Alert_1_Sex == 2 ? 'àñü' : 'ñÿ');
    data.back_value = escape(location.href);
    if(data.Alert_1_Event && (/^\-?\d+$/).test(data.Alert_1_Event)) data.Alert_1_JournalID = data.Alert_1_Event;
    this.alertID = data.Alert_1_AlertID;
    if(data['Alert_1_Type'] == 11 || data['Alert_1_Type'] == 20 || data['Alert_1_Type'] == 21) 
		data['Alert_1_Event'] = this.eventUnescape(data['Alert_1_Event']);
	if(data['Alert_1_Type'] == 13 || data['Alert_1_Type'] == 14 || data['Alert_1_Type'] == 15 || data['Alert_1_Type'] == 18 || data['Alert_1_Type'] == 21) {
		if(data.Alert_1_UserAvatarDir && data.Alert_1_UserAvatarID) {
			var small = (data['Alert_1_Type'] == 18 || data['Alert_1_Type'] == 21) ? 'small' : '';
			data.comm_avatar_url = this.photohost + data.Alert_1_UserAvatarDir + '_cavatar' + small + '/' +
									data.Alert_1_UserAvatarID + '?' + data.AvatarChangeTime;
		}
		else {
			data.comm_avatar_url = "http://img0.imgsmail.ru/mail/ru/images/community.jpg";
		}
	}
    return data;
}

ListAlert.prototype.setTitle = function(){
    this.chTitle = !this.chTitle;
    if(this.chTitle) {
		var t = this.alertContainer.getElementsByTagName('h5');
		if(t && t.length) document.title = t[0].lastChild.nodeValue;
	}
    else document.title = this.title;
}

ListAlert.prototype.deleteAlert = function(t){
    $(this.alertContainer.parentNode).hide();
	this.newAlert('&' + (this.alertID ? 'ida=' + this.alertID : this.mSpec ? this.mSpec + '=1' : 'mida=' + this.mSubmited));
}
ListAlert.prototype.eventUnescape = function(text) {
    text = text.replace(new RegExp("&lt;", "gi"), '<');
    text = text.replace(new RegExp("&gt;", "gi"), '>');
    text = text.replace(new RegExp("&quot;", "gi"), '"');
    text = text.replace(new RegExp("&amp;nbsp;", "gi"), ' ');
    text = text.replace(new RegExp("&amp;laquo;", "gi"), '«');
    text = text.replace(new RegExp("&amp;raquo;", "gi"), '»');
    return text;
}
ListAlert.prototype.massObserve = function(tags, event, observer){
    var elements = this.alertContainer.select(tags);
    for(var i = 0;i < elements.length; i++) elements[i].observe(event, observer);
    return elements;
}
ListAlert.prototype.boosterObserve = function(){
    if(window.Booster.inited) Booster.fetchAnchors(this.alertContainer);
	else if(window.MRIM) MRIM.fetch(this.alertContainer);
}
