function scrollingObject(containerId, viewerId, tableId, loaderId) {
	if (!document.getElementById) return;
	var container = getElementSafe(containerId);
	this.id = containerId; 
	scrollingObject.collection[this.id] = this;
	this.animString = "scrollingObject.collection." + this.id;
	this.loaderId = loaderId;
	this.load(viewerId, tableId);
}

scrollingObject.collection = {};
scrollingObject.defaultSpeed = scrollingObject.prototype.speed = 100; // default for mouseover or mousedown scrolling

scrollingObject.prototype.load = function(viewerId, tableId) {
	var container, viewer;
	this.viewer = viewer = getElementSafe(viewerId);
	this.viewer.style.position = 'absolute';
	this.viewerId = viewerId;
	this.tableId = tableId;
	this.y = 0;
	this.x = 0;
	this.shiftTo(0,0);
	this.ready = false;
	this.galleryLoadTimerId = window.setInterval(this.animString + ".checkImgLoad()", 60);
}

scrollingObject.prototype.completeLoad = function() {
	if (this.galleryLoadTimerId) {
		window.clearInterval(this.galleryLoadTimerId); this.galleryLoadTimerId = 0;
	}
	var container = getElementSafe(this.id);
	var viewer = getElementSafe(this.viewerId);
	var table = getElementSafe(this.tableId);
	var loader = getElementSafe(this.loaderId);

	this.hght = table.offsetHeight;
	this.maxY = (this.hght - container.offsetHeight > 0)? this.hght - container.offsetHeight: 0;
	this.wdth = table.offsetWidth;
	this.maxX = (this.wdth - container.offsetWidth > 0)? this.wdth - container.offsetWidth: 0;

	viewer.style.visibility = "visible";
	loader.style.visibility = "hidden";
	this.ready = true;
}

scrollingObject.prototype.checkImgLoad = function() {
	var imgTable = getElementSafe(this.tableId);
	var docImages = imgTable.getElementsByTagName("img");
	var loadedImages = 0;
	for (i=0; i<docImages.length; i++) {
		var img = docImages[i];
		if (img.complete) {
			loadedImages = loadedImages+1;
		} 
	}
	if (loadedImages == docImages.length) {
		this.completeLoad();
	}
}

scrollingObject.prototype.shiftTo = function(x, y) {
	if (this.viewer) {
		this.viewer.style.left = (this.x = x) + "px"; 
		this.viewer.style.top = (this.y = y) + "px";
	}
}

scrollingObject.prototype.initScrollVals = function(deg, speed) {
	if (!this.ready) return; 
	if (this.timerId) {
		window.clearInterval(this.timerId); this.timerId = 0;
	}
	this.speed = speed || scrollingObject.defaultSpeed;
	this.fx = (deg == 0)? -1: (deg == 180)? 1: 0;
	this.fy = (deg == 90)? 1: (deg == 270)? -1: 0;
	this.endX = (deg == 90 || deg == 270)? this.x: (deg == 0)? -this.maxX: 0; 
	this.endY = (deg == 0 || deg == 180)? this.y: (deg == 90)? 0: -this.maxY;
	this.viewer = getElementSafe(this.viewerId);
	this.lastTime = new Date().getTime();
	this.timerId = window.setInterval(this.animString + ".scroll()", 10);    
}

scrollingObject.prototype.scroll = function() {
	var now = new Date().getTime();
	var d = (now - this.lastTime)/1000 * this.speed;
	var dir = this.fy == 1 ? 'up': 'down';
	if (d > 0) { 
		var x = this.x + Math.round(this.fx * d); var y = this.y + Math.round(this.fy * d);
		if ((this.fx == -1 && x > -this.maxX) || (this.fx == 1 && x < 0) || 
				(this.fy == -1 && y > -this.maxY) || (this.fy == 1 && y < 0)) {
			this.lastTime = now;
			this.shiftTo(x, y);
			this.changeScrollControlImage(dir, true, false);
		} else {
			window.clearInterval(this.timerId); this.timerId = 0;
			this.shiftTo(this.endX, this.endY);
			this.changeScrollControlImage(dir, false, true);
		}
	}
}

