function ge(n){
	return document.getElementById(n);
}

function Gallery(image, navList, imgPath, srcs){
	this.current = 0;
	this.img = ge(image);
	this.list = ge(navList);
	this.textBoxes = null;
	this.lastDisplayBox = null;
	
	
	this.initialize = function(){
		_this = this;
		this.img.onclick = function(){
				_this.next();
		}
		for(var i = 0, len = srcs.length; i < len; i++ )
			this.createLi(i);
	}
	this.setTextBoxes = function(tb){
		this.textBoxes = tb;
	}
	this.createLi =  function(i){
		var li = this.list.appendChild(document.createElement("li"));
		if(this.textBoxes)
			this.textBoxes[i].style.display = "none";
		if(i == 0){
			li.className = "current";
			if(this.textBoxes){
				this.textBoxes[i].style.display = "";
				this.lastDisplayBox = this.textBoxes[i];
			}
		}
	
		li.innerHTML = '<img src="' + imgPath + 'tb_' + srcs[i] + '"/>';
		var _setClick = this.setClick;
		var o = this;
		li.onclick = function(){_setClick(o, i)};
	}
	this.setClick = function(o, i) {
		o.current = i, o.img.src = imgPath + srcs[i];
		if(o.textBoxes){
			var currentBox = o.textBoxes[i];
			currentBox.style.display = "";
			o.lastDisplayBox.style.display = "none";
			o.lastDisplayBox = currentBox;
		}
		var lis =  o.list.getElementsByTagName("li"), j =  lis.length;
		while(j--){
			var lic = lis[j].className;
			if(lic != 'next' && lic != 'previous') {
				lis[j].className = "";
			}
		}
		lis[i].className = "current";
	}
	this.next = function(){
		if(this.current < srcs.length - 1){
			this.current++;
			this.setClick(this, this.current);
		}
		else{
			this.setClick(this, 0);
		}
	}
	this.previous = function(){
		if(this.current > 0 )
			this.current--;
			this.setClick(this.current);
	}
}
var getMouse = function(e){
	var w = window, b = document.body;
	return {
	x: e.clientX + (w.scrollX || b.scrollLeft || b.parentNode.scrollLeft || 0),
	y: e.clientY + (w.scrollY || b.scrollTop || b.parentNode.scrollTop || 0)
	};
};
function scrollerY(obj){
	var s = ge(obj);
	
	for(var x = s.offsetLeft, y = s.offsetTop, v = s; v = v.offsetParent; x += v.offsetLeft, y += v.offsetTop);
	var w = s.offsetHeight, sw = (s.scrollHeight - w), it, m = 0;

	var scroll = function(){
		s.scrollTop += (((m - y) << 1) - w) / w * 10;
		if(s.scrollTop >= sw){
			clearInterval(it), it = 0;
		}
	}
	addEvent(s, "mousemove", function(e){
			m = getMouse(e).y;
			!it && (it = setInterval(scroll, 10));
		}
	);
	addEvent(s, "mouseout", function(e){
			clearInterval(it), it = 0;
		}
	);
}
function scrollerX(obj){
	var s = ge(obj);
	
	for(var x = s.offsetLeft, y = s.offsetTop, v = s; v = v.offsetParent; x += v.offsetLeft, y += v.offsetTop);
	var w = s.offsetWidth, sw = (s.offsetWidth - w), it, m = 0;

	var scroll = function(){
		s.scrollLeft += (((m - x) << 1) - w) / w * 10;
		if(s.scrollLeft >= sw){
			clearInterval(it), it = 0;
		}
	}
	addEvent(s, "mousemove", function(e){
			m = getMouse(e).x;
			!it && (it = setInterval(scroll, 10));
		}
	);
	addEvent(s, "mouseout", function(e){
			clearInterval(it), it = 0;
		}
	);
}

