var offX =0, offY =0, factor=8, factorBig = 32;
var frameWidth = 176; mouseTimers=[];
var currentPhotoIndex = 0, maxPhotoIndex = 0;
var ShowPhotoAppendix = "";

function writeDebugLog(str)
{
	if (window.console == undefined) {
		var e = document.getElementById("DebugLog");
		if (!e) return;
		e.innerHTML += ("<pre>" + str + "</pre>");
		if (e.log_length == undefined) e.log_length = 0;
		e.log_length += 1;
		if (e.log_length > 15) {
			e.removeChild(e.firstChild);
			e.removeChild(e.firstChild);
		}
	} else {
		str = str.replace(/<br*\s?.>/gi, "\n");
		console.log(str);
	}
}

function scrollSubgalleryView(e) //ctl, delta)
{
	delta = translateMouseWheelDelta(event);
	if (delta) {
		// need to keep code consistent between outer and inner scroll hamdlers
		subgallery = window;
		if (delta >0)
			frameLeft(frameWidth, window);
		else
			frameRight(frameWidth, window);

		if (event.preventDefault)
				event.preventDefault();
		event.returnValue = false;
	}

	return true;
}

function scrollPhotoView(e) //ctl, delta)
{
	delta = translateMouseWheelDelta(event);
	if (delta) {
		// need to keep code consistent between outer and inner scroll hamdlers
		photo_view = window;
		currentPhotoIndex -= (delta / Math.abs(delta));
		if (currentPhotoIndex  < 0) currentPhotoIndex = 0;
		if (currentPhotoIndex  > maxPhotoIndex) currentPhotoIndex = maxPhotoIndex;
		// if browser does not supply scroll info
		if (isNaN(window.scrollX)) {
			var r = document.getElementById("photo_scroll");
			var off = r.getElementsByTagName("td")[currentPhotoIndex].offsetLeft -5;
			window.scrollTo(off, 0);
		} else {
			if (isNaN(photo_view.scroll_timer))
				photo_view.scroll_timer = setInterval("adjustPhotoPos(photo_view)", 10);
		}
		if (event.preventDefault)
				event.preventDefault();
		event.returnValue = false;
	}

	return true;
}

function adjustPhotoPos(frame)
{
	var r = document.getElementById("photo_scroll").getElementsByTagName("td");
	var off = r[currentPhotoIndex].offsetLeft;
	var sign = (off - window.scrollX);
	if (Math.abs(off - window.scrollX) < factorBig) {
		window.scrollBy(off - window.scrollX, 0);
		clearInterval(photo_view.scroll_timer);
		photo_view.scroll_timer = "$";
	} else {
		if (sign < 0)
			window.scrollBy(-factorBig, 0);
		else {
			var lim = window.scrollX + factorBig;
			window.scrollBy(factorBig, 0);
			//console.log("index " + currentPhotoIndex + "\nlim: "+ lim + "\nscrollX: "+ window.scrollX );
			if (window.scrollX < lim) {
		//		window.scrollTo(window.scrollMaxX, 0);
				clearInterval(photo_view.scroll_timer);
				photo_view.scroll_timer = "$";
				//while(currentPhotoIndex > 0 && r[currentPhotoIndex].offsetLeft > window.scrollX)
				//	currentPhotoIndex--;
				return;
			}
		}
	}
}

function scrollStep(frame)
{
	if (offX > 0) 	{
		frame.scrollBy(factor, 0);
		offX -= 1;
	} else if (offX < 0) {
		frame.scrollBy(-factor, 0);
		offX += 1;
	}
	if (offY > 0) 	{
		frame.scrollBy(0, factor);
		offY -= 1;
	} else if (offY < 0) {
		frame.scrollBy(0, -factor);
		offY += 1;
	}
	if (offX ==0 && offY == 0) {
		clearInterval(mouseTimers.pop());
	}
}

function frameLeft(step, frame)
{
	offX -= step/factor;
	mouseTimers.push(setInterval("scrollStep("+frame.name+")", 5));
}

function frameRight(step, frame)
{
	offX += step/factor;
	mouseTimers.push(setInterval("scrollStep("+frame.name+")", 5));
}

function selectActiveView(itm, galleryId)
{
	if (itm.parentNode.lastSelected) itm.parentNode.lastSelected.className = "";

	itm.className = "sel";
	itm.parentNode.lastSelected = itm;
//	subgallery.location.href='subgallery.php?gid=' + galleryId;
}

function selectActiveGallery(itm, galleryId)
{
	if (itm.parentNode.lastSelected) itm.parentNode.lastSelected.className = "gallery_item";

	itm.className = "gallery_item selected";
	itm.parentNode.lastSelected = itm;
	subgallery.location.href='subgallery.php?id=' + galleryId + ShowPhotoAppendix;
}

function selectActiveSubgallery(itm, subgalleryId)
{
	if (itm.parentNode.parentNode.lastSelected) itm.parentNode.parentNode.lastSelected.className = "subgallery_item";
	itm.parentNode.parentNode.lastSelected = itm;
	itm.className = "subgallery_item selected";
	// in case we simulate anchors
	if (itm.nodeName != "A") subgallery.location.href='subgallery.php?gid=' + galleryId;
}

function contentHide()
{
	//document.body.style.opacity = 0;
	//document.body.tmrOpacity = setInterval(iterateOpacity(1), 5);
	//alert("hide");
	//return;
	//parent.document.getElementById('content_scroll').style.display = 'none';
}

function contentShow()
{
	var elm = document.body.getElementsByTagName("table")[0];
	document.body.className = "";
	if (elm.tmrOpacity) { // we're called by timer!
		if (elm.opacityIndex < 10) {
			var o = (elm.opacityIndex += 1) / 10;
			writeDebugLog("New opacity: " + o);
			elm.style.opacity = o;
			elm.style.filter = "alpha(opacity = "+ parseInt(elm.opacityIndex * 10) +")";
		} else {
			//elm.style.opacity = 1;
			//elm.setAttribute("style", "");
			elm.className = "";//image_visible";
			clearInterval(elm.tmrOpacity);
			elm.tmrOpacity = undefined;
		}

	} else {
		elm.tmrOpacity = setInterval("contentShow()", 20);
		elm.opacityIndex = 0;
	}
}

function centerMainWindow(containerID)
{
	return;
	var cell = document.getElementById(containerID);
	var w;
	if (window.innerHeight) {
		w = (window.innerHeight - 10) + "px";
		cell.style.height = w;
	} else {
		//alert(document.body.scrollHeight);
	}
}

function imageError(img)
{
	var delay = Math.ceil(Math.random() * 500);
	writeDebugLog("Failed to load image: " + img.src + "\nReload in "+ delay +"mSec");
	setTimeout(function() {	img.src = img.src;}, delay);
}