// hGallery hGallery.js v1.0, Fri May 05 2007

// Copyright (c) 2005-2007 Makerweb (http://www.makerweb.it)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For details, see http://www.makerweb.it

var hGallery = Class.create();

hGallery.prototype = {

	initialize: function(divId,xmlUrl,idGallery,imgFolderUrl) {
		this.inprogress = false;
		this.paramList = [];
		this.imageList = [];
		this.idGallery = idGallery;
		this.galleryDiv = document.getElementById(divId);
		this.xmlUrl = xmlUrl;
		this.params = {};
		this.intervalid;
		this.playing = false;
		this.render = false;
		this.overCaption = false;
		this.overImage = false;
		this.imgFolderUrl;
		if (imgFolderUrl===undefined) {
			this.imgFolderUrl = '/';
		} else {
			this.imgFolderUrl = imgFolderUrl+'/hgallery/';
		}
		
		this.Engine = {
			detect: function() {
				var UA = navigator.userAgent;
				this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
				this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
				this.isOpera = /Opera/.test(UA);
				this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
				this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
			}
		};
		this.Engine.detect();
		
	},
	
	load: function() {
		var idGallery = this.idGallery;
		this.getXml();
		if (document.getElementById('scrollGallery') && document.getElementById('scrollGallery').value=='1') document.getElementsByTagName('body')[0].style.overflow = 'hidden';
		Event.observe(window, 'resize', myhGallery[idGallery].refreshOffsets, false);
	},
	
	getXml: function() {
		var idGallery = this.idGallery;
		
		if (window.XMLHttpRequest) {
			var xmlData = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			var xmlData = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlData) {
			xmlData.onreadystatechange = function() {
				if (xmlData.readyState == 4) {
					if (xmlData.status == 200) {
						myhGallery[idGallery].parseXml(xmlData)
					}
				}
			};
			xmlData.open("GET", this.xmlUrl, true);
			xmlData.send("");
		}
	},
	
	trimString: function(sInString) {
		sInString = sInString.replace( /^\s+/g, "" );
		return sInString.replace( /\s+$/g, "" );
	},
	
	parseXml: function(xmlData) {

		var items = xmlData.responseText.split(/<gallery>/g);
		var parameters = items[1].split(/<parameters>/g);
		var images = items[1].split(/<image>/g);

		xmlData = false;
		var lines;
		var no2;
		var key;
		var pattern;
		var value;
		for(var no=1;no<parameters.length;no++){
			lines = parameters[no].split(/\n/g);
			this.paramList[no-1] = [];
			for(no2=0;no2<lines.length;no2++){
				key = lines[no2].replace(/<([^>]+)>.*/g,'$1');
				if (key) { key = this.trimString(key); }
				pattern = new RegExp("<\/?" + key + ">","g");
				value = lines[no2].replace(pattern,'');
				value = this.trimString(value);
				if (key=='/parameters') { break; }
				if (key!=='' && value!=='') {
					this.paramList[no-1][key] = value;
				}
			}
		}

		for(no=1;no<images.length;no++){
			lines = images[no].split(/\n/g);
			this.imageList[no-1] = [];
			for(no2=0;no2<lines.length;no2++) {
				key = lines[no2].replace(/<([^>]+)>.*/g,'$1');
				if (key) { key = this.trimString(key); }
				pattern = new RegExp("<\/?" + key + ">","g");
				value = lines[no2].replace(pattern,'');
				value = this.trimString(value);
				if (key=='/images') { break; }
				if (key!=='' && value!=='') {
					this.imageList[no-1][key] = value;
				}
			}
		}
		this.initializeParams();
		this.createGallery();
		this.populateGallery();
	},

	initializeParams: function() {
		
		this.params.loadingImage = this.imgFolderUrl+'loading.gif';
		this.params.playButton = this.imgFolderUrl+'play.jpg';
		this.params.playButtonHover = this.imgFolderUrl+'playh.jpg';
		this.params.pauseButton = this.imgFolderUrl+'pause.jpg';
		this.params.pauseButtonHover = this.imgFolderUrl+'pauseh.jpg';
		this.params.leftArrow = this.imgFolderUrl+'left.jpg';
		this.params.rightArrow = this.imgFolderUrl+'right.jpg';
		this.params.slider = this.imgFolderUrl+'slider.gif';
		this.params.track = this.imgFolderUrl+'track.jpg';
		this.params.captionCheck = this.imgFolderUrl+'caption_check.jpg';
		this.params.captionUncheck = this.imgFolderUrl+'caption_uncheck.jpg';
		
		this.params.sliderWidth = 16;
		this.params.sliderHeight = 30;
		this.params.trackWidth = 84;
		this.params.trackHeight = 30;
		this.params.arrowwidth = 20;
		this.params.arrowheight = 50;
		
		this.params.width = (this.paramList[0]["width"]!==undefined)?parseInt(this.paramList[0]["width"]):0;
		this.params.height = (this.paramList[0]["height"]!==undefined)?parseInt(this.paramList[0]["height"]):0;
		
		var i;
		for (i=0;i<this.imageList.length;i++) {
			if (parseInt(this.imageList[i]["width"])>this.params.width) this.params.width = parseInt(this.imageList[i]["width"]);
			if (parseInt(this.imageList[i]["height"])>this.params.height) this.params.height = parseInt(this.imageList[i]["height"]);
		}
		
		this.params.thumbwidth = (this.paramList[0]["thumbwidth"]!==undefined)?parseInt(this.paramList[0]["thumbwidth"]):70;
		this.params.thumbheight = (this.paramList[0]["thumbheight"]!==undefined)?parseInt(this.paramList[0]["thumbheight"]):50;
		this.params.transition = (this.paramList[0]["transition"]!==undefined)?this.paramList[0]["transition"]:'dissolve';
		this.params.showControls = (this.paramList[0]["showcontrols"]!==undefined)?this.paramList[0]["showcontrols"]:'no';
		this.params.caption = 'yes';
		this.params.duration = (this.paramList[0]["duration"]!==undefined)?parseInt(this.paramList[0]["duration"]):5;
		this.params.thumbs_bar_width = this.params.width-(this.params.arrowwidth*2);
		this.params.num_images = this.imageList.length;
		this.params.num_thumbs = Math.floor(this.params.thumbs_bar_width/this.params.thumbwidth);
		this.params.num_pages_thumb = Math.ceil(this.params.num_images/this.params.num_thumbs);
		this.params.spacer = Math.floor((this.params.thumbs_bar_width - (this.params.num_thumbs*this.params.thumbwidth))/(this.params.num_thumbs-1));
		if ((this.params.num_images%this.params.num_thumbs)!==0) {
			this.params.num_thumbs_lastpage = this.params.num_images%this.params.num_thumbs;
			this.params.last_page_width = (this.params.num_thumbs_lastpage*this.params.thumbwidth)+(this.params.spacer*(this.params.num_thumbs_lastpage-1))+this.params.num_thumbs_lastpage;
		} else {
			this.params.num_thumbs_lastpage = this.params.num_thumbs;
			this.params.last_page_width = this.params.thumbs_bar_width;
		}
		this.params.center = (this.params.thumbs_bar_width - this.params.thumbwidth)/2;
		this.params.thumbs_table_width = (this.params.thumbs_bar_width*(this.params.num_pages_thumb-1))+this.params.last_page_width;
		this.params.thumbsPosition = (this.paramList[0]["thumbsPosition"]!==undefined)?this.paramList[0]["thumbsPosition"]:'bottom';

	},
	
	createGallery: function() {

		var idGallery = this.idGallery;

		var divSlideShow = document.createElement('div');
		divSlideShow.id = 'hG_slideshow_'+idGallery;
		divSlideShow.style.width = this.params.width+'px';
		
		var divShow = document.createElement('div');
		divShow.id = 'hG_show_'+idGallery;
		divShow.style.width = this.params.width+'px';
		divShow.style.height = (this.params.height+10)+'px';
		divShow.style.position = 'relative';
		divShow.style.overflow = 'hidden';

		var viewImage = document.createElement('img');
		viewImage.id = 'hG_viewImage_'+idGallery;
		viewImage.style.display = 'none';
		viewImage.style.zIndex = '100';
		divShow.appendChild(viewImage);

		var divContainer = document.createElement('div');
		divContainer.id = 'hG_container_'+idGallery;
		divContainer.style.height = this.params.thumbheight+'px';
		divContainer.style.position = 'relative';
		divContainer.style.overflow = 'hidden';
		
		var imgLeftArrow = document.createElement('img');
		imgLeftArrow.src = this.params.leftArrow;
		
		var divLeftArrow = document.createElement('div');
		divLeftArrow.id = 'hG_leftarrow_'+idGallery;
		divLeftArrow.style.position =  'absolute';
		divLeftArrow.style.left = '0px';
		divLeftArrow.style.zIndex = '10';
		divLeftArrow.style.width = this.params.arrowwidth+'px';
		divLeftArrow.style.height = this.params.arrowheight+'px';
		divLeftArrow.style.cursor = 'pointer';
		divLeftArrow.appendChild(imgLeftArrow);
		divLeftArrow.backgroundColor = '#ffffff';
		divLeftArrow.onclick = function() {
			myhGallery[idGallery].slideLeft();
		};
		
		var imgRightArrow = document.createElement('img');
		imgRightArrow.src = this.params.rightArrow;
		
		var divRightArrow = document.createElement('div');
		divRightArrow.id = 'hG_rightarrow_'+idGallery;
		divRightArrow.style.position =  'absolute';
		divRightArrow.style.right = '-1px';
		divRightArrow.style.zIndex = '10';
		divRightArrow.style.width = this.params.arrowwidth+'px';
		divRightArrow.style.height = this.params.arrowheight+'px';
		divRightArrow.style.cursor = 'pointer';
		divRightArrow.appendChild(imgRightArrow);
		divRightArrow.backgroundColor = '#ffffff';
		divRightArrow.onclick = function() {
			myhGallery[idGallery].slideRight();
		};
		
		var divImages = document.createElement('div');
		divImages.id = 'hG_images_'+idGallery;
		divImages.style.position = 'absolute';
		divImages.style.height = this.params.thumbheight+'px';
		divImages.style.left = this.params.arrowwidth+'px';
		divImages.style.whiteSpace = 'nowrap';
		divImages.style.padding = '0px';
		
		if (this.params.num_pages_thumb<=1) {
			divLeftArrow.style.visibility = 'hidden';
			divRightArrow.style.visibility = 'hidden';
			divImages.style.textAlign = 'center';
			divImages.style.width = this.params.thumbs_bar_width+'px';
		} else {
			divImages.style.width = ((this.params.thumbs_bar_width*(this.params.num_pages_thumb-1))+this.params.last_page_width)+'px';
		}
		
		divContainer.appendChild(divLeftArrow);
		divContainer.appendChild(divRightArrow);
		divContainer.appendChild(divImages);
		
		if (this.params.showControls=='yes') {
			var divControls = document.createElement('div');
			divControls.id = 'hG_controls_'+idGallery;
			divControls.width = this.params.width+'px';
			
			var tableControls = document.createElement('table');
			var tbodyControls = document.createElement('tbody');
			var trControls = document.createElement('tr');
			var tdButton = document.createElement('td');
			var tdCaption = document.createElement('td');
			var tdSlider = document.createElement('td');
			var tdSpanSlider = document.createElement('td');
			tdSpanSlider.setAttribute('width','20');
			tableControls.setAttribute('align','center');
			tdButton.setAttribute('align','center');
			tdButton.setAttribute('valign','middle');
			tdCaption.setAttribute('align','right');
			tdCaption.setAttribute('valign','middle');
			tdSlider.setAttribute('align','left');
			tdSlider.setAttribute('valign','middle');
			tdSpanSlider.setAttribute('align','left');
			tdSpanSlider.setAttribute('valign','middle');
			trControls.appendChild(tdCaption);
			trControls.appendChild(tdButton);
			trControls.appendChild(tdSlider);
			trControls.appendChild(tdSpanSlider);
			tbodyControls.appendChild(trControls);
			tableControls.appendChild(tbodyControls);
			tableControls.border = 0;
			
			divControls.appendChild(tableControls);
			
			var playButton = document.createElement('img');
			playButton.src = this.params.playButton;
			playButton.style.cursor = 'pointer';
			playButton.className = 'hG_play';
			playButton.id = 'hG_button_'+idGallery;
			
			playButton.onclick = function() {
				myhGallery[idGallery].play();
			}
			playButton.onmouseover = function() {
				if (this.className=='hG_play') this.src = myhGallery[idGallery].params.playButtonHover;
				else if (this.className=='hG_pause') this.src = myhGallery[idGallery].params.pauseButtonHover;
			}
			playButton.onmouseout = function() {
				if (this.className=='hG_play') this.src = myhGallery[idGallery].params.playButton;
				else if (this.className=='hG_pause') this.src = myhGallery[idGallery].params.pauseButton;
			}
			tdButton.appendChild(playButton);
			
			var divSliderTrack = document.createElement('div');
			divSliderTrack.id = 'hG_sliderTrack_'+idGallery;
			divSliderTrack.style.width = this.params.trackWidth+'px';
			divSliderTrack.style.height = this.params.trackHeight+'px';
			divSliderTrack.style.backgroundImage = 'url('+this.params.track+')';
			
			var imgSlider = document.createElement('img');
			imgSlider.id = 'hG_imgSlider_'+idGallery;
			imgSlider.src = this.params.slider;
			imgSlider.style.cursor = 'pointer';
			
			divSliderTrack.appendChild(imgSlider);
			tdSlider.appendChild(divSliderTrack);
			
			var spanSlider = document.createElement('span');
			spanSlider.innerHTML = this.params.duration;
			spanSlider.style.fontFamily = 'Arial';
			spanSlider.style.fontSize = '13px';
			spanSlider.id = 'hG_spanSlider_'+idGallery;
			tdSpanSlider.appendChild(spanSlider);
			
			var imgCaption = document.createElement('img');
			imgCaption.id = 'hG_imgCaption_'+idGallery;
			imgCaption.className = 'hG_captionCheck';
			imgCaption.src = this.params.captionCheck;
			imgCaption.style.cursor = 'pointer';
			imgCaption.onclick = function() {
				if (this.className=='hG_captionCheck') {
					this.src = myhGallery[idGallery].params.captionUncheck;
					this.className = 'hG_captionUncheck';
					myhGallery[idGallery].params.caption = 'no';
					myhGallery[idGallery].hideCaption();
				} else {
					this.src = myhGallery[idGallery].params.captionCheck;
					this.className = 'hG_captionCheck';
					myhGallery[idGallery].params.caption = 'yes';
					myhGallery[idGallery].showCaption();
				}
			}
			tdCaption.appendChild(imgCaption);

		}
		
		if (this.params.thumbsPosition=='top') {
			if (this.params.showControls=='yes') divSlideShow.appendChild(divControls);
			divSlideShow.appendChild(divContainer);
			divSlideShow.appendChild(divShow);
		} else {
			divSlideShow.appendChild(divShow);
			divSlideShow.appendChild(divContainer);
			if (this.params.showControls=='yes') divSlideShow.appendChild(divControls);
		}
		
		this.galleryDiv.appendChild(divSlideShow);

		this.loadingImage = document.createElement('img');
		this.loadingImage.src = this.params.loadingImage;
		this.loadingImage.style.position = 'absolute';
		this.loadingImage.id = 'hG_loadingImage_'+idGallery;
		
		var divCaption = document.createElement('div');
		divCaption.id = 'hG_caption_'+idGallery;
		divCaption.style.backgroundColor = '#ffffff';
		divCaption.style.position = 'absolute';
		divCaption.style.display = 'none';
		divCaption.style.padding = '5px';
		divCaption.style.zIndex = '1000';
		divCaption.onmouseover = function() {
			myhGallery[idGallery].overCaption = true;
		}
		divCaption.onmouseout = function() {
			myhGallery[idGallery].overCaption = false;
			window.setTimeout('myhGallery['+idGallery+'].hideCaption()',10);
		}

		var spanCaption = document.createElement('span');
		spanCaption.id = 'hG_spanCaption_'+idGallery;
		spanCaption.style.color = '#000000';
		spanCaption.style.display = 'block';
		spanCaption.style.fontFamily = 'Tahoma';
		spanCaption.style.fontSize = '11px';
		spanCaption.style.lineHeight = '16px';
		
		divCaption.appendChild(spanCaption);
		document.getElementsByTagName('body')[0].appendChild(divCaption);
		
		this.activateSlider();
		this.refreshOffsets();
	},
	
	refreshOffsets: function() {
		var i;
		var idGallery;
		for(i=0;i<myhGallery.length;i++) {
			if (myhGallery[i]!==undefined) {
				idGallery = myhGallery[i].idGallery;
				if (document.getElementById('hG_caption_'+idGallery)) {
					var divCaption = document.getElementById('hG_caption_'+idGallery);
					divCaption.style.top = parseInt(findPosY(document.getElementById('hG_show_'+idGallery)));
					divCaption.style.left = parseInt(findPosX(document.getElementById('hG_show_'+idGallery)));
				}
			}
		}
	},
	
	populateGallery: function() {
		var idGallery = this.idGallery;
		var thumbsTable = document.createElement('table');
		var thumbsTbody = document.createElement('tbody');
		thumbsTable.id = 'hG_thumbs_'+idGallery;
		thumbsTable.border = 0;
		thumbsTable.setAttribute('cellpadding','0');
		thumbsTable.setAttribute('cellspacing','0');
		if (this.params.num_pages_thumb<=1) {
			thumbsTable.setAttribute('align','center');
		} else {
			thumbsTable.style.left = '0px';
		}
		thumbsTable.style.borderCollapse = 'collapse';
		thumbsTable.style.width = this.params.thumbs_table_width+'px';
		var thumbsTr = document.createElement('tr');

		var k = 0;
		var thumbsTd;
		var pageTable;
		var pageTbody;
		var pageTr;
		var pageTd;
		var tot_img;
		var thumb;
		var i;
		var j;
		for(i = 0; i < this.params.num_pages_thumb; i++) {
			thumbsTd = document.createElement('td');
			thumbsTd.setAttribute('valign','top');
			thumbsTd.setAttribute('align','left');
			
			if (i==(this.params.num_pages_thumb-1)) { thumbsTd.style.width = this.params.last_page_width+'px'; }
			else { thumbsTd.style.width = this.params.thumbs_bar_width+'px'; }
			
			pageTable = document.createElement('table');
			pageTbody = document.createElement('tbody');
			pageTable.border = 0;
			pageTable.setAttribute('cellpadding','0');
			pageTable.setAttribute('cellspacing','0');
			pageTable.setAttribute('width','100%');
			pageTable.setAttribute('height','100%');
			pageTable.style.borderCollapse = 'collapse';
			
			if (i==(this.params.num_pages_thumb-1)) {
				tot_img = this.params.num_thumbs_lastpage;
			} else {
				tot_img = this.params.num_thumbs;
			}
			
			pageTr = document.createElement('tr');

			for (j = 0; j < tot_img; j++) {

				pageTd = document.createElement('td');
				pageTd.setAttribute('align','center');
				pageTd.setAttribute('valign','top');
				
				thumb = document.createElement('img');
				thumb.setAttribute('src', this.imageList[k]["thumb"]);
				thumb.setAttribute('width', this.params.thumbwidth);
				thumb.setAttribute('height', this.params.thumbheight);
				thumb.style.cursor = 'pointer';
				this.unselectThumb(thumb);
				thumb.id = 'hG_thumb_'+k+'_'+idGallery;
				thumb.onclick = function() {
					myhGallery[idGallery].pause();
					myhGallery[idGallery].showImage(this);
				};
				thumb.onmouseover = function() {
					myhGallery[idGallery].overThumb(this);
				};
				thumb.onmouseout = function() {
					myhGallery[idGallery].outThumb(this);
				};

				pageTd.appendChild(thumb);
				pageTr.appendChild(pageTd);
				k++;
			}
			
			pageTbody.appendChild(pageTr);
			pageTable.appendChild(pageTbody);
			thumbsTd.appendChild(pageTable);
			thumbsTr.appendChild(thumbsTd);
		}
		thumbsTbody.appendChild(thumbsTr);
		thumbsTable.appendChild(thumbsTbody);
		document.getElementById('hG_images_'+idGallery).appendChild(thumbsTable);
		document.getElementById('hG_images_'+idGallery).style.height = '100px';

		window.setTimeout('myhGallery['+idGallery+'].showImage(document.getElementById(\'hG_thumb_0_'+idGallery+'\'))',500);
	},
	
	slideRight: function() {
		var idGallery = this.idGallery;
		if (this.params.num_pages_thumb>1) {
			var left = parseInt(document.getElementById('hG_thumbs_'+idGallery).style.left);
			var offset;
			if (left!=(-(this.params.thumbs_table_width-this.params.thumbs_bar_width))) {
				offset = -this.params.thumbs_bar_width;
				if ((left+offset)<(-(this.params.thumbs_table_width-this.params.thumbs_bar_width))) {
					offset = (-(this.params.thumbs_table_width-this.params.thumbs_bar_width))-left;
				}
				this.inprogress = true;
				new Effect.MoveBy('hG_thumbs_'+idGallery, 0, offset,{duration:2.0,transition: Effect.Transitions.slowstop,queue:{position:'front',scope:'galleria',limit:1},afterFinish:function(){ document.getElementById('hG_thumbs_'+idGallery).style.left = Math.floor(parseFloat(document.getElementById('hG_thumbs_'+idGallery).style.left));myhGallery[idGallery].inprogress=false}})
			}
		}
	},
	
	slideLeft: function() {
		var idGallery = this.idGallery;
		if (this.params.num_pages_thumb>1) {
			var left = parseInt(document.getElementById('hG_thumbs_'+idGallery).style.left);
			var offset;
			if (left!='0') {
				if ((Math.abs(left)-this.params.thumbs_bar_width)<0) {
					offset = this.params.thumbs_bar_width+Math.abs(left)-this.params.thumbs_bar_width;
				} else {
					offset = this.params.thumbs_bar_width;
				}
				this.inprogress = true;
				new Effect.MoveBy('hG_thumbs_'+idGallery, 0, offset,{duration:2.0,transition: Effect.Transitions.slowstop,queue:{position:'front',scope:'galleria',limit:1},afterFinish:function(){ document.getElementById('hG_thumbs_'+idGallery).style.left = Math.ceil(parseFloat(document.getElementById('hG_thumbs_'+idGallery).style.left));myhGallery[idGallery].inprogress=false}})
			}
		}
	},
	
	showImage: function(img) {
		if (!this.render) {
			var idGallery = this.idGallery;
			if(this.showing!=img) {
				if (!this.inprogress) {
					var fading = false;
					if (this.showing!=undefined) {
						var old_img = this.showing;
						fading = true;
						new Effect.Fade(old_img,{
							duration: 0.3,
							from: 0.99,
							to: 0.5,
							afterFinish: function() {
								myhGallery[idGallery].unselectThumb(old_img);
							}
						});
					}
					this.showing = img;
					this.selectThumb(img);
					
					if (this.params.num_pages_thumb>1) {
						var imgpos = parseInt(findPosX(img));
						var divpos = parseInt(findPosX(document.getElementById('hG_images_'+idGallery)));
						var imgpos = imgpos-divpos;
						var offset = -(imgpos-this.params.center);
				
						var left = parseInt(document.getElementById('hG_thumbs_'+idGallery).style.left);
						if ((left+offset)<(-(this.params.thumbs_table_width-this.params.thumbs_bar_width))) {
							offset = (-(this.params.thumbs_table_width-this.params.thumbs_bar_width))-left;
						} else if ((left+offset)>0) {
							offset = 0-left;
						}
						
						if ((Math.ceil(Math.abs(offset)))<this.params.thumbwidth) var duration = 0.1;
						else var duration = 1.0;
						this.inprogress = true;
						new Effect.MoveBy('hG_thumbs_'+idGallery, 0, offset,{duration:duration,transition: Effect.Transitions.exponential,queue:{position:'front',scope:'galleria',limit:1},afterFinish:function(){myhGallery[idGallery].inprogress=false;}})
					}
					this.viewImage();
				}
			}
		}
	},
	
	getId: function(thumbid) {
		var id = thumbid.replace('hG_thumb_','');
		var index = id.indexOf('_');
		id = id.substring(0,index);
		return parseInt(id);
	},
	
	viewImage: function() {
		var idGallery = this.idGallery;
		var id = this.getId(this.showing.id);
		
		this.showLoading();
		imgPreloader = new Image();
		imgPreloader.onload=function(){
			myhGallery[idGallery].renderTransition(id,this);
		}
		imgPreloader.src = this.imageList[id]["normal"];
	},
	
	renderTransition: function(id,newimg) {
		var idGallery = this.idGallery;
		this.hideLoading();
		this.render = true;
		var img = document.getElementById('hG_viewImage_'+idGallery);
		if (this.Engine.isKHTML) {
			newimg = document.createElement('img');	
			newimg.src = this.imageList[id]["normal"];
		}
		newimg.id = 'hG_viewImage_'+idGallery;
		newimg.style.display = 'none';
		newimg.style.position = 'absolute';
		newimg.style.left = '0px';

		if (this.params.thumbsPosition=='top') {
			var topPos = 15;
		} else {
			var topPos = this.params.height-parseInt(this.imageList[id]["height"]);
		}
		
		newimg.style.top = topPos+'px';
		
		switch(this.params.transition) {
			case 'dissolve':
				if (img.src=='') {
					img.parentNode.replaceChild(newimg,img);
					new Effect.Appear(newimg,{duration:0.5,from:0.01,to:0.99,afterFinish:function(){myhGallery[idGallery].render=false}})
					this.setImageEvents(id);
				} else {

					if (this.Engine.isKHTML) {
						left = left+8;
						top = top+8;
					}
					newimg.style.position = 'absolute';
					var left = Math.floor((this.params.width-parseInt(this.imageList[id]["width"]))/2);
					
					if (this.params.thumbsPosition=='top') {
						var top = 15;
					} else {
						var top = Math.floor(this.params.height-parseInt(this.imageList[id]["height"]));
					}
					var divShow = document.getElementById('hG_show_'+idGallery);
					left = left+parseInt(findPosX(divShow));
					top = top+parseInt(findPosY(divShow));
					newimg.style.left = left+'px';
					newimg.style.top = top+'px';
					newimg.style.zIndex = '100';
					var div = document.getElementById('hG_caption_'+idGallery);
					new Effect.Fade(div,{duration:0.5,queue:'end',scope:'caption',limit:1,afterFinish:function(){
						document.getElementsByTagName('body')[0].appendChild(newimg);
						new Effect.Appear(newimg,{
							duration: 0.5,
							from:0.01,
							to:0.99,
							afterFinish: function() {
								img.parentNode.removeChild(img);
								myhGallery[idGallery].setImageEvents(id);
								if ((myhGallery[idGallery].params.caption=='yes') && (myhGallery[idGallery].playing)) {
									myhGallery[idGallery].showCaption();
								}
								myhGallery[idGallery].resumePlaying();
							}
						});
						myhGallery[idGallery].render=false;
						
					}});
				}
			break;
			case 'hslide':
				var offset;
				var divShow;
				var left;
				if (img.src=='') {
					img.parentNode.replaceChild(newimg,img);
					new Effect.Appear(newimg,{duration:0.5,from:0.01,to:0.99,afterFinish:function(){myhGallery[idGallery].render=false}})
					this.setImageEvents(id);
					this.lastid = id;
				} else {
					
					divShow = document.getElementById('hG_show_'+idGallery);
					
					var tempimage1 = document.createElement('img');
					tempimage1.style.marginRight = '5px';
					
					var tempimage2 = document.createElement('img');
					
					var imageSize = Element.getDimensions(img);
					
					if (this.lastid<id) {
						tempimage1.src = img.src;
						tempimage2.src = newimg.src;
						offset = -(parseInt(imageSize.width)+5);
						left = '0px';
					} else if (this.lastid>id) {
						tempimage1.src = newimg.src;
						tempimage2.src = img.src;
						offset = parseInt(this.imageList[id]["width"])+5;
						left = '-'+offset+'px';
					}
					
					if (this.params.thumbsPosition == 'top') {
						tempimage1.style.left = '0px';
						tempimage2.style.left = (this.params.width+5)+'px';
						tempimage1.style.position = 'absolute';
						tempimage1.style.top = '0px';
						tempimage2.style.position = 'absolute';
						tempimage2.style.top = '0px';
					}

					if (this.params.thumbsPosition=='top') {
						topPos = 15;
					} else {
						topPos = this.params.height - Math.max(parseInt(imageSize.height),parseInt(this.imageList[id]["height"]));
					}
					var slidingDiv = document.createElement('div');
					slidingDiv.style.position = 'absolute';
					slidingDiv.style.top = topPos+'px';
					slidingDiv.style.left = left;
					slidingDiv.style.width = (parseInt(this.imageList[id]["width"])+5+parseInt(imageSize.width))+'px';
					slidingDiv.appendChild(tempimage1);
					slidingDiv.appendChild(tempimage2);
					slidingDiv.style.verticalAlign = 'top';
					
					divShow.appendChild(slidingDiv);
					
					var div = document.getElementById('hG_caption_'+idGallery);
					new Effect.Fade(div,{duration:0.5,queue:'end',scope:'caption',limit:1,afterFinish:function(){
						new Effect.MoveBy(slidingDiv, 0, offset,{
							duration:0.8,
							fps: 25,
							//transition: Effect.Transitions.exponential,
							beforeStart: function() {
								img.style.display = 'none';
								img.parentNode.replaceChild(newimg,img);
							},
							afterUpdate: function(effect) {
								if (effect.currentFrame==19) newimg.style.display = 'block';
							},
							afterFinish:function(){
								newimg.style.display = 'block';
								myhGallery[idGallery].render = false;
								slidingDiv.parentNode.removeChild(slidingDiv);
								myhGallery[idGallery].setImageEvents(id);
								if ((myhGallery[idGallery].params.caption=='yes') && (myhGallery[idGallery].playing)) {
									myhGallery[idGallery].showCaption();
								}
								myhGallery[idGallery].resumePlaying();
							}
						})
					}});
					
					this.lastid = id;
				}
			break;
			case 'vslide':
				var offset;
				var divShow;
				var left;
				if (img.src=='') {
					img.parentNode.replaceChild(newimg,img);
					new Effect.Appear(newimg,{duration:0.5,from:0.01,to:0.99,afterFinish:function(){myhGallery[idGallery].render=false}})
					this.setImageEvents(id);
					this.lastid = id;
				} else {
					
					divShow = document.getElementById('hG_show_'+idGallery);
					
					var tempimage1 = document.createElement('img');
					
					var tempimage2 = document.createElement('img');
					
					var imageSize = Element.getDimensions(img);
					
					topPos = this.params.height - parseInt(imageSize.height);
					var topPosNext = this.params.height - parseInt(this.imageList[id]['height']);

					if (this.lastid>id) {
						tempimage1.src = img.src;
						tempimage2.src = newimg.src;
						offset = -(parseInt(imageSize.height)+topPos+15);
						if (this.params.thumbsPosition=='top') {
							tempimage1.style.marginTop = '15px';
							tempimage1.style.marginBottom = (this.params.height-parseInt(imageSize.height))+'px';
							tempimage2.style.marginTop = '15px';
						} else {
							tempimage1.style.marginTop = topPos+'px';
							tempimage2.style.marginTop = topPosNext+'px';
							tempimage1.style.marginBottom = '15px';
						}
						top = '0px';
					} else if (this.lastid<id) {
						tempimage1.src = newimg.src;
						tempimage2.src = img.src;
						offset = parseInt(this.imageList[id]["height"])+topPosNext+15;
						top = '-'+offset+'px';
						if (this.params.thumbsPosition=='top') {
							tempimage1.style.marginTop = '15px';
							tempimage1.style.marginBottom = (this.params.height-parseInt(this.imageList[id]['height']))+'px';
							tempimage2.style.marginTop = '15px';
						} else {
							tempimage1.style.marginTop = topPosNext+'px';
							tempimage2.style.marginTop = topPos+'px';
							tempimage1.style.marginBottom = '15px';
						}
					}
					
					var slidingDiv = document.createElement('div');
					slidingDiv.style.position = 'absolute';
					slidingDiv.style.top = top;
					slidingDiv.style.left = '0px';
					slidingDiv.style.width = this.params.width;
					slidingDiv.style.height = parseInt(this.imageList[id]["height"])+15+this.params.height;
					slidingDiv.appendChild(tempimage1);
					slidingDiv.appendChild(tempimage2);
					
					divShow.appendChild(slidingDiv);
					
					var div = document.getElementById('hG_caption_'+idGallery);
					new Effect.Fade(div,{duration:0.5,queue:'end',scope:'caption',limit:1,afterFinish:function(){
						new Effect.MoveBy(slidingDiv, offset, 0,{
							duration:0.8,
							fps: 25,
							//transition: Effect.Transitions.slowstop,
							beforeStart: function() {
								img.style.display = 'none';
								img.parentNode.replaceChild(newimg,img);
							},
							afterUpdate: function(effect) {
								if (effect.currentFrame==19) newimg.style.display = 'block';
							},
							afterFinish:function(){
								newimg.style.display = 'block';
								myhGallery[idGallery].render = false;
								slidingDiv.parentNode.removeChild(slidingDiv);
								myhGallery[idGallery].setImageEvents(id);
								if ((myhGallery[idGallery].params.caption=='yes') && (myhGallery[idGallery].playing)) {
									myhGallery[idGallery].showCaption();
								}
								myhGallery[idGallery].resumePlaying();
						}})
					}});
					
					this.lastid = id;
				}
			break;
		}
		if (document.getElementsByTagName('body')[0].style.overflow == 'hidden' && document.getElementById('scrollGallery') && document.getElementById('scrollGallery').value=='1') document.getElementsByTagName('body')[0].style.overflow = 'auto';
	},
	
	resumePlaying: function() {
		if (this.playing) {
			var idGallery = this.idGallery;
			this.intervalid = window.setInterval('myhGallery['+idGallery+'].setNext()',this.params.duration*1000)
		}
	},
	
	setImageEvents: function(id) {
		var idGallery = this.idGallery;
		document.getElementById('hG_viewImage_'+idGallery).onmouseover = function() {
			myhGallery[idGallery].overImage = true;
			myhGallery[idGallery].showCaption();
		}
		document.getElementById('hG_viewImage_'+idGallery).onmouseout = function() {
			myhGallery[idGallery].overImage = false;
			window.setTimeout('myhGallery['+idGallery+'].hideCaption()',10);
		}
		if (this.imageList[id]["big"]!==undefined) {
			document.getElementById('hG_viewImage_'+idGallery).onclick = function() {
				myhGallery[idGallery].pause();
				myhGallery[idGallery].showBig();
			}
			document.getElementById('hG_viewImage_'+idGallery).style.cursor = 'pointer';
		}
	},
	
	showCaption: function() {
		var idGallery = this.idGallery;
		var div = document.getElementById('hG_caption_'+idGallery);
		var span = document.getElementById('hG_spanCaption_'+idGallery);
		var id = this.getId(this.showing.id);
		div.style.width = this.imageList[id]["width"]+'px';
		div.style.top = parseInt(findPosY(document.getElementById('hG_viewImage_'+idGallery)))+'px';
		div.style.left = parseInt(findPosX(document.getElementById('hG_viewImage_'+idGallery)))+'px';
		if ((this.params.caption=='yes') && (this.imageList[id]["caption"]!=undefined) && (this.imageList[id]["caption"]!='')) {
			new Effect.Appear(div,{
			to: 0.8,
			queue:'end',
			scope: 'caption',
			limit:1,
			duration:0.5,
			beforeStart:function(){
				span.innerHTML = myhGallery[idGallery].imageList[id]["caption"];
		}});
		}
	},
	
	hideCaption: function() {
		if ((!this.playing) && (!this.overCaption) && (!this.overImage)) {
			var idGallery = this.idGallery;
			var div = document.getElementById('hG_caption_'+idGallery);
			new Effect.Fade(div,{
				duration:0.5,
				scope:'caption',
				limit:1,
				queue:'end'
			});
		}
	},
	
	selectThumb: function(img) {
		img.className = 'hG_selThumb';
		if (this.Engine.isMSIE || this.Engine.isMSIE7) {
			img.style.filter = 'alpha(opacity=100)';
		} else {
			img.style.opacity = '1.0';
		}
	},
	
	unselectThumb: function(img) {
		img.className = 'hG_unselThumb';
		if (this.Engine.isMSIE || this.Engine.isMSIE7) {
			img.style.filter = 'alpha(opacity=50)';
		} else {
			img.style.opacity = '0.5';
		}
	},
	
	overThumb: function(img) {
		if (img.className!='hG_selThumb') {
			new Effect.Appear(img,{
				duration: 0.3,
				from: 0.5,
				to: 0.99
			});
		}
	},
	
	outThumb: function(img) {
		if (img.className!='hG_selThumb') {
			new Effect.Fade(img,{
				duration: 0.3,
				from: 0.99,
				to: 0.5
			});
		}
	},
	
	showLoading: function() {
		var idGallery = this.idGallery;
		var img = document.getElementById('hG_viewImage_'+idGallery);
		if (img.src=='') {
			var top = Math.floor(parseInt(findPosY(document.getElementById('hG_show_'+idGallery)))+(myhGallery[idGallery].params.height/2));
			var left = Math.floor(parseInt(findPosX(document.getElementById('hG_show_'+idGallery)))+(myhGallery[idGallery].params.width/2));
		} else {
			var imageSize = Element.getDimensions(img);
			var top = Math.floor(parseInt(findPosY(img))+(parseInt(imageSize.height)/2));
			var left = Math.floor(parseInt(findPosX(img))+(parseInt(imageSize.width)/2));
		}
		this.loadingImage.style.display = 'none';
		this.loadingImage.style.top = top+'px';
		this.loadingImage.style.left = left+'px';
		this.loadingImage.style.zIndex = '1101';
		document.getElementsByTagName('body')[0].appendChild(this.loadingImage);
		new Effect.Appear('hG_loadingImage_'+idGallery,{duration:0.3,from:0.01,to:0.99,queue:'end',scope:'loading'})
		if (this.playing) {
			window.clearInterval(this.intervalid);
		}
	},
	
	hideLoading: function() {
		var idGallery = this.idGallery;
		if (document.getElementById('hG_loadingImage_'+idGallery)) {
			new Effect.Fade('hG_loadingImage_'+idGallery,{duration:0.3,from:0.99,to:0.01,queue:'end',scope:'loading',afterFinish:function(){if (document.getElementById('hG_loadingImage_'+idGallery)) document.getElementsByTagName('body')[0].removeChild(document.getElementById('hG_loadingImage_'+idGallery))}})
			/*if (this.playing) {
				this.intervalid = window.setInterval('myhGallery['+idGallery+'].setNext()',this.params.duration*1000)
			}*/
		}
	},
	
	play: function() {
		var idGallery = this.idGallery;
		if (!this.playing) this.togglePlayButton();
		this.playing = true;
		this.intervalid = window.setInterval('myhGallery['+idGallery+'].setNext()',this.params.duration*1000)
		var num = this.getNextImg();
		if (num==0) {
			this.showImage(document.getElementById('hG_thumb_'+num+'_'+idGallery));
		} else if (num==this.imageList.length) {
			this.showImage(document.getElementById('hG_thumb_0_'+idGallery));
		}
		this.showCaption();
	},
	
	pause: function() {
		if (this.playing) this.togglePlayButton();
		this.playing = false;
		window.clearInterval(this.intervalid);
		this.hideCaption();
	},
	
	getNextImg: function() {
		var num;
		if (this.showing==undefined) {
			num = 0;
		} else {
			num = this.getId(this.showing.id)+1;
		}
		return num;
	},
	
	setNext: function() {
		var idGallery = this.idGallery;
		var num = this.getNextImg();
		if (num>(this.imageList.length-1)) {
			this.pause();
		} else {
			this.showImage(document.getElementById('hG_thumb_'+num+'_'+idGallery));
		}
	},
	
	togglePlayButton: function() {
		var idGallery = this.idGallery;
		var button = document.getElementById('hG_button_'+idGallery);
		if (button.className == 'hG_play') {
			button.src = this.params.pauseButton;
			button.className = 'hG_pause';
			button.onclick = function() {
				myhGallery[idGallery].pause();
			}
		} else {
			button.src = this.params.playButton;
			button.className = 'hG_play';
			button.onclick = function() {
				myhGallery[idGallery].play();
			}
		}
	},
	
	showBig: function() {
		var id = this.getId(this.showing.id);
		var w = parseInt(this.imageList[id]["widthbig"]);
		var h = parseInt(this.imageList[id]["heightbig"]);
		if (w>screen.width) w = screen.width;
		if (h>screen.height) h = screen.height;
		var l = Math.floor((screen.width-w)/2);
		var t = Math.floor((screen.height-h)/2);
		window.open("/view_img.php?url="+encodeURIComponent(this.imageList[id]["big"]),"","scrolbars=no, width=" + w + ",height=" + h + ",top=" + t + ",left=" + l + ", status=no, modal=yes");
	},
	
	activateSlider: function() {
		var idGallery = this.idGallery;
		var max;
		if (this.Engine.isKHTML) max = 12;
		else max = 10;
		if (this.params.showControls=='yes') {
			new Control.Slider('hG_imgSlider_'+idGallery,'hG_sliderTrack_'+idGallery,{
				range:$R(2,max),
				values:[2,3,4,5,6,7,8,9,10],
				sliderValue:this.params.duration,
				onChange:function(value){
					myhGallery[idGallery].params.duration=value;
					document.getElementById('hG_spanSlider_'+idGallery).innerHTML=value;
				},
				onSlide:function(value){
					document.getElementById('hG_spanSlider_'+idGallery).innerHTML=value;
				}
			});
		}
	}
}

var myhGallery = [];