Gallery=function(url,el, amount){
	var myConn = new XHConn();
	if (!myConn){
		document.getElementById(el).innerHTML ="Your browser does not support this content.";
		return false;
	}
	// reference itself
	var self=this;
	this.el=el;
	this.amount=amount;
	this.onLoad=function(xhttp){
		//alert("loaded");
		self.xhttp=xhttp;
		self.populate();
	}
	// append unique timestamp
	var d=new Date();
	
	GalleryObj.isOpen=true;
	myConn.connect(url, "GET", "cacheKiller="+d.getTime(), this.onLoad);
}
//
Gallery.prototype.getChildNodeByName=function(node,name){
	for(var n=node.firstChild;n;n=n.nextSibling){
		if(n.nodeName==name){
			return n
			break;
		}
	}
}
//
Gallery.prototype.getChildValueByName=function(node,name){
	for(var n=node.firstChild;n;n=n.nextSibling){
		if(n.nodeName==name){
			if(n.firstChild!=undefined){
				return (n.firstChild.nodeValue);
				break;
			}
		}
	}
}
//
Gallery.prototype.populate = function() {
	with(this){
		var xmldoc = xhttp.responseXML;
		var root_node = xmldoc.getElementsByTagName('site').item(0);
		var wrappers=[];
		var html = '';
		// digging down the xml data
		for (var n = root_node.firstChild; n; n=n.nextSibling) {
			// gallery node
			if(n.nodeName=='gallery'){;	
				html += "<div id='"+n.getAttribute('id')+"' name='"+n.getAttribute('name')+"'>";
				//html += "<h2>"+n.getAttribute('name')+"</h2>";
				var i=0;
				for (var nn = n.firstChild; nn; nn=nn.nextSibling) {
					if (nn.nodeName == 'album') {
						// album node
						var depth=1;
						var id=nn.getAttribute('id');
						var name=nn.getAttribute('name');
						if(name.indexOf("'")!=-1){
							name=(name.split("'")).join("&#145;");
						}
						//
						//html += "<h3><a href=\"#\" onclick=\"(new fx.Height('"+id+"_wrapper',{transition: fx.cubic})).toggle();\" >"+name+"</a></h3>";
						html += "<div id='"+id+"_wrapper' style=\"height:0px\" class='albumwrapper'>";
						wrappers.push(id+"_wrapper");
						html += "<div class='albumbg'></div>";
						html += "<div id='"+id+"' name='"+name+"' class='album' style=\"display:none\">";
						html += "<div class='spacer'></div>"
						var ii=0;
						for (var nnn = nn.firstChild; nnn; nnn=nnn.nextSibling) {
							if (nnn.nodeName == 'photo') {
								if(ii<amount || amount==undefined){
									var caption=(getChildValueByName(nnn, 'caption'));
									var id=nn.getAttribute('id');
									caption=caption!=undefined ? caption : '';
									if(caption.indexOf("'")!=-1){
										caption=(caption.split("'")).join("&#145;");
									}
									//
									var thumb={};
									thumb.w=(getChildNodeByName(nnn, 'thumb')).getAttribute('width');
									thumb.h=(getChildNodeByName(nnn, 'thumb')).getAttribute('height')
									thumb.src=(getChildNodeByName(nnn, 'thumb')).getAttribute('src');
									//
									var img={};
									img.w=(getChildNodeByName(nnn, 'img')).getAttribute('width');
									img.h=(getChildNodeByName(nnn, 'img')).getAttribute('height');
									img.src=(getChildNodeByName(nnn, 'img')).getAttribute('src');
									//
									html += "<div class='thumb'>";
									var title=name+" : "+depth;
									html += "<a href=\"#\" onclick=\"populateChild('"+img.src+"',"+img.w+","+img.h+",'"+escape(caption)+"','"+title+"','"+id+"',"+(depth-1)+");\"><img src=\""+thumb.src+"\" width="+thumb.w+" height="+thumb.h+" border=\"0\" alt=\"\"/></a>";
									html +="</div>"
									depth++;
								}else{
									break
								}
								ii++;
							}
						}
						html += "<div class='spacer'></div>"
						html += "</div></div>";
						i++;
					}
				}
				html += "<div class='spacer'></div>"
				html += "</div>";
			}
		}
		document.getElementById(el).innerHTML = html;
		i=0;
		expandID=setInterval(
        function(){
			if(i<wrappers.length){
				var id=wrappers[i]
				document.getElementById(id.substring(0,id.length-8)).style.display="block";
				(new fx.Height(id,{duration: 300})).toggle();
				i++;
			}else{
				clearInterval(expandID);		
			}
        },
        100);
		//
		if (GalleryObj.index!=undefined){
			findAnchor(GalleryObj.id,GalleryObj.index);
			delete GalleryObj.index
			delete GalleryObj.id
		}
	}
};
//
var GalleryObj={}
var win=0;
//
setGalleryObject=function(id,index){
	GalleryObj.index=index;
	GalleryObj.id=id;
}
//
findAnchor=function(id,index){
	a = (document.getElementById(id)).getElementsByTagName("A");
	c=a[Number(index)].onclick;
	c();
}
//
manageGallery=function(){
	if(	GalleryObj.isOpen){
		if (GalleryObj.index!=undefined){
			findAnchor(GalleryObj.id,GalleryObj.index);
			delete GalleryObj.index
			delete GalleryObj.id
		}
	}else{
		changeSection('images.html', 'IMAGES');
	}
}
//
populateChild = function(src, width, height, cap, title, id, index) {
	if(!win.closed && win!=0){
		win.close();
	}
	//
	win=(new Popup()).center('child.html',width+20,height+75);
	//
	stopLoad=function(){
		clearInterval(loadID);
	}
	//
	loadID=setInterval(
	function(){
		var atags = (document.getElementById(id)).getElementsByTagName("A");
		//
		if(win.isloaded!=undefined){
			clearInterval(loadID);
			//alert(atags[(index-1)])
			if(index-1>=0){
				win.prev=atags[(index-1)].onclick;
			}else{
				(win.document.getElementById('previous')).innerHTML="";
			};
			//
			if(atags[(index+1)]==undefined){
				(win.document.getElementById('next')).innerHTML="";
			}else{
				win.next=atags[(index+1)].onclick;
			};
			//
			(win.document.getElementById('image')).innerHTML ='<img src="'+src+'" />';
			if(cap.length>0){
				(win.document.getElementById('caption')).innerHTML = unescape(cap);
			}
			win.document.title=title;
			
		}
	},
    100);
}