//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="#3d57c7"; 	// child button background colour
var colourOn="#ffffff"					// all buttons colour when on - need to set in menu.css as well
var colourOff="#ffff00"; 			 // 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()
	{

 // get location of calling page and set depth parameter for relative addresses
 //depending on whether on internet or local server
      var locStr=document.location.toString();
      //alert(locStr);
      var pattern=/www/;
      var tempStr="";
      var remote=locStr.search(pattern);

      var count=0;
			var k =0;
			var depth;

			for (k=0;k<locStr.length;k++)
				{
				tempStr+=	locStr.charAt(k)
				if (locStr.charAt(k)=='/')
          {
            count++;
          }
				}

      if (remote == -1)
        {
          depth=count-4;
        }
      else
        {
          depth=count-3;
        }


    var preStr="";
    while (depth > 0)
      {
        preStr+="../";
        depth--;
      }
    //alert(preStr);


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


// declare menu element objects

// third level menu
    /*holidays

    hols20078E=new MenuElement(0,0,"hols20078","2007-8");
		hols20078E.url="getPage('"+preStr+"holidays/hols20078.pdf')";
*/



// second level menu objects



		//general

		allergiesE=new MenuElement(0,0,"allergies","Allergies");
		allergiesE.url="window.location='"+preStr+"blank.html'";

		carparkE=new MenuElement(0,0,"carpark","Car Parking");
		carparkE.url="window.location='"+preStr+"carpark.html'";

		dinnerMoneyE=new MenuElement(0,0,"dinnerMoney","Dinner Money");
		dinnerMoneyE.url="window.location='"+preStr+"meals.html'";


    holidaysE=new MenuElement(0,0,"holidays","Holidays");
		holidaysE.url="window.location='"+preStr+"hols.html'";

    homeworkE=new MenuElement(0,0,"homework","Homework");
    homeworkE.url="window.location='"+preStr+"homework.html'";

		latenessE=new MenuElement(0,0,"lateness","Lateness/Absence");
		latenessE.url="window.location='"+preStr+"lateness.html'";

		medicinesE=new MenuElement(0,0,"medicines","Medicines");
		medicinesE.url="window.location='"+preStr+"medicines.html'";

		timesE=new MenuElement(0,0,"times","School Times");
		timesE.url="window.location='"+preStr+"times.html'";

		peE=new MenuElement(0,0,"pe","PE");
		peE.url="window.location='"+preStr+"pe.html'";

		uniformE=new MenuElement(0,0,"uniform","School Uniform");
		uniformE.url="window.location='"+preStr+"uniform.html'";

    miscE=new MenuElement(0,0,"misc","Miscellaneous");
    miscE.url="window.location='"+preStr+"misc.html'";



		//links

		subject1E=new MenuElement(0,0,"subject1","Subject 1");
		subject1E.url="window.location='"+preStr+"blank.html'";

		subject2E=new MenuElement(0,0,"subject2","Subject 2");
		subject2E.url="window.location='"+preStr+"blank.html'";

		//childrensSection
		foundationE=new MenuElement(0,0,"foundation","Foundation Stage");
		foundationE.url="window.location='"+preStr+"foundationGallery.html'";

		ks1E=new MenuElement(0,0,"ks1","Keystage 1");
		ks1E.url="window.location='"+preStr+"ks1Gallery.html'";

		ks2E=new MenuElement(0,0,"ks2","Keystage 2");
		ks2E.url="window.location='"+preStr+"ks2Gallery.html'";

		wholeE=new MenuElement(0,0,"whole","Whole School");
		wholeE.url="window.location='"+preStr+"wholeSchoolGallery.html'";


// first level menu objects
	//info

		admissionsE=new MenuElement(0,0,"admissionsE","Admissions");
		admissionsE.url="window.location='"+preStr+"admissions.html'";

		pyramidE=new MenuElement(0,0,"pyramid","Gosforth Pyramid");
		pyramidE.url="window.location='"+preStr+"blank.html'";


		governorsE=new MenuElement(floatMenuTop,floatMenuLeft,"governors","Governors");
		governorsE.url="window.location='"+preStr+"governors.html'";

		generalE=new MenuElement(floatMenuTop,floatMenuLeft,"general","General");
		generalE.position="F";
		generalE.url="spawn(generalE)";
		generalE.child=new Array(allergiesE,carparkE,dinnerMoneyE,holidaysE,homeworkE,latenessE,medicinesE,peE,timesE,uniformE,miscE);
		makeChildren(generalE);

		ofstedE=new MenuElement(0,0,"ofstedE","Ofsted");
		ofstedE.url="window.location='"+preStr+"blank.html'";

		missionE=new MenuElement(0,0,"mission","Mission");
		missionE.url="window.location='"+preStr+"mission.html'";

		policiesE=new MenuElement(0,0,"policies","Key Policies");
		policiesE.url="window.location='"+preStr+"keyPolicies.html'";

		bookletE=new MenuElement(floatMenuTop,floatMenuLeft,"booklet","School Booklet");
		bookletE.url="window.location='"+preStr+"booklet.html'";

		staffE=new MenuElement(floatMenuTop,floatMenuLeft,"staff","Staff");
		staffE.url="window.location='"+preStr+"staff.html'";

	//news
		curriculumE=new MenuElement(0,0,"curriculum","Curriculum Topics");
		curriculumE.url="window.location='"+preStr+"curriculum.html'";

		diaryE=new MenuElement(0,0,"diaryE","Diary");
		diaryE.url="window.location='http://www.diary.firstschool.org.uk/cgi-bin/diary.pl?school=Gosforth Park'";

		goldenE=new MenuElement(0,0,"goldenE","Golden Book");
		goldenE.url="window.location='"+preStr+"goldenBook.html'";

		newsletterE=new MenuElement(0,0,"newsletter","Newsletters");
		newsletterE.url="window.location='"+preStr+"newsletters.html'";

		ptaE=new MenuElement(0,0,"pta","PTA");
		ptaE.url="window.location='"+preStr+"pta.html'";

		recentE=new MenuElement(0,0,"recent","Recent Events");
		recentE.url="window.location='"+preStr+"blank.html'";


	//contactsE

		childrensServicesE=new MenuElement(0,0,"childrensServices","Childrens' Services");
		childrensServicesE.url="window.location='"+preStr+"blank.html'";

		edWelfareE=new MenuElement(0,0,"edWelfare","Educational Welfare");
		edWelfareE.url="window.location='"+preStr+"blank.html'";

		healthE=new MenuElement(0,0,"health","Health");
		healthE.url="window.location='"+preStr+"blank.html'";

		parentsSupportE=new MenuElement(0,0,"parentsSupport","Parents' Support");
		parentsSupportE.url="window.location='"+preStr+"psa.html'";

		schoolE=new MenuElement(0,0,"schoolE","Contact School");
		schoolE.url="window.location='"+preStr+"contact.html'";

	//kidsE
		friendsE=new MenuElement(0,0,"friends","Playground Friends");
		friendsE.url="window.location='"+preStr+"blank.html'";

		gamesE=new MenuElement(0,0,"games","Fun and Games");
		gamesE.url="window.location='"+preStr+"blank.html'";

		competitionsE=new MenuElement(0,0,"competitions","Competitions");
		competitionsE.url="window.location='"+preStr+"blank.html'";

		clubsE=new MenuElement(0,0,"clubs","School Clubs");
		clubsE.url="window.location='"+preStr+"clubs.html'";

		linksE=new MenuElement(floatMenuTop,floatMenuLeft,"links","Links");
		linksE.url="window.location='"+preStr+"links.html'";

		/*
		linksE.position="F";
		linksE.url="spawn(linksE)";
		linksE.child=new Array(subject1E,subject2E);
		makeChildren(linksE);
		*/

	//galleries
		awardsE=new MenuElement(0,0,"awards","Awards");
		awardsE.url="window.location='"+preStr+"awards.html'";

		childrensSectionE=new MenuElement(floatMenuTop,floatMenuLeft,"childrensSection","Childrens Section");
		childrensSectionE.position="F";
		childrensSectionE.url="spawn(childrensSectionE)";
		childrensSectionE.child=new Array(foundationE,ks1E,ks2E,wholeE);
		makeChildren(childrensSectionE);

		tourE=new MenuElement(0,0,"tour","Tour Our School");
		tourE.url="window.location='"+preStr+"tour.html'";



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

// base level menu objects
		homeE=new MenuElement(0,0,"home","Home");
		homeE.url="window.location='"+preStr+"index.html'";

    parentsGuideE=new MenuElement(0,142,"parentsGuide","Parents Guide");
		parentsGuideE.url="window.location='"+preStr+"parentsGuide/index.html'";
    parentsGuideE.position=1;

		infoE=new MenuElement(0,284,"info","Information");
		infoE.position=2;
		infoE.url="toggle(infoE)";
		infoE.child=new Array(admissionsE,generalE,pyramidE,governorsE,policiesE,missionE,ofstedE,bookletE,staffE);
		makeChildren(infoE);


		newsE=new MenuElement(0,426,"news","News");
		newsE.position=3;
		newsE.url="toggle(newsE)";
		newsE.child=new Array(curriculumE,diaryE,goldenE,newsletterE,ptaE,recentE);
		makeChildren(newsE);

		contactsE=new MenuElement(0,568,"contacts","Contacts");
		contactsE.position=4;
		contactsE.url="toggle(contactsE)";
		contactsE.child=new Array(schoolE,childrensServicesE,healthE); //,parentsSupportE);

		kidsE=new MenuElement(0,710,"kids","Kids' Zone");
		kidsE.position=5;
		kidsE.url="toggle(kidsE)";
		kidsE.child=new Array(friendsE,gamesE,competitionsE,clubsE,linksE);
		makeChildren(kidsE);

		galleryE=new MenuElement(0,852,"gallery","Galleries");
		galleryE.position=6;
		galleryE.url="toggle(galleryE)";
		galleryE.child=new Array(awardsE,childrensSectionE,tourE);
		makeChildren(galleryE);

//specify what elements in main menu bar
		mainBar = new Array(homeE,parentsGuideE,infoE,newsE,contactsE,kidsE,galleryE);
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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>";
 // 	linkS += "<div class=\"menu\" id=\""+mainBar[1].id+"\" onmouseover=\"over('"+mainBar[1].id+"')\" onmouseout=\"off('"+mainBar[1].id+"')\" onclick=\""+mainBar[1].url+"\">"+mainBar[1].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=colourOn;
    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(800,1000);
						w.focus();
					}