scrollingObject.prototype.ceaseScroll = function() {
	if (!this.ready) return;
	if (this.timerId) {
		window.clearInterval(this.timerId); this.timerId = 0; 
	}
}

/* scrolling controls */
scrollingObject.prototype.setUpScrollControls = function(scrollsId) {
	if ( !document.getElementById || !document.getElementsByTagName ) return;
	var containerId = this.id; 
	var el = getElementSafe(scrollsId);
	if (el) this.scrollsId = scrollsId;
	var container = getElementSafe(containerId);
	var hrefs = el.getElementsByTagName('a');
	var cls, new_cls, parts, eventType;
	
	for (var i=0; hrefs[i]; i++) {
		cls = hrefs[i].className;
		parts = cls.split('_'); 
		eventType = parts[0];
		new_cls = cls.replace(eventType, containerId);
		hrefs[i].className = hrefs[i].className.replace(cls, new_cls);
		scrollingEvent.add(hrefs[i], eventType, scrollingObject.initScrollMouse );
		scrollingEvent.add(hrefs[i], 'mouseout', scrollingObject.stopScrollMouse );
		scrollingEvent.add(hrefs[i], 'click', function(e) { if (e && e.preventDefault) e.preventDefault(); return false; } ); 
	}
}

scrollingObject.prototype.changeScrollControlImage = function(dir, activeFlag, disabled) {
	if (!this.scrollsId) return;
	var scrollsId = this.scrollsId;
	var el = getElementSafe(scrollsId);
	var imgs = el.getElementsByTagName('img');
	var src, path, imgSrc, parts, img;
	
	for (var i=0; imgs[i]; i++) {
		src = imgs[i].src;
		path = src.slice(0, src.lastIndexOf("/")+1);
		imgSrc = src.slice(src.lastIndexOf("/")+1);
		parts = imgSrc.split('-');
		img = imgs[i];
		//change the button currently triggering mouseover
		if (!disabled) {
			if (dir == parts[0] && activeFlag) {
				img.src = path + parts[0] + "-button-over.gif";
			} else if (dir == parts[0] && !activeFlag) {
				img.src = path + parts[0] + "-button-off.gif";
			} else if (dir != parts[0] && activeFlag) {
				parts[0] = dir == 'up' ? 'down' : 'up';
				img.src = path + parts[0] + "-button-off.gif";
			}
		} else {
			if (dir == parts[0]) {
				img.src = path + "x-button-disabled.gif";
			}		
		}
	}
}

scrollingObject.getParameterFromClass = function(e, containerId, dir, speed) {
	var tgt = scrollingObject.getTargetLink(e);
	var cls = tgt.className;
	var parts = cls.split('_');
	if (containerId) return parts[0];
	if (dir) return parts[1];  
	if (speed) return parts[2];
}

scrollingObject.initScrollMouse = function(e) {
	var containerId = scrollingObject.getParameterFromClass(e, true, false, false);
//	scrollingObject.collection[containerId].refreshDims();
	var dir = scrollingObject.getParameterFromClass(e, false, true, false);
	var speed = scrollingObject.getParameterFromClass(e, false, false, true); 
	var deg = dir == 'up'? 90: dir == 'down'? 270: dir == 'left'? 180: dir == 'right'? 0: null;
	if ( deg != null ) {
		scrollingObject.collection[containerId].initScrollVals(deg, speed);
	}
}

scrollingObject.stopScrollMouse = function(e) {
	var containerId = scrollingObject.getParameterFromClass(e, true, false, false);
	var dir = scrollingObject.getParameterFromClass(e, false, true, false);
	scrollingObject.collection[containerId].ceaseScroll();
	scrollingObject.collection[containerId].changeScrollControlImage(dir, false);
}

scrollingObject.getTargetLink = function(e) {
	scrollingEvent.DOMit(e);
	var tgt = e.target;
	do {
		if ( tgt.tagName == 'A' ) {
			return tgt;
		}
	} while ( tgt = tgt.parentNode)
	return '';
}

