/**************************************************************************
***************************************************************************
**  BUTTON HANDLER OBJECT and VARIOUS FUNCTIONS                          **
***************************************************************************
**************************************************************************/
// Copyright 2007-2008 Collaborative Bike Map Project

/****************************************************************************
// Object ButtonInfo - Button information object with member for each screen button
// (for now, just whether it's enabled or disabled).
****************************************************************************/
function ButtonInfo () {
	this.newroute = {en:true, fn:routeAdd};
	this.finishroute = {en:true, fn:routeEnd};
	this.editroute = {en:true, fn:routeEdit};
	this.deleteroute = {en:true, fn:routeDelete};
	this.saveroute = {en:true, fn:dummy_save};
	this.cancelsave = {en:true, fn:dummy_filter};
	this.pointappend = {en:true, fn:enterAppendMode};
	this.pointinsert = {en:true, fn:enterInsertBeforeMode};
	this.pointafter = {en:true, fn:enterInsertAfterMode};
	this.erasepoint = {en:true, fn:deletePoint};
	this.segnew = {en:true, fn:endSegment};
	this.segjoin = {en:true, fn:noop};
	this.intersect = {en:true, fn:dummy_int};
	this.selectmult = {en:true, fn:dummy_select_mult};
	this.selectall = {en:true, fn:dummy_select_all};          
	this.reverse = {en:true, fn:dummy_reverse};
	this.undo = {en:true, fn:dumpRouteXML};
	this.redo = {en:true, fn:openHelp};
	this.segnavfirst = {en:true, fn:navigate, parms:[SEG, "first"]};
	this.segnavprev = {en:true, fn:navigate, parms:[SEG, "prev"]};
	this.segnavnext = {en:true, fn:navigate, parms:[SEG, "next"]};
	this.segnavlast = {en:true, fn:navigate, parms:[SEG, "last"]};
	this.pntnavfirst = {en:true, fn:navigate, parms:[PNT, "first"]};
	this.pntnavprev = {en:true, fn:navigate, parms:[PNT, "prev"]};
	this.pntnavnext = {en:true, fn:navigate, parms:[PNT, "next"]};
	this.pntnavlast = {en:true, fn:navigate, parms:[PNT, "last"]};
}

/*****************************************************************************/
// Check if specified button is enabled.  Returns true if enabled.
/*****************************************************************************/
ButtonInfo.prototype.enabled = function (buttonid) {
	return (this[buttonid].en);
}

/*****************************************************************************/
// Enable or disable specified button (per flag)
/*****************************************************************************/
ButtonInfo.prototype.enable = function (buttonid, enableflag) {
	if (this[buttonid].en != enableflag) {
		this[buttonid].en = enableflag;
		if (!enableflag) {
			setStyle (buttonid, "opacity", "0." + BUTTON_OPACITY_INACTIVE_TIMES_100);
			setStyle (buttonid, "filter", "alpha(opacity=" + BUTTON_OPACITY_INACTIVE_TIMES_100 + ")");
		}
		else {
			setStyle (buttonid, "opacity", "");
			setStyle (buttonid, "filter", "");
		}
	}
}

/*****************************************************************************/
// Invoke event handler for specified button, if button is enabled.
// NOT a member function.
/*****************************************************************************/
ButtonInfo.prototype.invoke = function (buttonid) {
	if (this[buttonid].en) {      // if button enabled
		if (this[buttonid].parms == undefined)      // if no parms specified
			this[buttonid].fn();      // invoke function with no parms
		else
			this[buttonid].fn.apply (null, this[buttonid].parms);     // else call with specified parms
	}
}


/*****************************************************************************/
// Set all buttons to enabled or disabled
/*****************************************************************************/
ButtonInfo.prototype.enableall = function (enableflag) {
	var member;
	for (member in this) 
		this.enable (member, enableflag);
}


/*******************************************************************
*******************************************************************
** Icon button routines
*******************************************************************
********************************************************************/
function icondown (id) {
	//dumpText ("Testfunction called for id = " + id);
	if (ButtonStates.enabled(id)) {
		var elmt = document.getElementById (id + "_cell");
		elmt.style.borderWidth = "3px 1px 1px 3px";
	}
}

