//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).
var preStr="";
//set child button colours - note: main button colours set in style sheet menu.css

var childBackground="#0047eb"; 	// child button background colour
var childColourOn="#ff0000";
var childColourOff="#ffff00";
var colourOn="#0000ff"
var colourOff="#ffffff"; 			 // main menu button colour when off - need to set in menu.css as well
var schoolStr="";
var remote="";

// 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='"+preStr+"'";		// 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=140;
		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();
      var pattern=/www/;
      var tempStr="";
      remote=locStr.search(pattern); //decide whether local or on internet
      var count=0;
			var k =0;
			var depth;
			for (k=0;k<locStr.length;k++)
				{
				
				if (locStr.charAt(k)=='/')
          {
            count++;
          }
        if (count == 3)
          {
            tempStr+=  locStr.charAt(k)
          }
				}

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

    var preStr="";
    while (depth > 0)
      {
        preStr+="../";
        depth--;
      }
     // alert(locStr);
   // alert(preStr);
    schoolStr=tempStr.slice(1); // not used
    

//////////////////////////////////////////////////////////////////
// declare menu element objects

// second level menu objects


    
    
    //Lunch menus
    
    summerMenuE=new MenuElement(0,0,"summerMenu","Summer Menu");
    summerMenuE.url="getPage("+preStr+"'documents/lunchMenuSummer.pdf')";
    
    winterMenuE=new MenuElement(0,0,"winterMenu","Winter Menu");
    winterMenuE.url="getPage("+preStr+"'documents/lunchMenuWinter.pdf')";
    
    
    //Outdoor Learning
    
    
    winterE=new MenuElement(0,0,"winter","Winter");
    winterE.url="window.location='"+preStr+"outDoorLearning/winter/index.html'";


    //galleriesE
    
    mowbrayGalleryE=new MenuElement(0,0,"mowbrayGallery","Mowbray Gallery");
    mowbrayGalleryE.url="window.location='"+preStr+"mowbrayGallery.html'";
    
    
    //clubs

    cookeryE=new MenuElement(0,0,"cookery","Cookery");
    cookeryE.url="window.location='"+preStr+"clubs/cookery/index.html'";

    ictE=new MenuElement(0,0,"ict","ICT");
    ictE.url="window.location='"+preStr+"clubs/ict/index.html'";
		
		footballE=new MenuElement(0,0,"football","Football");
		footballE.url="window.location='"+preStr+"clubs/football/index.html'";

    //music

    guitarsE=new MenuElement(0,0,"guitars","Guitar Group");
    guitarsE.url="window.location='"+preStr+"music/guitars/index.html'";
    
    winterSolsticeE=new MenuElement(0,0,"winterSolstice","Winter Solstice");
    winterSolsticeE.url="window.location='"+preStr+"music/winterSolstice/index.html'";
    
    christmasE=new MenuElement(0,0,"christmas","Music at Christmas");
    christmasE.url="window.location='"+preStr+"music/christmas/index.html'";

    //healthy school

    yogaE=new MenuElement(0,0,"yoga","Yoga");
    yogaE.url="window.location='"+preStr+"healthySchool/yoga/index.html'";

    llamaE=new MenuElement(0,0,"llama","Happy Llama");
    llamaE.url="window.location='"+preStr+"healthySchool/llama/index.html'";

