﻿
function ajaxGallery(objName,div,file,imgpath,imgsizebig,delay)
{
	this.name = objName;
	this.onoff = true;
	this.filepath = imgpath;
	this.delay = delay
	this.pointer = 0;
	this.pageSize = 1;
	this.id = [];
	this.img = [];
	this.type = [];
	this.resolution = [];
	this.author = [];
	this.lead = [];
	this.title = [];
	this.url = [];
	this.imgsizebig = imgsizebig;
	this.mouseoverBol = 0;
	this.xml = null;
	this.showdiv = div;
	this.pagerdiv = div + "Pager";
	this.counter = div + "Counter";
	
	
	this.images = new Array();
	document.write('<div id="'+ div +'Pager"></div>');
	//document.write('<div id="'+ div +'SlideShow" onclick="'+ this.name +'.Slide();"><a href="javascript:void(null);"></a></div>');
	document.write('<div id="'+ div +'"><div><img src="http://www.polskieradio.pl/_img/loading.gif" align="top" vspace="2"> Ladowanie...</div></div>');
	document.write('');

	document.write('<br/>');
	this.pause(100);
	
	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);
}

ajaxGallery.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);
	}
}

ajaxGallery.prototype.handleResponse = function()
{ 
var a;
if (this.request.readyState == 4)
	{
		if (this.request.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			this.xml = this.request.responseXML;
			var	imglist="";
			
			a = 0;
			for (var i=this.xml.getElementsByTagName("photos")[0].getAttribute("count")-1; i>-1; i--)
			{
				try
				{	
						var node = this.xml.getElementsByTagName("photo");
						this.id[a] = node[i].getAttribute("id");
						this.type[a] = node[i].getAttribute("type");
						this.resolution[a] = node[i].getAttribute("resolution");
						this.author[a] = (node[i].getElementsByTagName("author")[0].firstChild != null)? node[i].getElementsByTagName("author")[0].firstChild.nodeValue : ""; 
						this.title[a] = (node[i].getElementsByTagName("title")[0].firstChild != null)? node[i].getElementsByTagName("title")[0].firstChild.nodeValue : "";
						this.lead[a] = (node[i].getElementsByTagName("desc")[0].firstChild != null)? node[i].getElementsByTagName("desc")[0].firstChild.nodeValue : "";
						this.img[a] = this.id[a] + "_" + this.imgsizebig + "." + this.type[a];
			
					var instanceOfTicker=this
					document.getElementById(this.showdiv).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
					document.getElementById(this.showdiv).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
					a++;
				}
				catch(ev)
				{
					alert(ev.message);
				}
			}
			this.preload();
			this.writemsg();
		}
	}
}


ajaxGallery.prototype.Slide = function()
{

	if(this.onoff==true)
	{
		this.slideshow = true;
		this.writemsg();
		this.onoff=false;
	}
	else
	{
		
		this.onoff=true;
		this.slideshow = false;
		this.writemsg();
	}
	
	
}
ajaxGallery.prototype.writemsg = function()
{
	var instanceOfTicker = this
	if (this.slideshow)
	{

		if (this.mouseoverBol==1)
			setTimeout(function(){instanceOfTicker.writemsg()}, this.delay);
		else
		{
			this.html(this.pointer);
			if (this.pointer < this.title.length-1)this.pointer = this.pointer+1; else this.pointer = 0;
			setTimeout(function(){instanceOfTicker.writemsg()}, this.delay);
		}
	}
	else
	{
		this.html(this.pointer);	
	}
	
}

ajaxGallery.prototype.html = function (pointer)
{
	var number = pointer;
	var number2 = pointer;
	document.getElementById(this.showdiv).innerHTML = "";
	
	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>';
		}

		var __str = "";
		__str += '<div>'+ img +'<div id="top"><span id="'+this.counter+'"></span>&#160;|&#160;';
		if(this.title[number] != '')
		{
			__str += '<span class="title">'+this.title[number]+'</span>&#160;|&#160;';
		}
		if(this.lead[number] != '')
		{
			__str += '<span class="lead">'+this.lead[number]+'</span>&#160;|&#160;';
		}
		__str += '<span class="author">foto: '+this.author[number]+'</span></div></div>';
		
		document.getElementById(this.showdiv).innerHTML += __str;

		if (number == this.title.length-1) number = -1;
		if (number < 0) number == this.title.length-1;
		number++;
	}

	if(this.xml.getElementsByTagName("photos")[0].getAttribute("count") > this.pageSize)
	{
		var tmp = '<div id="'+ this.showdiv +'_prev"><span><a href="javascript:void(null);"></a></span></div><div id="'+ this.div +'SlideShow" onclick="'+ this.name +'.Slide();"><a href="javascript:void(null);"></a></div><div id="'+ this.showdiv +'_next"><span><a href="javascript:void(null);"></a></span></div>';
		
		document.getElementById(this.pagerdiv).innerHTML = tmp;
		
		//document.getElementById(this.pagerdiv).innerHTML = '<div id="'+ this.showdiv +'_prev"><span><a href="javascript:void(null);">&#171;&#160;poprzednie</a></span></div>';
		//document.getElementById(this.pagerdiv).innerHTML += '<div id="'+ div +'SlideShow" onclick="'+ this.name +'.Slide();"><a href="javascript:void(null);"></a></div>';
		//document.getElementById(this.pagerdiv).innerHTML += '<div id="'+ this.showdiv +'_next"><span><a href="javascript:void(null);">nastepne&#160;&#187;</a></span></div>';
		document.getElementById(this.counter).innerHTML = pointer+1 +"/"+this.xml.getElementsByTagName("photos")[0].getAttribute("count");
		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()}
	}
	
}


ajaxGallery.prototype.thisfoto = function(arg)
{
	this.pointer = arg;
	this.html(this.pointer);
}
ajaxGallery.prototype.prv = function()
{
	this.slideshow=false;
	if (this.pointer > 0) this.pointer--; else this.pointer = this.lead.length-1;
	this.html(this.pointer);
	
}

ajaxGallery.prototype.next = function()
{
	this.slideshow=false;
	if (this.pointer < this.lead.length-1)this.pointer++; else this.pointer = 0;
	this.html(this.pointer);
}

ajaxGallery.prototype.show = function(arg)
{
	this.pointer = arg;
	this.html(this.pointer);
}

ajaxGallery.prototype.preload = function () {

		for (var i=0; i<=this.img.length-1; i++) {
			this.images[i] = new Image();
			
		}
		
		this.load(0);
		for (var i=0; i<=this.img.length-1; i++) 
		{
			setTimeout((function(obj, j) {return function() { obj.load(j); }})(this, i), i*100);
		}
	}
	
ajaxGallery.prototype.load = function (num) {
		
			this.images[num].src = this.filepath + this.img[num];
	}



ajaxGallery.prototype.pause =function(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}