/*

	Config:

	arr_images     - array of slideshow images
	elm_container  - pointer to the parent container
	elm_label      - pointer to the Pause/Resume operator
	int_delay      - amount of delay between images in SECONDS
	int_trans_type - type of slideshow transition (0 = fade, 1 = wipe)
	str_name       - pointer to this object (eg, "obj_slideshow_14")

*/

function Slideshow(obj_config) {

	// Declarations
	this.arr_elms   = [];
	this.bit_active = true;

	this.int_delay_timeout  = 0;
	this.int_delay_interval = 10;

	this.int_img_curr = 0;
	this.int_img_next = 1;

	this.int_index_curr = 0;
	this.int_index_next = 1;

	this.int_trans_inc   = 0;
	this.int_trans_index = 0;
	this.int_trans_start = 0;
	this.int_trans_stop  = 0;

	this.int_height_container = 0;
	this.int_width_container  = 0;

	this.obj_interval = false;
	this.obj_timeout  = false;

	this.obj_config = obj_config;

	this.int_max_shrunk_height = 0;

	// Methods
	this.init = function() { /* {{{ */
		// CorpTicket:51029 - static summary of one item if asked for - changed by BIrons on Nov  3, 2009
		if (!this.obj_config.arr_images.length || (this.obj_config.arr_images.length < 1)) {

			if (this.obj_config.elm_container) {

				while (this.obj_config.elm_container.hasChildNodes()) {
					this.obj_config.elm_container.removeChild(this.obj_config.elm_container.firstChild);
				}

				this.obj_config.elm_container.appendChild(document.createTextNode('Sorry, there are no images to display.'));

			}

			if (this.obj_config.elm_label) {
				this.obj_config.elm_label.childNodes[0].nodeValue = 'X';
				this.obj_config.elm_label.parentNode.href         = 'javascript://';
			}

			return;

		}

		this.int_delay_timeout    = (this.obj_config.int_delay * 1000);
		this.int_width_container  = this.obj_config.elm_container.offsetWidth;
		this.int_height_container = this.int_width_container;

		// Forces the container into a square
		this.obj_config.elm_container.style.height   = this.int_width_container + 'px';
		this.obj_config.elm_container.style.position = 'relative';

		this.loadImages();
		this.makeImages();
		this.start(this.int_index_curr + 1);

		//this.obj_config.elm_container.style.height   = this.int_max_shrunk_height + 'px';
//alert(this.int_max_shrunk_height);

	} /* }}} */

	this.execTransInterval = function() { /* {{{ */

		this.int_trans_index += this.int_trans_inc;

		if (this.int_trans_index >= this.int_trans_stop) {

			if (this.obj_interval) {
				clearInterval(this.obj_interval);
			}

			this.obj_interval = false;

			this.finalizeTrans();

			if (this.bit_active) {
				this.start(this.int_index_next + 1);
			}

		} else {

			if (this.obj_config.int_trans_type == 1) {
				this.setTransFade();
			} else if (this.obj_config.int_trans_type == 2) {
				this.setTransWipe();
			}

		}

	} /* }}} */

	this.finalizeTrans = function() { /* {{{ */

		if (this.obj_config.int_trans_type == 1) {

			this.setOpacity(this.arr_elms[this.int_img_curr], 100);
			this.setOpacity(this.arr_elms[this.int_img_next], 100);

		} else if (this.obj_config.int_trans_type == 2) {

			this.setClip(this.arr_elms[this.int_img_curr], 0, this.int_width_container);
			this.setClip(this.arr_elms[this.int_img_next], 0, this.int_width_container);

		}

		this.int_index_curr = this.int_index_next;

		if (this.int_img_curr == 0) {
			this.int_img_curr = 1;
			this.int_img_next = 0;
		} else {
			this.int_img_curr = 0;
			this.int_img_next = 1;
		}

		this.arr_elms[this.int_img_next].style.visibility = 'hidden';
		this.arr_elms[this.int_img_curr].style.zIndex     = 1;
		this.arr_elms[this.int_img_next].style.zIndex     = 2;

	} /* }}} */

	this.getImageDims = function(elm_img) { /* {{{ */

		var flt_ratio  = 0,
			int_width  = elm_img.width,
			int_height = elm_img.height;

		if (int_width > int_height) {

			if (int_width > this.int_width_container) {
				flt_ratio = (this.int_width_container / int_width);
			}
			
		} else {

			if (int_height > this.int_height_container) {
				flt_ratio = (this.int_height_container / int_height);
			}

		}

		int_width  = parseInt(int_width  * flt_ratio);
		int_height = parseInt(int_height * flt_ratio);

		return [int_width, int_height];

	} /* }}} */

	this.loadImage = function(str_src) { /* {{{ */

		var elm_img = new Image();
		elm_img.src = str_src;

	} /* }}} */

	this.loadImages = function() { /* {{{ */

		var int_width, int_height, flt_ratio;

		for (var _i = 0; _i < this.obj_config.arr_images.length; _i++) {

			int_width  = this.obj_config.arr_images[_i][1],
			int_height = this.obj_config.arr_images[_i][2];

			if ((int_width > this.int_width_container) || (int_height > this.int_height_container)) {

				if (int_width > int_height) {
					flt_ratio = (this.int_width_container / int_width);
				} else {
					flt_ratio = (this.int_height_container / int_height);
				}

				this.obj_config.arr_images[_i][1] = parseInt(flt_ratio * int_width);
				this.obj_config.arr_images[_i][2] = parseInt(flt_ratio * int_height);
				if (this.obj_config.arr_images[_i][2] > this.int_max_shrunk_height) {
					this.int_max_shrunk_height = this.obj_config.arr_images[_i][2];
				}

			}/* else if ((int_width < this.int_width_container) && (int_height < this.int_height_container)) {

				if (int_width > int_height) {
					flt_ratio = (this.int_width_container / int_width);
				} else {
					flt_ratio = (this.int_height_container / int_height);
				}

				this.obj_config.arr_images[_i][1] = parseInt(flt_ratio * int_width);
				this.obj_config.arr_images[_i][2] = parseInt(flt_ratio * int_height);

			}*/

			setTimeout(this.obj_config.str_name + '.loadImage("' + this.obj_config.arr_images[_i][0] + '");', (_i * 100));

		}

	} /* }}} */

	this.makeImage = function() { /* {{{ */

		var elm_img = document.createElement('img');
		elm_img.alt = 'Slideshow';
		elm_img.src = RootPath + 'graphics/trans.gif';

//		elm_img.style.position = 'absolute';
		elm_img.style.left          = '0px';
		elm_img.style.top           = '0px';
		elm_img.style.paddingBottom = '20px';

		var elm_con = document.createElement('div');

		elm_con.appendChild(elm_img);
		elm_con.appendChild(document.createElement('div'));
		elm_con.appendChild(document.createElement('div'));

		elm_con.style.textAlign = 'center';
		elm_con.style.position  = 'absolute';

		return elm_con;

	} /* }}} */

	this.makeImages = function() { /* {{{ */

		this.arr_elms[0] = this.makeImage();
		this.arr_elms[1] = this.makeImage();
		
		this.setSource(this.arr_elms[0], this.int_index_curr);
		
		if (this.obj_config.arr_images.length > 1) {
			this.setSource(this.arr_elms[1], this.int_index_next);
		}

		this.arr_elms[this.int_img_curr].style.zIndex     = 1;
		this.arr_elms[this.int_img_next].style.zIndex     = 2;
		this.arr_elms[this.int_img_next].style.visibility = 'hidden';

		while (this.obj_config.elm_container.hasChildNodes()) {
			this.obj_config.elm_container.removeChild(this.obj_config.elm_container.firstChild);
		}

		this.obj_config.elm_container.appendChild(this.arr_elms[0]);
		this.obj_config.elm_container.appendChild(this.arr_elms[1]);

	} /* }}} */

	this.next = function() { /* {{{ */

		if (!this.obj_interval) {
			this.start(this.int_index_curr + 1, true);
		}

	} /* }}} */

	this.pause = function() { /* {{{ */

		var str_label = 'Pause';

		if (this.obj_timeout) {

			clearTimeout(this.obj_timeout);

			str_label        = 'Resume';
			this.bit_active  = false;
			this.obj_timeout = false;

		} else {

			this.bit_active = true;

			this.start(this.int_index_curr + 1);

		}

		if (this.obj_config.elm_label) {
			this.obj_config.elm_label.childNodes[0].nodeValue = str_label;
		}

	} /* }}} */

	this.prev = function() { /* {{{ */

		if (!this.obj_interval) {
			this.start(this.int_index_curr - 1, true);
		}

	} /* }}} */

	this.setClip = function(elm_target, int_clip_left, int_clip_right) { /* {{{ */

		elm_target.style.clip = 'rect(0px, ' + int_clip_right + 'px, ' + this.int_width_container + 'px, ' + int_clip_left + 'px)';

	} /* }}} */

	this.setOpacity = function(elm_target, int_opacity) { /* {{{ */

		elm_target.style.filter  = 'alpha(opacity=' + int_opacity + ')';
		elm_target.style.opacity = (int_opacity / 100);

	} /* }}} */

	this.setSource = function(elm_target, int_index) { /* {{{ */

		elm_target.childNodes[0].style.width  = this.obj_config.arr_images[int_index][1] + 'px';
		elm_target.childNodes[0].style.height = this.obj_config.arr_images[int_index][2] + 'px';
		elm_target.childNodes[0].src          = this.obj_config.arr_images[int_index][0];

		elm_target.childNodes[1].innerHTML    = this.obj_config.arr_images[int_index][3];
		elm_target.childNodes[2].innerHTML    = this.obj_config.arr_images[int_index][4];

	} /* }}} */

	this.setTransFade = function() { /* {{{ */

		var int_opacity_curr = (100 - this.int_trans_index);
		var int_opacity_next = this.int_trans_index;
		
		this.setOpacity(this.arr_elms[this.int_img_curr], int_opacity_curr);
		this.setOpacity(this.arr_elms[this.int_img_next], int_opacity_next);

	} /* }}} */

	this.setTransInterval = function() { /* {{{ */

		this.setTransVars();

		if (this.obj_config.int_trans_type == 1) {
			this.setTransFade();
		} else if (this.obj_config.int_trans_type == 2) {
			this.setTransWipe();
		}

		this.arr_elms[this.int_img_next].style.visibility = 'visible';

		if (this.obj_interval) {

			clearInterval(this.obj_interval);

			this.obj_interval = false;

		}

		this.obj_interval = setInterval(this.obj_config.str_name + '.execTransInterval();', this.int_delay_interval);

	} /* }}} */

	this.setTransTimeout = function(bit_set, bit_set_now) { /* {{{ */

		if (this.obj_timeout) {

			clearTimeout(this.obj_timeout);

			this.obj_timeout = false;

		}

		if (bit_set) {

			if (bit_set_now) {
				this.setTransInterval();
			} else {
				this.obj_timeout = setInterval(this.obj_config.str_name + '.setTransInterval();', this.int_delay_timeout);
			}

		}

	} /* }}} */

	this.setTransVars = function() { /* {{{ */

		if (this.obj_config.int_trans_type == 1) {

			// Fade
			this.int_trans_inc   = 10;
			this.int_trans_start = 0;
			this.int_trans_stop  = 100;

		} else if (this.obj_config.int_trans_type == 2) {

			// Wipe
			this.int_trans_inc   = parseInt(this.int_width_container / 10);
			this.int_trans_start = 0;
			this.int_trans_stop  = this.int_width_container;

		}

		this.int_trans_index = this.int_trans_start;

	} /* }}} */

	this.setTransWipe = function() { /* {{{ */

		var int_clip_right = (this.int_width_container - this.int_trans_index);

		this.setClip(this.arr_elms[this.int_img_curr], 0, int_clip_right);
		this.setClip(this.arr_elms[this.int_img_next], int_clip_right, this.int_width_container);

	} /* }}} */

	this.start = function(int_next, bit_start_now) { /* {{{ */

		if (int_next >= this.obj_config.arr_images.length) {
			int_next = 0;
		} else if (int_next < 0) {
			int_next = (this.obj_config.arr_images.length - 1);
		}

		//this.bit_active     = true;
		this.int_index_next = int_next;

		this.setSource(this.arr_elms[this.int_img_next], this.int_index_next);
		if (this.obj_config.arr_images.length > 1) {
			this.setTransTimeout(true, bit_start_now);
		}

	} /* }}} */

}
