var gallery   = null;
var photos    = null;
var animation = null;

function scrollGallery(direction) {
	gallery = document.getElementById('gallery');
	photos  = document.getElementById('photos');
	
	if (direction) {
		animation = window.setInterval('scrollAnimation("'+direction+'")', 50)
	} else {
		window.clearInterval(animation);
	}
}

function scrollAnimation(direction) {
	var current = Math.round(Number(photos.style.marginLeft.substr(0, photos.style.marginLeft.length - 2)));

	if (direction == 'right') {
		var minimum = 0 - (photos.clientWidth - gallery.clientWidth);
		if (current > minimum) {
			var position = current - 5;
		} else {
			var position = minimum;	
		}
	} else if (direction == 'left') {
		if (current > 0 || current == 0) {
			var position = 0;	
		} else {
			var position = current + 5;	
		}
	}
	
	photos.style.marginLeft = String(position) + 'px';	
}

function getWindowSize() {
	if (typeof(window.innerWidth) == 'number') {
	   	return Array(window.innerWidth, window.innerHeight);
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		return Array(document.documentElement.clientWidth, document.documentElement.clientHeight);
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		return Array(document.body.clientWidth, document.body.clientHeight);
	} else {
		return Array(0, 0);
	}	
}

function getWindowScroll() {
	if (typeof(window.pageYOffset) == 'number') {
		return Array(window.pageXOffset, window.pageYOffset);
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		return Array(document.body.scrollLeft, document.body.scrollTop);
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		return Array(document.documentElement.scrollLeft, document.documentElement.scrollTop);
	} else {
		return Array(0, 0);	
	}
}

function showFoto(id) {
	var e = document.getElementById('fotoPopup');
	if (e) {
		var imgLoad = document.createElement('img');
			imgLoad.onload = function () {
				while (e.firstChild) e.removeChild(e.firstChild);
				e.appendChild(imgLoad);
				showPopup();
				sizePopup();				
			}
			imgLoad.src = path + '_loading.gif';	

		var imgFoto = new Image();
			imgFoto.onload = function () {
				while (e.firstChild) e.removeChild(e.firstChild);
				e.appendChild(imgFoto);
				sizePopup();
			}
			imgFoto.src = path + 'php/oferta/podkolanowki/wzory/' + id + '.jpg';
	}
	
}

function sizePopup() {
	var e = document.getElementById('fotoPopup');
	if (e) {
		var siz = getWindowSize();
		var scr = getWindowScroll();
		e.style.left = ( (siz[0] / 2) - (e.clientWidth  / 2) + scr[0] ) + 'px';
		e.style.top  = ( (siz[1] / 2) - (e.clientHeight / 2) + scr[1] ) + 'px';
	}
}

function showPopup() {
	var e = document.getElementById('hideWindow');
	if (e) {
		var size = getWindowSize();
		var main = document.getElementById('mainContainer');			
		var sizeX = (main.offsetWidth  > size[0] ? main.offsetWidth  : size[0]);
		var sizeY = (main.offsetHeight > size[1] ? main.offsetHeight : size[1]);		
		e.style.width   = '100%';
		e.style.height  = sizeY + 'px';		
		e.style.display = 'block';
	}	
	var e = document.getElementById('fotoPopup');
	if (e) {
		e.style.display = 'block';
	}
}

function hidePopup() {
	var e = document.getElementById('hideWindow');
	if (e) {
		e.style.display = 'none';
	}	
	var e = document.getElementById('fotoPopup');
	if (e) {
		e.style.display = 'none';
	}
}
