var Gallery = {
	init: function(){
		Element.insert(document.body, { bottom: '<div id="galleryviewer" class="galleryviewer" style="width: 310px; display:none;"><div class="galleryviewerreset">'+
					   							'<div class="galleryviewerbody"><div id="closeViewer" class="close"><a href="javascript:Gallery.close();"></a></div>'+
												'<div id="imgGalPrev" class="previous" onclick="Gallery.prev();" style="display:none;"></div>'+
												'<div id="imgGalNext" class="next" onclick="Gallery.next();" style="display:none;"></div>'+
												'<div id="imgDisplay"><img src="/images/spacer.gif" width="300" height="300" /></div><div id="imgDetailBox" class="details">'+
												'<span id="imgCount"></span><h5 id="imgCaption"></h5><p id="imgDescription"></p></div></div></div></div>'+
												'<div id="blackout" class="blackout" style="display:none;" onclick="Gallery.close();"></div>' });
		this.cover					= $('blackout');
		this.viewer					= $('galleryviewer');
		this.imgbox					= $('imgDisplay');
		this.caption				= $('imgCaption');
		this.description			= $('imgDescription');
		this.details				= $('imgDetailBox');
		this.counter				= $('imgCount');
		this.closer					= $('closeViewer');
		this.prevBtn				= $('imgGalPrev');
		this.nextBtn				= $('imgGalNext');
		this.total					= this.imagelist.length;
		this.prevIndex				= '';
		this.openedIndex			= '';
		this.nextIndex				= '';
		this.initialized 			= true;
	},
	imagelist : new Array(),
	open: function(ele,dir){
		if(!this.initialized) this.init();
		if(this.openedIndex!='') $('galleryImage_'+this.openedIndex).className = 'image';
		if(this.openedIndex==0) $('galleryImage_0').className = 'image';

		ele.className 			 	 = 'clicked';
		if(this.viewer.style.display == 'block') this.load();
		this.openedIndex 			 = parseFloat(ele.id.replace(/galleryImage_/g,""));
		this.fullPh					 = figure.dimensions().pageheight;
		this.pageH					 = parseFloat(document.viewport.getHeight());
		this.pageW					 = parseFloat(document.viewport.getWidth());
		this.scrollPos				 = parseFloat(document.viewport.getScrollOffsets().top);
		this.cover.style.height		 = this.fullPh+'px';
		this.viewerH 				 = parseFloat(Element.getHeight(this.viewer));
		this.viewerW 				 = parseFloat(Element.getWidth(this.viewer));
		this.viewer.style.top 		 = Math.round((this.scrollPos+(this.pageH/2))-(this.viewerH/2))+'px';
		this.viewer.style.left 	 	 = Math.round((this.pageW/2)-(this.viewerW/2))+'px';
		this.cover.style.display 	 = 'block';
		this.viewer.style.display	 = 'block';
		
		var currImg 		= new Image();
			currImg.onload  = function(){ 
				Gallery.set(currImg);
		};
			currImg.src 	= ele.href;

		this.scrolltop				 = 
		window.onscroll = function(){ 
			window.scrollTo(this.scrollPos,0);
		};
		if(!dir) return false;
	},
	preload: function(){
		if(this.openedIndex < this.total-1){
			this.nextIndex 	= this.openedIndex+1;
			var nextImg 	= new Image();
				nextImg.src = $('galleryImage_'+this.nextIndex).href;
				this.nextBtn.style.visibility = 'visible';
		}else{
			this.nextIndex = '';
			this.nextBtn.style.visibility = 'hidden';
		}
		if(this.openedIndex > 0){
			this.prevIndex = this.openedIndex-1;
			var prevImg 	= new Image();
				prevImg.src = $('galleryImage_'+this.prevIndex).href;
				this.prevBtn.style.visibility = 'visible';
		}else{
			this.prevIndex = '';
			this.prevBtn.style.visibility = 'hidden';
		}
	},
	_mouseover: function(e){
		var mouse	= figure.mouse(e);
		if(mouse.x > this.bindbox.left && mouse.x < this.bindbox.right && mouse.y > this.bindbox.top && mouse.y < this.bindbox.bottom){
			return;
		}else{
			Gallery.prevBtn.style.display = 'none';
			Gallery.nextBtn.style.display = 'none';
			document.onmousemove = function(){};
		}
	},
	buttons: function(){
		document.onmousemove = function(e){
			Gallery._mouseover(e);	
		};
			Gallery.prevBtn.style.display = 'block';
			Gallery.nextBtn.style.display = 'block';
	},
	set: function(currImg){
		this.viewer.style.width		 = (currImg.width+10)+'px';
		var ele						 = $('galleryImage_'+this.openedIndex);
		this.caption.innerHTML		 = ele.title;
		this.description.innerHTML	 = ele.firstDescendant().innerHTML;
		this.counter.innerHTML		 = "Image "+(this.openedIndex+1)+" of "+this.total;
		 var imgsrc					 = currImg.src;
		this.imgbox.innerHTML		 = '<img src="'+imgsrc+'" onmouseover="Gallery.buttons();" />';
		this.viewerH 				 = parseFloat(Element.getHeight(this.viewer));
		this.viewerW 				 = parseFloat(Element.getWidth(this.viewer));
		this.viewer.style.top 		 = Math.round((this.scrollPos+(this.pageH/2))-(this.viewerH/2))+'px';
		this.viewer.style.left 	 	 = Math.round((this.pageW/2)-(this.viewerW/2))+'px';
		this.bindbox 				 = figure.element(this.imgbox);
		//this.details.style.width 	 = (this.viewerW-10)+'px';
		//alert('Gallery.set() : '+this.viewerW+', '+(this.viewerW-10));
		this.preload();
	},
	close: function(){
		this.load();
		this.cover.style.display 	= 'none';
		this.viewer.style.display	= 'none';
		$('galleryImage_'+this.openedIndex).className = 'image';
		this.prevIndex				= '';
		this.openedIndex			= '';
		this.nextIndex				= '';
		window.onscroll 			= '';
	},
	load:function(img){
		this.currImgH 				= parseFloat(Element.getHeight(this.imgbox));
		this.currImgW 				= parseFloat(Element.getWidth(this.imgbox));
		this.imgbox.innerHTML 	 	= '<img src="/images/spacer.gif" width="'+this.currImgW+'" height="'+this.currImgH+'" />';
	},
	next: function(){
		this.open($('galleryImage_'+this.nextIndex),true);
	},
	prev: function(){
		this.open($('galleryImage_'+this.prevIndex),true);
	}
};










