/*
gallery.js
(C) 2006 Covagre Ltd
*/

var Gallery = {
	init: function() {
		// Add onclick behaviour to all links with rel="lightbox"
		var links = document.getElementsByTagName("a");
		for(var i=0; i<links.length; i++) {
			if(links[i].href && (links[i].rel=="gallery"))
				links[i].onclick = function(){ Gallery.clickopen(this); return false; };
		}
		
		// Create project window
		this.gallery = document.createElement("div");
		this.gallery.setAttribute("id", "gallery_window");
		this.gallery.style.display = "none";
		
    //document.body.appendChild(this.project);
    document.body.appendChild(this.gallery);
	},
	
	clickopen: function(obj) {
		var sizeAndScroll = Common.getSizeAndScroll();
		// show overlay & 'progress bar'
		Common.showOverlay();
		Common.showLoading();
		Common.loadingLink.onclick = this.clickclose.bind(this);
    
		var that = this;
		var name = obj.href;
		// load description
    
    this.ajax = new Ajax.Updater(
		{success: 'gallery_window'}, name, 
		{method: 'get', evalScripts:true, onFailure: this.clickclose, onComplete: this.go}
		);
		
		return false;
	},
  
  go: function() {
    var sizeAndScroll = Common.getSizeAndScroll();
    Common.hideLoading();
    Common.hideOverlay();
    $("gallery_window").style.display = "block";
    $("gallery_window").style.left = (sizeAndScroll[0])/2-460 + 'px';
    Common.showOverlay();
    var links = document.body.getElementsByTagName("a");
		for(var i=0; i<links.length; i++) {
			if(links[i].href && (links[i].rel=="close"))
				links[i].onclick = function(){ Gallery.clickclose(this); return false; };
		}
  },
	// }}}
	clickclose: function(obj) {
		Common.hideOverlay();
		Common.hideLoading();
		this.gallery.style.display = "none";
    this.gallery.innerHTML = "";
	}

}


Common.onLoadEvent(Gallery.init.bind(Gallery));

