﻿//
// Javascript for change content based on timer
// JCL, CHCHSWS, Gen-i 2009
//

var __qlc = null;

// get the singlton of the query list controller.

function GeniGetQLC()
{
    if(__qlc == null)
    {
        __qlc = new GeniQueryListController();
    }
    return __qlc;
}

//
// Steps to work on this controller:
//		Add a container insert to the controller"
//		GeniGetQLC().AddInsert(new GeniQueryListInsert('Container1', numberOfItemsToDisplay, isTimerEnabled));
//
// Container can be found by GetInsert function:
//		GeniGetQLC().GetInsert(ID) where ID is the id attribute of the container (div)
//		Functions for inserts:
//			insert.SetTimerEnabled
//			insert.SetRandomNextItem
//
// Items to show would need to be created and added into each insert
//		GeniQueryListItem(id, content)		 (id is not used at the moment, but can be used for the function GetItem(id))
//		insert.AddItem(new GeniQueryListItem('1', "Hello1"));

//

//
// To update the content once call the function:
//		GeniGetQLC().UpdateInserts();
// To start the timer update:
//		GeniGetQLC().StartUpTimerUpdate(timerInverval), and
//		GeniGetQLC().StopTimerUpdate();
//

GeniQueryListController = function() {
	this._Inserts = new Array();
	this._TimerInterval = 5000;
	this._TimerEnabled = false;
}

GeniQueryListController.prototype = {
	AddInsert: function(pInsert) {
		this._Inserts.push(pInsert);
	},
	GetInsert: function(insertId) {
		for (k1 in this._Inserts) {
			var insert = this._Inserts[k1];
			if (insert._id === insertId) {
				return insert;
			}
		}
	},
	UpdateInserts: function() {
		for (k1 in this._Inserts) {
			var insert = this._Inserts[k1];
			insert.UpdateNext();
		}
	},
	TimerUpdateInserts: function() {
		//GeniGetQLC().UpdateInserts();
		for (k1 in this._Inserts) {
			var insert = this._Inserts[k1];
			if (insert)
			{
				if (insert._TimerUpdateEnabled)
				{
					insert.UpdateNext();
				}
			}
		}
		if (this._TimerEnabled) {
			var t = setTimeout('GeniGetQLC().TimerUpdateInserts()', this._TimerInterval);
		}
	},
	StartUpTimerUpdate: function(pIntervalMS) {
		if (pIntervalMS)
		{
			this._TimerInterval = pIntervalMS;
		}
		
		this._TimerEnabled = true;
		var t = setTimeout('GeniGetQLC().TimerUpdateInserts()', this._TimerInterval);
		//this.TimerUpdateInserts();
	},
	StopTimerUpdate : function()
	{
		this._TimerEnabled = false;
	},
	ConsolidateInsertItems : function()
	{
		for (k1 in this._Inserts) {
			var insert = this._Inserts[k1];
			insert.ConsolidateItems();
		}
	}
}

GeniQueryListInsert = function(pInsertId, pNumItemsToShow, pTimerEnabled) {
	this._Container = document.getElementById(pInsertId);
	this._id = pInsertId;
	this._Items = Array();
	this._CurrentItemIndex = 0;
	this._TimerUpdateEnabled = false;
	this._IsRandomNextItem = false;
	
	if (pNumItemsToShow) {
		this._NumberOfItemsToShow = pNumItemsToShow;
	} else {
		this._NumberOfItemsToShow = 1;
	}
	
	if (pTimerEnabled)
	{
		this._TimerUpdateEnabled = pTimerEnabled;	
	}
	
}

