AllNInformer = {
	data:{},
	
	container:false,

	serverUrl:"http://allnewspoint.com/informer/index.php",

	tags:'',
	
	id:0,
	
	rolloutstep:0,
	
	rollouttime:500,
	
	rolloutsteps:50,
	
	options:{
		bgcolor:false,
		bcolor:false,
		width:false,
		height:false,
		header_color:false,
		header_size:false,
		text_color:false,
		text_size:false,
		target_blank:false,
		shortnews:false,
		onlyheader:false,
		hlcolor:false,
		title:false,
		tcolor:false,
		tsize:false,
		messagelike:false,
		spath:false
	},
	
	defautls:{
		bgcolor:'#FFFFFF',
		bcolor:'#000000',
		width:200,
		height:400,
		header_color:'#000000',
		header_size:12,
		text_color:'#000000',
		text_size:11,
		target_blank:0,
		shortnews:1,
		onlyheader:0,
		hlcolor:'#FFFFFF',
		title:'',
		tcolor:'#FFFFFF',
		tsize:20,
		messagelike:0,
		spath:'http://allnewspoint.com/static/images/'
	},
	
	load:function (
			id,
			tags,
			options
			)
	{
		id = parseInt(id);
		if (isNaN(id) || !id)return;
		this.id = parseInt(id);

		if (typeof tags != 'undefined')
		{
			this.tags = tags;
		}
		
		for (var i in this.defautls)
		{
			if (typeof options != 'undefined' && typeof options[i] != 'undefined')
			{
				this.options[i] = options[i];
			}
		}
		
		var fileref=document.createElement('script');
		if (typeof fileref != "undefined")
		{
			fileref.setAttribute("type","text/javascript");
			fileref.setAttribute("src", this.serverUrl+"?func=updateInformerViews&id="+this.id+(this.tags!=''?'&tags='+encodeURIComponent(this.tags):''));
			document.getElementsByTagName("head")[0].appendChild(fileref);
		}
	},
	
	prepareBox:function()
	{
		this.container.style.overflow = 'hidden';
		this.container.style.cursor = 'pointer';
		this.container.style.backgroundColor = this.options.bgcolor;
		this.container.style.width = (this.options.width-2)+"px";
		
		this.container.style.padding = '0';
		this.container.style.margin = '0';
		
		this.container.style.border = "1px solid "+this.options.bcolor;
		
		if ( this.options.height > 10 )
		{
			this.container.style.height = (this.options.height-2)+"px";
		}
		
		if (this.options.messagelike == 1)
		{
			// Absolute position for container
			this.container.style.position = "fixed";
			this.container.style.bottom = "-1000px";
			this.container.style.borderBottom = '0px none';
			this.container.style.zIndex = '100';
			// Hiding
			this.container.style.right = "-10000px";
		}
	},

	processRequest:function()
	{
		this.container = document.getElementById("AllNInformer"+this.id);
		
		if (!this.container || !this.data.status)return;
		
		var AllNInformer_Headers = new Array();
		var AllNInformer_Texts = new Array();
		var AllNInformer_Imgs = new Array();
		var containers = {};
		
		switch (this.data.status)
		{
			case 'ok':
				for (var i in this.defautls)
				{
					if (this.options[i] === false)
					{
						if (typeof this.data.infdata == 'undefined' || typeof this.data.infdata[i] == 'undefined')
						{
							this.options[i] = this.defautls[i];
						}
						else
						{
							this.options[i] = this.data.infdata[i];
						}
					}
				}
				
				// Checking for IE6
				if (document.all && !window.opera && !window.XMLHttpRequest)
				{
					return;
				}
				
				this.prepareBox();
				
				var hlstyle = document.createElement('style');
				if (this.options.hlcolor.toLowerCase() != this.options.bgcolor.toLowerCase() && typeof hlstyle != "undefined")
				{
					hlstyle.innerHTML = '#AllNInformer'+this.id+' table:hover {background-color: '+this.options.hlcolor+';}';
					document.getElementsByTagName("head")[0].appendChild(hlstyle);
				}
				
				var headD = document.createElement("div");
				headD.style.color = this.options.bgcolor;
				headD.style.backgroundColor = this.options.text_color;
				headD.style.margin = '0';
				
				if(this.options.messagelike == 1 && !this.options.title)
				{
					headD.style.padding = '2px 17px 2px 5px';
				}
				else
				{
					headD.style.padding = '2px 5px';
				}
				headD.title = "Request: '"+this.data.request+"', processed by formula '"+this.data.formula+"'";

				if (this.options.messagelike == 1)
				{
					// Close button
					headD.innerHTML = '<img src="'+this.options.spath+'closeinformer.gif" title="Закрыть" style="position:absolute;margin-left:'+(this.options.width-20)+'px;" onclick="var el = document.getElementById(\'AllNInformer'+this.id+'\');el.parentNode.removeChild(el);" width="9px" height="9px"/>';
				}
				
				// Setting title
				if (this.options.title)
				{
					var hdt = document.createElement('div');
					hdt.innerHTML = this.options.title;
					hdt.style.color = this.options.tcolor;
					hdt.style.textAlign = 'left';
					hdt.style.font = 'normal '+this.options.tsize+'px Arial, Tahoma, sans-serif';
					headD.appendChild(hdt);
				}
				else
				{
					var headDsmall = document.createElement("div");
					headDsmall.style.font = 'normal 9px Arial, Tahoma, sans-serif';
					headDsmall.style.textAlign = 'right';
					if ( this.options.height > 10 )
						headDsmall.style.height = '10px';
					headDsmall.innerHTML = 'provided by <a href="http://allnewspoint.com" style="text-decoration: underline; color: '+headD.style.color+'">allnewspoint.com</s>';
					headD.appendChild(headDsmall);
				}
				
				this.container.appendChild(headD);

				var bodyD = document.createElement('div');
				if ( this.options.height > 10 )
					bodyD.style.overflowY = 'auto';
				bodyD.style.cursor = 'pointer';
				bodyD.style.width = (this.options.width-2)+"px";
				if ( this.options.height > 10 )
					bodyD.style.height = (this.options.height-16)+"px";
				//bodyD.style.padding = "0 5px";
				bodyD.style.borderWidth = "0";
				this.container.appendChild(bodyD);
				
				if (this.data.command == 'updateInformerViews')
				{
					var j = 0;
					var nc = this.data.data.length;
					
					for(var i = 0; i<nc; i++)
					{
						j++;
						var href = this.data.data[i]['link']+'?informerid='+this.data.informer;
						var header = this.data.data[i]['title'];//'2';
						var text   = this.data.data[i]['text'];//'asd';
						var img	   = this.data.data[i]['image'];
						if(text.length>200 && this.options.shortnews == 1) text = text.substring(0,200)+"...";
						
						// That's our container table
						containers[j] = document.createElement("table");
						containers[j].style.width = '100%';
						containers[j].style.textDecoration = 'none';
						containers[j].style.margin = "0.4em 0";
						containers[j].cellSpacing = "0";
						containers[j].cellPadding = "0";
						containers[j].border = "0";
						
						var tbody = document.createElement("tbody");
						var tr = document.createElement("tr");
						var tdt = document.createElement("td");
						tdt.vAlign = "top";
						tdt.align = "left";
						if (!img)
						{
							tdt.style.padding = "0 5px";
						}
						else
						{
							tdt.style.padding = "0 0 0 5px";
						}
						
						// Creating text
						var texta = document.createElement("a");
						texta.href = href;
						texta.style.display = 'block';
						texta.style.textDecoration = 'none';
						if (this.options.target_blank)
						{
							texta.target="_blank";
						}
						// Header
						AllNInformer_Headers[i] = document.createElement("span");
						AllNInformer_Headers[i].style.color = this.options.header_color;
						AllNInformer_Headers[i].style.fontSize = this.options.header_size + "px";
						AllNInformer_Headers[i].style.fontWeight = "bold";
						AllNInformer_Headers[i].style.textDecoration = "underline";
						AllNInformer_Headers[i].innerHTML = header;
						// Text
						if(this.options.onlyheader!=1)
						{
							AllNInformer_Texts[i] = document.createElement("div");
							AllNInformer_Texts[i].style.color = this.options.text_color;
							AllNInformer_Texts[i].style.fontSize = this.options.text_size + "px";
							AllNInformer_Texts[i].innerHTML = text;
						}
						texta.appendChild(AllNInformer_Headers[i]);

						if(this.options.onlyheader!=1)
						{
							texta.appendChild(AllNInformer_Texts[i]);
						}
						
						tdt.appendChild(texta);
						
						tr.appendChild(tdt);
						
						// Image - new table cell
						if (img)
						{
							var tdi = document.createElement("td");
							tdi.vAlign = "top";
							tdi.align = "right";
							tdi.style.padding = "0 5px 0 0";
							var imga = document.createElement("a");
							imga.href = href;
							
							AllNInformer_Imgs[i] = document.createElement("img");
							AllNInformer_Imgs[i].src = img;
							AllNInformer_Imgs[i].border = 0;
							AllNInformer_Imgs[i].style.margin = '0px 0px 3px 5px';

							AllNInformer_Imgs[i].vspace = 3;
							AllNInformer_Imgs[i].hspace = 3;
							
							imga.appendChild(AllNInformer_Imgs[i]);
							tdi.appendChild(imga);
							tr.appendChild(tdi);
						}
						
						tbody.appendChild(tr);
						containers[j].appendChild(tbody);
						bodyD.appendChild(containers[j]);
					}
				}
				
				this.container.style.backgroundRepeat = 'no-repeat';
				this.container.style.backgroundPosition = 'top left';
				this.container.style.backgroundImage = 'url("http://allnewspoint.com/static/images/i_bg.png")';
				
				// Checking height
				this.container.style.bottom = "-"+this.container.offsetHeight+"px";
				// Setting rollout data
				this.rolloutstep = this.container.offsetHeight/this.rolloutsteps;
				// Starting animation
				this.container.style.right = "10px";
				this.popUpInformer();
				break;
			case 'error':
				
				break;
			case 'stub':
					this.prepareBox();
					this.container.innerHTML = this.data.data;
				break;
		}
	},
	
	popUpInformer: function ()
	{
		this.container.style.bottom = (parseInt(this.container.style.bottom)+this.rolloutstep)+"px";
		
		if (parseInt(this.container.style.bottom) >= 0)
		{
			this.container.style.bottom = "0px";
			return;
		}

		setTimeout("AllNInformer.popUpInformer()",parseInt(this.rollouttime/this.rolloutsteps));
	}
}

