//
//	Gallery Class Declaration
//	- initialize()
//	- start()
//	- changeCaption()
//	- close()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//

var Gallery = Class.create();

var preloadimages = new Array();

var DESCRIPTIONWIDTH = 700;

Gallery.prototype = {
	
	// initialize()
	//
	initialize: function() {
		
		if (!document.getElementsByTagName){ return; }
		
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('exhibitiongallery'))){
				anchor.onclick = function () {myGallery.start(this); return false;}
			}
		}
		
		var objBody = document.getElementsByTagName("body").item(0);

		// Create (semi-transparent) overlay layer but hide it
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.onclick = function() {
				myGallery.close();
		};
		objBody.appendChild(objOverlay);
		Element.hide('overlay');
		     
		// Create container for images
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objBody.appendChild(objLightbox); 

		// Create container for description
		/*var objDescriptionContainer = document.createElement("div");
		objDescriptionContainer.setAttribute('id','descriptionContainer');
		objBody.appendChild(objDescriptionContainer); 
		*/
		   
		// Add "X" close symbol to description container
		/*
		var objXCloseLink = document.createElement("a");
		objXCloseLink.setAttribute('id','closeLink');
		objXCloseLink.setAttribute('href','#');
		objXCloseLink.onclick = function() { myGallery.close(); return false; }
		objDescriptionContainer.appendChild(objXCloseLink);
		var objXClose = document.createElement("img");
		objXClose.setAttribute('src','/images/x.gif');
		objXCloseLink.appendChild(objXClose);
		*/

		// Add description paragraph to description container
		/*var objImgDescription = document.createElement("p");
		objImgDescription.setAttribute('id','descriptionParagraph');
		objImgDescription.innerHTML="";
		objDescriptionContainer.appendChild(objImgDescription);
		*/
		
		// Hide image and description containers
		Element.hide('lightbox');
		//Element.hide('descriptionContainer');
		

		
	},
	
	start: function(imageLink) {	
		
		var series = imageLink.getAttribute('rel').substring(17);
		
		if (!document.getElementsByTagName){ return; }
		
		var arrayPageSize = getPageSize();
		
		var pageWidth = arrayPageSize[0];
		var pageHeight = arrayPageSize[1];
		
		var objLightbox = document.getElementById("lightbox");
		
		var count = 0;
		
		var imagesloaded=0;
		
		new Ajax.Request('/exhibitions/exhibitionpictures?exhibitionseries='+series, {
			method: 'get',
			onComplete: function(transport) {
				var pictures = transport.responseXML.getElementsByTagName('picture');
				var theImages = new Array(pictures.length);

				for (i=0; i< pictures.length; i++) {
					var picture = pictures[i];
					var filenode = picture.firstChild;
					var artistnode = filenode.nextSibling;
					var untitlednode = artistnode.nextSibling;
					var titlenode = untitlednode.nextSibling;
					var captionnode = titlenode.nextSibling;
					var dimensionsnode = captionnode.nextSibling;
					var widthnode = dimensionsnode.nextSibling;
					var heightnode = widthnode.nextSibling;
					var file = filenode.firstChild.nodeValue;
					var info = '';
					if (artistnode.firstChild) info += artistnode.firstChild.nodeValue + '<br/>';
					if (untitlednode.firstChild) {
						if (untitlednode.firstChild.nodeValue == 'true') info += 'Untitled<br/>';
						else {
							if (titlenode.firstChild) info += '<span class="image_title">'+titlenode.firstChild.nodeValue+'</span><br/>';
						}
					}
					if (captionnode.firstChild) info += captionnode.firstChild.nodeValue + '<br/>';
					if (dimensionsnode.firstChild) info += dimensionsnode.firstChild.nodeValue;
					var width = widthnode.firstChild.nodeValue;
					var height = heightnode.firstChild.nodeValue;
					// For each image, create an array to store dimension/positioning information.
					// theImages[i][0] : width
					// theImages[i][1] : height
					// theImages[i][2] : min. x value for positioning of image center
					// theImages[i][3] : min. y value for positioning of image center
					// theImages[i][4] : max. x value for positioning of image center
					// theImages[i][5] : max. y value for positioning of image center
					// theImages[i][6] : position candidate (center of image), x value
					// theImages[i][7] : position candidate (center of image), y value
					// theImages[i][8] : name of div
					theImages[i]=new Array(9);
					theImages[i][0] = width;
					theImages[i][1] = height;
					var objImgContainer = document.createElement('div');
					var objImgContainerId =imageLink.getAttribute('rel')+'_'+i;
					objLightbox.appendChild(objImgContainer);
					Element.hide(objImgContainer);objImgContainer.setAttribute('id', objImgContainerId);
					objImgContainer.style.position = 'absolute';
					var objImg = document.createElement("img");
					objImg.setAttribute('id',imageLink.getAttribute('rel')+'_'+i+'pic');
					objImg.setAttribute('src',file);
					objImgContainer.appendChild(objImg);
					
					
					var objDescriptionParagraph = document.createElement("p");
					//objDescriptionParagraph.setAttribute('class','imgDescription');
					objDescriptionParagraph.setAttribute('id','description'+i);
					objDescriptionParagraph.setAttribute('class','imgDescription');
					//objDescriptionParagraph.style.backgroundColor = "yellow";
					objDescriptionParagraph.style.position = "absolute";
					
					objDescriptionParagraph.style.width = DESCRIPTIONWIDTH+'px';
					
					objDescriptionParagraph.innerHTML= info;
					
					objLightbox.appendChild(objDescriptionParagraph);
					Element.hide(objDescriptionParagraph);
					
					ADD_DHTML(objImgContainer.id);
					dd.elements[objImgContainer.id].myDescription = info;
					dd.elements[objImgContainer.id].resizeTo(width, height);
					dd.elements[objImgContainer.id].setPickFunc(myGallery.pickUpdate);
					
					dd.elements[objImgContainer.id].setDragFunc(myGallery.dragUpdate);
					theImages[i][8] = objImgContainer.id;
					//dd.elements[objImgContainer.id].moveTo(Math.floor(Math.random()*(arrayPageSize[0]-width)),Math.floor(Math.random()*(arrayPageSize[1]-height)));
					//myGallery.changeCaption(info);
				}


				// Calculate margins
				for (i=0; i < theImages.length; i++) {
					theImages[i][2] = theImages[i][0]/2;
					theImages[i][4] = arrayPageSize[2]-theImages[i][2];
					theImages[i][3] = theImages[i][1]/2;
					theImages[i][5] = arrayPageSize[3]-theImages[i][3];
				}

				// Make sure padding between images is large enough
				var donePositioning = false;
				var iterations = 0;
				
				var diameter = Math.sqrt(Math.pow(arrayPageSize[2],2)+Math.pow(arrayPageSize[3],2))
				var minimalDistance = diameter/(theImages.length*2);
					
				while (!donePositioning && iterations < 100) {
					iterations++;
					// Throw darts: Randomly find positions for image centers.
					for (i=0; i < theImages.length; i++) {
						theImages[i][6] = Math.floor(Math.random()*(theImages[i][4]-theImages[i][2]))+theImages[i][2];
						theImages[i][7] = Math.floor(Math.random()*(theImages[i][5]-theImages[i][3]))+theImages[i][3];
					}
				
					donePositioning = true;
					
					// For each image
					for (i=0; i < theImages.length; i++) {
						// Check distance to every other image
						for (j=0; j < theImages.length; j++) {
							if (i!=j) {
								var distance = Math.sqrt(Math.pow(theImages[j][6]-theImages[i][6],2)+Math.pow(theImages[j][7]-theImages[i][7],2));
								if (distance < minimalDistance)
									donePositioning = false;
							}
						}
					}
				}
				
									
				//Set positions.
				for (i=0; i < theImages.length; i++) {
					dd.elements[theImages[i][8]].moveTo(theImages[i][6]-theImages[i][0]/2, theImages[i][7]-theImages[i][1]/2);
					Element.show(theImages[i][8]);
					document.getElementById("description"+i).style.left = theImages[i][6]-DESCRIPTIONWIDTH/2+'px';
					document.getElementById("description"+i).style.top = theImages[i][7]+theImages[i][1]/2+'px';
					Element.show("description"+i);
				}		
				
				/*var report = "Report (min. distance:"+minimalDistance+", "+iterations+" iterations):\n";	
				for (i=0; i < theImages.length; i++) {
					report += "Image "+i+"\n";
					report += "Distances: ";
						// Check distance to every other image
					for (j=0; j < theImages.length; j++) {
						if (i!=j) {
							var distance = Math.floor(Math.sqrt(Math.pow(theImages[j][6]-theImages[i][6],2)+Math.pow(theImages[j][7]-theImages[i][7],2)));
							report += j+":"+distance + " ";
						}
					}
					report += "\n";
				}
				alert(report); */
				
			}
		});
		


		
		Element.setHeight('overlay', arrayPageSize[1]);
		if (navigator.platform.indexOf("Linux") == -1)
			new Effect.Appear('overlay', {from: 0.0, to: 0.8});
		else
			Element.show('overlay');
		Element.show('lightbox');
		// Element.show('descriptionContainer');
		
		
	},
	changeCaption: function(caption) {
		/*
		var descriptionP = document.getElementById('descriptionParagraph');
		if (descriptionP)
			descriptionP.innerHTML = caption;
		*/
	},
	
	pickUpdate: function() {
		var numba = dd.obj.name.substr(dd.obj.name.indexOf('_')+1);
		document.getElementById('description'+numba).style.zIndex = dd.obj.z;
	},
	
	dragUpdate: function() {
		var numba = dd.obj.name.substr(dd.obj.name.indexOf('_')+1);
		var styleTop = document.getElementById(dd.obj.name).style.top;
		document.getElementById('description'+numba).style.top = parseInt(styleTop.substr(0, styleTop.indexOf('px')))+dd.obj.h+'px';
		document.getElementById('description'+numba).style.left = dd.obj.x-DESCRIPTIONWIDTH/2+dd.obj.w/2+'px';
	},

	
	close: function() {
		var lightbox = document.getElementById('lightbox');
		
		while (lightbox.hasChildNodes()) {
		
			
			// Remove dragndrop functionality
			if (lightbox.firstChild.id.indexOf('description') == -1)
				dd.elements[lightbox.firstChild.id].del();
			
			// Remove image div/description from DOM
			lightbox.removeChild(lightbox.firstChild);
		}
		//myGallery.changeCaption('');
		//Element.hide('descriptionContainer');
		//Element.hide('lightbox');
		Element.hide('overlay');
	}
}



function initGallery() {
	myGallery = new Gallery();
}

Event.observe(window, 'load', initGallery, false);

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});


function my_PickFunc()
{
	myGallery.changeCaption(dd.obj.myDescription);
}

SET_DHTML(CURSOR_MOVE);

		
