AllNInformer = {
	data:{},
	
	serverUrl:"http://allnewspoint.com/informer/index.php",
	
	tags:'',
	
	id:0,
	
	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,
	
	load:function (
			id,
			tags,
			bgcolor,
			bcolor,
			width,
			height,
			header_color,
			header_size,
			text_color,
			text_size,
			target_blank,
			shortnews,
			onlyheader
			)
	{
		if (isNaN(parseInt(id)) || !parseInt(id))return;
		this.id = parseInt(id);
		
		if (typeof tags != 'undefined')
		{
			this.tags = tags;
		}
		if (typeof bgcolor != 'undefined')
		{
			this.bgcolor = bgcolor;
		}
		if (typeof bcolor != 'undefined')
		{
			this.bcolor = bcolor;
		}
		if (typeof width != 'undefined')
		{
			this.width = width;
		}
		if (typeof height != 'undefined')
		{
			this.height = height;
		}
		if (typeof header_color != 'undefined')
		{
			this.header_color = header_color;
		}
		if (typeof header_size != 'undefined')
		{
			this.header_size = header_size;
		}
		if (typeof text_color != 'undefined')
		{
			this.text_color = text_color;
		}
		if (typeof text_size != 'undefined')
		{
			this.text_size = text_size;
		}
		if (typeof target_blank != 'undefined')
		{
			this.target_blank = target_blank;
		}
		if (typeof shortnews != 'undefined')
		{
			this.shortnews = shortnews;
		}
		if (typeof onlyheader != 'undefined')
		{
			this.onlyheader = onlyheader;
		}
		
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", this.serverUrl+"?func=updateInformerViews&id="+this.id+(this.tags!=''?'&tags='+encodeURIComponent(this.tags):''));
		
		if (typeof fileref!="undefined")
		{
			document.getElementsByTagName("head")[0].appendChild(fileref);
		}
	},
	
	prepareBox:function(AllNInformer_Container)
	{
		AllNInformer_Container.style.overflow = 'hidden';
		AllNInformer_Container.style.cursor = 'pointer';
		AllNInformer_Container.style.backgroundColor = this.bgcolor;
		AllNInformer_Container.style.width = (this.width-2)+"px";
		if ( this.height > 10 )
			AllNInformer_Container.style.height = (this.height-2)+"px";
		
		AllNInformer_Container.style.border = "1px solid "+this.bcolor;
		AllNInformer_Container.style.padding = '0';
		AllNInformer_Container.style.margin = '0';
	},

	processRequest:function()
	{
		var AllNInformer_Container=document.getElementById("AllNInformer"+this.id);
		
		if (!AllNInformer_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':
				var headD = document.createElement("div");
				headD.style.color = this.bgcolor;
				headD.style.backgroundColor = this.text_color;
				headD.style.margin = '0';
				headD.style.padding = '2px 5px';
				headD.style.font = 'normal 9px Arial, Tahoma, sans-serif';
				headD.style.textAlign = 'right';
				if ( this.height > 10 )
					headD.style.height = '10px';
				headD.innerHTML = 'provided by <a href="http://allnewspoint.com" style="text-decoration: underline; color: '+headD.style.color+'">allnewspoint.com</s>';
				headD.title = "Запрос: '"+this.data.request+"', отработка по формуле '"+this.data.formula+"'";
				AllNInformer_Container.appendChild(headD);

				var bodyD = document.createElement('div');
				if ( this.height > 10 )
					bodyD.style.overflowY = 'auto';
				bodyD.style.cursor = 'pointer';
				bodyD.style.width = (this.width-12)+"px";
				if ( this.height > 10 )
					bodyD.style.height = (this.height-16)+"px";
				bodyD.style.padding = "0 5px";
				bodyD.style.borderWidth = "0";
				AllNInformer_Container.appendChild(bodyD);
				
				if (this.data.command == 'updateInformerViews')
				{
					this.prepareBox(AllNInformer_Container);
					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.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.marginTop = "0.8em";
						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";
						
						// Creating text
						var texta = document.createElement("a");
						texta.href = href;
						texta.style.display = 'block';
						texta.style.textDecoration = 'none';
						if (this.target_blank)
						{
							texta.target="_blank";
						}
						// Header
						AllNInformer_Headers[i] = document.createElement("span");
						AllNInformer_Headers[i].style.color = this.header_color;
						AllNInformer_Headers[i].style.fontSize = this.header_size + "px";
						AllNInformer_Headers[i].style.fontWeight = "bold";
						AllNInformer_Headers[i].style.textDecoration = "underline";
						AllNInformer_Headers[i].innerHTML = header;
						// Text
						if(this.onlyheader!=1)
						{
							AllNInformer_Texts[i] = document.createElement("div");
							AllNInformer_Texts[i].style.color = this.text_color;
							AllNInformer_Texts[i].style.fontSize = this.text_size + "px";
							AllNInformer_Texts[i].innerHTML = text;
						}
						texta.appendChild(AllNInformer_Headers[i]);

						if(this.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";
							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]);
					}
				}
				
				AllNInformer_Container.style.backgroundRepeat = 'no-repeat';
				AllNInformer_Container.style.backgroundPosition = 'top left';
				AllNInformer_Container.style.backgroundImage = 'url("http://allnewspoint.com/static/images/i_bg.png")';
				
				
				break;
			case 'error':
				
				break;
			case 'stub':
					this.prepareBox(AllNInformer_Container);
					AllNInformer_Container.innerHTML = this.data.data;
				break;
		}
	}
}
