function gallery(){
	var uls = document.getElementsByTagName('ul');
	var p = document.getElementsByTagName('p');
	var div = document.getElementsByTagName('div');
	
	var rightScroll = document.getElementById('scroll-right');
	var leftScroll = document.getElementById('scroll-left');
	var closeGall = document.getElementById('close');
	var gallPlace = document.getElementById('gallery-place');
	var photo = document.getElementById('photo');
	var closePos = document.getElementById('closePos');
	 if(!closePos) return;
	var next = document.getElementById('foto-str-r');
	var previous = document.getElementById('foto-str-l');
	var filter = []; 
	var infra = [];
	var filterLi = [];
	var shadow = [];
	var web = [];
	var desc = [];
	var ulGall = [];
	var lies = [];
	var cam = [];
	var gall = [];
	var webc = [];
	var myLies = []; 
	
	
	var rightDiv = [];
	var leftDiv = [];
	
	var ulWidth;
	var scrolling;
	var borders;
	
	
	closePos.style.width = 440 + 'px';
	for (var i=0;i<uls.length;i++){
		if ((" "+uls[i].className+" ").indexOf(' gallery-infra ') != -1){ulGallInfra.push(uls[i]);}
	}
	for (var i=0;i<uls.length;i++){
		if ((" "+uls[i].className+" ").indexOf(' gallery ') != -1){ulGall.push(uls[i]);}
		if ((" "+uls[i].className+" ").indexOf(' filter ') != -1){filter.push(uls[i]);}
	}
	for (var i=0;i<ulGall.length;i++){
		lies = ulGall[i].getElementsByTagName('li');
	}
	for (var i=0;i<filter.length;i++){
		filterLi = filter[i].getElementsByTagName('li');
	}
	for (var i=0;i<lies.length;i++){
		if ((" "+lies[i].className+" ").indexOf(' infra ') != -1){infra.push(lies[i]);}
	}
	
	for (var i=0;i<p.length;i++){
		if ((" "+p[i].className+" ").indexOf(' desc ') != -1){desc.push(p[i]);}
	}
	/*********************web************************/
	for (var i=0;i<div.length;i++){
		if ((" "+div[i].className+" ").indexOf(' web-cam ') != -1){web.push(div[i]);}
		if ((" "+div[i].className+" ").indexOf(' web ') != -1){webc.push(div[i]);}
		if ((" "+div[i].className+" ").indexOf(' cam ') != -1){cam.push(div[i]);}
		if ((" "+div[i].className+" ").indexOf(' shadow-horizontal ') != -1){shadow.push(div[i]);}
		if (((" "+div[i].className+" ").indexOf(' left ') != -1) && ((" "+div[i].parentNode.className+" ").indexOf(' shadow ') != -1)){leftDiv.push(div[i]);}
		if (((" "+div[i].className+" ").indexOf(' right ') != -1) && ((" "+div[i].parentNode.className+" ").indexOf(' shadow ') != -1)){rightDiv.push(div[i]);}
	}
	var margin = 25 * cam.length;
	if(cam[0])
	{
		var webWidth = cam[0].offsetWidth * cam.length;
		for (var i=0;i<webc.length;i++){
			webc[i].style.width = webWidth + margin + 'px';
		}
		for (var i=0;i<web.length;i++){
			web[i].onclick = function(){
				gallPlace.style.display = 'block';
				for (var j=0;j<web.length;j++){
					web[j].className = web[j].className.replace(/\bactive\b/gi,'');
				}
				for (var j=0;j<lies.length;j++){
					lies[j].className = lies[j].className.replace(/\bactive\b/gi,'');
				}
				this.className += ' active';
				for (var j=0;j<desc.length;j++){
					desc[j].style.display = 'none';
					if ((" "+desc[j].id+" ").match(" "+this.id+" ")){
						desc[j].style.display = 'block';
						desc[j].style.left = as.getElementPosition(this).left + 1780 + 'px';
						desc[j].style.top = as.getElementPosition(this).top - 590 + 'px';
						desc[j].style.width = this.offsetWidth + 'px';
						
					}
				}
				//photo.src = this.firstChild.src;
				//photo.src = '/i/p1.gif';
				//closePos.style.width = parseInt(photo.parentNode.offsetWidth) + 'px';	
				//closePos.style.width = 440 + 'px';	
			}
		}
	}
	/*********************gallery***********************/
	for (var i=0;i<lies.length;i++){
		lies[i].onclick = function(){
			gallPlace.style.display = 'block';
			for (var j=0;j<web.length;j++){
				web[j].className = web[j].className.replace(/\bactive\b/gi,'');
			}
			for (var j=0;j<lies.length;j++){
				lies[j].className = lies[j].className.replace(/\bactive\b/gi,'');
			}
			
			
			this.className += ' active';
			for (var j=0;j<desc.length;j++){
				desc[j].style.display = 'none';
				if ((" "+desc[j].id+" ").match(" "+this.id+" ")){
					desc[j].style.display = 'block';
					desc[j].style.left = as.getElementPosition(this).left - 205 + 'px';
					desc[j].style.top = as.getElementPosition(this).top - 515 + 'px';
					desc[j].style.width = this.offsetWidth + 'px';
					
				}
			}
			//photo.src = this.firstChild.src;
			//photo.src = '/i/p1.gif';
			
			//closePos.style.width = parseInt(photo.parentNode.offsetWidth) + 'px';
			
			//tmp_width = parseInt(photo.parentNode.offsetWidth) + 'px';
		}
	}
	if(!lies[0]) return;
	var liLength = lies[0].offsetWidth;
	var margin = 0;
	var widthM;
	
	for (var n=0;n<lies.length;n++){
		(function(){lies[n].style.left = margin + 'px';
		//lies[0].style.left = margin + 'px';
		lies[n].style.width = lies[n].firstChild.clientWidth;
		liLength = liLength + (lies[n].offsetWidth - (lies[n].offsetWidth-100));
		margin = margin + 100;
		widthM = margin + (lies[n].offsetWidth-100) ;
		//console.log(lies[n].firstChild);
		})(n)
	}
	for (var n=0;n<ulGall.length;n++){
		ulGall[n].style.left = -100 + 'px';
	}
	ulWidth = widthM - 100;
	for (var i=0;i<shadow.length;i++){
		shadow[i].style.width = parseInt(ulGall[0].parentNode.offsetWidth) + 'px';
	}
	var left = 0;
	var rightBord = ulGall[0].parentNode.offsetWidth;
	var interval = null;
	ulGall[0].style.width = ulWidth + 'px';
	var right = ulGall[0].offsetWidth - parseInt(ulGall[0].parentNode.offsetWidth);
	
	for (var i=0;i<lies.length;i++){
		lies[i].onmouseover = function(){
			this.style.marginLeft = - (this.offsetWidth-100) / 2 + 'px';
			if (this == lies[lies.length-1]){this.style.marginLeft = 0 + 'px';}
			if (this == lies[0]){ this.style.marginLeft = 0 + "px"}
		}
		lies[i].onmouseout = function(){
			this.style.marginLeft = 0 + 'px';
			if (this == lies[0]){this.parentNode.style.left = -100 + 'px';}
		}
	}
	
	if (ulWidth < ulGall[0].parentNode.offsetWidth){
		ulGall[0].parentNode.parentNode.style.width = ulWidth + 'px';
		ulGall[0].parentNode.style.width = ulWidth + 'px';
		rightScroll.style.display = 'none';
		leftScroll.style.display = 'none';
	}
	else{
		ulGall[0].style.width = ulWidth + 200 + 'px';
		rightScroll.style.display = 'block';
		leftScroll.style.display = 'block';
		for (var j=0;j<leftDiv.length;j++){
			leftDiv[j].style.left = 12 + 'px';
		}
		for (var j=0;j<rightDiv.length;j++){
			rightDiv[j].style.right = 9 + 'px';
		}
	}
	
	var leftPosition = parseInt(ulGall[0].style.left);
	
	var scrollingNow = true;
	function scrollLeft(){
		for (var i=0;i<lies.length;i++){
			scrolling = (lies[i].offsetWidth - (lies[i].offsetWidth-100)) * 4;
		}
		for (var h=0;h<ulGall.length;h++){
		if (ulWidth - ulGall[h].parentNode.offsetWidth + leftPosition < scrolling ){scrolling =  ulWidth - ulGall[h].parentNode.offsetWidth + leftPosition;}
		}
		for (var i=0;i<scrolling;i++) {
			setTimeout(function() {
				scrollingLeft();
				if (i==scrolling-1){scrollingNow = true;}
						
			},i);
		}
	}
	function scrollingLeft(){
		scrollingNow = false;
		leftPosition -= 1;
		ulGall[0].style.left = leftPosition + 'px';
		
	}
	rightScroll.onclick = function (e){
		stopEvent(e);
		if (!scrollingNow){stopEvent(e);}
		else{
			if (leftPosition <= -right){clearInterval(interval);}
			else {scrollLeft();}
			for (var j=0;j<desc.length;j++){
				desc[j].style.display = 'none';
			}
		}
		//scrollingNow = true;
	}
	function scrollRight(){
		for (var i=0;i<lies.length;i++){
			scrolling = (lies[i].offsetWidth - (lies[i].offsetWidth-100)) * 4;
		}
		if (-leftPosition < scrolling ){scrolling =  -leftPosition;}
		for (var i=0;i<scrolling;i++) {
			setTimeout(function() {
				scrollingRight();
				if (i==scrolling-1){scrollingNow = true;}
			},i);
		}
	}
	function scrollingRight(){
		leftPosition += 1;
		ulGall[0].style.left = leftPosition + 'px';
		scrollingNow = false;
	}
	leftScroll.onclick = function (e){
		stopEvent(e);
		if (!scrollingNow){stopEvent(e);}
		else{
			if (leftPosition >= left){clearInterval(interval);}
			else {scrollRight();}
			for (var j=0;j<desc.length;j++){
				desc[j].style.display = 'none';
			}
		}
		//scrollingNow = true;
	}
	
	/*********************1*********************/
	
	closeGall.onclick = function(e){
		stopEvent(e);
		gallPlace.style.display = 'none';
		
	}
	
	/*******************filter-foto***********************/
	for (var i=0;i<filterLi.length;i++){
		filterLi[i].onclick = function(e){
			stopEvent(e);
			ulWidth = 965;
			ulGall[0].parentNode.parentNode.style.width = ulWidth + 'px';
			ulGall[0].parentNode.style.width = ulWidth + 'px';
			for (var j=0;j<desc.length;j++){
				desc[j].style.display = 'none';
			}
			for (var j=0;j<lies.length;j++){
				lies[j].style.display = 'none';
				lies[j].className = lies[j].className.replace(/\bactive\b/gi,'');
			}
			var liLength = lies[0].offsetWidth;
			var margin = 0;
			for (var j=0;j<lies.length;j++){
				if ((" "+this.className+" ").match(lies[j].className.replace(/\bactive\b/gi,''))){
					lies[j].style.display = 'block';
						//for (var k=1;k<lies.length;k++){
					lies[j].style.left = margin + 'px';
					//console.log(margin);
					liLength = liLength + (lies[j].offsetWidth - (lies[j].offsetWidth-100));
						//}
					margin = margin + 100;
					widthM = margin +  (lies[j].offsetWidth-100);
		
				}
			}
			ulWidth = widthM ;
			//console.log(margin);
			if (ulWidth < ulGall[0].parentNode.offsetWidth){
				ulGall[0].parentNode.parentNode.style.width = ulWidth + 'px';
				ulGall[0].parentNode.style.width = ulWidth + 'px';
				rightScroll.style.display = 'none';
				leftScroll.style.display = 'none';
			}
			else{
				rightScroll.style.display = 'block';
				leftScroll.style.display = 'block';
				rightScroll.style.right = - 12 + 'px';
				leftScroll.style.left = - 12 + 'px';
				for (var j=0;j<leftDiv.length;j++){
					leftDiv[j].style.left = 1 + 'px';
				}
				for (var j=0;j<rightDiv.length;j++){
					rightDiv[j].style.right = 2 + 'px';
				}
			}
			ulGall[0].style.left = 0 + 'px';
		}
	}
	
}


/*if (ulWidth < ulGall[0].parentNode.offsetWidth){
		ulGall[0].parentNode.parentNode.style.width = ulWidth + 'px';
		ulGall[0].parentNode.style.width = ulWidth + 'px';
		rightScroll.style.display = 'none';
		leftScroll.style.display = 'none';
	}
	else{
		rightScroll.style.display = 'block';
		leftScroll.style.display = 'block';
		for (var j=0;j<leftDiv.length;j++){
			leftDiv[j].style.left = 12 + 'px';
		}
		for (var j=0;j<rightDiv.length;j++){
			rightDiv[j].style.right = 15 + 'px';
		}
	}*/



function stopEvent(e){
	var event = e || window.event;
	event.preventDefault ? event.preventDefault() : event.returnValue = false;
}
function superOnload(fnName) {
	var ol = window.onload;
	if (typeof ol == "function") {
		window.onload = function() {
			ol();
			fnName();
		}
	}
	else {
		window.onload = fnName;
	}
}
superOnload(gallery);

