var galleryThumbLimit = 5;
var galleryThumbFirst = 0;
var galleryThumbNum = 0;
var galleryNum = 0;
var centerTbl;
var objPaginator;
var objNextLink;
var slideShowAction = 'pause';
var slideShowDelay = 4500; // 4.5 s.
var imageContainerW = 656;
var imageContainerH = 496;
var windowLoaded = false;
var galleryWindowLoaded = false;

function truncate(str, limit){
	if (str.length<=limit) return str;
	var ret = str.substr(0, limit-3);
	ret = ret + '...';
	return ret;
}

function getElementPos(obj){
	var l = 0;
	var t = 0;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	while (obj) {
		l += obj.offsetLeft;
		t += obj.offsetTop;
		if ((obj.tagName != "TABLE") && (obj.tagName != "BODY")) {
			l += (obj.clientLeft)?obj.clientLeft:0;
			t += (obj.clientTop)?obj.clientTop:0;
		}
		obj = obj.offsetParent;
	}
	var res = new Object();
	res.x = l;
	res.y = t;
	res.left = l;
	res.top = t;
	res.w = w;
	res.h = h;
	res.width = w;
	res.height = h;
	return res;
}

function galleryThumbOnMouseOver(){
	this.parentNode.className='hover';
	if (this.imageNum != 'undefined' && this.imageNum != null){
		var el = document.getElementById('imgTitle_' + this.imageNum);
		el.style.display = '';
		//alert(getElementPos(this.parentNode).top + ' ' + el.offsetHeight);
		el.style.width = '120px';
		el.style.left =  (this.parentNode.offsetLeft + 50) +'px';
		el.style.top = (getElementPos(this.parentNode).top - el.offsetHeight - 13) + 'px';
	}
}

function galleryThumbOnMouseOut(){
	var arr = this.id.split('_');
	if (arr[1] == galleryNum) 	{this.parentNode.className='selected';}
	else if(arr[1] != galleryNum)	{
		this.parentNode.className='normal';
	}
	if (this.imageNum != 'undefined' && this.imageNum != null)	document.getElementById('imgTitle_' + this.imageNum).style.display = 'none';
}

function galleryChange(){
	document.getElementById('imgTitle_' + this.imageNum).style.display = 'none';
	slideShowAction = 'play';
	slideShow();
	//galleryThumbNum = 0;
	activeImage = 0;
	objPaginator.firstPage =0;
	showPaginator();
	objPaginator.style.display = 'none';
	this.parentNode.className = 'selected';
	document.getElementById('td_thumb_gallery_' + galleryNum).className = 'normal';
	var arr = this.id.split('_');
	galleryNum = parseInt(arr[1], 10);
	myLightbox.start(0);
	// alert('galleryThumbOnMouseOut delete id_img=' + arr[1] + ', galleryThumbNum=' + galleryThumbNum +', galleryNum=' + galleryNum);
	galleryThumbUpdate();
}

function galleryThumbUpdateByFirst(first){
	if (first == 0)
			document.getElementById('id_galleryBtnLeft').className='galleryBtnLeft_disabled';
	else	document.getElementById('id_galleryBtnLeft').className='galleryBtnLeft';
	var i=0;
	for (i=0, c=0; i<MY_GALLERY.length; i++) {
		if (i>=first && c<galleryThumbLimit) {
			c++;
			document.getElementById('td_thumb_gallery_'+i).style.display = '';
		}else{
			document.getElementById('td_thumb_gallery_'+i).style.display = 'none';
		}
	}

	if (first + galleryThumbLimit >= MY_GALLERY.length)
			document.getElementById('id_galleryBtnRight').className='galleryBtnRight_disabled';
	else	document.getElementById('id_galleryBtnRight').className='galleryBtnRight';
}

function galleryThumbUpdate(){
	if (galleryNum>2){
		if((galleryNum+2)>=MY_GALLERY.length) {
			galleryThumbFirst = MY_GALLERY.length - galleryThumbLimit;
		}else{
			galleryThumbFirst = galleryNum -2;
		}
	}
	else galleryThumbFirst = 0;
	galleryThumbUpdateByFirst(galleryThumbFirst);

}

