﻿/*  ajaxInfo
	pobiera i przetwarza listę plików wideo
	
	Parametry:
	objName = nazwa obiektu
	div = ID diva, który zostanie utworzony
	file = plik z xmlem do wczytania
	
	przykładowe wywołanie:
	<script type="text/javascript">
		var info = new ajaxInfo("info","infoContent","xml_info.aspx");
	</script>
*/ 

function Cut(size,txt,dots)
{
	if (txt.length > size)
	{
		return (txt.substring(0, txt.lastIndexOf(' ',size)) + dots);
	}
	return txt;
}
function ajaxInfo(objName,div,file,imgpath)
{
	this.name = objName;
	
	this.filepath = imgpath;
	
	this.pointer = 0;
	this.pageSize = 4;
	this.id = [];
	this.img = [];
	this.date = [];
	this.lead = [];
	this.leader = [];
	this.title = [];
	this.url = [];
	this.category = [];
	
	this.mouseoverBol = 0;
	this.xml = null;
	this.showdiv = div;
	this.pagerdiv = div + "Pager";
	
	document.write('<div id="'+ div +'"><div style="font-family:Tahoma, Helvetica, sans-serif;font-size:11px;"><img src="http://www.polskieradio.pl/_img/loading.gif" align="top" vspace="2"> Ładowanie...</div></div>');
	document.write('');
	document.write('<div id="'+ div +'Pager"></div>');
	
	this.request = new function()
	{
		if (window.XMLHttpRequest) {
			request = new XMLHttpRequest(); // Mozilla, Safari, Opera
		} else if (window.ActiveXObject) {
			try {
				request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
			} catch (e) {
				try {
					request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
				} catch (e) {}
			}
		}
		
		return request;
	}
	
	this.getXMLfile(file);
}

ajaxInfo.prototype.getXMLfile = function(file)
{
	if (this.request)
	{
		var instanceOfTicker=this;
		var url=file;
		this.request.onreadystatechange=function(){instanceOfTicker.handleResponse();}
		this.request.open('GET', url, true);
		this.request.send(null);
	}
}

ajaxInfo.prototype.handleResponse = function()
{ 
	var tmp="";
	var tmpdate="";
	
	if (this.request.readyState == 4)
	{
		if (this.request.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			this.xml = this.request.responseXML;
			
			for (var i=0; i<this.xml.getElementsByTagName("article").length; i++)
			{
				try
				{
					var node = this.xml.getElementsByTagName("article");
					this.category[i] = node[i].getElementsByTagName("category")[0].firstChild.nodeValue;
					this.id[i] = node[i].getElementsByTagName("id")[0].firstChild.nodeValue;
					this.tmpdate = node[i].getElementsByTagName("date")[0].firstChild.nodeValue.substring(0,16);
					this.date[i] = this.tmpdate.substring(8,10) + "-" +this.tmpdate.substring(5,7) + "-" + this.tmpdate.substring(0,4) +" " +this.tmpdate.substring(11,16); //2008-03-31 23:09
					this.title[i] = node[i].getElementsByTagName("title")[0].firstChild.nodeValue;
					this.leader[i] = (node[i].getElementsByTagName("leader")[0].firstChild != null)? node[i].getElementsByTagName("leader")[0].firstChild.nodeValue : "";
					tmp = (node[i].getElementsByTagName("lead")[0].firstChild != null)? node[i].getElementsByTagName("lead")[0].firstChild.nodeValue : "";				
					this.lead[i] = 	Cut(100,tmp,'...');
					this.img[i] = (node[i].getElementsByTagName("img")[0].firstChild != null)? node[i].getElementsByTagName("img")[0].firstChild.nodeValue : "";
					this.url[i] = node[i].getElementsByTagName("url")[0].firstChild.nodeValue;
				
					var instanceOfTicker=this
					document.getElementById(this.showdiv).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
					document.getElementById(this.showdiv).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
				}
				catch(ev)
				{
					alert(ev.message);
				}
			}
			this.writemsg();
		}
	}
}

ajaxInfo.prototype.writemsg = function()
{
	this.html(this.pointer);
}

ajaxInfo.prototype.html = function (pointer)
{
	var number = pointer;
	var number2 = pointer;
	document.getElementById(this.showdiv).innerHTML = "";
	//var pageSize = this.xml.getElementsByTagName("article").length;
	for (var i=0; i<this.pageSize; i++)
	{
		//img
		var img = "";
		if(this.img[number] != "")
		{
			img = '<span class="img"><img src="'+ this.filepath + this.img[number] +'"></span>';
		}
		
		document.getElementById(this.showdiv).innerHTML +='<div> \
			<span class="category">'+ this.category[number] +'</span> \
			<a href="'+ this.url[number] +'?id='+this.id[number]+'" title="'+this.title[number]+'" > \
			<span class="date">'+this.date[number]+'</span> \
			<span class="title">'+this.title[number]+'</span> \
			<span class="leader">'+this.leader[number]+'</span> \
			'+ img +' \
			<span class="lead">'+this.lead[number]+'</span> \
			</a> \
			</div>';
		if (number == this.title.length-1) number = -1;
		if (number < 0) number == this.title.length-1;
		number++;
	}
	
	if(this.xml.getElementsByTagName("article").length > this.pageSize)
	{
		document.getElementById(this.pagerdiv).innerHTML = '<div id="'+ this.showdiv +'_prev"><span>poprzednie</span></div>';
		document.getElementById(this.pagerdiv).innerHTML += '<div id="'+ this.showdiv +'_next"><span>następne</span></div>';

		// wiązanie akcji do strzałek
		var instanceOfTicker=this;
		document.getElementById(this.showdiv + "_prev").onmouseover=function(){instanceOfTicker.mouseoverBol=1}
		document.getElementById(this.showdiv + "_prev").onmouseout=function(){instanceOfTicker.mouseoverBol=0}
		document.getElementById(this.showdiv + "_prev").onclick=function(){instanceOfTicker.prv()}
		
		document.getElementById(this.showdiv + "_next").onmouseover=function(){instanceOfTicker.mouseoverBol=1}
		document.getElementById(this.showdiv + "_next").onmouseout=function(){instanceOfTicker.mouseoverBol=0}
		document.getElementById(this.showdiv + "_next").onclick=function(){instanceOfTicker.next()}
	}
}

ajaxInfo.prototype.prv = function()
{
	if (this.pointer > 0) this.pointer--; else this.pointer = this.lead.length-1;
	this.html(this.pointer);
}

ajaxInfo.prototype.next = function()
{
	this.html(this.pointer);
	if (this.pointer < this.lead.length-1)this.pointer++; else this.pointer = 0;
}

ajaxInfo.prototype.show = function(arg)
{
	this.pointer = arg;
	this.html(this.pointer);
}
