/*
 *------------------------------------------------------------------
 * toolbar.js
 *
 * Copyright (c) 2003, 2004 by cisco Systems, Inc.
 * All rights reserved.
 *------------------------------------------------------------------
 */

// constants used for aligning the toolbar icons.
var LEFT = 1;
var RIGHT = 2;

// preloading the cisco logo.
// The other toolbar icons need to be preloaded in their respective pages.
var tb_logo_off = new Image();  tb_logo_off.src = "images/logo.gif";

// img_over method to set the toolbar icon on mouse over
function img_over(name) {
    if (document.images) {
        imgOn = eval( "tb_" + name + "_on.src" );
        document.images[ name+"_img" ].src = imgOn;
	}
}

// img_out method to set the toolbar icon on mouse out
function img_out(name) {
    if (document.images) {
	    imgOff = eval( "tb_" + name + "_off.src" );
        document.images[ name+"_img" ].src = imgOff;
	}
}

// The icon constructor
function Icon(name, tip, fn, url, align) {
	this.name = name;	// The unique name of the icon.
	this.tooltip = tip;	// The tooltip that needs to be displayed for this icon
	this.action = fn;	// The method that needs to be executed on clicking this icon
	this.url = url;		// TBD: 
	this.align = align;	// The alignment of the icon LEFT or RIGHT.
	this.id = 0;		// The index of this icon in the Toolbar icons array.
	this.draw = _drawIcon;	// Method to draw this icon.
}

// handlTbClick method will be called on clicking on any of the toolbar
// icons.
function handleTbClick(id) {
	if(toolbar.frozen) {
		alert(toolbar.freezeReason);
		return;
	}
	if((id != null) && (toolbar.icons[id].action)) {
		toolbar.icons[id].action();
	}
}

// _drawIcon returns the html code for displaying the icons.
function _drawIcon() {
	var str = "<td width='30'>";
	str += "<img name='" + this.name + "_img' src='" + eval("tb_" + this.name + "_off.src") + "'";
	str += " height='34' title='" + this.tooltip + "' onClick='handleTbClick(" + this.id + ");'";
	str += " style='cursor: pointer; cursor: hand;'";
	str += " onMouseOut='img_out(&quot;" + this.name + "&quot;);'";
	str += " onMouseOver='img_over(&quot;" + this.name + "&quot;);'";
	str += "></img>";
	str += "</td>";
	return str;
}

// The toolbar constructor.
function Toolbar() {
	this.icons = new Array();	// The icons in this toolbar
	this.addIcon = _addTbIcon;	// The method to add the toolbar icon.
	this.draw = _drawToolbar;	// Method to draw the toolbar.
	this.leftIcons = 0;			// The count of icons that need to be aligned to the left
	this.freeze = _freezeToolbar;	// Method to freeze the toolbar
	this.unFreeze = _unFreezeToolbar;	// Method to unfreeze the toolbar
	this.frozen = false;		// Variable to store the freeze state.
	this.freezeReason = null;	// The reason for freezing the toolbar.
}

// _freezeToolbar method will freeze the toolbar. No action will
// be performed on clicking the toolbar icons.
function _freezeToolbar(reason) {
	if(reason == null)
		return;
	this.freezeReason = reason;
	this.frozen = true;
}

// _unFreezeToolbar method will freeze the toolbar.
function _unFreezeToolbar() {
	this.freezeReason = null;
	this.frozen = false;
}

// Function to add the toolbar icon.
function _addTbIcon(icon) {
	icon.id = this.icons.length;
	this.icons[this.icons.length] = icon;
	if(icon.align == LEFT) this.leftIcons++;
}

function spacer() {
	var str = "<td width='5'><img width='5' src='images/spacer.gif'></img></td>"
	return str;
}

// Function to draw the cisco logo
function drawLogo() {
	var str = "<td width='30'>";
	str += "<img src='" + tb_logo_off.src + "' height='34' title='' onClick=''></img>";
	str += "</td>";
	return str;
}

// Function to draw the toolbar
function _drawToolbar() {
	var str = "<table border='0' cellspacing='0' cellpadding='0' height='34' width=100%><tr>";
	for (var i=0; i<this.icons.length; i++) {
		str += this.icons[i].draw();
//		if (i == (this.leftIcons-1)) {	// Ignore the alignment specified. Draw all icons to the left
		if (i == (this.icons.length-1)) {
			str += "<td width='100%'>&nbsp;</td>";
		}
	}
	str += drawLogo();
	str += "</tr></table>";
	return str;
}