function galleryBtnLeftOnClick(){
	if (galleryThumbFirst>0) {
		galleryThumbFirst --;
		galleryThumbUpdateByFirst(galleryThumbFirst);
	}
}

function galleryBtnRightOnClick(){
	if (galleryThumbFirst+galleryThumbLimit<MY_GALLERY.length) {
		galleryThumbFirst ++;
		galleryThumbUpdateByFirst(galleryThumbFirst);
	}
}

function _buildGalleryThumbsTable(objImageDataContainer) {
	var tbl=document.createElement("table");
		tbl.className = 'gallery';
		//tbl.style.width = imageContainerW;
	var row=tbl.insertRow(0);
	var leftCell=row.insertCell(0);
	leftCell.id = 'id_galleryBtnLeft';
	leftCell.className = 'galleryBtnLeft_disabled';
	leftCell.onmouseover = function (){
		if (this.className != 'galleryBtnLeft') return;
		this.className = 'galleryBtnLeft_hover'
	};
	leftCell.onmouseout = function (){
		if (this.className != 'galleryBtnLeft') return;
		this.className = 'galleryBtnLeft'
	};
	leftCell.onclick = galleryBtnLeftOnClick;

	var i=0;
	centerTbl=document.createElement("table");
	centerTbl.className='center';
	var cCell=row.insertCell(1);
		cCell.appendChild(centerTbl);
		cCell.style.textAlign = 'center';
	var centerRow=centerTbl.insertRow(0);
	if (galleryNum < galleryThumbNum) galleryNum = galleryThumbNum;

	for (i=0, c=0; i<MY_GALLERY.length; i++, c++) {
		var centerCell = centerRow.insertCell(c);
			centerCell.id = 'td_thumb_gallery_'+i;
		if (i == galleryNum) centerCell.className = 'selected';
		var imgTitle = document.createElement('div');
			imgTitle.setAttribute('id', 'imgTitle_'+i);
			imgTitle.className = 'popupTitle';
			var b = document.createElement("b");
			imgTitle.appendChild(b);
			var arrMargin = [9,6,5,4,3,2,1,1,1];
			for(j=0; j<9; j++) {
				var bb = document.createElement("b");
					bb.className = 'popupTitleLine';
					bb.style.margin = '0pt '+arrMargin[j]+'px';
				b.appendChild(bb);
			}
			var txtBox = document.createElement("div");
				txtBox.className = 'popupTitleTextBox';
				txtBox.appendChild(document.createTextNode(MY_GALLERY[i].name));
			imgTitle.appendChild(txtBox);
			var b = document.createElement("b");
			imgTitle.appendChild(b);
			for(j=8; j>=0; j--) {
				var bb = document.createElement("b");
					bb.className = 'popupTitleLine';
					bb.style.margin = '0pt '+arrMargin[j]+'px';
				b.appendChild(bb);
			}
			var b = document.createElement("b");
			imgTitle.appendChild(b);
			for(j=20; j>=0; j--){
				var bb = document.createElement("b");
					bb.className = 'popupTitleLine';
					bb.style.width = j+'px';
					bb.style.marginLeft = '35%';
				b.appendChild(bb);
			}
			imgTitle.style.display = 'none';

			//
		var img = document.createElement('img');
		img.src=MY_GALLERY[i].smallPhoto;
		img.id='gallery_'+i;
		img.imageNum=i;
		img.className='gallery';
		img.popupTitle = MY_GALLERY[i].name;
        if (MY_GALLERY[i].items == 'undefined') {
			img.onclick = function(){
				alert('This category has no photos');
			};
		} else {
			img.onclick=galleryChange;
		}
		img.onmouseover = galleryThumbOnMouseOver;
		img.onmouseout 	= galleryThumbOnMouseOut;

		if (i<galleryThumbNum || c>=galleryThumbLimit) {
			//centerCell.style.display='none';
			//centerCell.style.border='solid lime 1px';
		}
		//centerCell.appendChild(imgTitle);
		document.getElementById('outerImageContainer').appendChild(imgTitle);
		centerCell.appendChild(img);
		//centerCell.appendChild(document.createTextNode('hell0'));
	}
	for (i=0, c=0; i<MY_GALLERY.length; i++, c++) {
		if (i<galleryThumbNum || c>=galleryThumbLimit) {
			centerTbl.rows[0].cells[i].style.display='none';
		}
	}

	//alert(center_tbl);
	//objImageDataContainer.appendChild(center_tbl);
	//row.appendChild(centerTbl);

	var rightCell=row.insertCell(2);
	rightCell.id = 'id_galleryBtnRight';
	if (galleryThumbLimit >= MY_GALLERY.length)	{
		rightCell.className = 'galleryBtnRight_disabled';
	} else rightCell.className = 'galleryBtnRight';
	rightCell.onmouseover = function (){
		if (this.className != 'galleryBtnRight') return;
		this.className = 'galleryBtnRight_hover'
	};
	rightCell.onmouseout = function (){
		if (this.className != 'galleryBtnRight') return;
		this.className = 'galleryBtnRight'
	};
	rightCell.onclick = galleryBtnRightOnClick;

	var el_center=document.createElement('center');
	el_center.appendChild(tbl);
	objImageDataContainer.appendChild(el_center);
	objImageDataContainer.style.display = 'none';

}

