/*# ****************************************************************************************
# *                                                                                        *
# *   		           JavaScript Image Toggle Module		                               *
# *                                                                                        *
# *                        [ February 13, 2006 ] 		                                   *
# *			                  www.bhunet.com                                               *
# *                                                                                        *
# ******************************************************************************************
#
# Copyright 2006, BhuNet Designs & Solutions (Pvt.) Ltd. All rights reserved.
#
# Author: Deven (Devendra Chand - deven.pgmr@gmail.com)
#
# JavaScript Image Toggle Module is codded for Moustail Communications (Pvt.) Ltd. by BhuNet
# Designs & Solutions (Pvt.) Ltd. This module is not a freeware and thus may not be sold, 
# distributed and or copied either electronically or otherwise in whole or in part, but may 
# be used and modified for purposes by MouseTail Communications (Pvt.) Ltd. only, as long as
# this copyright/usage note remains intact and unchanged in each place it appears. 
*/

var tblatts = new Object(); // attributes for the main toggler table
tblatts['width'] = '100%';
tblatts['cellspacing'] = '0';
tblatts['border'] = '0';
tblatts['cellpadding'] = '0';

var toggledbcol = '#F7F7F7'; // background color
var toggledborder = '#dddddd'; // border color
var closerclass = ''; // close button class name
var togglerTbl = null; // the togglertable object;

var tH = new Object(); // the toggler object;

/*********************** addTogglerItem(label,itemdesc,imgsrc,cname) *************************/
/* Function Name: addTogglerItem                                                             */
/* Arguement: item label, item descrition, imagesource, itemclassanme                        */
/* return Type: void                                                                         */
/* Description:                                                                              */
/* this function stores all the items and their associated attributes.                       */
/* this function must be called before any other functions are called.                       */
/*********************************************************************************************/
function addTogglerItem(label,itemdesc,imgsrc,cname)
{
	closerclass = closerclass == '' ? cname : closerclass;
	img = new Image();
	img.src = imgsrc;
	if(typeof tH[label] == 'undefined'){
		var tmp = new Object();
		tmp['row'] = null;
		tmp['cName'] = cname;
		tmp['idesc'] = itemdesc;
		tmp['imgs'] = new Array();
		tmp['v_L'] = false;
		tH[label] = tmp;
		tmp = null;
	}
	tH[label]['imgs'].push(img.src);
}

/************************** loadToggler(void) ****************************/
/* Function Name: loadToggler                                            */
/* Arguement: void                                                       */
/* return Type: void                                                     */
/* Description:
/* this function loads the toggler interface. It writes the content      */
/* where it is called. So it can be called at any place in the web page. */ 
/*************************************************************************/
function loadToggler()
{
    document.write(getTogglerContent('toggler'));
	togglerTbl = document.getElementById('toggler');
}

/************************ toggle(tbl,row,link) ***************************/
/* Function Name: toggle                                                 */
/* Arguement: TableRowObject, itemlabel;                                 */
/* return Type: boolean false (to prevent from the page being submitted) */
/* Description:                                                          */
/* This function accepts two arguements - tablerowobject                 */
/* and itemlabel. Inserts a new row after the rowobject,                 */
/* inserts a cell in the newly inserted row and then inserts             */
/* contetns to that cell as returned by getToggledContent() function.    */
/*************************************************************************/
function toggle(rowObj,label)
{
	if(rowObj){
	    if(tH[label]['row'] == null) tH[label]['row'] = rowObj;
	}
	if(tH[label]['v_L'] === false){
	    newRow = togglerTbl.insertRow(tH[label]['row'].rowIndex+1);
	    newCell = newRow.insertCell(0);
		newCell.style.padding = '1px 0px 10px 5px';
	    newCell.innerHTML = getToggledContent(label);
		tH[label]['v_L'] = true;
	}
	else{
	    togglerTbl.deleteRow(tH[label]['row'].rowIndex+1);
		tH[label]['v_L'] = false;
	}
	return false;
}

/********************** getTogglerContent(strid) *************************/
/* Function Name: getTogglerContent                                      */
/* Arguement: string id                                                  */
/* return Type: string representation of the toggler table               */
/* Description:                                                          */
/* this function is helper function to loadToggler()                     */
/* It returns the string representaion of toggler table. the design can  */
/* be customied                                                          */
/*************************************************************************/
function getTogglerContent(id)
{
    var content = '';
	var atts = '';
	for(var key in tblatts) atts += key+' = "'+tblatts[key]+'" ';
	content += '<table '+atts+' id="'+id+'">';
	for(var key in tH){
	    itemClass = tH[key]['cName'] != null ? tH[key]['cName'] : '';
		content += '<tr>';
	    content += '<td>';
	    content += '<a href="./" onclick="return toggle(this.parentNode.parentNode,this.innerHTML)" class="'+itemClass+'">'+key+'</a>';
	    content += '</td>';
	    content += '</tr>';
	}
	content += '</table>';
	return content;
}

/*********************** getToggledContent(label) ************************/
/* Function Name: getToggledContent                                      */
/* Arguement: string item label                                          */
/* return Type: string representation of the toggled table               */
/* Description:                                                          */
/* this function is helper function to toggle()                          */
/* It returns the string representaion of toggled content                */
/*************************************************************************/
function getToggledContent(label)
{
    var imgs  = typeof tH[label] != 'undefined' ? tH[label]['imgs'] : [];
	var content = '';
	content += '<table width="100%" cellspacing="0" cellpadding="0">';
	content += '<tr><td width="90%" style="padding: 1px 5px 1px 5px;border-left: 1px solid '+toggledborder+';border-top: 1px solid '+toggledborder+';border-bottom: 1px solid '+toggledborder+';background-color:'+toggledbcol+';font-weight:bold">'+tH[label]['idesc']+'</td>';
	content += '<td align="right" style="border-right: 1px solid '+toggledborder+';border-right: 1px solid '+toggledborder+';border-top: 1px solid '+toggledborder+';border-bottom: 1px solid '+toggledborder+';background-color:'+toggledbcol+'"><table><tr><td style="border-left: 1px solid '+toggledborder+';padding:1px 5px 1px 5px"><a class="'+closerclass+'" href="./" onclick="return toggle(null,\''+label+'\');"> [Close] </a></td></tr></table></td></tr>';
	content += '<tr><td colspan="2" style="border-left: 1px solid '+toggledborder+';border-right: 1px solid '+toggledborder+';border-bottom: 1px solid '+toggledborder+'"><table>';
	for(i=0;i<imgs.length;i++){
		content += '<tr>';
		content += '<td>';
		content += '<img src="'+imgs[i]+'">';
		content += '</td>';
		content += '</tr>';
	}
	content += '</table>';
	content += '</td></table></tr>';
	content += '</table>';
	return content;
}

/************************* setBorder(str col) ************************/
/* Function Name: setBorder                                          */
/* Arguement: string color                                           */
/* return Type: null                                                 */
/* Description:                                                      */
/* sets the border color                                             */
/*********************************************************************/
function setBorder(col)
{
    toggledborder = col;
}

// sets the background color
function setBackgroundColor(col)
{
    toggledbcol = col;
}

// sets the close botton class name
function setCloserClass(name)
{
	closerclass = name;
}