// first level menu objects
	//info
    aimsE=new MenuElement(0,0,"aims","School Aims");
    aimsE.url="window.location='"+preStr+"aims.html'";

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

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

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

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

    formsE=new MenuElement(floatMenuTop,floatMenuLeft,"forms","Useful Forms");
    formsE.url="window.location='"+preStr+"forms.html'";


		policiesE=new MenuElement(floatMenuTop,floatMenuLeft,"policies","School Policies");
		policiesE.url="window.location='"+preStr+"policies.html'";
    
    parentsE=new MenuElement(floatMenuTop,floatMenuLeft,"parents","Parents");
    parentsE.url="window.location='"+preStr+"parents.html'";

    prospectusE=new MenuElement(floatMenuTop,floatMenuLeft,"prospectus","Prospectus");
    prospectusE.url="window.location='"+preStr+"prospectus.html'";
    
    
   

		ofstedE=new MenuElement(0,0,"ofsted","Ofsted");
    ofstedE.url="getPage("+preStr+"'ofsted/report2009.pdf')";

    
    equalityE=new MenuElement(0,0,"equality","Equality");
    equalityE.url="getPage('"+preStr+"documents/equality.pdf')";


    
    
    lunchMenuE=new MenuElement(floatMenuTop,floatMenuLeft,"lunchMenu","Lunch Menu");
    lunchMenuE.position="F";
    lunchMenuE.url="spawn(lunchMenuE)";
    lunchMenuE.child=new Array(summerMenuE,winterMenuE);
    makeChildren(lunchMenuE);
	//news