function paginatorPageOnClick(){
	var arr = this.id.split('_');
	//objPaginator.style.display = 'none';
	//alert(this.pageItemNum);
	if (this.pageItemNum == 7){
		objPaginator.firstPage+=3;
	}else if (this.pageItemNum == 1 && objPaginator.firstPage>=3){
		objPaginator.firstPage-=3;
	}
	myLightbox.changeImage(parseInt(arr[1],10));
	return false;
}

function showPaginator(){
	var pageIndex = 2;
	for (var i=0; i<7; i++){
		objPaginator.childNodes[i+pageIndex].style.display='none';
	}
	if (objPaginator.firstPage + 6 == activeImage) objPaginator.firstPage+=3;
	else if (objPaginator.firstPage>2 && objPaginator.firstPage == activeImage) objPaginator.firstPage -=3;
	for (var i = objPaginator.firstPage, c=0; i<MY_GALLERY[galleryNum].items.length && c<7; i++, c++) {
		objPaginator.childNodes[c+pageIndex].setAttribute('id','activeImage_'+i)
		objPaginator.childNodes[c+pageIndex].firstChild.nodeValue = i+1;
		objPaginator.childNodes[c+pageIndex].onclick = paginatorPageOnClick;
		objPaginator.childNodes[c+pageIndex].style.display='';
		objPaginator.childNodes[c+pageIndex].className ='';
	}
	objPaginator.style.display = '';
	objPaginator.childNodes[activeImage - objPaginator.firstPage + pageIndex].className = 'selected';
}
var idTimer;

function slideShow(){
	if (slideShowAction !='play') {
		slideShowAction = 'play';
		document.getElementById('playLink').firstChild.nodeValue = textstop;
		idTimer = setTimeout("objNextLink.onclick()", slideShowDelay);
	}else{
		slideShowAction = 'pause';
		document.getElementById('playLink').firstChild.nodeValue = textplay;
		clearTimeout(idTimer);
	}
	return false;
}

