// store the ID of the currently clicked button
var currentSelection = -1;  // -1 = initial dummy value
var toolbarData;
var lastObj;
var lastMouseoutImg;

function initButton(data){
	toolbarData = data;
}

function setFirstButton(eid, img){
	if (img == null || img ==""){
		img = toolbarData[eid][2];
	}else{
		lastMouseoutImg = img;
		img = img+".jpg";
		currentSelection = eid;
	}
	document.getElementById(eid).setAttribute('src', img);
	lastObj = document.getElementById(eid);
}

// function to change button state on hover
function highlightItem(obj, state) {
    // get button ID
    i = obj.getAttribute('id');
    // check if it is currently clicked
    // if not, change state (normal=0, highlight=1)
    if (i != currentSelection) {
        if (state == 1) {
            obj.setAttribute('src', toolbarData[i][1]);
        } else {
            obj.setAttribute('src', toolbarData[i][0]);
        }                    
    }
}

// function to change button state on click
function selectItem(obj) {
    // get button ID
    i = obj.getAttribute('id');

    // set selected button to click state
    obj.setAttribute('src', toolbarData[i][2]);
	if(lastObj!=null){
		lastObj.setAttribute('src', toolbarData[i][0]);
	}
    // publish ID of clicked button
    currentSelection = i;
	lastObj = obj;
}


// function to change button state on hover
function changeItem(obj,img, state) {
    // get button ID
    i = obj.getAttribute('id');
    // check if it is currently clicked
    // if not, change state (normal=0, highlight=1)
    if (i != currentSelection) {
        if (state == 1) {
            obj.setAttribute('src', img+".jpg");
        } else {
            obj.setAttribute('src', img+"_sw.jpg");
        }

		if (state == 3){
			if (lastObj != null){
				lastObj.setAttribute('src', lastMouseoutImg+"_sw.jpg");
			}
			obj.setAttribute('src', img+".jpg");
			currentSelection = i;
			lastObj = obj;
			lastMouseoutImg = img;					
		}
    }
}

