YAHOO.namespace("integways");
		
/**
*	Toggle the moreinfo classes on or off, i.e show or hide using display none/block
* http://www.lutsr.nl/yui/toggle/
*/
YAHOO.integways.toggle = function(e, o){
	id = o.waytitle;
	
	if(YAHOO.util.Dom.getStyle('tmp-moreinfo-id-' + id, 'display') == 'block'){
		YAHOO.util.Dom.get('tmp-state-id-' + id).innerHTML = ' [more info] ';
		YAHOO.util.Dom.setStyle('tmp-moreinfo-id-' + id, 'display', 'none');
	}
	else{
		YAHOO.util.Dom.get('tmp-state-id-' + id).innerHTML = ' [collapse] ';
		YAHOO.util.Dom.setStyle('tmp-moreinfo-id-' + id, 'display', 'block');
	}
	
	// stop default link to be followed if any
	YAHOO.util.Event.preventDefault(e);
}

/**
*	Initializes everything
*	@param String tag The tags we are using, for eg li, div, p
*	@param String waytitleClass the class for the waytitle
*	@param String moreinfoClass the class for the moreinfos
*/
YAHOO.integways.init = function(tag, waytitleClass, moreinfoClass){
	// get all the nodes
	var waytitleNodes = YAHOO.util.Selector.query(tag + '.' + waytitleClass);
	var moreinfoNodes = YAHOO.util.Selector.query(tag + '.' + moreinfoClass);
	
	// check if the waytitleNodes and moreinfoNodes are same length 
	if(waytitleNodes.length == moreinfoNodes.length){
	
		// set CSS cursor pointer for waytitle class
		YAHOO.util.Dom.setStyle(waytitleNodes, 'cursor', 'pointer');
		
		// hide the moreinfos class by setting display none for moreinfo
		YAHOO.util.Dom.setStyle(moreinfoNodes, 'display', 'none');
		
		// assign temporary ids for the waytitles for easier DOM manipulation afterwards
		for(var i=0;i<waytitleNodes.length;i++){
			waytitleNodes[i].id = 'tmp-waytitle-id-' + i;
			// add the [+] sign here
			waytitleNodes[i].innerHTML =  '<div class = "title">' + waytitleNodes[i].innerHTML + '</div>' 
			+ '<span id="tmp-state-id-' + i + '"> [more info] </span>';
			// add the onclick listener to that waytitle to show the moreinfo
			YAHOO.util.Event.addListener('tmp-waytitle-id-' + i, 'click', YAHOO.integways.toggle, {"waytitle" : i});
		}
		
		// set ids for moreinfo
		for(var i=0;i<moreinfoNodes.length;i++){
			moreinfoNodes[i].id = 'tmp-moreinfo-id-' + i;
		}
	}
	else{
		// show an error message if length of waytitle and moreinfos not same
		alert('Number of waytitle and moreinfo incorrect');
	}
}

YAHOO.util.Event.addListener(window, 'load', function(){
	YAHOO.integways.init('div', 'integways-waytitle', 'integways-moreinfo');
});