function _buildGalleryPhotoPaginator(parentObj){
	objPaginator = document.createElement('div');
	objPaginator.className = 'padinator';
	objPaginator.firstPage = 0;
	var img = document.createElement('img');
		img.src = 'img/blank.gif';
		img.style.fontSize = '12pt';
	objPaginator.appendChild(img);
	var objPrevLink = document.createElement("a");
		objPrevLink.setAttribute('id','prevLink');
		objPrevLink.setAttribute('href','#');
		img = document.createElement('img');
		img.src = 'img/paginatorPrev.png';
		img.style.fontSize = '12pt';
		objPrevLink.appendChild(img);
		objPaginator.appendChild(objPrevLink);

	for (var i=0; i<7; i++) {
		var a = document.createElement('a');
			a.href="#";
			a.pageItemNum = i+1;
			a.appendChild(document.createTextNode(i+1));
			objPaginator.appendChild(a);
	}
	//objPaginator.appendChild(document.createTextNode(' '));
		objNextLink = document.createElement("a");
		objNextLink.setAttribute('id','nextLink');
		objNextLink.setAttribute('href','#');
		img = document.createElement('img');
		img.src = 'img/paginatorNext.png';
		img.style.fontSize = '12pt';
		objNextLink.appendChild(img);
		//objNextLink.style.display = 'none';
		objPaginator.appendChild(objNextLink);

	var objPlayLink = document.createElement("a");
		objPlayLink.setAttribute('id','playLink');
		objPlayLink.setAttribute('href','#');
		objPlayLink.appendChild(document.createTextNode(textplay));
		objPlayLink.onclick = slideShow;
		objPaginator.appendChild(objPlayLink);
	//alert(Element.getWidth(hoverNav));
	objPaginator.style.display = 'none';
	parentObj.appendChild(objPaginator);
}

function buildGalleryThumbWindow(objImageDataContainer){
	_buildGalleryThumbsTable(objImageDataContainer);
}

var Gallery = Class.create();
var myGallery;