function iconup (id) {
	//dumpText ("Testfunction2 called for id = " + id);
	var elmt = document.getElementById (id + "_cell");
	elmt.style.borderWidth = "1px 3px 3px 1px";
}

function iconin (id) {
	//dumpText ("Testfunction called for id = " + id);
	if (ButtonStates.enabled(id)) {
		var elmt = document.getElementById (id + "_cell");
		elmt.style.borderColor = "red";
	}
}

function iconout (id) {
	//dumpText ("Testfunction called for id = " + id);
	var elmt = document.getElementById (id + "_cell");
	elmt.style.borderWidth = "1px 3px 3px 1px";
	elmt.style.borderColor = "black";
}


/*******************************************************************
* Set icon properties 
********************************************************************/
/* function setIconProperties () {
	var len, i;
	var id;

	var leftcolumn = document.getElementById ("leftcol");
	var icons = getElementsByClassName (leftcolumn, "td", "iconcell");
	var len = icons.length;
	dumpText ("Length = " + len);
	
	for (i=0; i<len; i++) {
		id = icons[i].getAttribute("id");
		if (id != null) {
			icons[i].onmouseup = iconup;
			icons[i].onmousedown = icondown;
			icons[i].onmouseover = iconin;
			icons[i].onmouseout = iconout;
			dumpText ("id = " + id);
		}
		// icons[i].mouseup = id + ".style='border-width:1px 3px 3px 1px";
		// icons[i].mousedown = id + ".style='border-width:3px 1px 1px 3px";
		// setStyle (id, 'border', '1px 3px 3px 1px')";
		// icons[i].mousedown = "setStyle (icons[i], 'border', '3px 1px 1px 3px')";
		// icons[i].mouseover = function() {setStyleByObject (icons[i], "border-color", "blue");};
		// icons[i].mouseout = function() {setStyleByObject (icons[i], "border-color", "black");};
	}
}      */

/******************************************************************************
******************************************************************************
// ENABLE / DISABLE BUTTONS
******************************************************************************
******************************************************************************/

/*******************************************************************/
// Enable/disable buttons for view mode
/********************************************************************/
function setButtons_ViewMode() {
	ButtonStates.enableall (false);
	// Disable all except...
	ButtonStates.enable ("newroute", true);
	// Temp stuff...
	ButtonStates.enable ("undo", true);
	ButtonStates.enable ("redo", true);
	// Set navigation buttons if route selected, otherwise disable
	setNavButtons (true);
}

/*******************************************************************/
// Enable/disable buttons for new empty route
/********************************************************************/
function setButtons_NewEmptyRoute() {
	ButtonStates.enableall (false);        // disable all but...
	//  ButtonStates.enable ("newroute", true);
	// Temp stuff...
	ButtonStates.enable ("undo", true);
	ButtonStates.enable ("redo", true);
}

/*******************************************************************/
// Enable buttons after adding first route point (or deleting 2nd point)
/********************************************************************/
function setButtons_FirstRoutePoint() {
	// Enable these
	ButtonStates.enable ("finishroute", true);         // shouldn't really
	ButtonStates.enable ("deleteroute", true);
	ButtonStates.enable ("cancelsave", true);
	ButtonStates.enable ("pointappend", true);
	ButtonStates.enable ("pointinsert", true);
	ButtonStates.enable ("pointafter", true);
	ButtonStates.enable ("erasepoint", true);
	ButtonStates.enable ("selectmult", true);
	ButtonStates.enable ("selectall", true);
	ButtonStates.enable ("pointafter", true);
	// Disable these (don't worry about already disabled buttons)
	ButtonStates.enable ("intersect", false);
	ButtonStates.enable ("saveroute", false);
	ButtonStates.enable ("segnew", false);
	ButtonStates.enable ("segjoin", false);
	ButtonStates.enable ("reverse", false);
	ButtonStates.enable ("undo", true);
	ButtonStates.enable ("redo", true);
	
	// Enable/disable navigation buttons...
	// NOTE: If SelectedSegment/SelectedPoint haven't been set/updated yet, 
	// this might not be accurate and this will have to will be called again once 
	// SelectedSegment/SelectedPoint are updated!
	setNavButtons (true);
	// need to handle segment buttons separately!
}

