/**
 * Home Random Controlable Images
 *
 * Copyright (c) 2010. by MassVision
 * Author: Mladen Mijatov
 */
var home_images = {
	slideshow_timer: null,
	slideshow_speed: 5 * 1000,
	animation_speed: 20,
	current_item: 0,
	items: [],

	/**
	 * Initialise random controlable images
	 * @param string list_id
	 */
	init: function(list_id) {
		var container = document.getElementById(list_id);
		var list = container.getElementsByTagName('ul')[0];

		if (list == undefined) return;

		this.prepareList(list);

		this.items = list.getElementsByTagName('li');
		for (var i=0; i<this.items.length; i++)
			this.prepareItem(this.items[i]);

		this.showItem(this.current_item);
		this.startSlideShow();
	},

	/**
	 * Prepare list styles
	 * @param resource list
	 */
	prepareList: function(list) {
		with (list.style) {
			position = 'relative';
		}
	},

	/**
	 * Show specified item and hide others
	 * @param integer index
	 */
	showItem: function(index) {
		var result = null;

		// ensure we hide item text
		this.items[this.current_item].animation_state = 2;

		for (var i=0; i<this.items.length; i++) {
			var item = this.items[i];
			item.style.display = (index == i) ? 'block' : 'none';

			if (index == i)
				result = item;
		}
		this.current_item = index;

		return result;
	},

	/**
	 * Start image slideshow
	 */
	startSlideShow: function() {
		if (this.slideshow_timer != null) return;
		var event_handler = function() { home_images.intervalSlideshow(); }
		this.slideshow_timer = setInterval(event_handler, this.slideshow_speed);
	},

	/**
	 * Stop image slideshow
	 */
	stopSlideShow: function() {
		if (this.slideshow_timer != null) {
			clearInterval(this.slideshow_timer);
			this.slideshow_timer = null;
		}
	},

	/**
	 * Stop image slideshow and force focus on certain item
	 * @param integer
	 */
	forceShowItem: function(index) {
		this.stopSlideShow();
		this.showItem(index);
	},

	/**
	 * Resume slideshow on certain event
	 */
	resumeSlideAnimation: function() {
		this.startSlideShow();
	},

	/**
	 * Prepare specified item
	 * @param resource item
	 */
	prepareItem: function(item) {
		var image = item.getElementsByTagName('img')[0];
		var text = item.getElementsByTagName('div')[0];
		var text_height = this.getDim(text)[1];

		// prepare item
		with (item.style) {
			position = 'absolute';
			top = '0px';
			left = '0px';
		}

		item.onmouseover = this.showText;
		item.onmouseout = this.hideText;

		// prepare text
		with (text.style) {
			position = 'absolute';
			bottom = '-'+text_height.toString()+'px';
			left = '0px';
		}
		text.old_bottom = text.style.bottom;
		item.text_height = text_height;
	},

	/**
	 * Return object dimensions
	 * @param resource obj
	 * @return array
	 */
	getDim: function(obj) {
		if (obj.clientWidth)
			return [obj.clientWidth, obj.clientHeight]; else
			return [obj.width, obj.height];
	},

	/**
	 * Display text on mouse over
	 * Note: 'this' is LI element since this function
	 *       is called as event handler
	 */
	showText: function() {
		var text = this.getElementsByTagName('div')[0];
		var item = this;
		var event_handler = function() { home_images.intervalText(item); }

		this.animation_state = 1;
		if (this.animation_timer == undefined || this.animation_timer == null)
			this.animation_timer = setInterval(event_handler, home_images.animation_speed);

		text.old_status = window.status;
		window.status = text.innerHTML;
		home_images.stopSlideShow();
	},

	/**
	 * Hide text on mouse out
	 * Note: 'this' is LI element since this function
	 *       is called as event handler
	 */
	hideText: function() {
		var text = this.getElementsByTagName('div')[0];
		var item = this;
		var event_handler = function() { home_images.intervalText(item); }

		this.animation_state = 2;
		if (this.animation_timer == undefined || this.animation_timer == null)
			this.animation_timer = setInterval(event_handler, home_images.animation_speed);

		window.status = text.old_status;
		home_images.startSlideShow();
	},

	/**
	 * Event triggered on slideshow interval
	 */
	intervalSlideshow: function() {
		var next_item = Math.floor( Math.random() * this.items.length );
		this.showItem(next_item);
	},

	/**
	 * Event triggered on text animation interval
	 */
	intervalText: function(item) {
		var text = item.getElementsByTagName('div')[0];
		var current_position = parseInt(text.style.bottom.substring(0, text.style.bottom.length-2));

		switch (item.animation_state) {
			case 1: // show animation
				current_position += 2;
				if (current_position >= 0) {
					current_position = 0;
					clearInterval(item.animation_timer);
					item.animation_timer = null;
				}
				break;

			case 2: // hide animation
				current_position -= 2;
				if (current_position <= -item.text_height) {
					current_position = -item.text_height;
					clearInterval(item.animation_timer);
					item.animation_timer = null;
				}
				break;
		}
		text.style.bottom = current_position.toString()+'px';
	}
}

// Print Order

function printOrder(_url)
{/*   version 1.4.0.00 */
   printWindow = window.open(_url,
                       "printWindow","toolbar=0, location=0, status=1, resizable=1, menubar=1, "+
                       "scrollbars=1, width=750, height=540");
   printWindow.focus();
}