GeniQueryListInsert.prototype = {

	AddItem: function(pItem) {
		this._Items.push(pItem);
	},
	GetItem: function(itemId) {
		if (this._Items.length > 0)
		{
    		for (k1 in this._Items) {
    			var item = this._Items[k1];
    			if (item._id === itemId) {
    				return item;
    			}
    		}
		}
	},
	GetNextItem: function() {
	
		var item = null;
		var index = 0;
		
    	if (this._IsRandomNextItem)
    	{
	    	if (this._Items.length > 1)
	    	{
	    		index = Math.floor(Math.random() * this._Items.length);
	    		while( index == this._CurrentItemIndex)
	    		{
	    			index = Math.floor(Math.random() * this._Items.length);
	    		}
	    		this._CurrentItemIndex = index;
	    	} else
	    	{
		    	this._CurrentItemIndex = 0;
		    }
		} else
		{
			index = this._CurrentItemIndex + 1;
			
			if (this._Items.length <= index) {
				index = 0;
			}
       		this._CurrentItemIndex = index;
		}

   		item = this._Items[this._CurrentItemIndex];
  		return item;
	},
	//	UpdateNext: function() {
	//		if (this._Container) {
	//			var item = this.GetNextItem();
	//			if (item) {
	//				this._Container.innerHTML = item._Content;
	//			}
	//		}
	//	},
	UpdateNext: function() {
		if (this._Container) {
			var content = "";
			
			for (i = 0; i < this._NumberOfItemsToShow; i++) {
				var item = this.GetNextItem();
				if (item) {
					content = content + item._Content;
				}
			}
			this._Container.innerHTML = content;
			/*
			//this._Container.innerHTML = content;
			for (k =0; k< this._Container.childNodes.length; k++)
			{
				n = this._Container.childNodes[k];
				n.style.display = "none";
				this._Container.removeNode(n);
			}
			
			for (i = 0; i < this._NumberOfItemsToShow; i++) {
				var item = this.GetNextItem();
				if (item) {
					item._ItemObj.style.display = 'block';
					this._Container.appendChild(item._ItemObj);
				}

			}
			*/
			/*
			this._Container..childNodes().;
			
			for (i = 0; i < this._NumberOfItemsToShow; i++) {
				var item = this.GetNextItem();
				if (item) {
					this._Container.;
				}
			}
			*/

		}
	},
	ConsolidateItems : function()
	{
		// find all items from document, and add it to the container if not added already.
		var i=1;	// starting from 1
		var checkId = this._id + '_' + i;
		var itemDiv = document.getElementById(checkId);
		while (itemDiv)
		{
			var item = this.GetItem(i);
			{
    			if ( item == null )
    			{
    				//this.AddItem( new GeniQueryListItem( i, itemDiv.innerHTML));
    				this.AddItem( new GeniQueryListItem( i, itemDiv));
    			}
			}
			i = i + 1;
    		checkId = this._id + '_' + i;
    		itemDiv = document.getElementById(checkId);
		}
	},
	SetTimerEnabled : function (pBool)
	{
		this._TimerUpdateEnabled = pBool;
	},
	SetRandomNextItem : function (pBool)
	{
		this._IsRandomNextItem  = pBool;
	}
	
}

GeniQueryListItem = function (id, pObject, pContent)
{
	this._id = id;
	this._ItemObj = pObject;
	this._Content = '';
	if (pContent == null)
	{
		this._Content = this._ItemObj.innerHTML;	
	} else
	{
		this._Content = pContent;	
	}
}


// Expand close controller

var __ecc = null;

function GeniGetECC()
{
    if(__ecc == null)
    {
        __ecc = new GeniExpandCloseController();
    }
    return __ecc ;
}

GeniExpandCloseController = function()
{
	this._ExpandedItems = Array();
	this._StockItems = Array();
}