/*******************************************************************/
// Disable buttons after second route point
/********************************************************************/
function setButtons_SecondRoutePoint() {
	// Enable these
	ButtonStates.enable ("finishroute", true);
	ButtonStates.enable ("saveroute", true);
	ButtonStates.enable ("segnew", true);
	ButtonStates.enable ("reverse", true);
	ButtonStates.enable ("intersect", true);
	// Enable/disable navigation buttons...
	// NOTE: If SelectedSegment/SelectedPoint haven't been set/updated yet, 
	// this might not be accurate and this will have to will be called again once 
	// SelectedSegment/SelectedPoint are updated!
	setNavButtons (true);

	// need to handle segment buttons separately!
}

/******************************************************************************
******************************************************************************
// HANDLE NAV BUTTONS
******************************************************************************
******************************************************************************/
/*******************************************************************/
// Enable or disable navigation buttons.  Enable according to
// current segment & point, if input enable_flag = true
// (if not passed, defaults to true).  
// Disable regardless if enable_flag = false.
// NOTE: If SelectedSegment/SelectedPoint haven't been set/updated yet, 
// this might not be accurate and will have to will be called again once 
// SelectedSegment/SelectedPoint are updated.
/********************************************************************/
function setNavButtons (enable_flag) {
	var enable = false;
	var firstseg, firstpnt, lastseg, lastpnt;
	
	// debugger;
	// get input value
	if (enable_flag == undefined) 
		enable = true;
	else
		enable = enable_flag;

	// override if no current segment and point
	if (SelectedRoute == null || SelectedSegment == null || SelectedRoute == null) {
		enable = false;
	}

	// alert ("In setNavButtons, enable = " + enable);
	
	if (enable) {
		firstseg = (SelectedRoute.seglist.firstnode == SelectedSegment);
		lastseg = (SelectedRoute.seglist.lastnode == SelectedSegment);
		firstpnt = (SelectedRoute.pointlist.firstnode == SelectedPoint);
		lastpnt = (SelectedRoute.pointlist.lastnode == SelectedPoint);
		/* segnum = SelectedRoute.seglist.getIndex (SelectedSegment);
		pntnum = SelectedRoute.pointlist.getIndex (SelectedPoint);
		firstseg = (segnum == 0);
		firstpnt = (pntnum == 0);
		lastseg = (segnum < SelectedRoute.seglist.length - 1)
		lastpnt = (pntnum < SelectedRoute.pointlist.length - 1) */
	}
	
	// enable or disable buttons
	ButtonStates.enable ("segnavfirst", enable);
	ButtonStates.enable ("segnavprev", enable && !firstseg);
	ButtonStates.enable ("segnavnext", enable && !lastseg);
	ButtonStates.enable ("segnavlast", enable);
	ButtonStates.enable ("pntnavfirst", enable);
	ButtonStates.enable ("pntnavprev", enable && !firstpnt);
	ButtonStates.enable ("pntnavnext", enable && !lastpnt);
	ButtonStates.enable ("pntnavlast", enable);
}



/******************************************************************************
******************************************************************************
// MISCELLANEOUS
******************************************************************************
******************************************************************************/

/****************************************************************************
// This is provided to be called from HTML.
// Invoke event handler for specified button, if button is enabled.
// NOT a member function.
****************************************************************************/
function handleButton (buttonid) {
	ButtonStates.invoke(buttonid);      // invoke function
}


/****************************
// Temp functions.  
*****************************/
function dummy_int() {
	alert ("The Intersection function is not yet implemented.  In the future this will allow you to define intersections between routes.");
}
function dummy_reverse() {
	alert ("The Reverse function is not yet implemented.");
}
function dummy_select_mult() {
	alert ("The Select Multiple function is not yet implemented.");
}
function dummy_save() {
	alert ("The Save function is not yet implemented.");
}
function dummy_select_all() {
	alert ("The Select All function is not yet implemented.");
}
function dummy_filter() {
	alert ("The Filter function is not yet implemented.  In the future this will allow you to filter displayed routes by bikeway type.");
} 
function dummy_join() {
	alert ("The Join function is not yet implemented.  In the future this will allow you to join two segments or routes.");
}



/****************************
// No-op function.  
// NOT a member function.
*****************************/
function noop() {}