/*
		diaryE=new MenuElement(0,0,"diaryE","Diary");
		//diaryE.url="window.location='http://www.diary.firstschool.org.uk/cgi-bin/diary.pl?school=Mowbray'";
		diaryE.url="window.location='http://www.mowbray.firstschool.org.uk/cgi-bin/diary/diary.pl?school=Mowbray'";
*/


    topicsE=new MenuElement(0,0,"topics","Topic Information");
    topicsE.url="window.location='"+preStr+"topics.html'";


    lettersE=new MenuElement(0,0,"letters","Letters Home");
		lettersE.url="window.location='"+preStr+"letters.html'";


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

    anEyeOnGuidepostE=new MenuElement(0,0,"anEyeOnGuidepost","Our Local Area");
    anEyeOnGuidepostE.url="window.location='"+preStr+"anEyeOnGuidepost/index.html'";
    
    
    fsOverviewE=new MenuElement(0,0,"fsOverview","Foundation Overview");
    fsOverviewE.url="getPage('"+preStr+"foundation/overview.pdf')";



	//schoolLifeE


		councilE=new MenuElement(0,0,"council","School Council");
		councilE.url="window.location='"+preStr+"soon.html'";
    
    frenchE=new MenuElement(0,0,"french","French");
    frenchE.url="window.location='"+preStr+"french/index.html'";
    
		mowbrayMatesE=new MenuElement(0,0,"mowbrayMates","Mowbray Mates");
		mowbrayMatesE.url="window.location='"+preStr+"mates/index.html'";

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

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

		galleriesE=new MenuElement(0,0,"galleries","Galleries");
		galleriesE.url="window.location='"+preStr+"galleries.html'";
    
    galleriesE=new MenuElement(floatMenuTop,floatMenuLeft,"galleries","Galleries");
    galleriesE.position="F";
    galleriesE.url="spawn(galleriesE)";
    galleriesE.child=new Array(mowbrayGalleryE);
    makeChildren(galleriesE);


    clubsE=new MenuElement(floatMenuTop,floatMenuLeft,"clubs","Clubs");
    clubsE.position="F";
    clubsE.url="spawn(clubsE)";
    clubsE.child=new Array(cookeryE,footballE,ictE);
    makeChildren(clubsE);

    musicE=new MenuElement(floatMenuTop,floatMenuLeft,"music","Music");
    musicE.position="F";
    musicE.url="spawn(musicE)";
    musicE.child=new Array(guitarsE,christmasE,winterSolsticeE);
    makeChildren(musicE);
    
    outDoorLearningE=new MenuElement(floatMenuTop,floatMenuLeft,"outDoorLearning","Outdoor Learning");
    outDoorLearningE.position="F";
    outDoorLearningE.url="spawn(outDoorLearningE)";
    outDoorLearningE.child=new Array(winterE);
    makeChildren(outDoorLearningE);
    
    healthySchoolE=new MenuElement(floatMenuTop,floatMenuLeft,"healthySchool","Healthy School");
    healthySchoolE.position="F";
    healthySchoolE.url="spawn(healthySchoolE)";
    healthySchoolE.child=new Array(llamaE,yogaE);
    makeChildren(healthySchoolE);


		// Classes


		//Science

    tiddlywinksE=new MenuElement(0,0,"tiddlywinks","Tiddlywinks");
    tiddlywinksE.url="window.location='"+preStr+"projects/tiddlywinks/index.html'";
    
    homeworkE=new MenuElement(0,0,"homework","Homework");
    homeworkE.url="window.location='"+preStr+"projects/homework/index.html'";
    
    hinduismE=new MenuElement(0,0,"hinduism","Hinduism");
    hinduismE.url="window.location='"+preStr+"projects/hinduism/index.html'";
    
    authorVisitE=new MenuElement(0,0,"authorVisit","Author Visit");
    authorVisitE.url="window.location='"+preStr+"projects/authorVisit/index.html'";
    
    scienceE=new MenuElement(0,0,"science","Science");
    scienceE.url="window.location='"+preStr+"projects/science/science.html'";

    gardeningE=new MenuElement(0,0,"gardening","Gardening");
    gardeningE.url="window.location='"+preStr+"projects/gardening/index.html'";

    africaE=new MenuElement(0,0,"africa","Visiting Uganda");
    africaE.url="window.location='"+preStr+"africa/index.html'";


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

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

		infoE=new MenuElement(2,132,"info","Information");
		infoE.position=1;
		infoE.url="toggle(infoE)";
		infoE.child=new Array(aimsE,contactE,equalityE,governorsE,holidaysE,lunchMenuE,ofstedE,parentsE,prospectusE,staffE,formsE);
		makeChildren(infoE);

		newsE=new MenuElement(2,261,"news","News");
		newsE.position=2;
		newsE.url="toggle(newsE)";
		newsE.child=new Array(anEyeOnGuidepostE,fsOverviewE,topicsE,lettersE,newsletterE);
		makeChildren(newsE);

		schoolLifeE=new MenuElement(2,390,"schoolLife","School Life");
		schoolLifeE.position=3;
		schoolLifeE.url="toggle(schoolLifeE)";
		schoolLifeE.child=new Array(clubsE,frenchE,gamesE,galleriesE,healthySchoolE,mowbrayMatesE,musicE,outDoorLearningE,councilE,tourE);
		makeChildren(schoolLifeE);

		classesE=new MenuElement(2,519,"classes","Our Classes");
		classesE.position=4;
		classesE.url="window.location='"+preStr+"classes.html'";


    projectsE=new MenuElement(2,648,"projects","Projects");
    projectsE.position=5;
    projectsE.url="toggle(projectsE)";
    projectsE.child=new Array(authorVisitE,gardeningE,hinduismE,homeworkE,scienceE,tiddlywinksE,africaE);
    makeChildren(projectsE);

    howToHelpE=new MenuElement(2,777,"howToHelp","Helping At Home");
    howToHelpE.position=6;
    howToHelpE.url="window.location='"+preStr+"howToHelp.html'";



//specify what elements in main menu bar
		mainBar = new Array(homeE,infoE,newsE,schoolLifeE,classesE,projectsE,howToHelpE);
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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)*(-30)*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=childColourOff;
			}
	}


//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";
// check for submenu item and set different colour value.

    var listNames="home#info#news#schoolLife#classes#projects#howToHelp"; // must use string for listNames for proper use of indexOf
 //   alert(listNames.indexOf(name));
    if (listNames.indexOf(name) == -1)
      {
        window.document.getElementById(name).style.color=childColourOn;
      }
  }

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

    // check for submenu item and set different colour value.

    var listNames="home#info#news#schoolLife#classes#projects#howToHelp";

    if (listNames.indexOf(name) == -1)
      {
        window.document.getElementById(name).style.color=childColourOff;
      }
	}

// 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,600);
      w.focus();
    }