GeniExpandCloseController.prototype = {

	_AddItem: function(pItem) {
		this._ExpandedItems.push(pItem);
	},
	_GetItem: function(pCategoryId) {
		if (this._ExpandedItems.length > 0)
		{
    		for (k1 in this._ExpandedItems) {
    			var item = this._ExpandedItems[k1];
    			if (item._CategoryId === pCategoryId ) {
    				return item;
    			}
    		}
		}
	},
	_GetItemFromStock: function( id, containerId) {
		if (this._ExpandedItems.length > 0)
		{
    		for (k1 in this._ExpandedItems) {
    			var item = this._ExpandedItems[k1];
    			if ((item._Id == id) && (item._ContainerId == containerId)) {
    				return item;
    			}
    		}
		}
	},	
	Expand : function( id, categoryId, containerId, pExpandClass, pCloseClass)
	{
		var item = this._GetItem(categoryId);
		if (item)
		{
			if (item._Id != id)
			{
				this._ExpandedItems.pop(item);
				item._Hide();
				this._StockItems.push(item);
			} else
			{
				//Do nothing for Expand only
			}
		} 
		// expand the item, and add to the list
		// find it from stock first
		var newItem = this._GetItemFromStock(id, containerId);
		if (newItem == null)
		{		
			new GeniExpandCloseItem( id, categoryId, containerId, pExpandClass, pCloseClass);
		}
		newItem._Expand();
		this._AddItem( newItem);
	},
	ToggleExpand : function( id, categoryId, containerId, pExpandClass, pCloseClass)
	{
		var item = this._GetItem(categoryId);
		oldId = '';
		oldContainerId = '';
		
		//alert(id +','+ categoryId +','+ containerId +','+ pExpandClass +','+ pCloseClass);
		
		if (item)
		{
			this._ExpandedItems.pop(item);
			item._Close();
			oldId = item._Id;
			oldContainerId = item._ContainerId;
			this._StockItems.push(item);
		} 
//		alert( oldId  + ',' + oldContainerId );
		
		if ((oldId != id) || (oldContainerId != containerId))
		{
			// expand the item, and add to the list
			var newItem = this._GetItemFromStock(id, containerId);
			if (newItem == null)
			{		
				newItem = new GeniExpandCloseItem( id, categoryId, containerId, pExpandClass, pCloseClass);
			}

			newItem._Expand();
			this._AddItem( newItem);
		}
	}
}


GeniExpandCloseItem = function( id, categoryId, containerId, pExpandClass, pCloseClass)
{
	this._Id = id;
	this._CategoryId = categoryId;
	this._ContainerId = containerId;
	this._Container = document.getElementById(containerId);
	this._ItemObject = null;
	this._ItemShort = document.getElementById(this._Id + '_Short');
	this._ItemLong  = document.getElementById(this._Id + '_Long');
	
	if (this._Container != null)
	{
		//this._ItemObeject = this._Container.getElementById(id);
		//this._ItemObeject = this._Container.childNodes[id];
		this._ItemObject = this._FindItemFromChild(id);
	}
	this._ExpandClass = '';
	this._CloseClass = '';
	
	this._ExpandClass = pExpandClass;
	this._CloseClass = pCloseClass;
}

GeniExpandCloseItem.prototype = {

	_Expand: function (){
		if (this._ItemObject)
		{
			this._ItemObject.style.display = 'block';
		}
		if (this._Container)
		{
			this._Container.className = this._ExpandClass;
		}
		this._RollupShowLongContent();
	},
	_Close: function(){
		if (this._ItemObject)
		{
			this._ItemObject.style.display = 'none';
		}
		if ((this._Container) && (this._CloseClass != ''))
		{
			this._Container.className = this._CloseClass;
		}
		this._RollupShowShortContent();

	},
	_FindItemFromChild: function (pId)
	{
		for(i =0; i< this._Container.childNodes.length; i++)
		{
			if (this._Container.childNodes[i].id == pId)
			{
				return this._Container.childNodes[i];
			}
		}
		return null;
	},
	_RollupShowShortContent: function ()
	{
		if (this._ItemShort && this._ItemLong)
		{
			this._ItemShort.style.display = 'block';
			this._ItemLong.style.display = 'none';
		}

	},
	_RollupShowLongContent : function ()
	{
		if (this._ItemShort && this._ItemLong)
		{
			this._ItemShort.style.display = 'none';
			this._ItemLong.style.display = 'block';
		}
	},
	_RollupToggleContent : function ()
	{
		if (this._ItemShort && this._ItemLong)
		{
			if (this._ItemShort.style.display == 'none')
			{
				this._ItemShort.style.display = 'block';
				this._ItemLong.style.display = 'none';
			} else
			{
				this._ItemShort.style.display = 'none';
				this._ItemLong.style.display = 'block';
			}
		}
	}
}