var halfX;
var halfY;
var fullX;
var fullY;

function setAlpha(obj,per) {
	inone = per / 100;
	if (inone >= 1) inone = 0.99;
	obj.style.opacity = inone;
	obj.style.MozOpacity = inone;
	obj.style.KhtmlOpacity = per / 100;
	obj.style.filter = "alpha(opacity=" + per + ")";
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function initial() {
	IE = document.all ? true : false;
	fox = new RegExp("Firefox","i");
	chrome = new RegExp("Chrome","i");
	safari = new RegExp("Safari","i");
	opera = new RegExp("Opera","i");
	if (IE) {
		fullX = document.body.clientWidth;
		fullY = getWindowHeight();
	}
	else if (fox.test(navigator.userAgent) || chrome.test(navigator.userAgent) || safari.test(navigator.userAgent) || opera.test(navigator.userAgent)) {
		fullX = document.body.clientWidth;
		fullY = getWindowHeight();
	}
	else {
		fullX = pageXOffset;
		fullY = pageYOffset;
	}
	halfX = Math.floor(fullX / 2);
	halfY = fullY / 2;
}
initial();

if(IE == true){
	var _nTop = 229;
}else{
	var _nTop = 230;
}
var _nLeft = halfX - 445;
//var _nTop = 229;//230
var _nHeight = 348;
var _nWidth = 517;
var _arrCol = new Array();
var _elementWidth = 30;
var _elementWidthAll = 560;
var _bReady = false;
//tasted, scent, sound, touch, spirit, signts
var _arrColCol = new Array("#b99651","#e1d0b0","#d4bba7","#602416","#7e0f14","#ebcf9c");
if (!_strDir) var _strDir = "images/gallerys/";
if (!_arrCols) var _arrCols = new Array();
if (!_arrStrPics) var _arrStrPics = new Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg","06.jpg","07.jpg","08.jpg","09.jpg","10.jpg","11.jpg","12.jpg","13.jpg","14.jpg","15.jpg","16.jpg","17.jpg","18.jpg","19.jpg","20.jpg");
var _arrPics = new Array();
var _eleStack;
var _eleStack2;
var _nBegin = 0;
var _nEleWidth;
var _nEleLeft;
var _nEleLeftLmt;
var _divTop;
var _divWhite;
var _nPicsLoaded = 1;
var _nAlpha;
var _nLast;
var _nSel;
var _nLastCol;
var _bLocked = false;
var _nNext = 1;

function picLoaded() {
	_nPicsLoaded++;
	if (_nPicsLoaded >= _arrStrPics.length) {
		_divTop.innerHTML = "";
		_bReady = true;
		if (_arrCols.length > 0) {
			selPic(0,_arrCols[0]);
			if (_arrStrPics.length > 1) setTimeout(autoPlay,10000);
		}
		else _divTop.style.backgroundImage = "url(" + _arrPics[0].src + ")";
	}
	else document.getElementById("strLoading2").innerHTML = "Photos loading... (" + _nPicsLoaded + " of " + _arrStrPics.length + ")";
}

function autoPlay() {
	selPic(_nNext,_arrCols[_nNext]);
	_nNext++;
	if (_nNext >= _arrCols.length) {
		_nNext = 0;
	}
	setTimeout(autoPlay,10000);
}

function clickMe() {
	//cngOnly(this.myIndex);
	if (this.myIndex == (_arrCol.length - 1)) return;
	changeTo(this.myIndex);
}

function selPic(n,c) {
	if (_nSel == n) return;
	if (_bLocked) return;
	_bLocked = true;
	_nSel = n;
	if (c == _arrCol[_arrCol.length - 1].myId) {
		cngOnly();
	}
	else {
		bFound = false;
		for (i = 0;i < _arrCol.length;i++) {
			if (_arrCol[i].myId == c) {
				c = i;
				bFound = true;
				break;
			}
		}
		if (!bFound) c = _arrCol.length - 1;
		_nLastCol = c;
		changeTo(c);
	}
}

function changeTo(n) {
	_eleStack = _arrCol[n];
	_nBegin = n + 1;
	_nEleWidth = _elementWidthAll;
	_nEleLeft = parseInt(_arrCol[_nBegin].style.left);
	_nEleLeftLmt = parseInt(_arrCol[n].style.left);
	changeTo2();
}

function changeTo2() {
	_nEleWidth += 5;
	_nEleLeft -= 5;
	for (i = _nBegin;i < _arrCol.length;i++) {
		ele = _arrCol[i];
		ele.style.left = _nEleLeft + ((i - _nBegin) * _elementWidth) + "px";
		ele.style.width = _nEleWidth + "px";
	}
	if (_nEleLeft <= _nEleLeftLmt) {
		var arrNew = new Array();
		o = 0;
		for (i = 0;i < _arrCol.length;i++) {
			ele = _arrCol[i];
			if (ele == _eleStack) {
				ele.style.zIndex = _arrCol.length;
				ele.myIndex = _arrCol.length - 1;
			}
			else {
				arrNew[o] = ele;
				ele.style.zIndex = o + 1;
				ele.myIndex = o;
				o++;
			}
		}
		arrNew[arrNew.length] = _eleStack;
		_eleStack.style.zIndex = _arrCol.length + 3;
		_eleStack.style.width = "1px";
		_arrCol = arrNew;
		nLast = _arrCol.length - 1;
		_nEleWidth = 0;
		_nEleLeft = (nLast * _elementWidth) + _nLeft + _elementWidthAll;
		_eleStack.style.left = _nEleLeft + "px";
		_nBegin = (nLast * _elementWidth) + _nLeft;
		setTimeout(changeTo3,25);
		return;
	}
	setTimeout(changeTo2,25);
}

function changeTo3() {
	n = Math.ceil((_nEleLeft - _nBegin) / 4);
	_nEleLeft -= n;
	_nEleWidth += n;
	if (_nEleLeft <= _nBegin) {
		_nEleLeft = _nBegin;
		_nEleWidth = _elementWidthAll;
	}
	_eleStack.style.left = _nEleLeft + "px";
	_eleStack.style.width = _nEleWidth + "px";
	if (_nEleLeft <= _nBegin) {
		_eleStack.style.zIndex = _arrCol.length;
		_nAlpha = 0;
		_divTop.style.left = (parseInt(_arrCol[_arrCol.length - 1].style.left) + 30) + "px";
		_divTop.style.top = _arrCol[_arrCol.length - 1].style.top;
		_divTop.style.zIndex = _arrCol.length + 2;
		_divWhite.style.zIndex = _divTop.style.zIndex - 1;
		_divWhite.style.display = "none";
		_divTop.style.backgroundImage = "url(" + _arrPics[_nSel].src + ")";
		fadeIn();
		return;
	}
	setTimeout(changeTo3,25);
}

function cngOnly() {
	_nAlpha = 100;
	_divWhite.style.display = "none";
	fadeOut();
}
function fadeOut() {
	_nAlpha -= 20;
	setAlpha(_divTop,_nAlpha);
	if (_nAlpha <= 0) {
		_divTop.style.backgroundImage = "url(" + _arrPics[_nSel].src + ")";
		fadeIn();
		return;
	}
	setTimeout(fadeOut,25);
}

function fadeIn() {
	_nAlpha += 20;
	setAlpha(_divTop,_nAlpha);
	if (_nAlpha >= 100) {
		_divWhite.style.display = "";
		_divWhite.style.zIndex = _divTop.style.zIndex - 1;
		setTimeout(flash,0);
		return;
	}
	setTimeout(fadeIn,25);
}
function flash() {
	_nAlpha -= 20;
	setAlpha(_divTop,_nAlpha);
	if (_nAlpha <= 60) {
		setTimeout(flash2,25);
		return;
	}
	setTimeout(flash,35);
}
function flash2() {
	_nAlpha += 20;
	setAlpha(_divTop,_nAlpha);
	if (_nAlpha >= 100) {
		_divWhite.style.display = "none";
		_bLocked = false;
		return;
	}
	setTimeout(flash2,25);
}

for (i = 0;i < _arrColCol.length;i++) {
	_arrCol[i] = document.createElement("div");
	ele = _arrCol[i];
	ele.style.backgroundColor = _arrColCol[i];
	ele.style.backgroundImage = "url(images/gal_bg_" + i + ".png)";
	ele.style.backgroundPosition = "0px " + (_nHeight - 95) + "px";
	ele.style.backgroundRepeat = "no-repeat";
	ele.style.position = "absolute";
	ele.style.width = _elementWidthAll + "px";
	ele.style.height = _nHeight + "px";
	ele.style.left = (_nLeft + (i * _elementWidth)) + "px";
	ele.style.top = _nTop + "px";
	ele.myIndex = i;
	ele.myId = i;
	ele.style.zIndex = ele.myIndex + 1;
	//ele.onclick = clickMe;
	document.body.appendChild(ele);
}
_arrCol[_arrCol.length - 1].style.width = _elementWidthAll;
_divTop = document.createElement("div");
_divTop.style.zIndex = _arrCol.length + 2;
_divTop.style.position = "absolute";
_divTop.style.left = (parseInt(_arrCol[_arrCol.length - 1].style.left) + 30) + "px";
_divTop.style.top = _nTop + "px";
_divTop.style.width = _nWidth + "px";
_divTop.style.height = _nHeight + "px";
_divTop.style.fontSize = "11px";
_divTop.style.color = "#888888";
_divTop.style.backgroundColor = "#000000";
_divTop.innerHTML = "<div style=\"padding-top:200px\" align=\"center\"><div id=\"strLoading2\">Preparing...</div></div>";
_divWhite = document.createElement("div");
//_divWhite.style.backgroundColor = "#000000";
_divWhite.style.zIndex = _divTop.style.zIndex - 1;
_divWhite.style.position = "absolute";
_divWhite.style.left = _divTop.style.left;
_divWhite.style.top = _divTop.style.top;
_divWhite.style.width = _divTop.style.width;
_divWhite.style.height = _divTop.style.height;
_divWhite.style.display = "none";
document.body.appendChild(_divTop);
document.body.appendChild(_divWhite);

for (i = 0;i < _arrStrPics.length;i++) {
	_arrPics[i] = new Image();
	pic = _arrPics[i];
	pic.onload = picLoaded;
	pic.src = _strDir + _arrStrPics[i];
}
