function maxImage(fileName) {
	prepareImageView();
	document.getElementById("currentPhoto").src = fileName;

	return false;
}

function showImage(albumId, imageFile, maxImages) {
	prepareImageView();

	//set image
	document.getElementById('nextimage').className = "active";
	document.getElementById('previmage').className = "active";

	document.getElementById("currentPhoto").src = 'gallery/'+albumId+'/normal/'+imageFile + '.jpg';

	if(imageFile > 1) {
		document.getElementById('previmage').onclick = function() {
			showImage(albumId, (parseInt(imageFile) - 1) ,maxImages);
		};
		document.getElementById('previmage').style.visibility = "visible";
	} else {
		document.getElementById('previmage').style.visibility = "hidden";
	}

	if(imageFile < maxImages) {
		document.getElementById('nextimage').onclick = function() {
			showImage(albumId, (parseInt(imageFile) + 1) ,maxImages);
		};
		document.getElementById('nextimage').style.visibility = "visible";
	} else {
		document.getElementById('nextimage').style.visibility = "hidden";
	}

	return false;
}

function prepareImageView() {

	document.getElementById('photoBackground').className = "active";
	fadeIn('photoBackground');
	document.getElementById('photoContainer').className = "active";
	document.getElementById('outterPhotoContainer').className = "active";
	document.getElementById('closeimage').className = "active";
	document.getElementById('currentPhoto').className = "active";

	var arrayPageSize = this.getPageSize();

	document.getElementById('photoBackground').style.width = arrayPageSize[0] + 'px';
	document.getElementById('photoBackground').style.height = arrayPageSize[1] + 'px';

	var scrollPos = getScrollXY();

	document.getElementById('outterPhotoContainer').style.top = scrollPos[1] + 'px';
}

function hideImage() {
	fadeOut('photoBackground');
	document.getElementById('photoContainer').className = "inactive";
	document.getElementById('outterPhotoContainer').className = "inactive";
	document.getElementById('nextimage').className = "inactive";
	document.getElementById('previmage').className = "inactive";
	document.getElementById('closeimage').className = "inactive";
	document.getElementById('currentPhoto').className = "inactive";
}

function getPageSize() {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) {
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

var TimeToFade = 200.0;

function fadeIn(eid) {
	var element = document.getElementById(eid);
	if(element == null)
		return;
	element.FadeState = -2;
	element.FadeState = element.FadeState == 2 ? -1 : 1;
	element.FadeTimeLeft = TimeToFade;
	setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 10);

}

function fadeOut(eid) {
	var element = document.getElementById(eid);
	if(element == null)
		return;
	element.FadeState = 2;

	element.FadeState = element.FadeState == 2 ? -1 : 1;
	element.FadeTimeLeft = TimeToFade;
	setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 10);

}

function animateFade(lastTick, eid) {
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var element = document.getElementById(eid);

	if(element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = '
		+ (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		if(element.FadeState == -2) {
			element.className = "inactive";
		}
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	if(element.FadeState == 1)
		newOpVal = 1 - newOpVal;

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 10);
}
