var mugogallerythumbscrollanim = null;

function mugogallery(action, senderel) {
	var galleryel = document.getElementById('fsimagediv');
	var fsimagesizemax = {width: 644, height: 483};
	var thumbsize = {width: 128, height: 96, marginw: 1};
	if (action == 'open') {
		var thumbselobjheight = 96;
		var galleryelotherelementsheight = thumbselobjheight+20;
		var pelheight = 27;
		var galleryelwinheight = fsimagesizemax.height+galleryelotherelementsheight;
		var windowheight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			windowheight = parseInt(window.innerHeight);
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			windowheight = parseInt(document.documentElement.clientHeight);
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			windowheight = parseInt(document.body.clientHeight);
		}
		var imgsrc = '';
		var origimgsrc = '';
		var origimgsize = {width: 0, height: 0};
		var imgindex = 0;
		var imgcount = 0;
		var imgdescription = '';
		if (galleryelwinheight > (windowheight - 50)) galleryelwinheight = windowheight-50;
		var galleryelwintop = Math.round((windowheight/2)-(galleryelwinheight / 2));
		if (galleryelwintop < 0) galleryelwintop = 0;
		var fsimagesize = {width: fsimagesizemax.width, height: (galleryelwinheight-galleryelotherelementsheight)};
		if (galleryel == null) {
		var thumbsarr = new Array();
		var thumbels = senderel.parentNode.parentNode.parentNode.getElementsByTagName('a');
		for (var ithumbel=0;ithumbel<thumbels.length;ithumbel++) {
			var thumbel = thumbels[ithumbel];
			if (thumbel.getElementsByTagName('img').length == 1) {
			thumbsarr.push({src:thumbel.getAttribute('data-imgsrc'),imageindex:imgcount+1,description:thumbel.getAttribute('title'),origthumbsrc:thumbel.getElementsByTagName('img')[0].getAttribute('src'),origthumbsize:{width:parseInt(thumbel.getElementsByTagName('img')[0].offsetWidth),height:parseInt(thumbel.getElementsByTagName('img')[0].offsetHeight)}});
			if (thumbel == senderel) imgindex = imgcount+1;
			imgcount = imgcount + 1;
			}
		}
		imgsrc = 'subpages/image.php?imgwidth=' + fsimagesize.width + '&imgheight=' + fsimagesize.height + '&usecrop=false&upscale=false&imgfn=' + encodeURIComponent(thumbsarr[imgindex-1].src);
		imgdescription = thumbsarr[imgindex-1].description;
		galleryel = document.createElement('div');
		galleryel.id = 'fsimagediv';
		galleryel.style.top = galleryelwintop + 'px';
		galleryel.style.display = 'none';
		if (!IsIE78Moz()) galleryel.style.position = 'absolute';
		var fsimageholderel = document.createElement('div');
		fsimageholderel.id = 'fsimageholder';
		fsimageholderel.style.height = fsimagesize.height + 'px';
		var imgel = document.createElement('img');
		fsimageholderel.appendChild(imgel);
		var loaderel = document.createElement('div');
		loaderel.className = 'loader';
		var loaderimgel = document.createElement('img');
		loaderimgel.src = 'design/mugogalleryfsloader.gif';
		loaderel.appendChild(loaderimgel);
		fsimageholderel.appendChild(loaderel);
		var ael = document.createElement('a');
		ael.className = 'prev';
		ael.style.height = fsimagesize.height + 'px';
		ael.setAttribute('href','#');
		ael.onclick = function() {mugogallery('prev', null); return false;};
		fsimageholderel.appendChild(ael);
		var ael = document.createElement('a');
		ael.className = 'next';
		ael.style.height = fsimagesize.height + 'px';
		ael.setAttribute('href','#');
		ael.onclick = function() {mugogallery('next', null); return false;};
		fsimageholderel.appendChild(ael);
		var ael = document.createElement('a');
		ael.className = 'close';
		ael.setAttribute('href','#');
		ael.onclick = function() {mugogallery('close', null); return false;};
		fsimageholderel.appendChild(ael);
		var ael = document.createElement('a');
		ael.className = 'shoplink';
		ael.id = 'galleryfsshoplink';
		ael.style.display = 'none';
		ael.setAttribute('target','_blank');
		ael.setAttribute('href','#');
		fsimageholderel.appendChild(ael);
		var pel = document.createElement('p');
		pel.style.top = parseInt(fsimagesize.height - pelheight) + 'px';
		fsimageholderel.appendChild(pel);
		galleryel.appendChild(fsimageholderel);
		var thumbsel = document.createElement('div');
		var thumbsulel = document.createElement('ul');
		thumbsulel.className = 'clearfix';
		for (var ithumbel=0;ithumbel<thumbsarr.length;ithumbel++) {
			var thumbel = thumbsarr[ithumbel];
			var thumbliel = document.createElement('li');
			var thumbael = document.createElement('a');
			thumbael.setAttribute('href', '#');
			thumbael.setAttribute('title',thumbel.description);
			thumbael.setAttribute('data-imgsrc',thumbel.src);
			if (ithumbel+1 == imgindex) thumbael.className = 'active';
			thumbael.onclick = function() {mugogallery('open', this); return false;};
			var thumbimgel = document.createElement('img');
			thumbimgel.setAttribute('width', thumbsize.width);
			thumbimgel.setAttribute('height', thumbsize.height);
			thumbimgel.src = thumbel.origthumbsrc;
			thumbael.appendChild(thumbimgel);
			thumbimgel.src = 'subpages/image.php?imgwidth=' + thumbsize.width + '&imgheight=' + thumbsize.height + '&isthumb=1&cancreatethumb=1&createblank1x1=1&usecrop=true&imgfn=' + encodeURIComponent(thumbel.src);
			thumbliel.appendChild(thumbael);
			thumbsulel.appendChild(thumbliel);
		}
		thumbsulel.style.width = parseInt(((thumbsize.width + thumbsize.marginw) * imgcount) + 10) + 'px';
		thumbsel.setAttribute('data-curscroll', 0);
		thumbsel.appendChild(thumbsulel);
		galleryel.appendChild(thumbsel);
		document.body.appendChild(galleryel);
		mugogallery('showloader', null);
		imgel.onload = function() {mugogallery('hideloader', null);};
		imgel.src = imgsrc;
		thumbsel.scrollLeft = 0;
		galleryel.setAttribute('data-imgcount', imgcount);
		galleryel.setAttribute('data-imgwidth', fsimagesize.width);
		galleryel.setAttribute('data-imgheight', fsimagesize.height);
		var thumbselopenanim = new Animator({transition: Animator.makeEaseOut(1), duration: 500});
		thumbselopenanim.addSubject(new NumericalStyleSubject(thumbsel, 'height', 0, thumbselobjheight));
		thumbselopenanim.jumpTo(0);
		thumbselopenanim.seekTo(1);
		} else {
		imgcount = parseInt(galleryel.getAttribute('data-imgcount'));
		var pel = galleryel.getElementsByTagName('p')[0];
		var thumbels = galleryel.getElementsByTagName('ul')[0].getElementsByTagName('a');
		if (parseInt(galleryel.getAttribute('data-curimage')) > 0) thumbels[parseInt(galleryel.getAttribute('data-curimage'))-1].className = '';
		for (var ithumbel=0;ithumbel<thumbels.length;ithumbel++) {
			var thumbel = thumbels[ithumbel];
			if (thumbel == senderel) {
				imgindex = ithumbel+1;
				imgsrc = 'subpages/image.php?imgwidth=' + fsimagesize.width + '&imgheight=' + fsimagesize.height + '&usecrop=false&upscale=false&imgfn=' + encodeURIComponent(thumbel.getAttribute('data-imgsrc'));
				imgdescription = thumbel.getAttribute('title');
				thumbel.className = 'active';
				break;
			}
		}
		mugogallery('showloader', null);
		var imgel = galleryel.getElementsByTagName('img')[0];
		imgel.src = imgsrc;
		}
		var imgdescriptionlink = '';
		if (imgdescription.indexOf("|") >= 0) {
			var tmpimgdescriptionarr = imgdescription.split("|",3);
			imgdescription = tmpimgdescriptionarr[0];
			imgdescription.trim();
			imgdescriptionlink = tmpimgdescriptionarr[1];
			imgdescriptionlink.trim();
		}
		pel.innerHTML = '<span>' + imgindex + '/' + imgcount + '</span>' + imgdescription;
		if (imgdescriptionlink != '') {
			document.getElementById('galleryfsshoplink').setAttribute('href', imgdescriptionlink);
			document.getElementById('galleryfsshoplink').style.display = '';
		} else {
			document.getElementById('galleryfsshoplink').setAttribute('href', '#');
			document.getElementById('galleryfsshoplink').style.display = 'none';
		}
		galleryel.setAttribute('data-curimage', imgindex);
		if (galleryel.style.display == 'none') {
		pageOpenPageBlend(0);
		galleryel.style.display = '';
		document.getElementById('pageblenddiv').onclick = function() {mugogallery('close', null);};
		}
		mugogallery('refreshprevnextbtns', null);
		mugogallery('scrollthumb', null);
	}
	if (action == 'close') {
		pageClosePageBlend();
		if (galleryel != null) galleryel.parentNode.removeChild(galleryel);
	}
	if ((action == 'next') || (action == 'prev')) {
		if (galleryel != null) {
		var imgcount = parseInt(galleryel.getAttribute('data-imgcount'));
		if (action == 'next') {
		var showimgindex = parseInt(galleryel.getAttribute('data-curimage'))+1;
		} else {
		var showimgindex = parseInt(galleryel.getAttribute('data-curimage'))-1;
		}
		if ((showimgindex <= imgcount) && (showimgindex > 0)) {
		var fsimagesize = {width: parseInt(galleryel.getAttribute('data-imgwidth')), height: parseInt(galleryel.getAttribute('data-imgheight'))};
		var pel = galleryel.getElementsByTagName('p')[0];
		var thumbels = galleryel.getElementsByTagName('ul')[0].getElementsByTagName('a');
		if (parseInt(galleryel.getAttribute('data-curimage')) > 0) thumbels[parseInt(galleryel.getAttribute('data-curimage'))-1].className = '';
		var thumbel = thumbels[showimgindex-1];
		var imgindex = showimgindex;
		var imgsrc = 'subpages/image.php?imgwidth=' + fsimagesize.width + '&imgheight=' + fsimagesize.height + '&usecrop=false&upscale=false&imgfn=' + encodeURIComponent(thumbel.getAttribute('data-imgsrc'));
		var imgdescription = thumbel.getAttribute('title');
		thumbel.className = 'active';
		mugogallery('showloader', null);
		var imgel = galleryel.getElementsByTagName('img')[0];
		imgel.src = imgsrc;
		var imgdescriptionlink = '';
		var imgdescriptionlinkcaption = 'shop';
		if (imgdescription.indexOf("|") >= 0) {
			var tmpimgdescriptionarr = imgdescription.split("|",3);
			imgdescription = tmpimgdescriptionarr[0];
			imgdescription.trim();
			imgdescriptionlink = tmpimgdescriptionarr[1];
			imgdescriptionlink.trim();
		}
		pel.innerHTML = '<span>' + imgindex + '/' + imgcount + '</span>' + imgdescription;
		if (imgdescriptionlink != '') {
			document.getElementById('galleryfsshoplink').setAttribute('href', imgdescriptionlink);
			document.getElementById('galleryfsshoplink').style.display = '';
		} else {
			document.getElementById('galleryfsshoplink').setAttribute('href', '#');
			document.getElementById('galleryfsshoplink').style.display = 'none';
		}
		galleryel.setAttribute('data-curimage', imgindex);
		mugogallery('refreshprevnextbtns', null);
		mugogallery('scrollthumb', null);
		}
		}
	}
	if (action == 'refreshprevnextbtns') {
		if (galleryel != null) {
		galleryel.getElementsByTagName('a')[0].style.display = (parseInt(galleryel.getAttribute('data-curimage')) > 1) ? '':'none';
		galleryel.getElementsByTagName('a')[1].style.display = (parseInt(galleryel.getAttribute('data-curimage')) < parseInt(galleryel.getAttribute('data-imgcount'))) ? '':'none';
		}
	}
	if (action == 'scrollthumb') {
		if (galleryel != null) {
		var divh = galleryel.getElementsByTagName('ul')[0].parentNode;
		var curscroll = parseInt(divh.getAttribute('data-curscroll'));
		var newscroll = 0;
		var itemwidth = thumbsize.width + thumbsize.marginw;
		var scrollmax = (itemwidth * (parseInt(divh.getElementsByTagName('li').length) - 5));
		newscroll = (parseInt(galleryel.getAttribute('data-curimage')) - 3) * itemwidth;
		if (newscroll < 0) newscroll = 0;
		if (newscroll > scrollmax) newscroll = scrollmax;
		if (newscroll != curscroll) {
		if (mugogallerythumbscrollanim != null) {
		clearInterval(mugogallerythumbscrollanim.intervalId);
		mugogallerythumbscrollanim.clearSubjects();
		mugogallerythumbscrollanim = null;
		}
		mugogallerythumbscrollanim = new Animator({transition: Animator.makeEaseOut(1), duration: 1000});
		mugogallerythumbscrollanim.startscrollpos = curscroll;
		mugogallerythumbscrollanim.setscrollpos = newscroll;
		mugogallerythumbscrollanim.addSubject(function (value) {var divh = document.getElementById('fsimagediv').getElementsByTagName('ul')[0].parentNode; var scrollpos = mugogallerythumbscrollanim.startscrollpos + Math.round(value*(mugogallerythumbscrollanim.setscrollpos-mugogallerythumbscrollanim.startscrollpos)); divh.scrollLeft = scrollpos; divh.setAttribute('data-curscroll', scrollpos);});
		mugogallerythumbscrollanim.seekFromTo(0, 1);
		}
		}
	}
	if ((action == 'showloader') || (action == 'hideloader')) {
		if (galleryel != null) {
		var loaderel = galleryel.getElementsByTagName('img')[0].parentNode.getElementsByTagName('div');
		if (loaderel.length > 0) loaderel[0].style.display = (action == 'showloader') ? '':'none';
		}
	}
}
