var popup = {
	isIE: /MSIE/.test(navigator.userAgent),
	isIElt7: /MSIE (\d+\.\d+);/.test(navigator.userAgent) && parseInt(RegExp.$1) < 7,
	isIE7: /MSIE (\d+\.\d+);/.test(navigator.userAgent) && parseInt(RegExp.$1) == 7,
	tmpl: {
		cont_tmpl: '<div id="content_%ID%"></div><div id="title_%ID%" onclick="popup.close()">Unread<span class="number" id="unread_%ID%"></span></div>',
		"default": '<div onclick="popup.close(%ID%)">Title</div><div>%TEXT%</div>'
	},
	conf: {
		flash_name: 'exchange',
		main_class: 'popup_area',
		popup_class: 'popupbox2'
	},
	cont: null,
	reg: {},
	
	trace: function(txt){
		try{ window.document[this.conf.flash_name].trace("[JS]: " + txt); }catch(e){};
	},
	
	scroller: function(start, stop, fi, fs, dur, to){
		dur = dur? dur : 1000;
		to = to? to : 20;
		var j = 0, timescroll = new Date().getTime();
		for(var i in start){ stop[i] -= start[i]; j += stop[i]; }
		if(!j) return null;
		return setInterval(function(){
			var x = (new Date().getTime() - timescroll) / dur;
			if(x > 1) x = 1;
			var y = ((-Math.cos(x * Math.PI) / 2) + 0.5);
			var res = {};
			for(var i in start) res[i] = Math.round(start[i] + stop[i] * y);
			if(fi) fi(res);
			if(x == 1 && fs) fs();
		}, to);
	},
	
	fixcoord: function(){
		if(this.isIE){
			var doc = document.documentElement || document.body;
			if(this.isIElt7) return { left: doc.clientWidth + doc.scrollLeft, top: doc.clientHeight + doc.scrollTop };
			else return { left: doc.clientWidth, top: doc.clientHeight };
		}else{
			return { left: window.innerWidth - 20, top: window.innerHeight };
		}
	},
	
	create: function(){
		var body = document.getElementsByTagName("body")[0],
		rid = Math.floor(Math.random()*1000000),
		el = document.createElement("div");
		el.className = this.conf.main_class;
		el.innerHTML = this.tmpl.cont_tmpl.replace(/%ID%/g, rid);
		el.style.visibility = "hidden";
		el.style.position = (this.isIElt7) ? "absolute" : "fixed";
		//body.insertBefore(el, body.childNodes[0]);
		body.appendChild(el);
		el.style.zIndex = 255;
		
		var res = {
			element: el,
			offset: {left: el.offsetWidth, top: 0},
			pos: popup.fixcoord(),
			openint: null, closeint: null, scrollint: null, contentint: null,
			content: document.getElementById("content_" + rid),
			title: document.getElementById("title_" + rid),
			unread: document.getElementById("unread_" + rid),
			count: 0, width: 0, size: 0, isopen: false, isexpand: false,
			queue: {},
			
			fixpos: function(){ this.element.style.left = (this.pos.left - this.offset.left) + "px"; this.element.style.top = (this.pos.top - this.offset.top) + "px"; },
			show: function(show){ this.element.style.display = show? "block" : "none"; if(!show){ this.content.style.overflow = "hidden"; this.content.style.height = "0px"; } },
			showtitle: function(){ this.title.style.display = (this.count > 1)? "block" : "none"; this.unread.innerHTML = this.count; },
			clearint: function(){
				if(this.scrollint) clearInterval(this.scrollint);
				if(this.openint) clearInterval(this.openint);
				if(this.closeint) clearInterval(this.closeint);
				if(this.contentint) clearInterval(this.contentint);
				this.contentint = this.closeint = this.openint = this.scrollint = null; 
			},
			
			open: function(noscroll, expand){ if(this.closeint) return;
				this.count = 0;
				this.clearint();
				this.show(true);
				if(expand){
					this.isexpand = true;
					for(var n in this.queue) if(this.queue[n].toint) clearTimeout(this.queue[n].toint);
				}
				
				if(!this.width) this.width = this.element.offsetWidth;
				for(var i = this.content.childNodes.length, size = 0; i > 0 ; i--){
					var j = i - 1, id = this.content.childNodes[j].id.split("_")[1];
					if(id && !this.queue[id].elemint){
						size += this.content.childNodes[j].offsetHeight;
						//if(size < this.pos.top / 2) this.size = size;
						if(this.isexpand){
							if(size < this.pos.top) this.size = size;
						} else if(!this.count) this.size = size;
						this.count++;
					}
				}
				this.showtitle();
				
				if(this.size != size){
					//this.content.style.overflow = "auto";
					//this.element.style.width = this.width + 20 + "px";
					if(this.isopen){
						this.scrollint = popup.scroller(
							{top: this.content.scrollTop},
							{top: this.content.scrollHeight},
							function(out){ res.content.scrollTop = out.top; },
							function(){ res.clearint(); }
						);
					}else this.content.scrollTop = this.content.scrollHeight - this.size;
				}else{
					this.content.scrollTop = 0;
					//this.content.style.overflow = "hidden";
					//this.element.style.width = this.width + "px";
				}
				this.openint = popup.scroller(
					{left: this.offset.left, top: this.offset.top, height: this.content.offsetHeight},
					{left: this.element.offsetWidth, top: this.title.offsetHeight + this.size, height: this.size},
					function(out){
						res.offset.left = out.left; res.offset.top = out.top; res.fixpos();
						res.content.style.height = out.height + "px";
						res.element.style.visibility = "visible";
					},
					function(){ res.clearint(); }
				);
				this.isopen = true;
			},
			
			close: function(){
				this.clearint();
				this.closeint = popup.scroller(
					{left: this.offset.left, top: this.offset.top},
					{left: this.offset.left, top: 0},
					function(out){ res.offset.left = out.left; res.offset.top = out.top; res.fixpos(); },
					function(){
						res.count = 0;
						res.clearint();
						for(var i in res.queue){
							if(res.queue[i].elemint) clearInterval(res.queue[i].elemint);
							if(res.queue[i].toint) clearTimeout(res.queue[i].toint);
						}
						while(res.content.childNodes.length){
							res.content.removeChild(res.content.lastChild);
						}
						res.queue = {};
						res.show(false);
					}
				);
				this.isopen = this.isexpand = false;
			}
		}
		res.show(false);
		setInterval( function(){ res.pos = popup.fixcoord(); res.fixpos(); }, 500 );
		return res;
	},
	
	push: function(msg_array){
		if(!this.cont) this.cont = this.create();
		for(var i = 0, j = 0; i < msg_array.length; i++) if(!this.cont.queue[msg_array[i].id]){
			this.cont.queue[msg_array[i].id] = msg_array[i];
			j++;
		}
		if(j) this.open();
		return true;
	},
	
	open: function(){ if(!this.cont) return;
		var op = 0;
		for(var i in this.cont.queue) if(!this.cont.queue[i].isopen){
			var msg = '', dt = null;
			try{ dt = eval("("+this.cont.queue[i].text+")") }catch(e){ this.dismiss(this.cont.queue[i].id); }
			this.cont.queue[i].isopen = true;
			if(dt && typeof dt == "object"){
				var el = document.createElement("div");
				el.id = "popup_" + this.cont.queue[i].id;
				el.className = this.conf.popup_class;
				msg = this.tmpl[this.tmpl[this.cont.queue[i].code]? this.cont.queue[i].code : "default"].replace(/%ID%/g, this.cont.queue[i].id);
				for(var j in dt){ if(!this.reg[j]) this.reg[j] = new RegExp("%"+j+"%", "g"); if(j[0] != "_") msg = msg.replace(this.reg[j], dt[j]); }
				msg = msg.replace(/\[(.*?)\]/g, function(){
					try{ eval("var v=" + arguments[1]); }catch(e){ alert("Exception in templater script: " + e) };
					return v;
				});
				for(var j in dt){ if(j[0] == "_") msg = msg.replace(this.reg[j], dt[j]); }
				el.innerHTML = msg;
				this.cont.content.appendChild(el);
				
				if(this.cont.queue[i].close && !this.cont.isexpand)
				this.cont.queue[i].toint = setTimeout("popup.close("+this.cont.queue[i].id+")", this.cont.queue[i].close);
				
				op++;
			}
		}
		if(op) setTimeout("popup.cont.open()", 50);
	},
	
	close: function(id){ if(!this.cont) return;
		if(id){
			if(!this.cont.queue[id]) return;
			this.dismiss(id);
			if(--this.cont.count > 0){
				var el = document.getElementById("popup_" + id);
				if(el){
					if(!this.isIE7){
						el.style.overflow = "hidden";
						this.cont.queue[id].elemint = this.scroller(
							{top: el.offsetHeight},
							{top: 0},
							function(out){ el.style.height = out.top + "px"; },
							function(){
								clearInterval(popup.cont.queue[id].elemint);
								el.parentNode.removeChild(el);
								delete popup.cont.queue[id];
							}
						);
					}else{
						el.parentNode.removeChild(el);
						delete popup.cont.queue[id];
					}
				}
				this.cont.open(true);
			}else this.clear();
		}else{
			this.dismiss();
			this.clear();
		}
	},
	
	clear: function(){ if(!this.cont) return;
		this.cont.close();
	},
	
	dismiss: function(id){
		try{
			if(id) window.document[this.conf.flash_name].noticeIsShow(id);
			else window.document[this.conf.flash_name].noticeIsClear();
		}catch(e){};
	}
}
