/**
 * function open_lightbox()
 *
 * open lightbox by simulating "click" event on first image in gallery
 *
 * no params needed ;-)
 */
function open_lightbox() {
	if (document.getElementById("eg_image1")) {
		if (document.createEvent) {
			var evt = document.createEvent("MouseEvents");
			evt.initEvent("click", true, false);
			document.getElementById("eg_image1").dispatchEvent(evt);
		} else if (document.createEventObject) {
			document.getElementById("eg_image1").fireEvent("onclick");
		}
	}
}


/**
 * function eg()
 *
 * initialize object, set following properties (only customizable ones are listed):
 *  .step_visible - ammount of visible images
 *  .step_length - how many total pixels to scrol within one step
 *  .move_length - how many pixels to move in one cycle
 */
function eg() {

	this.left = 0;
	this.next_left = 0;
	this.direction = null;
	this.moving = false;

	this.step_visible = 3;
	this.step_length = 149;
	this.move_length = 20;
	this.next_move = 0;

	if (document.getElementById("egc")) {
		this.egc = document.getElementById("egc");
		this.step_max = this.egc.childNodes.length;
		this.egc.style.width = this.step_max * this.step_length + "px";
	}

	/**
	 * function eg_scroll()
	 *
	 * check if scroll can be done. if true, call move function
	 *
	 * @param string direction "scrolling direction (left|right)"
	 */
	this.eg_scroll = function(direction) {

		/**
		 * continue only if object is not moving right now
		 */
		if (this.moving == false) {
			this.direction = direction;

			/**
			 * get next target left position and move length
			 */
			this.next_left = this.direction == "left" ? this.left + this.step_length : this.left - this.step_length;
			this.next_move = this.direction == "left" ? -1 * this.move_length : this.move_length;

			/**
			 * check if we really can scroll
			 */
			if (this.next_left >= (this.step_max - this.step_visible) * -1 * this.step_length && this.next_left <= 0) {
				this.moving = true;
				this.eg_move();
			}
		}
	}

	/**
	 * function eg_move()
	 *
	 * move element until this.next_left limit reached
	 *
	 * no params needed ;-)
	 */
	this.eg_move = function() {

		/**
		 * set new left position
		 */
		this.left -= this.next_move;
		this.egc.style.left = this.left + "px";

		/**
		 * if we've reached next limit, stop scrolling and adjust position by that limit.
		 * else repeat itself within 10 milliseconds
		 */
		if ((this.direction == "right" && this.left > this.next_left) || (this.direction == "left" && this.left < this.next_left)) {
			setTimeout("this.eg_move();", 10);
		} else {
			this.left = this.next_left;
			this.egc.style.left = this.left + "px";
			this.moving = false;
		}
	}
}



/**
 * init exposer's gallery object on window load
 */
window.onload = eg;

