//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="";


// 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+""+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=450;
		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="";
      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;
        }
//alert(depth);

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


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



// declare menu element objects
		//level 2

		// reports

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

		churchE=new MenuElement(0,0,"church","Church");
		churchE.url="window.location='"+preStr+"inspections.html'";

		//galleries
		gallery2008E=new MenuElement(0,0,"gallery2008","2007 - 08");
		gallery2008E.url="window.location='"+preStr+"/cgi-bin/gallery/gallery2008.pl'";

		gallery2007E=new MenuElement(0,0,"gallery2007","2006 - 07");
		gallery2007E.url="window.location='"+preStr+"/cgi-bin/gallery/gallery.pl'";


		//classes
		earlyYearsE=new MenuElement(0,0,"earlyYears","Early Years");
		earlyYearsE.url="window.location='"+preStr+"earlyYears.html'";

		class1E=new MenuElement(0,0,"class1","Class 1");
		class1E.url="window.location='"+preStr+"class1.html'";

		class2E=new MenuElement(0,0,"class2","Class 2");
		class2E.url="window.location='"+preStr+"class2.html'";

////////////////////////////////
// second level menu objects

	//info
		brochureE=new MenuElement(0,0,"brochure","Brochure");
		brochureE.url="window.location='"+preStr+"brochure.html'";

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

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

		extendedSchoolsE=new MenuElement(0,0,"extendedSchools","Extended Schools");
		extendedSchoolsE.url="window.location='"+preStr+"extendedSchools.html'";

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


    inspectionsE=new MenuElement(floatMenuTop,floatMenuLeft,"inspections","Inspections Reports");
		inspectionsE.url="spawn(inspectionsE)";
		inspectionsE.position="F";
		inspectionsE.child=new Array(ofstedE,churchE);
		makeChildren(inspectionsE);

	//	inspectionsE=new MenuElement(0,0,"inspections","Inspections Reports");
	//	inspectionsE.url="window.location='"+preStr+"ofsted.html'";

		lunchesE=new MenuElement(0,0,"lunches","Lunches");
		lunchesE.url="window.location='"+preStr+"lunches.html'";

		ethosE=new MenuElement(0,0,"ethos","Ethos Statement");
		ethosE.url="window.location='"+preStr+"ethos.html'";

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

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

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

		eventsE=new MenuElement(0,0,"events","News and Events");
		eventsE.url="window.location='"+preStr+"events.html'";

		holsE=new MenuElement(0,0,"hols","Holiday Dates");
		holsE.url="window.location='"+preStr+"hols.html'";

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

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


	//school life
		classesE=new MenuElement(floatMenuTop,floatMenuLeft,"classes","Classes");
		classesE.url="spawn(classesE)";
		classesE.position="F";
		classesE.child=new Array(earlyYearsE,class1E,class2E);
		makeChildren(classesE);

		galleryE=new MenuElement(floatMenuTop,floatMenuLeft,"gallery","Galleries");
		galleryE.url="spawn(galleryE)";
		galleryE.position="F";
		galleryE.child=new Array(gallery2008E,gallery2007E);
		makeChildren(galleryE);

		goodCausesE=new MenuElement(0,0,"goodCauses","Good Causes");
		goodCausesE.url="window.location='"+preStr+"goodCauses.html'";

		meetTheStaffE=new MenuElement(0,0,"meetTheStaff","Meet The Staff");
		meetTheStaffE.url="window.location='"+preStr+"/cgi-bin/gallery/gallery2008.pl?albumString=Meet%20The%20Staff%239'";

		musicE=new MenuElement(0,0,"music","Music");
		musicE.url="window.location='"+preStr+"music.html'";

		playtimesE=new MenuElement(0,0,"playtimes","Playtimes");
		playtimesE.url="window.location='"+preStr+"playtimes.html'";

		libraryE=new MenuElement(0,0,"library","Library");
		libraryE.url="window.location='"+preStr+"library.html'";

		poemE=new MenuElement(0,0,"poem","Our Poem and Song");
		poemE.url="window.location='"+preStr+"poem.html'";

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

		schoolCouncilE=new MenuElement(0,0,"schoolCouncil","School Council");
		schoolCouncilE.url="window.location='"+preStr+"council.html'";

		stBartsE=new MenuElement(0,0,"stBarts","St Bartholomew's");
		stBartsE.url="window.location='"+preStr+"stBarts.html'";

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

		villageE=new MenuElement(0,0,"village","Whittingham");
		villageE.url="window.location='"+preStr+"village/index.html'";

		worshipE=new MenuElement(0,0,"worship","Worship");
		worshipE.url="window.location='"+preStr+"worship.html'";



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

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

		jokesE=new MenuElement(0,0,"jokes","Jokes");
		jokesE.url="window.location='"+preStr+"jokes.html'";

		childrenE=new MenuElement(0,0,"children","Childrens' Pages");
		childrenE.url="window.location='"+preStr+"childrensPages.html'";



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

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

		infoE=new MenuElement(0,150,"info","Information");
		infoE.position=1;
		infoE.url="toggle(infoE)";
		infoE.child=new Array(brochureE,contactE,curriculumE,extendedSchoolsE,governorsE,inspectionsE,lunchesE,ethosE,staffE,uniformE);
		makeChildren(infoE);

		newsE=new MenuElement(0,300,"news","News");
		newsE.position=2;
		newsE.url="toggle(newsE)";
		newsE.child=new Array(lettersE,diaryE,eventsE,holsE,newsletterE);
		makeChildren(newsE);

		lifeE=new MenuElement(0,450,"life","School Life");
		lifeE.position=3;
		lifeE.url="toggle(lifeE)";
		lifeE.child=new Array(classesE,galleryE,goodCausesE,libraryE,meetTheStaffE,musicE,playtimesE,poemE,ptaE,schoolCouncilE,stBartsE,tourE,villageE,worshipE);
		makeChildren(lifeE)

		kidsE=new MenuElement(0,600,"kids","Children's Zone");
		kidsE.position=4;
		kidsE.url="toggle(kidsE)";
		kidsE.child=new Array(clubsE,gamesE,jokesE,childrenE);
		makeChildren(kidsE)

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

	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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="#69dc92";
				document.getElementById(group.child[i].id).style.color="#000080";
			}
	}


//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="#ff0000";
    window.document.getElementById(name).style.cursor="pointer";
  }

function off(name)
  {
    window.document.getElementById(name).style.color="#0000b0";
    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,600);
						w.focus();
					}