if (document.getElementsByClassName == undefined) {
	document.getElementsByClassName = function(cl) {
		var retnode = [];
		var myclass = new RegExp('\\b'+cl+'\\b');
		var elem = this.getElementsByTagName('*');
		for (var i = 0; i < elem.length; i++) {
			var classes = elem[i].className;
			if (myclass.test(classes)) {
				retnode.push(elem[i]);
				}
			}
		return retnode;
		}
	}

var myGallery = new Array();
var aGallery = new Array(-1,0);
var moveG = new Array();
var step0 = 2
var mystep = step0;

function myPic(gal,img) {
	if (gal < 0) {
		if (img) {
			aGallery[1] += img;
			if (aGallery[1] < 0)
				aGallery[1] = myGallery[aGallery[0]].length - 1;
			else if (aGallery[1] >= myGallery[aGallery[0]].length)
				aGallery[1] = 0;
			writePic();
			}
		else {
			document.getElementById('bigpic').style.display = 'none';
			document.getElementById('picinfo').innerHTML = '';
			}
		}
	else {
		aGallery[0] = gal;
		aGallery[1] = img;
		writePic();
		}
	}

function writePic() {
	document.getElementById('ipic').style.visibility = 'hidden';
	var inf = myGallery[aGallery[0]][aGallery[1]];
	document.getElementById('ipic').setAttribute('alt',inf[1]);
	document.getElementById('bigpic').style.display = 'block';
	var myimage = new Image();
	myimage.onload = function() {
		var w = myimage.width;
		var h = myimage.height;
		var nh = (h > document.getElementById('bigpic').offsetHeight - 140?document.getElementById('bigpic').offsetHeight - 140:h);
		var nw = (nh == h?w:Math.round(nh*w/h));
		if (nw > document.getElementById('bigpic').offsetWidth - 50) {
			nh = Math.round((document.getElementById('bigpic').offsetWidth - 50)*nh/nw);
			nw = document.getElementById('bigpic').offsetWidth - 50;
			}
		var str = '<b>Изображение '+(aGallery[1]+1)+' из '+myGallery[aGallery[0]].length+'</b> '+inf[1].replace('<','&lt;');
		document.getElementById('picinfo').innerHTML = str;

		document.getElementById('ipic').style.width = nw + 'px';
		document.getElementById('ipic').style.height = nh + 'px';
		document.getElementById('mpic').style.width = nw + 'px';
		document.getElementById('mpic').style.height = nh + 'px';
		document.getElementById('wpic').style.width = nw + 2 + 'px';
		document.getElementById('ipic').src = inf[0];

		document.getElementById('ipic').style.visibility = 'visible';
		document.getElementById('apic').setAttribute('href',inf[0]);
		}
	myimage.src = inf[0];
	}

function galcorrect(n) {
	var obj = document.getElementById('gll_'+n);
//	document.getElementById('toL'+n).style.display = (obj.offsetLeft < 0?'block':'none');
//	document.getElementById('toR'+n).style.display = (obj.offsetWidth + obj.offsetLeft > obj.parentNode.offsetWidth?'block':'none');
	}

function movegall(gal,ort) { if (ort || ort === 0 || moveG[gal]) {
	if (ort !== 0) moveG[gal] = ort;
	if (moveG[gal]) {
		obj = document.getElementById('gll_'+gal);
		var x = obj.offsetLeft;
		x += moveG[gal] * mystep;
		mystep += 2;
		if (mystep > 60) mystep = 60;
		if (x >= 40 || obj.offsetWidth + 80 <= obj.parentNode.offsetWidth) {
			x = 40;
			moveG[gal] = null;
			mystep = step0;
			}
		else if (obj.offsetWidth + 80 > obj.parentNode.offsetWidth && obj.parentNode.offsetWidth - obj.offsetWidth - 40  >= x) {
			x = obj.parentNode.offsetWidth - obj.offsetWidth - 40;
			moveG[gal] = null;
			mystep = step0;
			}
		obj.style.left = x + 'px';
		if (moveG[gal]) {
			setTimeout('movegall('+gal+',0);',50);
			}
		else {
			moveG[gal] = null;
			mystep = step0;
			galcorrect(gal);
			}
		}
	else {
		mystep = step0;
		galcorrect(gal);
		}
	} else { moveG[gal] = null; mystep = step0; } }

window.onload = function() {
	var dm = window.location.hostname;
	dm = new RegExp('^https?:\/\/'+dm,'g');
	var gal = document.getElementsByClassName('gallery');
	for (var i = 0; i < gal.length; i ++) {
		myGallery[i] = new Array();
		gal[i].style.overflow = 'hidden';
		gal[i].style.overflowX = 'hidden';
		gal[i].style.width = 'auto';
		var str = '';
		var img = gal[i].getElementsByTagName('img');
		for (var a = 0; a < img.length; a ++) {
			var src = img[a].getAttribute('src');
			var alt = img[a].getAttribute('alt');
			src = src.replace(dm,'');
			var newsrc = src.replace('/textor/preview.php?h=118&a=','/textor_images/');
			myGallery[i][a] = new Array(newsrc,(alt?alt:''));
			str += '<a href="'+newsrc+'" target=_blank><img src="'+src+'" onClick="myPic('+i+','+a+');return false;"></a>'+"\n";
			}
		gal[i].innerHTML = '<div id="gll_'+i+'" class=innr>'+str+'</div><a href=# class="toL toLR" id=toL'+i+' onMouseDown="movegall('+i+',1);" onClick="return false;" onMouseUp="movegall('+i+',null);" onMouseOut="movegall('+i+',null);"></a><a href=# class="toR toLR" id=toR'+i+' onMouseDown="movegall('+i+',-1);" onClick="return false;" onMouseUp="movegall('+i+',null);" onMouseOut="movegall('+i+',null);"></a></div>';
		moveG[i] = null;
		setTimeout('galcorrect('+i+');',1500);
		}

	if (gal.length) {
		document.getElementsByTagName('body')[0].innerHTML += '<div id=bigpic><div id=wpic><a href="#" id=picclose onClick="myPic(-1,0);return false;">[закрыть]</a><div id=mpic><a id=apic href=# onClick="myPic(-1,1);return false;"><img src="/textor_img/black12.png" id=ipic></a></div><a href=# id=picleft class=picnav onClick="myPic(-1,-1);return false;">&laquo; предыдущее</a><a href=# id=picright class=picnav onClick="myPic(-1,1);return false;">следующее &raquo;</a><div id=picinfo></div></div></div>';
		}
	}
