// imageArray.js
// Variables to control the Image Gallery

//==========images gallery=============================
function ImageGallery(formVar, imgFolder, glryName){
	this.gallery = new Array();
	this.formVar = formVar;
	this.imgFolder = imgFolder;
	this.glryName = glryName;
	this.curr_index = 0;
}

ImageGallery.prototype = {
	addImage: function(_aImg){
		this.gallery = document.getElementById(this.formVar).value.split(",");
		var removed = true;
		while (removed)
		{
			removed = false;
			for (var i = 0; i < this.gallery.length; i++)
			{
				if (this.gallery[i].length == 0 || this.gallery[i].toLowerCase().indexOf('add image') >= 0)
				{
					this.gallery.splice(i, 1);
					removed = true;
					break;
				}
			}
		}
		document.getElementById(this.formVar).value = this.gallery.toString();
		if (_aImg != "" && _aImg != "0")
		{
			this.gallery.push(_aImg);
			this.advanceGallery();
			document.getElementById(this.formVar).value = this.gallery.toString();
		}
	},

	showGallery: function()
	{
		if (this.imgFolder == "")
			this.imgFolder = "../assets/" + document.getElementById('token').value + "/images/";
		var stIndx = this.curr_index;
		for (var i=0; i < 8; i++)
		{
			var elName = "" + i + this.glryName + "_glryimage";
			var oEl = document.getElementById(elName);
			if ((stIndx + i) < this.gallery.length && this.gallery[stIndx + i] != "")
			{
				oEl.src = this.imgFolder + this.gallery[stIndx + i];
			}
			else
				oEl.src = "images/blank.png";
		}
	},

	moveGalleryIndex: function(addVal)
	{
		this.curr_index += addVal;
		while (this.curr_index >= 0 && this.curr_index + 7 >= this.gallery.length)
			this.curr_index--;
		if (this.curr_index < 0)
			this.curr_index = 0;
	},

	init: function ()
	{
		this.addImage('');
		this.moveGalleryIndex(0);
		this.showGallery();
	},

	rewindGallery: function ()
	{
		this.moveGalleryIndex(-1);
		this.showGallery();
	},

	advanceGallery: function ()
	{
		this.moveGalleryIndex(1);
		this.showGallery();
	},

	removeImage: function(pos){
		pos += this.curr_index;
		if (pos < this.gallery.length) {
			this.gallery.splice(pos, 1);
			document.getElementById(this.formVar).value = this.gallery.toString();
			this.moveGalleryIndex(0);
			this.showGallery();
		}
	}
}


//==========image viewer for front page and movie pages=============================
function ImageViewer()
{
	this.imageList = new Array();
	this.idList = new Array();
	this.elList = new Array();
	this.idTypeList = new Array();
	this.titleList = new Array();
	this.textList = new Array();
	this.currPos = 0;
	this.imageOnly = false;
	this.timer_id = 0;
}

ImageViewer.prototype = {
	addImage: function(_aImg){
		this.imageList.push(_aImg);
	},
	addId: function(_aId){
		this.idList.push(_aId);
	},
	addEl: function(_aEl){
		this.elList.push(_aEl);
	},
	addIDType: function(_aType){
		this.idTypeList.push(_aType);
	},
	addTitle: function(_aTitle){
		this.titleList.push(_aTitle);
	},
	addText: function(_aText){
		this.textList.push(_aText);
	},
	setImageOnly: function (_aflag) {
		this.imageOnly = _aflag;
	},
	show: function(){
		this.setImages();
	},
	setNextPos: function() {
		this.currPos++;
		if (this.currPos == this.imageList.length)
			this.currPos = 0;
	},
	setPrevPos: function() {
		this.currPos--;
		if (this.currPos == -1)
			this.currPos = this.imageList.length - 1;
	},
	setImages: function() {
		if (this.imageList.length == 0)
			return;

		var saveCurrPos = this.currPos;		
		for (var i = 0; i < this.elList.length; i++)
		{
			$j("#" + this.elList[i]).attr('src', this.imageList[this.currPos]);
			if (!this.imageOnly) {
				$j("#" + this.elList[i]).attr('linktype', this.idTypeList[this.currPos]);
				$j("#" + this.elList[i]).attr('linkid', this.idList[this.currPos]);
				$j("#" + this.elList[i].replace('Img', 'TxtT')).html(this.titleList[this.currPos]);
				$j("#" + this.elList[i].replace('Img', 'TxtB')).html(this.textList[this.currPos]);
			}
			this.setNextPos();			
		}
		this.currPos = saveCurrPos;
	}
};

