window.addEvent('domready', function() {
	
	var tipMe = new Tips($$('.styleTip'), {
		showDelay: 400,
		
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 600, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
	
	
/* END DOM READY */	
});

function putInCentre(lyr, h, w) { 

var d = document; 
var rootElm = (d.documentElement && d.compatMode == 'CSS1Compat') ? d.documentElement : d.body; 


var top = (d.body.scrollTop > rootElm.scrollTop) ? d.body.scrollTop : rootElm.scrollTop;

var vpw = self.innerWidth ? self.innerWidth : rootElm.clientWidth; // viewport width 
var vph = self.innerHeight ? self.innerHeight : rootElm.clientHeight; // viewport height 
lyr.style.left = ((vpw - w) / 2) + 'px';  

var dtop = top + (vph - h)/2;
dtop = dtop - 20;
dtop = (dtop > 2) ? dtop : 2;
lyr.style.top = dtop +'px'; 


lyr.style.visibility = "visible";
} 



function GetCollection(xmldoc, url){
	var xmlrows = xmldoc.getElementsByTagName("collection");
	var text;

	for (var r=0; r < xmlrows.length; r++){
		var xmlrow = xmlrows[r];
		var Name = xmlrow.getAttribute("Name");
		var xmldescription = xmlrow.getElementsByTagName("description")[0];

		if (xmldescription.firstChild){
			var description = xmldescription.firstChild.data;
			document.getElementById("gDescription").innerHTML = '<strong>'+Name+'</strong>: ' + description;
		} else {
			description = "";
			document.getElementById("gDescription").innerHTML = '<strong>'+Name+'</strong>';	
		}
		var imagerows = xmlrow.getElementsByTagName("image");			
		var thumbs = document.createDocumentFragment();

		for (var c = 0; c < imagerows.length; c++){
			var imagerow = imagerows[c];
			var imagename = imagerow.getAttribute("imagename");

			var height = imagerow.getElementsByTagName("height")[0].firstChild.data;
			var width = imagerow.getElementsByTagName("width")[0].firstChild.data;
			if (imagerow.getElementsByTagName("title")[0].firstChild){
				var title = imagerow.getElementsByTagName("title")[0].firstChild.data;
			}
			if (imagerow.getElementsByTagName("description")[0].firstChild){			
				var image_description = imagerow.getElementsByTagName("description")[0].firstChild.data;
			} else {
				var image_description = ' ';			
			}
			if (imagerow.getElementsByTagName("photographer")[0].firstChild){			
				var image_description = image_description + " - Photo By " + imagerow.getElementsByTagName("photographer")[0].firstChild.data;
			} 
			if (c == 0){
				var oImg=document.getElementById("gImage");
				oImg.setAttribute('alt', title);
				oImg.setAttribute('src', imagename);
				oImg.setAttribute('height', height +'px');
				oImg.setAttribute('width', width +'px');
				oImg.setAttribute('title', image_description);
			}

			var div = document.createElement("div");
			div.className="ThumbDiv";
			div.setAttribute('title', image_description);
			var info = encodeURI( imagename + ":" + height + ":" + width + ":" + image_description);
			div.setAttribute('id', info);

			div.onclick = function(){
				swapMain(this);
				return false;
			}

//			var div.className = "ThumbDiv"; 

			var tImg=document.createElement("img");

			tImg.setAttribute('src', imagename);			

			if (height > width){
				var ratio = 67 / width;
				var iwidth = 67;
				var iheight = Math.round(height * ratio);
			} else {
				var ratio = 50 / height;
				var iheight = 50;
				var iwidth = Math.round(width * ratio);
			}
			tImg.setAttribute('height', iheight+'px');
			tImg.setAttribute('width', iwidth+'px');


			div.appendChild(tImg);
			thumbs.appendChild(div);

		}


		tCell = document.getElementById("gThumbs");
		tCell.innerHTML = '<img src="Images/spacer.gif" width="200" height="1">';
		tCell.appendChild(thumbs);

	}
	return(false);
}

function PopCollection(cid){
	lyr = document.getElementById('ImagePop');
	var dh = 580;
	var dw = 900;
	lyr.style.height = dh + 'px';
	lyr.style.width = dw + 'px';		

	putInCentre(lyr, dh, dw);
//	document.getElementById('ImageContent').innerHTML = '<img src="'+iname+'">';
var d = document; 
var rootElm = (d.documentElement && d.compatMode == 'CSS1Compat') ? d.documentElement : d.body; 
	rootElm.style.overflowX = 'hidden';
var top = (d.body.scrollTop > rootElm.scrollTop) ? d.body.scrollTop : rootElm.scrollTop;

var vpw = self.innerWidth ? self.innerWidth : rootElm.clientWidth; // viewport width 
var vph = self.innerHeight ? self.innerHeight : rootElm.clientHeight; // viewport height 

	document.getElementById('iShim').style.width = "1" + vpw + "px";
	document.getElementById('iShim').style.height = "1" + vph + "px";
	document.getElementById('iShim').style.display = "block";
	document.getElementById('iShim').zIndex = "50";


	var fb = new Date();
	
	var url = 'xgcol.php?cid='+cid+'&fb='+fb.getMilliseconds();
	var request = HTTP.newRequest();
	request.onreadystatechange = function() {
		if (request.readyState==4) { 
			if (request.status == 200){
				GetCollection(request.responseXML, url);
			}
		}
	}
	request.open("GET", url, true);
	request.setRequestHeader("User-Agent", "XMLHttpRequest");
	request.send(null);

	return(false);
}
function hidePop(){
	document.getElementById('ImagePop').style.visibility = "hidden";
	document.getElementById('iShim').style.display = "none";	
	var rootElm = (document.documentElement && d.compatMode == 'CSS1Compat') ? document.documentElement : document.body; 
	rootElm.style.overflowX = 'auto';

	return(false);
}

function swapMain(image){	
	var idetails = decodeURI(image.id).split(":");
	var height = idetails[1] + 'px';
	var width = idetails[2] + 'px';
	var title = idetails[3];
	var oImg=document.getElementById("gImage");
	oImg.setAttribute('src', "Images/spacer.gif");

	oImg.setAttribute('src', idetails[0]);
	oImg.setAttribute('height', height);
	oImg.setAttribute('width', width);
	oImg.setAttribute('title', title);

}