/* event listeners/handling */
var scrollingEvent = {
	add: function(obj, eventType, func) {
		if (obj.addEventListener) obj.addEventListener(eventType, func, false);
		else obj.attachEvent("on" + eventType, func);
	}, 

	DOMit: function(e) { 
		e = e? e: window.event;
		if (!e.target) e.target = e.srcElement;
		if (e.target.nodeType == 3) e.target = e.target.parentNode;
		if (!e.preventDefault) e.preventDefault = function () { e.returnValue = false; return false; }
		if (!e.stopPropagation) e.stopPropagation = function () { e.cancelBubble = true; }
		return e;
	}
}

// pop up chosen image
function popUpImage(showPopup, el)
{
	if (isPopupSliding()) return;
	var popupDiv = getElementSafe("popup");
	if (showPopup) {
		resetPopupDivs()
		var imgs = el.getElementsByTagName("img");
		var src = imgs[0].src;
		var path = src.slice(0, src.lastIndexOf("/")+1);
		var imgSrc = src.slice(src.lastIndexOf("/")+1);
		var parts = imgSrc.split('.');
		var newImgSrc = path + parts[0] + "-mid." + parts[1];
		getElementSafe("imagearea").innerHTML = "<img id='popupimg' src='" + newImgSrc +"' style='position: relative; top: 5px;'>";
		var img = getElementSafe("popupimg");
		setUpPopupDivs(img, imgSrc);
		slideOutPopup("imagepopupcontainer", 10, 20, "wide", "imagepopupcontent");
		if (pageTracker) {
			pageTracker._trackPageview('/galleries.shtml/'+imgSrc);
		}
	} else {
		if (!isPopupSliding()) {
			resetPopupDivs();
			var hashParts = window.location.hash.split("#");
			window.location.hash = hashParts[1];
		}
	}
}

function setUpPopupDivs(img, imgSrc) {
	var imgPadding = 10;
	var container = getElementSafe("imagepopupcontainer");
	var topbar = getElementSafe("topbar");
	var bottombar = getElementSafe("bottombar");	
	var imagearea = getElementSafe("imagearea");
	var imagepopupcontent = getElementSafe("imagepopupcontent");
	var imageloader = getElementSafe("imageloader");

	imgSrc = "bottom-bar-" + imgSrc;
	var bottomBarContent = getElementSafe(imgSrc);
	bottombar.innerHTML = bottomBarContent.innerHTML;

	var conH,conW,conL,conT,imgW,imgH,topH,bottomH;
	
	conH = getItemHeight(container);
	conW = getItemWidth(container);
	conT = getItemTop(container);
	conL = getItemLeft(container, true);
	topH = getItemHeight(topbar);
	bottomH = getItemHeight(bottombar);

	var onload = function() {
		imgW = img.width;
		imgH = img.height;
		
		imagearea.style.width = (imgW + imgPadding) + 'px';
		imagearea.style.height = (imgH + imgPadding) + 'px';
		imagepopupcontent.style.width = (imgW + imgPadding) + 'px';
		imagepopupcontent.style.height = (imgH + imgPadding + topH + bottomH) + 'px';
		imagepopupcontent.style.left = (((conW - (imgW + imgPadding))/2) + conL) + 'px';
		imagepopupcontent.style.top = (((conH - (imgH + imgPadding + topH + bottomH))/2) + conT) + 'px';
		if (ga_slideComplete) {
			imagepopupcontent.style.visibility = "visible";
			imageloader.style.visibility = "hidden";
		} else {
			ga_slideCompleteTimerId = window.setInterval("imgLoadShow();",1000);
		}
		
	}
	
	if (img.complete) {
		onload();
	} else {
		img.onload = onload;
	}
}

function imgLoadShow() {
	if (ga_slideComplete) {
		var imagepopupcontent = getElementSafe("imagepopupcontent");
		var imageloader = getElementSafe("imageloader");
		imagepopupcontent.style.visibility = "visible";
		imageloader.style.visibility = "hidden";
		window.clearInterval(ga_slideCompleteTimerId);
	}
}

function resetPopupDivs() {
	switchDivVisibility("imagepopupcontainer","hidden");
	switchDivVisibility("imagepopupcontent","hidden");
	switchDivVisibility("imageloader","hidden");
	getElementSafe("imagearea").innerHTML = "";
	endPopupSlide();
}