var figure = {
 page: function(){
		var pd = this.dimensions();
		return {height: pd.pageheight, width: pd.pagewidth}
 },
 element: function(ele){
	 	var ele = $(ele);
		var cacheEle = ele;
		var top=right=bottom=left=height=width=0;
		if (ele.offsetParent) {
				top   = cacheEle.offsetTop;
				left  = cacheEle.offsetLeft;
			while (cacheEle = cacheEle.offsetParent) {
				top  += cacheEle.offsetTop;
				left += cacheEle.offsetLeft;
			}
		}
		var display = ele.style.display;
		if (display != 'none' && display != null){
			width = ele.offsetWidth;
			height= ele.offsetHeight;
		}else{
			var els = ele.style;
			var originalVisibility = els.visibility;
			var originalPosition = els.position;
			var originalDisplay = els.display;
				els.visibility = 'hidden';
				els.position = 'absolute';
				els.display = 'block';
			var originalWidth = ele.clientWidth;
			var originalHeight = ele.clientHeight;
				els.display = originalDisplay;
				els.position = originalPosition;
				els.visibility = originalVisibility;
			width = originalWidth;
			height= originalHeight;
		}
		if(width==0 && height==0){
			width = ele.width;
			height= ele.height;
		}
		if(width==0 && height==0){
			width = ele.style.width;
			height= ele.style.height;
		}
		right = left+width;
		bottom= top+height
			return {top: top, right:right , bottom: bottom, left: left, height: height, width: width };
 },
 viewport: function(){
		var vpd = this.dimensions();
		return {height: vpd.windowheight, width: vpd.windowwidth}
 },
 dimensions: function(){
		var xScroll, yScroll, windowWidth, windowHeight;
		if(window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}else if(document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}else{
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		if(self.innerHeight){
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight){
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}else if(document.body){
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		}else{
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		}else{
			pageWidth = xScroll;
		}
		return {pageheight: pageHeight, pagewidth: pageWidth, windowheight: windowHeight, windowwidth: windowWidth};
},

 scroll: function(){
		var yScroll;
		 if (document.documentElement && document.documentElement.scrollTop){
			yScroll = document.documentElement.scrollTop;
		}else if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
		}
		return yScroll;
 },
 mouse: function(e){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return {x: posx, y: posy};
 }
};




	window.onload = function(){
		window.onload;
		// CHECK FOR IMAGE GALLERY BLOCK AND GENERATE POPUP LINKS
		var arrImages = $A(document.getElementsByClassName('image'));
		if(arrImages.length > 0){
			var i = 0;
			arrImages.each(function(item) { 
				if(Element.readAttribute(item,'rel') == "gallery"){
					item.onclick = function(){return Gallery.open(this)};
					item.id		 = "galleryImage_"+i;
					Gallery.imagelist.push(item);
					i++;
				}
			});	
		}
		windowLoaded = true;
	}