Gallery.prototype = {
	initialize: function(objParent) {
		var galleryWindowContainer = document.createElement("div");
		galleryWindowContainer.setAttribute('id', 'galleryWindowContainer');
		galleryWindowContainer.style.display = 'none';
		objParent.appendChild(galleryWindowContainer);

		// LOAD CSS IMAGES
		var arrImg = [
			'img/galleryWindowBg.png',
			'img/galleryItem.png',
			'img/galleryClose.png'
		];
		for (var i=0; i<arrImg.length; i++) {
			var im = new Image();
			im.src = arrImg[i];
		}
		// --------------------------

		var objTitle = document.createElement("div");
		objTitle.setAttribute('id','galleryTitle');
		galleryWindowContainer.appendChild(objTitle);
		var Title = document.createTextNode(textphotogallery);
		objTitle.appendChild(Title);

		var objTitle2 = document.createElement("div");
		objTitle2.setAttribute('id','galleryTitle2');
		galleryWindowContainer.appendChild(objTitle2);
		var Title2 = document.createTextNode(textchooseanalbum);
		objTitle2.appendChild(Title2);

		var objGalleryThumbContainer = document.createElement('div');
			objGalleryThumbContainer.setAttribute('id', 'galleryThumbContainer');
			objGalleryThumbContainer.style.display = 'none'
		this.firstNum = 0;
		for (var i=0; i<MY_GALLERY.length; i++) {
			var tbl = document.createElement('table');
				tbl.setAttribute('id', 'galleryItem');
				tbl.style.display = 'none';
				tbl.numGallery = i;
				tbl.onclick = function () {
                    if (typeof MY_GALLERY[this.numGallery].link == 'string') {
                        document.location = MY_GALLERY[this.numGallery].link;
                        return false;
                    }
					document.getElementById('td_thumb_gallery_' + galleryNum).className = 'normal';
					document.getElementById('td_thumb_gallery_' + this.numGallery).className = 'selected';
					galleryNum = this.numGallery;
					galleryThumbUpdate();
					myLightbox.start();
					return false;
				};
			var row0 = tbl.insertRow(0);
			var cell0 = row0.insertCell(0);
				cell0.style.height = '145px';
				cell0.verticalAlign = 'middle';
			var row1 = tbl.insertRow(1);
			var cell1 = row1.insertCell(0);
				cell1.verticalAlign = 'bottom';
				objGalleryThumbContainer.appendChild(tbl);
			var img = document.createElement('img');
				img.src = MY_GALLERY[i].photo;
				cell0.appendChild(img);
			var caption = document.createElement('div');
				caption.setAttribute('id', 'galleryItemCaption');
			var a = document.createElement('a');
				a.href="#";
				a.onclick = tbl.onclick;
				a.appendChild(document.createTextNode(truncate(MY_GALLERY[i].name, 40)));
				a.title = MY_GALLERY[i].name;
				caption.appendChild(a);
				cell1.appendChild(caption);
			var galleryItemCaptionNumPhotos = document.createElement('div');
				galleryItemCaptionNumPhotos.setAttribute('id', 'galleryItemCaptionNumPhotos');
            var caption = '';
            if (typeof MY_GALLERY[i].caption == 'string') {
                caption = MY_GALLERY[i].caption;
            } else {
                caption = '(' + MY_GALLERY[i].items.length + ' photos)';
            }
				galleryItemCaptionNumPhotos.appendChild(document.createTextNode(caption));
				cell1.appendChild(galleryItemCaptionNumPhotos);
		}
		galleryWindowContainer.appendChild(objGalleryThumbContainer);

		var objBottomNavCloseLink = document.createElement("a");
		objBottomNavCloseLink.setAttribute('id','bottomNavClose');
		objBottomNavCloseLink.setAttribute('href','#');
		objBottomNavCloseLink.onclick = function() { myGallery.end(); return false; }
		galleryWindowContainer.appendChild(objBottomNavCloseLink);

		var objBottomNavCloseImage = document.createElement("img");
		objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
		objBottomNavCloseLink.appendChild(objBottomNavCloseImage);

		// PAGINATOR
		this.initPaginator(galleryWindowContainer);

		var objBottomCloseLink = document.createElement("a");
		objBottomCloseLink.setAttribute('id','bottomCloseLink');
		objBottomCloseLink.setAttribute('href','#');
		objBottomCloseLink.appendChild(document.createTextNode(textclose));
		objBottomCloseLink.onclick = function() { myGallery.end(); return false; }
		var div = document.createElement('div');
			div.setAttribute('id','bottomCloseLinkDiv');;
			div.style.display = 'none';
			div.appendChild(objBottomCloseLink);
		galleryWindowContainer.appendChild(div);

	},
	initPaginator: function (parentObj){
		this.objPaginator = document.createElement('div');
		this.objPaginator.className = 'gallery_padinator';
		this.objPaginator.style.display = 'none';
		parentObj.appendChild(this.objPaginator);
		this.objPaginator.setAttribute('id','galleryPaginator');
		this.objPaginator.firstPage = 1;
		var objPrevLink = document.createElement("a");
			objPrevLink.setAttribute('id','galleryPrevLink');
			objPrevLink.setAttribute('href','#');
			img = document.createElement('img');
			img.src = 'img/paginatorPrev.png';
			objPrevLink.appendChild(img);
		this.objPaginator.appendChild(objPrevLink);

		for (var i=0; i<10; i++) {
			var a = document.createElement('a');
				a.href="#";
				a.pageNum = i+1;
				a.onclick = function (){
					myGallery.showGalleryPage(this.pageNum);
					return false;
				}
				a.appendChild(document.createTextNode(i+1));
				this.objPaginator.appendChild(a);
		}
			objNextLink = document.createElement("a");
			objNextLink.setAttribute('id','galleryNextLink');
			objNextLink.setAttribute('href','#');
			img = document.createElement('img');
			img.src = 'img/paginatorNext.png';
			objNextLink.appendChild(img);
			//objNextLink.style.display = 'none';
			this.objPaginator.appendChild(objNextLink);

		//objPaginator.style.display = 'none';
	},
	showGalleryThumbs: function() {
		var obj = document.getElementById('galleryThumbContainer');
		obj.style.display = '';
		for(var i=0; i<obj.childNodes.length; i++) {
			if (i>= this.firstNum && i< this.firstNum +6){
				obj.childNodes[i].style.display = '';
			}else{
				obj.childNodes[i].style.display = 'none';
			}
		}
	},
	showGalleryPage: function(page) {
		this.objPaginator.style.display = '';
		if (page == 1) this.objPaginator.childNodes[0].style.display = 'none';
		var pageCount = Math.ceil(MY_GALLERY.length / 6);
		for(var i=1; i<=10; i++) {
			if (i <= pageCount){
				this.objPaginator.childNodes[i].className = '';
				if (i == page) this.objPaginator.childNodes[i].className = 'selected';
				this.objPaginator.childNodes[i].firstChild.nodeValue = this.objPaginator.firstPage + i - 1;
				this.objPaginator.childNodes[i].pageNum = this.objPaginator.firstPage + i - 1;
			}else{
				this.objPaginator.childNodes[i].style.display = 'none';
			}
		}
		this.objPaginator.childNodes[11].style.display = 'none';
		this.firstNum = (page-1)*6;
		this.showGalleryThumbs();
	},
	start: function(imageLink) {
		hideSelectBoxes();
		hideFlash();

		var arrayPageSize = getPageSize();
		Element.setWidth('overlay', arrayPageSize[0]);
		Element.setHeight('overlay', arrayPageSize[1]);
		Element.hide('outerImageContainer');
		Element.hide('imageDataContainer');
		new Effect.Appear('overlay', {
				duration: overlayDuration, from: 0.0, to: overlayOpacity ,
				afterFinish: function(){
					//	Element.show('galleryThumbContainer');
						//myGallery.showGalleryThumbs();
						setTimeout('myGallery.showGalleryThumbs()', 500);
						setTimeout('myGallery.showGalleryPage(1)', 500);
						setTimeout('Element.show(\'bottomCloseLinkDiv\')', 500);
					}
				}
			);

		imageArray = [];
		imageNum = 0;

		if (!document.getElementsByTagName){ return; }

		if (MY_GALLERY[galleryNum].items != undefined){
			for (var i=0; i<MY_GALLERY[galleryNum].items.length; i++) {
				imageArray.push(new Array(MY_GALLERY[galleryNum].items[i].photo, MY_GALLERY[galleryNum].items[i].name));
			}
		}

		var arrayPageScroll = getPageScroll();
		var lightboxTop = 30; //	arrayPageSize[3] / 10;
		//alert(lightboxTop);
		//alert(Element.getHeight('lightbox'));
		window.scroll(0,0);
		var lightboxLeft = arrayPageScroll[0];
		Element.setTop('lightbox', lightboxTop);
		Element.setLeft('lightbox', lightboxLeft);

		Element.show('lightbox');
		Element.show('galleryWindowContainer');
		this.resizeGalleryContainer(648, imageContainerH + 60);
	},

	resizeGalleryContainer: function( imgWidth, imgHeight) {

		// get curren width and height
		this.widthCurrent = Element.getWidth('galleryWindowContainer');
		this.heightCurrent = Element.getHeight('galleryWindowContainer');

		// get new width and height
		var widthNew = (imgWidth  + (borderSize * 2));
		var heightNew = (imgHeight  + (borderSize * 2));

		// scalars based on change from old to new
		this.xScale = ( widthNew / this.widthCurrent) * 100;
		this.yScale = ( heightNew / this.heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = this.widthCurrent - widthNew;
		hDiff = this.heightCurrent - heightNew;
		if(!( hDiff == 0)){ new Effect.Scale('galleryWindowContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
		if (!galleryWindowLoaded) {
			if(!( wDiff == 0)){ new Effect.Scale('galleryWindowContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }
			galleryWindowLoaded = true;
		}

		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
		}
		//Element.setHeight('prevLink', imgHeight);
		//Element.setHeight('nextLink', imgHeight);
		//Element.setWidth( 'imageDataContainer', widthNew);

		//this.showImage();
	},
	end: function() {
		//this.disableKeyboardNav();
		Element.hide('lightbox');
		new Effect.Fade('overlay', { duration: overlayDuration});
		showSelectBoxes();
		setTimeout("showFlash()", 500);
		//showFlash();
	}


} // class

//alert(MY_GALLERY[0].items[0].name);
