//declarations and preliminary assignments
var linkS=""        //string to write links HTML
var menuS="";       //string to write menu HTML
var menuUp=false;   //Are any menus visible?
var mname="";       //name of menu that is currently open
var v=1;						//direction that submenu will expand vertically
var menuGroup =""; //group that a sub belongs to
var floatMenuTop=-300;		// vertical position from link div of floating menu (-300 is 300px upwards).
var floatMenuLeft=150;		// horizontal position from link div of floating menu (150 is 150px left).
var mainBar;							//the main menu bar (always visible).

//set child button colours - note: main button colours set in style sheet menu.css

var childBackground="#90a0ff"; 	// child button background colour
var colourOff="#ffe178"; 			 // all buttons colour when off - need to set in menu.css as well


// constructor for Menu object
function MenuElement(tIn,lIn,idIn,labelIn)
	{
		//attributes
		this.top=tIn;
		this.left=lIn;
		this.id=idIn;											// id of menu element for code manipulation and styles
		this.position=0;									//menus's position in main menu - positions = "F" denote floating menus.
		this.up=false; 										//is the menu expanded to show its elements?
		this.label=labelIn; 							// label that will appear in the menu box.
		this.url="window.location=''";		// the action that will occur when the menu is clicked.
			
		//methods
		
	}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

//set menu expand direction and floating menu relative position - from link div
function makeMenu(vIn)
	{
		v=vIn;
		floatMenuTop=-100*v;
		floatMenuLeft=150;
		declare();
		build()
	}

