/*---------------------------------------------------------------------------------------------
	Makes rollover image menus (with pre-loaded images for quick behaviour) simple.
	
	To use:
		image file names have to be consistent in that for each of the 3 states (at rest, over and
		selected) the file names are in the format xxxxrest.xxx, xxxxover.xxx and xxxxsel.xxx as 
		a simple string search/replace figures out the filenames.
		
		All of the image files must be in the same directory.
		
		Insert images in your HTML.  Give them unique IDs and a link.
		
		At the bottom of the HTML page, insert a JS section and call RegisterButtons(), passing as
		arguments the ids (as strings) of all of the buttons that have to 'get along'
		
		RegisterButtons() can only be called once, so make it good.
		
		For a button that causes a Popup(), call ConvertToPopup(), set the target of the link of
		the button to '_blank' and the selected button will not change when this button is selected.
		
		Dat's it.
---------------------------------------------------------------------------------------------*/
var selectedButton=null;
var preloadImages=new Array();


function ButtonRec(){
	this.rest=new Image();
	this.over=new Image(), 
	this.sel=new Image();
/*	
	AssignMouseEventHandlers(this.rest);
	AssignMouseEventHandlers(this.over);
	AssignMouseEventHandlers(this.sel);
*/
}

function AssignMouseEventHandlers(button){
	button.onmouseover=new Function("MouseOver(this);");
	button.onmouseout=new Function("MouseOut(this);");
	button.onclick=new Function("OnClick(this);");
}

function RegisterButtons(){
	for(var i=0;i<arguments.length;i++){
		var registeredButton=getDOMlayerToShow(arguments[i]);
		AssignMouseEventHandlers(registeredButton);

		var butt=new ButtonRec();
		var filename=registeredButton.src;

		butt.rest.src=registeredButton.src;
		butt.over.src=filename.replace("rest.","over.");
		butt.sel.src=filename.replace("rest.","sel.");
		registeredButton.value=i;
		preloadImages[i]=butt;
	}
}

function ConvertToPopup(buttonID){
	var butt=getDOMlayerToShow(buttonID);	
	butt.IsPopup=true;
/*
	butt.onclick=new Function("return popUpWindow('" + butt.parentElement.href + "',15,15,600,500);");
	butt.parentElement.href=null;
*/
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height){
  if(popUpWin){
    if(!popUpWin.closed) 
			popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	return false;
}


function MouseOver(button){
	if(button==selectedButton)
		return;
	button.src=preloadImages[button.value].over.src;	
	try{
			getDOMlayerToShow("soundOver").play();
	}
	catch(ex){
		
	}
}

function MouseOut(button){
	if(button==selectedButton)
		return;
	button.src=preloadImages[button.value].rest.src;	
}

function OnClick(button){
	if(button.IsPopup){
		return;
	}
	else{
		if(selectedButton){
			selectedButton.src=preloadImages[selectedButton.value].rest.src;	
		}
		button.src=preloadImages[button.value].sel.src;	
		selectedButton=button;
	}
	try{
		getDOMlayerToShow("soundClick").play();
	}
	catch(ex){
		
	}
}

function getDOMlayerToShow(layerToShow) {
  if(document.layers) {
    return document.layers[layerToShow];
  } 
  else if(document.all && !document.getElementById) {
     return document.all[layerToShow];
  } 
  else if(document.getElementById) {
     return document.getElementById(layerToShow);
  }
   else {
     return null;
  }
}