var clicked=new Array(), inc=new Array(), flyout=new Array(), buttonCells=new Array(), condition = new Array(), buttonFrames=new Array();
var pre, post, cell, pushedButton, intVar;

function setup(){

for (element=1;element<=buttonCount;element++){
	clicked[element]=0;
	inc[element]=0;
}

   if (document.getElementById){
      //Netscape 6 specific code
      pre = 'document.getElementById("';
      post = '").style';
   }
   
   if (document.all){
      //IE4+ specific code
      pre = 'document.all.';
      post = '.style';
   }


   if (document.layers){
      //Netscape 4 specific code
      pre = 'document.';
      post = '';
   }
   
	if (!document.layers){
		for (i=1;i<=buttonCount;i++){
			flyout[i]=eval(pre + "sub" + i + post);
			flyout[i].width=0;	
			flyout[i].height=0;
			buttonCells[i]=eval(pre + 'butCell' + i + post);
		}	

// Pre-load button Animation Frames
		for (i=1;i<=numFrames;i++){
			buttonFrames[i]=new Image;
			buttonFrames[i].src='images/button_f0' + i + '.gif';
		}

// Start flyout/button animation cycler
		intVar=setInterval('controlFlyout()',15);
	}
	
//	if (document.cookie && document.cookie.indexOf('activeButton=')!= -1){
//	  pushedButton=parseInt(document.cookie.substring(document.cookie.indexOf('activeButton=')+13));

	if (document.URL && document.URL.indexOf('page')!= -1){
	  var pos=document.URL.indexOf('page')+4
	  pushedButton=parseInt(document.URL.substring(pos,pos+1));
	  buttonCells[pushedButton].backgroundImage='url(images/button_f01.gif)';
	  buttonCells[pushedButton].backgroundColor=calcColor(color2dark,0.30);
	  buttonCells[pushedButton].color=color1dark;
	}
}


function overAction(cell){
	  condition[cell]='over';
	  inc[cell]=1;
}

function outAction(cell){
	condition[cell]='out';
	inc[cell]=-1;
}

function clickAction(cell){
	animFrame[cell]=5;
	condition[cell]='click';
}
	

function controlFlyout(){
	for (i=1;i<=buttonCount;i++){
	  if (inc[i]){
		curSize=parseInt(flyout[i].width)
		if (curSize<151 && inc[i]==1 && subButtons[i].length>0){
			 flyout[i].left=calcX(i-1);
			 flyout[i].top=calcY(i-1);
 			 flyout[i].visibility="visible";
			 flyout[i].width=(curSize+15);	
			 flyout[i].height=Math.min((curSize+45),310);
		} else if (inc[i]==(-1)){
			flyout[i].visibility="hidden";
			flyout[i].width=0;	
			flyout[i].height=0;
		}
		if (pushedButton!=i || condition[i]=='click'){ 
		 switch (condition[i]){
		   case 'over':
			 if (animFrame[i]<numFrames){
			   animFrame[i]+=1;
			   buttonCells[i].backgroundImage='url(images/button_f0' + animFrame[i] + '.gif)';
			  } else {
			   buttonCells[i].backgroundColor=calcColor(color2dark,0.50);
			   buttonCells[i].color=color1dark;
			   condition[i]='stop';
			  }
			  break;
			case 'out':
			 if (animFrame[i]>midFrame){
			   animFrame[i]-=1;
			   buttonCells[i].backgroundImage='url(images/button_f0' + animFrame[i] + '.gif)';
			   buttonCells[i].backgroundColor=color2dark;
			   buttonCells[i].color=color2light;
			 } else {
			   condition[i]='stop';
			   inc[i]=0;
			 }
			 break;
			case 'click':
			 if(subButtons[i].length==0){
				 if (animFrame[i]>1){
				   animFrame[i]-=1;
				   buttonCells[i].backgroundImage='url(images/button_f0' + animFrame[i] + '.gif)';
				 } else {
				   parent.location.href='page'+i+'.htm';
				   condition[i]='stop';
//				   document.cookie='activeButton='+i;
				 }
				 break;
			 }
		 }
		}
	  }
	}
}


function clickButton(num){
	if (document.layers && clicked[num]==0){
		for (i=1;i<=buttonCount;i++){
			if (num==i) continue;
			flyout2=eval(pre + "sub" + i + post);
			flyout2.visibility="hidden";
			clicked[i]=0;
		}
		if(subButtons[num].length==0){parent.location.href='page'+num+'.htm'};
//		if (mainNavLinks[num]!="#") document.location.href=mainNavLinks[num];
		flyout=eval(pre + "sub" + num + post);
		flyout.left=calcX(num-1)-5;
		flyout.top=calcY(num-1)-5;
		flyout.visibility="visible";
		clicked[num]=1

	} else if (document.layers && clicked[num]==1){
		flyout=eval(pre + "sub" + num + post);
		flyout.visibility="hidden";
		clicked[num]=0
	}
	if (!document.layers){
		parent.location.href=mainNavLinks[num];
	}
}


function openControl(){
	window.open('common/controlPanel.htm?templateChoice=contemporary','editor','width=550,height=475,scrollbars=yes, status=yes, left=' + String(screen.availWidth-580) + ',top=' + String(screen.availHeight-520))
}

function calcColor(hexColor,trans){
//	parse for RGB values of selected color, then calculate new RGB values for transparency where 'trans' is percent transparency
	var R=parseInt("0x" + hexColor.substr(1,2));
	var G=parseInt("0x" + hexColor.substr(3,2));
	var B=parseInt("0x" + hexColor.substr(5,2));
//	var trans=0.75;
	var R1=Math.floor((R+(255-R)*trans)).toString(16);
	var G1=Math.floor((G+(255-G)*trans)).toString(16);
	var B1=Math.floor((B+(255-B)*trans)).toString(16);
	var newColor=("#"+R1+G1+B1);
	return newColor
}
