﻿
function ajaxGallery(objName,div,file,imgpath,imgsizebig,imgsizemini,galeriaid)
{
	this.imglist="";
	
	this.name = objName;
	this.filepath = imgpath;
	
	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.imgsizemini = imgsizemini;
	this.galeriaid = galeriaid;
	this.mouseoverBol = 0;
	this.xml = null;
	this.showdiv = div;
	this.pagerdiv = div + "Pager";
	this.allimagesdiv = div + "AllImages";
	this.images = new Array();
	
	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('');
	

	
	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()
{ 
	if (this.request.readyState == 4)
	{
		if (this.request.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			this.xml = this.request.responseXML;
			
			
			a = 0;
			for (var i=0; i<=this.xml.getElementsByTagName("photos")[0].getAttribute("count")-1; i++)
			{
				try
				{
					if(a==6) break;
					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.img[a] = this.id[a] + "_" + this.imgsizebig + "." + this.type[a];
				
					this.imglist +='<div class="mini" style="background:url('+ this.filepath + this.id[a] + "_" + this.imgsizemini + "." + this.type[a] +') no-repeat;" onclick="'+ this.name +'.thisfoto('+a+');"></div>';
					a++;

				}
				catch(ev)
				{
					alert(ev.message);
				}
			}
			this.preload();
			this.writemsg();

		}
	}
}

ajaxGallery.prototype.writemsg = function()
{
	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 ='<a id="photo_big" href="galeria/galeria.aspx?gr=13&gl='+ this.galeriaid +'" style="background:url('+ this.filepath + this.img[number] +') no-repeat;"></a>';
		}
		img +='<div id="photo_mini">'+	this.imglist+ '</div>';
		document.getElementById(this.showdiv).innerHTML = img ;
		
		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 instanceOfTicker=this;
	}
	
}

ajaxGallery.prototype.thisfoto = function(arg)
{
	this.pointer = arg;
	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];
	}