function declare()
	{	

// declare menu element objects 

// second level menu objects

	// classes
	
		earlyYearsE=new MenuElement(0,0,"earlyYears","Early Years");
		earlyYearsE.url="window.location='earlyYears.html'";
		
		neptuneE=new MenuElement(0,0,"neptune","Neptune");
		neptuneE.url="window.location='neptune.html'";
		
		saturnE=new MenuElement(0,0,"saturn","Saturn");
		saturnE.url="window.location='saturn.html'";
		
		jupiterE=new MenuElement(0,0,"jupiter","Jupiter");
		jupiterE.url="window.location='jupiter.html'";

	//office
		holFormsE=new MenuElement(0,0,"holForms","Holiday Forms");	
		holFormsE.url="getPage('documents/LeaveOfAbsesnceForm.doc')";
		
		

	//news
		
		announcementsE=new MenuElement(0,0,"announcements","Announcements");
		announcementsE.url="window.location='announcements.html'";
		
		newsletterE=new MenuElement(0,0,"newsletter","Newsletter");
		newsletterE.url="getPage('documents/newsletter.doc')";
		
		recentE=new MenuElement(0,0,"recent","Recent News");
		recentE.url="window.location='recent.html'";
		
	//staff
		adminE=new MenuElement(0,0,"admin","Management &#38; Admin.");
		adminE.url="window.location='adminStaff.html'";
		
		teachingE=new MenuElement(0,0,"teaching","Teaching Staff");	
		teachingE.url="window.location='teachers.html'";
			
		supportE=new MenuElement(0,0,"support","Support Staff");	
		supportE.url="window.location='support.html'";
	// List Clubs
		musicE=new MenuElement(0,0,"music","Music Tuition");
		musicE.url="window.location='music.html'";
		
		kickboxingE=new MenuElement(0,0,"kickboxing","Kickboxing Club");
		kickboxingE.url="window.location='kickboxing.html'";
		
		
// first level menu objects
	//info
		aimsE=new MenuElement(0,0,"aims","Our Aims");
		aimsE.url="window.location='aims.html'";
			
		diaryE=new MenuElement(0,0,"diaryE","Diary");
		diaryE.url="window.location='http://www.diary.firstschool.org.uk/cgi-bin/diary.pl?school=Grange View'";			
		
		governorsE=new MenuElement(0,0,"governors","Governors");
		governorsE.url="window.location='governors.html'";
		
		holsE=new MenuElement(0,0,"hols","Holidays");	
		holsE.url="window.location='hols.html'";
		
		newsE=new MenuElement(floatMenuTop,floatMenuLeft,"news","News");
		newsE.position="F";
		newsE.url="spawn(newsE)";
		newsE.child=new Array(announcementsE,newsletterE,recentE);
		makeChildren(newsE);
		
		officeE=new MenuElement(floatMenuTop,floatMenuLeft,"office","Office");
		officeE.position="F";
		officeE.url="spawn(officeE)";
		officeE.child=new Array(holFormsE);
		makeChildren(officeE);
		

    ofstedE=new MenuElement(0,0,"ofsted","Ofsted Report");	
		ofstedE.url="getPage('documents/ofsted2007.pdf')";
		
		policiesE=new MenuElement(0,0,"policies","Policies");
		policiesE.url="window.location='policies.html'";
		
		prospectusE=new MenuElement(0,0,"prospectus","Prospectus");
		prospectusE.url="window.location='brochure.html'";
		
		staffE=new MenuElement(floatMenuTop,floatMenuLeft,"staff","Staff");
		staffE.position="F";
		staffE.url="spawn(staffE)";
		staffE.child=new Array(adminE,teachingE,supportE);
		makeChildren(staffE);
		
		
	
	//school life
		aditlE=new MenuElement(0,0,"aditl","A Day In The Life");
		aditlE.url="window.location='aditl.html'";
		
		
		classesE=new MenuElement(floatMenuTop,floatMenuLeft,"classes","Classes");
		classesE.position="F";
		classesE.url="spawn(classesE)";
		classesE.child=new Array(earlyYearsE,neptuneE,saturnE,jupiterE);
		makeChildren(classesE);
	
		
		
		galleryE=new MenuElement(0,0,"gallery","Gallery");
		galleryE.url="window.location='gallery.html'";
		
		tourE=new MenuElement(0,0,"tour","Tour Our School");
		
		
	//clubsE
		
		
		listClubsE=new MenuElement(floatMenuTop,floatMenuLeft,"listClubs","Clubs");
		listClubsE.position="F";
		listClubsE.url="spawn(listClubsE)";
		listClubsE.child=new Array(kickboxingE,musicE);
		makeChildren(listClubsE);
		
		extendedE=new MenuElement(0,0,"extended","Extended school");
		extendedE.url="window.location='extendedSchool.html'";
				
		gamesE=new MenuElement(0,0,"games","Games");

		
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
// base level menu objects
		homeE=new MenuElement(0,0,"home","Home");
		homeE.url="window.location='index.html'";
		
		contactE=new MenuElement(0,150,"contact","Contact Us");
		contactE.position=1;
		contactE.url="window.location='contact.html'";
		
		infoE=new MenuElement(0,300,"info","Information");
		infoE.position=2;
		infoE.url="toggle(infoE)";
		infoE.child=new Array(aimsE,diaryE,governorsE,holsE,newsE,officeE,ofstedE,policiesE,prospectusE,staffE);
		makeChildren(infoE);		
				
		lifeE=new MenuElement(0,450,"life","School Life");
		lifeE.position=3;
		lifeE.url="toggle(lifeE)";
		lifeE.child=new Array(aditlE,classesE,galleryE,tourE);
		makeChildren(lifeE);		
		
		clubsE=new MenuElement(0,600,"clubs","After School");
		clubsE.position=4;
		clubsE.url="toggle(clubsE)";
		clubsE.child=new Array(listClubsE,extendedE,gamesE);
		makeChildren(clubsE);
//specify what elements in main menu bar
		mainBar = new Array(homeE,contactE,infoE,lifeE,clubsE);
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function build(vIn)
  {	
  //set up initial divs to take menu details
  	linkS += "<div class=\"menu\" id=\""+mainBar[0].id+"\" onmouseover=\"over('"+mainBar[0].id+"')\" onmouseout=\"off('"+mainBar[0].id+"')\" onclick=\""+mainBar[0].url+"\">"+mainBar[0].label+"</div>";
  	for (var i=1;i<mainBar.length;i++)
			{
  			linkS += "<div id = \"menu"+i+"\"></div>";
  		}
  	linkS += "<div id = \"menuF\"></div>";
	//Write string to page
		document.getElementById("link").innerHTML = linkS;
		document.getElementById(homeE.id).style.top=homeE.top+"px";
 		document.getElementById(homeE.id).style.left=homeE.left+"px";
	for (var i=1;i<mainBar.length;i++)
		{
	// add menu details
			menuS = "<div class=\"menu\" id=\""+mainBar[i].id+"\" onmouseover=\"over('"+mainBar[i].id+"')\" onmouseout=\"off('"+mainBar[i].id+"')\" onclick=\""+mainBar[i].url+"\">"+mainBar[i].label+"</div>";
			document.getElementById("menu"+mainBar[i].position).innerHTML = menuS;
			document.getElementById(mainBar[i].id).style.top=mainBar[i].top+"px";
 			document.getElementById(mainBar[i].id).style.left=mainBar[i].left+"px";
 		}
	}

//Change menu direction of expansion - up or down - depending on position
function menuDirection(groupIn)
	{
		var group=groupIn;
		var t=0;
		var startTop=group.top;
		var l=group.left
		
		document.getElementById(group.id).style.top=startTop+"px";
		document.getElementById(group.id).style.left=l+"px";
				
    for (var i=0;i<group.child.length;i++)
			{
				t=startTop+(i+1)*(-24)*v;
						
				document.getElementById(group.child[i].id).style.top=t+"px";
				document.getElementById(group.child[i].id).style.left=l+"px";
				document.getElementById(group.child[i].id).style.backgroundColor=childBackground;
				document.getElementById(group.child[i].id).style.color=colourOff;
			}		
	}


//create new menu at higher level
function spawn(groupIn)
  {
  	if (!groupIn.up)
  		{
	  		toggle(groupIn.parent);
	  	}
	  toggle(groupIn);
	  groupIn.up=!groupIn.up
		//alert("\t menuUp= "+menuUp+"\t mname= "+mname.id+"\t "+groupIn.id+".up= "+groupIn.up);
	}

// toggle first level menus 
function toggle(groupIn)
  {
	  	
	  if (!menuUp)
		  {
				mname=groupIn;
				
		    showMenu(groupIn);
			}
		else
		  {	
		  
		    hideMenu(mname);
				if (mname!=groupIn)
				  {	  
				    mname=groupIn;
				   
		        showMenu(groupIn);
					}
		  }
		
	}

//select menu and make visible
function showMenu(groupIn)
  {
    menuS= "<div class=\"menu\" id=\""+groupIn.id+"\" onmouseover=\"over('"+groupIn.id+"')\" onmouseout=\"off('"+groupIn.id+"')\" onclick=\""+groupIn.url+"\">"+groupIn.label+"</div>";
		   
		for (var i=0;i<groupIn.child.length;i++)
		  {
 				menuS += "<div class=\"menuA\" id=\""+groupIn.child[i].id+"\" onmouseover=\"over('"+groupIn.child[i].id+"')\" onmouseout=\"off('"+groupIn.child[i].id+"')\" onclick=\""+groupIn.child[i].url+"\">"+groupIn.child[i].label+"</div>";
 			}
 								
		document.getElementById("menu"+groupIn.position).innerHTML = menuS;
		menuUp=true;
		menuDirection(groupIn);
	}

//Minimise menu
function hideMenu(groupIn)
  {
  	if (groupIn.position == "F")
  		{
				document.getElementById("menuF").innerHTML = "";
			}
		else
			{
				document.getElementById("menu"+groupIn.position).innerHTML = "<div class=\"menu\" id=\""+groupIn.id+"\" onmouseover=\"over('"+groupIn.id+"')\" onmouseout=\"off('"+groupIn.id+"')\" onclick=\""+groupIn.url+"\">"+groupIn.label+"</div>";
				document.getElementById(groupIn.id).style.top=groupIn.top+"px";
   			document.getElementById(groupIn.id).style.left=groupIn.left+"px";
			}
		menuUp=false;
	}


//associate element objects as children  with groupIn as parent
function makeChildren(groupIn)
	{
		for (var i=0;i<groupIn.child.length;i++)
			{
				groupIn.child[i].parent=groupIn;
			}
	}

//mouse over highlighting
function over(name)
  {
  	window.document.getElementById(name).style.color="#ff8000";
    window.document.getElementById(name).style.cursor="pointer";
  }

function off(name)
  {
    window.document.getElementById(name).style.color=colourOff;
    window.document.getElementById(name).style.cursor="default";
	}

// open up a new window and load url into it
function getPage(urlIn)
		    	{       
		    		var url=urlIn   
		        w=window.open(url,"newWindow");
						w.moveTo(100,20);
						w.resizeTo(1000,700);
						w.focus();
					}