function replaceDetailImage(el, newLoad)
{
	var imgs = el.getElementsByTagName("img");
	var elHash = el.hash;
	var src = imgs[0].src;
	var path = src.slice(0, src.lastIndexOf("/")+1);
	var imgSrc = src.slice(src.lastIndexOf("/")+1);
	var parts = imgSrc.split('.');
	if (newLoad && window.location.hash) {
		wlHash = window.location.hash;
		parts = (wlHash.substring(1, wlHash.length)).split('.');
		imgSrc = wlHash.slice(wlHash.lastIndexOf("#")+1);
		elHash = wlHash;
	}
	var newImgSrc = path + parts[0] + "-mid." + parts[1];
	var imgArea = getElementSafe("image-detailarea");
	var imgLoader = getElementSafe("image-loader");
	var imgAreaH = getItemHeight(imgArea);
	imgLoader.style.visibility = "visible";
	imgArea.style.visibility = "hidden";
	imgArea.innerHTML = "<img id='popupimg' src='" + newImgSrc +"' style='position: relative;'>";
	var img = getElementSafe("popupimg");
	
	var optionPopupDiv = getElementSafe("option-popup");
	optionPopupDiv.style.visibility = "hidden";
	
	var infoBarDiv = getElementSafe("image-infobar");
	while (infoBarDiv.hasChildNodes()) {
		infoBarDiv.removeChild(infoBarDiv.lastChild);
	}
	var infoBarReplace = getElementSafe("image-infobar-" + imgSrc);
	for (i=0; i<infoBarReplace.childNodes.length; i++) {
		var tempNode = infoBarReplace.childNodes[i].cloneNode(true);
		infoBarDiv.appendChild(tempNode);
	}
	var optionBarDiv = getElementSafe("image-optionbar");
	while (optionBarDiv.hasChildNodes()) {
		optionBarDiv.removeChild(optionBarDiv.lastChild);
	}
	var optionBarReplace = getElementSafe("image-optionbar-" + imgSrc);
	if (optionBarReplace != null && optionBarReplace.hasChildNodes()) {
		for (i=0; i<optionBarReplace.childNodes.length; i++) {
			if (optionBarReplace.childNodes[i].nodeType != 3 && optionBarReplace.childNodes[i].id == "optionbar-detail") {
				var tempNode = optionBarReplace.childNodes[i].cloneNode(true);
				optionBarDiv.appendChild(tempNode);
			}
		}
	}		
	optionBarDiv.appendChild(optionPopupDiv);
	if (pageTracker) {
		pageTracker._trackPageview('/gallery.shtml/'+imgSrc);
	}
	
	var onload = function() {
		var imgH = img.height;
		var imgTop = (imgAreaH-imgH)/2;
		img.style.top = imgTop + "px";
		imgArea.style.visibility = "visible";
		imgLoader.style.visibility = "hidden";
		window.location.hash = elHash;
	}

	if (img.complete) {
		onload();
	} else {
		img.onload = onload;
	}
}

function showOptionPopup(el) {
	var imgs = el.getElementsByTagName("img");
	var imgParts = imgs[0].title.split(":");

	var optionPopupContentDiv = getElementSafe("option-popup-content");
	while (optionPopupContentDiv.hasChildNodes()) {
		optionPopupContentDiv.removeChild(optionPopupContentDiv.lastChild);
	}
	var optionPopupReplace = getElementSafe("image-optionbar-" + imgParts[0]);
	for (i=0; i<optionPopupReplace.childNodes.length; i++) {
		if (optionPopupReplace.childNodes[i].nodeType != 3) {
			if (optionPopupReplace.childNodes[i].id == "optionbar-"+ imgParts[1]) {
				var tempNode = optionPopupReplace.childNodes[i].cloneNode(true);
				optionPopupContentDiv.appendChild(tempNode);
			}
		}
	}
	
	var iconTop = (el.parentNode.offsetTop) - 2;
	var optionPopupDiv = getElementSafe("option-popup");
	var optionPopupIconDiv = getElementSafe("option-popup-icon");
	optionPopupIconDiv.style.top = iconTop + "px";
	optionPopupDiv.style.visibility = "visible";
}

function hideOptionPopup() {
	var optionPopupDiv = getElementSafe("option-popup");
	optionPopupDiv.style.visibility = "hidden";
}
