/*
Homepage scripts
*/
function resizeColofonView(){
	//resized the last viewport so that it fits the window automatically
	var intWindowWidth=objWindowSize.x;
	var intDivWidth=intWindowWidth-intColofonWidth+10;
	try{
		objPageElements.eldivcolofon.setStyle('width', intDivWidth);
	}catch(err){};
}

function setupOrderForm(){
	var elForm=$('orderKrooning');
	var elSubmitResults=$('orderResult');
	Locale.use("nl-NL");
	new Form.Validator.Inline(elForm);

	new Form.Request(elForm, elSubmitResults, {
		requestOptions: {
			spinnerOptions: {
    		message: 'Sending the form...'
			}
		}
	});

	return true;

}

function loadHomePage(){
	//the navigation links on the homepage
	var strUrl = "home.php";

	var objOptions = {
		url: strUrl,
		update: objPageElements.eldivwrapperhome,
		onFailure: function(obj){
			//ajax call failed
			alert('failed to load homepage content');
		}
	};
	var objRequestHtml=new Request.HTML(objOptions).get();
	//objPageElements.elbuttonkrooning.setProperty('src','images/system/krooning_G.png');
}


function setupHomePage(){
	/*
	Sets up the logic for the home page
	*/
	//retrieve the homepage specific elements
	objPageElements.eldivcolofon=objPageElements.eldivwrapperhome.getElement('div#div_scale');
	var arrNavigationCbx=$$('td#view1 table.home_menu-horizontal img.navcbx');
	if(arrNavigationCbx.length>0){
		objPageElements.arrimgnavcheckbx=arrNavigationCbx;
	}


	var bolDone=setupOrderForm();

	//set the last column in the homepage to a specific width so that we can scroll nicely
	var elColofon=objPageElements.eldivwrapperhome.getElement('table.table_standard tbody tr td#view7 table.table_standard');
	var objSizeColofon=elColofon.getSize();

	intColofonWidth=objSizeColofon.x;
	//alert(intColofonWidth);

	resizeColofonView();

	var arrNavigationLinks=objPageElements.eldivwrapperhome.getElements('a.navlink');
	arrNavigationLinks.addEvents({
		click: function(event){
			//stop the link
			event.stop();

			//initiate the scrolling effect
			var strIdToScrollTo=this.get('href').replace(/(.*)(#|\/)(.*)/, '$3');
			elToScrollTo=$(strIdToScrollTo);
			fxHomeScroll.toElement(elToScrollTo, 'x');
		},
		mouseenter: function(){
			//image swap effect
			if(objPageElements.arrimgnavcheckbx!=null){
				var i=this.get('href').replace(/(.*)(#|\/)(view)(\d+)/, '$4').toInt();
				if(i>1){
					if(objPageElements.arrimgnavcheckbx.length >= i){
						objPageElements.arrimgnavcheckbx[i-1].setProperty('src','images/system/check.png')
					}
				}
			}
		},
		mouseleave: function(){
			//image swap effect
			if(objPageElements.arrimgnavcheckbx!=null){
				var i=this.get('href').replace(/(.*)(#|\/)(view)(\d+)/, '$4').toInt();
				if(i>1){
					if(objPageElements.arrimgnavcheckbx.length >= i){
						objPageElements.arrimgnavcheckbx[i-1].setProperty('src','images/system/uncheck.png')
					}
				}
			}
		}
	});
}

/*
Student data pages
*/
function toggleViewHomeStudent(){
	//shows home of student view
	if(objPageState.homevisible){
		//objPageElements.eldivwrapperhome.setStyle('visibility', 'hidden');
		//objPageElements.eldivwrapperstudent.setStyle('visibility', 'visible');
		objPageElements.eldivwrapperhome.dissolve();
		objPageElements.eldivwrapperstudent.reveal();

		objPageState.homevisible=false;
	}else{
		//objPageElements.eldivwrapperhome.setStyle('visibility', 'visible');
		//objPageElements.eldivwrapperstudent.setStyle('visibility', 'hidden');
		objPageElements.eldivwrapperhome.reveal();
		objPageElements.eldivwrapperstudent.dissolve();

		objPageState.homevisible=true;

		if(!bolHomePageShown){
			setupHomePage();
			bolHomePageShown=true;
		}
	}
}

function loadStudentDataHtml(strStudentId, intDivIndex){
	/*
	Retrieves new student data using ajax calls
	*/
	var arrContentDiv=$$('div.student_content_wrapper div#SlideItMoo_vertical_inner div.SlideItMoo_element');

	var strUrl = "student.php";

	//the data to sent to the remote utility
	var objData={
		id: strStudentId
	};

	//variant b - using request.html  and the update property object
	var objOptions = {
		url: strUrl,
		update: arrContentDiv[intDivIndex],
		onFailure: function(obj) {
			//ajax call failed
			alert('failed to load content');
		}
	};
	var objRequestHtml=new Request.HTML(objOptions).get(objData);
}

function getStudentIdPrevNext(strCurrentStudentId, strDirection){
	/*
	Looks into the student id array and finds the previous or next student id
	*/
	var strSudentIdReturn=null;

	//determine array to use to finde previous and next student (are we showing the filtered list)
	var arrStudentIdCurrent;
	if(objPageState.filteredmenu){
		//arrStudentIdCurrent=Array.clone(arrStudentIdFiltered);
		arrStudentIdCurrent=arrStudentIdFiltered;
	}else{
		//arrStudentIdCurrent=Array.clone(arrStudentId);
		arrStudentIdCurrent=arrStudentId;
	}
	//alert(objPageState.filteredmenu);
	//alert(arrStudentIdCurrent.join());

	var intArrayIndexCurrent=arrStudentIdCurrent.indexOf(strCurrentStudentId).toInt();

	if(strDirection=="prev"){
		if(intArrayIndexCurrent==0){
			strStudentIdReturn=arrStudentIdCurrent.getLast();
		}else{
			strStudentIdReturn=arrStudentIdCurrent[intArrayIndexCurrent-1];
		}
	}else{
		if(strCurrentStudentId==arrStudentIdCurrent.getLast()){
			strStudentIdReturn=arrStudentIdCurrent[0];
		}else{
			strStudentIdReturn=arrStudentIdCurrent[intArrayIndexCurrent+1];
		}
	}
	//alert(strStudentIdReturn);
	return strStudentIdReturn;
}

function loadInitialStudentHtml(){
	/*
	Loads the initial student data into the layers
	*/
	//update the level 2 menu
	markMenuElementActive(objPageState.idcurrent);
	return loadStudentHtml({id: objPageState.idcurrent, mode: 'initial'});
}



function loadStudentHtml(objArguments){
	//load the selected student content in the divs
	var intStudentId=objArguments.id;
	var strMode=objArguments.mode;

	//1) current student
	loadStudentDataHtml(intStudentId, 0);

	//2) previous student
	var intStudentIdPrev=getStudentIdPrevNext(intStudentId, 'prev');
	loadStudentDataHtml(intStudentIdPrev, 2)

	//3) next student
	var intStudentIdNext=getStudentIdPrevNext(intStudentId, 'next');
	loadStudentDataHtml(intStudentIdNext, 1);
	
	//resize the div to the current visible layer
	resizeStudentContentDiv.delay(200, null, 0);
	
	if(strMode=='initial')return true;
	if(strMode=='menu')objPageState.idcurrent=intStudentId;
}

function repositionVerticalNavigationElements(){
	//repositions the "up" and "down" layers so that they stay in focus not matter how the user has scrolled the layer
	var objScroll=objPageElements.eldivwrapperstudent.getScroll();
	var intScrollX=objScroll.x;
	var intWindowWidth=objWindowSize.x;

	//calculate new left position
	var intLeftPos=intScrollX+(intWindowWidth/2)-(intVerticalNavigationElementWidth/2);
	intLeftPos=intLeftPos.round();

	//if(!Browser.ie)objPageElements.eldivnavprev.unpin();
	objPageElements.eldivnavprev.setStyle('left', intLeftPos);
	//if(!Browser.ie)objPageElements.eldivnavprev.pin();

	//if(!Browser.ie)objPageElements.eldivnavnext.unpin();
	objPageElements.eldivnavnext.setStyle('left', intLeftPos);
	//if(!Browser.ie)objPageElements.eldivnavnext.pin();

}


/*
Search routines
*/
function studyAreaSearchStart(studyarea, strElementId){

	/*
	Search by studyarea
	*/
	//toggle the class in the panel
	var elSpan=$(strElementId);
	if(elSpan.hasClass('active')){
		elSpan.removeClass('active');
	}else{
		elSpan.addClass('active');
	}

	//clear the checkbox
	objPageElements.checkboxshowall.checked=false;

	//retrieve all the active elements
	var arrActiveSpans=objPageElements.elbody.getElements('div.content_wrapper div#menu_search div#menu_search_content table.table_standard div.search_layer div.search_study span.active');
	if(arrActiveSpans.length>0){
		//render the query
		var strQuery="";
		arrActiveSpans.each(function(elSpan, index){
			strQuery+=elSpan.get('text');
			if(index<arrActiveSpans.length-1)strQuery+="|";
		});

		//hide the "no results" message
		objPageElements.elbody.getElement('div#menu_level2_content div.no_results').setStyle('display', 'none');
		//alert(strQuery);
		var objData={
			q: strQuery
		};

		executeSearchRemote(objData);
	}else{
		objPageElements.arrnavdiv.setStyle('display', 'block');
	}
}

function textSearchStart(key){
	/*
	Plain text search
	*/
	if (key==""){
		alert("Geef een zoekterm in...");
		return;
	}else{
		var objData={
			k: key
		};

		executeSearchRemote(objData);
	}
}

function executeSearchRemote(objData){
	//performs an ajax call to retrieve the search results in JSON format
	var objOptions = {
		url: 'search_process.php',
		onFailure: function(obj) {
			//ajax call failed
			alert('failed to load search results');
		},
		onSuccess: function(objResult, strResult){
			//swicth to the student view if the search was executed from the home panel
			if(objPageState.homevisible){
				toggleViewHomeStudent();
			}

			//filter the students in the menu based on the results retrieved in JSON format from the server
			var arrStudentIdFound=objResult.arrstudentid;
			//alert(arrStudentIdFound.join());
			//update the global variable containing the array of filtered student id's
			arrStudentIdFiltered=Array.clone(arrStudentIdFound);

			//set the current id to the first element in the Array
			if(arrStudentIdFound.length>0){
				objPageState.idcurrent=arrStudentIdFound[0];
			}

			var bolDone=filterMenuLevel2(arrStudentIdFound);

			//load the new students in the interface
			if(arrStudentIdFound.length>0){
				//alert(objPageState.idcurrent);
				loadInitialStudentHtml();
			}
		}
	};
	var objRequestHtml=new Request.JSON(objOptions).get(objData);
}

function clearSearchForm(){
	/*
	Clears the user input from the search form
	*/
	//clear the search box
	$('searchkey').value="";

	//clear the studyarea list
	var arrActiveSpans=objPageElements.elbody.getElements('div.content_wrapper div#menu_search div#menu_search_content table.table_standard div.search_layer div.search_study span.active');
	arrActiveSpans.removeClass('active');
}

/*
Menu
*/
function markMenuElementActive(intStudentId){
	/*
	Marks an element in the level 2 menu as active
	*/
	//unselect the currently selected student
	var arrDivCurrent=objPageElements.elbody.getElements('div.content_wrapper div#menu_level2_wrapper div#menu_level2 div#menu_level2_content div.bg_students table.table_standard tr.menu_items td div.current');
	arrDivCurrent.removeClass('current');

	//select the current student
	var elDivCurrent=objPageElements.elbody.getElement('div.content_wrapper div#menu_level2_wrapper div#menu_level2 div#menu_level2_content div.bg_students table.table_standard tr.menu_items td div#s'+intStudentId);
 	elDivCurrent.addClass('current');

 	//scroll the div into position
	fxMenuLevel2Scroll.toElement(elDivCurrent);
}

function filterMenuLevel2(arrStudentIdFound){
	//"filters" the menu level 2 to only show the students that match the search query
	if(arrStudentIdFound.length>0){
		objPageElements.arrnavdiv.each(function(elDiv, intIndexDiv){
			var intStudentIdCurrent=elDiv.id.substring(1);
			var bolShow=false;
			if(arrStudentIdFound.contains(intStudentIdCurrent))bolShow=true;

			if(bolShow){
				//alert(intStudentIdCurrent);
				elDiv.setStyle('display', 'block');
			}else{
				elDiv.setStyle('display', 'none');
			}
		})
	}else{
		objPageElements.arrnavdiv.setStyle('display', 'none');
		objPageElements.elbody.getElement('div#menu_level2_content div.no_results').setStyle('display', 'block');
	}

	//update the global page state object
	objPageState.filteredmenu=true;

	return true;
}

function showFullMenu(elCbx){
	/*
	Fired when the checkbox on the search form is clicked
	*/
	if(elCbx.checked){
		clearSearchForm();
		removeFilterMenuLevel2();
	}
}

function removeFilterMenuLevel2(){
	objPageElements.arrnavdiv.setStyle('display', 'block');
	//update the global page state object
	objPageState.filteredmenu=false;
}

function resizeStudentContentDiv(intDivNr){
	//resizes the wrapper div of the student data to fit the width of the currently visible student content
	var arrContentDiv=objPageElements.eldivwrapperstudent.getElements('div.student_content_wrapper div#SlideItMoo_vertical_inner div.SlideItMoo_element');
	//determine the width of the layer that will become visible
	var elToMeasure=arrContentDiv[intDivNr].getElement('table');
	objPageElements.divslidemooinner.setStyle('width', elToMeasure.getSize().x+5);
}

/*
Effects
*/
function setupPageEffects(){
	/*
	Sets up all the Mootools effects required in this page
	*/
	//setup the scroller effect for the content inside the level 2 menu
	fxMenuLevel2Scroller = new Scroller(objPageElements.elmenulevel2, {
		area: 50
	});

	fxMenuLevel2Scroller.start();


	//setup the slide in-out effect for the menu level 2 layer
	fxMenuLevel2Slide = new Fx.Slide(objPageElements.elmenulevel2wrapper, {
		wrapper: objPageElements.elmenulevel2,
		mode: 'horizontal',
		onComplete: function(el){
			var intMarginLeft=el.getStyle('margin-left').toInt();
			if(intMarginLeft < -10){
				//menu is not visible
				objPageState.menulevel2open=false;

				//set the wrapper to visible (only needed on page load)
				el.setStyle('visibility', 'visible');
			}else{
				//menu is visible
				objPageState.menulevel2open=true;
			}
		}
	});

	//setup the slide function for the search form
	fxMenuSearchSlide = new Fx.Slide(objPageElements.elmenusearch, {
		wrapper: objPageElements.elmenusearchcontent,
		mode: 'horizontal',
		onComplete: function(el){
			var intMarginLeft=el.getStyle('margin-left').toInt();
			if(intMarginLeft < -10){
				//menu is not visible
				objPageState.menusearchopen=false;

				//set the wrapper to visible (only needed on page load)
				el.setStyle('visibility', 'visible');
			}else{
				//menu is visible
				objPageState.menusearchopen=true;
			}
		}
	});

	//setup the effect for sliding student information in and out
	fxSlideItMoo = new SlideItMoo({
		overallContainer: 'SlideItMoo_vertical_outer',
		elementScrolled: 'SlideItMoo_vertical_inner',
		thumbsContainer: 'SlideItMoo_vertical_items',
		itemsVisible:1,
		elemsSlide:1,
		duration:intSlideEffectDuration,
		itemsSelector: '.SlideItMoo_element',
		itemWidth: 800,
		itemHeight: 640,
		slideVertical: true,
		showControls:1,
		direction:-1,
		autoSlide:null,
		onChange: function(intDivIndexCurrent){
			//retrieve data from the global page state object
			var intDivIndexStored=objPageState.indexcurrent;
			var intStudentIdStored=objPageState.idcurrent;
			var intStudentIdNew=null;
			var intStudentIdToRetrieve=null;

			//determine if we moved to previous or next
			var strDirection="prev";
			if(intDivIndexCurrent==1 && intDivIndexStored==0)strDirection="next";
			if(intDivIndexCurrent==2 && intDivIndexStored==1)strDirection="next";
			if(intDivIndexCurrent==0 && intDivIndexStored==2)strDirection="next";
			//alert(strDirection);

			//determine where to load new data in
			if(strDirection=="prev"){
				//load new data into the next div - index 2 (the next student)
				intStudentIdNew=getStudentIdPrevNext(intStudentIdStored, 'prev');
				intStudentIdToRetrieve=getStudentIdPrevNext(intStudentIdNew, 'prev');
				loadStudentDataHtml.delay(intSlideEffectDuration+200, null, [intStudentIdToRetrieve, 2])

				//determine the width of the layer that will become visible
				resizeStudentContentDiv(0);
			}else{
				//load new data into the previous div - index 1 (the previous student)
				intStudentIdNew=getStudentIdPrevNext(intStudentIdStored, 'next');
				intStudentIdToRetrieve=getStudentIdPrevNext(intStudentIdNew, 'next');
				loadStudentDataHtml.delay(intSlideEffectDuration+200, null, [intStudentIdToRetrieve, 1])

				//determine the width of the layer that will become visible
				resizeStudentContentDiv(1);
			}

			fxStudentScroll.toLeft();

			//update the global page state object
			objPageState.idcurrent=intStudentIdNew;
			objPageState.indexcurrent=intDivIndexCurrent;

			//update the level 2 menu
			markMenuElementActive(objPageState.idcurrent);
		}
	})

	//the scrolling effect on the krooning/home layer
	if(Browser.isMobile){
		fxHomeScroll=new Fx.Scroll(window);
		fxStudentScroll=new Fx.Scroll(window, {duration: 0});
	}else{
		fxHomeScroll=new Fx.Scroll(objPageElements.eldivwrapperhome);
		fxStudentScroll=new Fx.Scroll(objPageElements.eldivwrapperstudent, {duration: 0});
	}



	//the scrolling effect on the menu items (to move active students into the viewing position)
	fxMenuLevel2Scroll=new Fx.Scroll(objPageElements.elmenulevel2, {offset: {x: 0, y: -100}});

	return true;
}

function setupElementEvents(){
	/*
	Setup the events on the various elements
	*/

	//events on "students" button
	objPageElements.elbuttonlevel2.addEvents({
		click: function(event){
			event.stop();

			if(objPageState.homevisible){
				//button toggles back to student view
				toggleViewHomeStudent();
			}else{
				//test if the menu is open
				if(objPageState.menulevel2open){
					var bolRemoveSearch=false;
					var bolClearFilter=false;
					var bolRemoveMenu=false;

					//1) search and menu are open but menu is not filtered
					//- remove search menu
					if(objPageState.menusearchopen && !objPageState.filteredmenu){
						bolRemoveSearch=true;
					}else{
						//2) search and menu are open and menu filtered
						//- remove search menu
						//- clear filter in menu
						if(objPageState.menusearchopen && objPageState.filteredmenu){
							bolRemoveSearch=true;
							bolClearFilter=true;
						}else{
							//3) only filtered menu is open
							//- clear the filter
							if(objPageState.filteredmenu){
								bolClearFilter=true;
							}else{
								//4) full menu is open
								//- hide the menu
								bolRemoveMenu=true;
							}
						}
					}

					/*
					Execute the actions
					*/
					if(bolRemoveSearch){
						fxMenuSearchSlide.slideOut().chain(function(){
							objPageElements.elmenusearch.removeClass('menu_search_indented');
						});
					}

					if(bolClearFilter){
						//clear the search form and show all the elements in the menu again
						clearSearchForm();
						removeFilterMenuLevel2();
					}

					if(bolRemoveMenu){
						fxMenuLevel2Slide.slideOut();
					}

				}else{
					//open the menu
					//before opening the menu, make sure that the current student is into the view
					//select the current student
					var elDivCurrent=objPageElements.elbody.getElement('div.content_wrapper div#menu_level2_wrapper div#menu_level2 div#menu_level2_content div.bg_students table.table_standard tr.menu_items td div.current');
				 	//scroll the div into position
					fxMenuLevel2Scroll.toElement(elDivCurrent);

					//open the menu
					fxMenuLevel2Slide.slideIn();
				}

			}


		},
		mouseenter: function(){
			objPageElements.elbuttonlevel2.setProperty('src','images/system/studenten_G_open.jpg');
		},
		mouseleave: function(){
			objPageElements.elbuttonlevel2.setProperty('src','images/system/studenten_K.png');
		}
	});

	//events on "search" button
	objPageElements.elbuttonsearch.addEvents({
		click: function(event){
			event.stop();

			if(objPageState.menulevel2open){
				var bolRemoveSearch=false;
				var bolClearFilter=false;
				var bolRemoveMenu=false;

				//1) menu and search panel are open
				//- close search panel
				if(objPageState.menusearchopen){
					bolRemoveSearch=true;
				}else{
					//2) menu open
					//- close menu
					//- clear filter and search panel
					bolRemoveMenu=true;
					bolClearFilter=true;
				}

				/*
				Execute the actions
				*/
				if(bolRemoveSearch){
					fxMenuSearchSlide.slideOut();
				}

				if(bolRemoveMenu){
					fxMenuLevel2Slide.slideOut().chain(function(){
						if(bolClearFilter){
							//clear the search form and show all the elements in the menu again
							clearSearchForm();
							removeFilterMenuLevel2();
						}
					});
				}

			}else{
					//first slide the navigation menu open and then the search menu
					//before opening the menu, make sure that the current student is into the view
					var elDivCurrent=objPageElements.elbody.getElement('div.content_wrapper div#menu_level2_wrapper div#menu_level2 div#menu_level2_content div.bg_students table.table_standard tr.menu_items td div.current');
				 	//scroll the div into position
					fxMenuLevel2Scroll.toElement(elDivCurrent);

					fxMenuLevel2Slide.slideIn().chain(function(){
						//add the class to reposition
						objPageElements.elmenusearch.addClass('menu_search_indented');
						fxMenuSearchSlide.slideIn();
					})

			}


		},

		mouseenter: function(){
			objPageElements.elbuttonsearch.setProperty('src','images/system/zoeken_G_open.jpg');
		},
		mouseleave: function(){
			objPageElements.elbuttonsearch.setProperty('src','images/system/zoeken_K.png');
		}

	});

	//events on "krooning" button
	objPageElements.elbuttonkrooning.addEvents({
		click: function(event){
			event.stop();
			
			
			
			if(objPageState.menulevel2open){
				//if the search menu is also open - move that one away first
				if(objPageState.menusearchopen){
					fxMenuSearchSlide.slideOut().chain(function(){
						objPageElements.elmenusearch.removeClass('menu_search_indented');
						fxMenuLevel2Slide.slideOut().chain(function(){
							if(objPageState.filteredmenu){
								//clear the search form and show all the elements in the menu again
								clearSearchForm();
								removeFilterMenuLevel2();
							}
							//switch student - home view
							toggleViewHomeStudent();
						});
					});
				}else{
					fxMenuLevel2Slide.slideOut().chain(function(){
						if(objPageState.filteredmenu){
							//clear the search form and show all the elements in the menu again
							clearSearchForm();
							removeFilterMenuLevel2();
						}
						//switch student - home view
						toggleViewHomeStudent();

					});
				}
			}else{
				//switch student - home view
				toggleViewHomeStudent();
			}

			//vicky: when click, always close menu_search_indented
			objPageElements.elmenusearch.removeClass('menu_search_indented');
		},
		mouseenter: function(){
			objPageElements.elbuttonkrooning.setProperty('src','images/system/krooning_G_open.jpg');
		},
		mouseleave: function(){
			objPageElements.elbuttonkrooning.setProperty('src','images/system/krooning_K.png');
		}
	});

	//to reposition the navigation up and down elements we need to monitor the scroll event on the students layer
	objPageElements.eldivwrapperstudent.addEvents({
		scroll: function(){
			//if(Browser.ie){
				repositionVerticalNavigationElements();
			//}
		}

	})

	//free text search input field needs to clear the checkbox "show all"
	objPageElements.elbody.getElement('div.content_wrapper div#menu_search div#menu_search_content table.table_standard tr td input#searchkey').addEvent('keydown', function(){
		objPageElements.checkboxshowall.checked=false;
	})

	//sets up the elements in the the navigation menu
	objPageElements.arrnavdiv.each(function(elDiv, intIndexDiv){

		//disable the link
		var elLink=elDiv.getElement('a');
		elLink.addEvent('click', function(event){
			event.stop()
			//instead of the link, fire the event on the tr
			this.getParent('div').fireEvent('click');
		});

		//add the click event to the div
		elDiv.addEvents({
			click: function(event){
				var elLink=this.getElement('a');
				var strHref=elLink.get('href');
				var strStudentId=strHref.split('=')[1].toInt();

				objPageState.idcurrent=strStudentId;
				loadStudentHtml({id: strStudentId, mode: 'menu'});

				//update the level 2 menu
				markMenuElementActive(objPageState.idcurrent);
			},
			mouseenter: function(){
				this.addClass('active');
			},
			mouseleave: function(){
				this.removeClass('active');
			}
		});

	})

	return true;
}


function setupArrowKeysEvents(){
  document.addEvent('keydown', function(event){
  	if(event.key=="up" && !objPageState.homevisible){
  		objPageElements.elbutnavprev.fireEvent('click');
  	}

  	if(event.key=="down" && !objPageState.homevisible){
  		objPageElements.elbutnavnext.fireEvent('click');
  	}
	})

  return true;
}



function setupUiElements(){
	if(Browser.isMobile){

	}else{


		//determine the height of the viewable div
		var intHeight=656;
		if(Browser.ie){
			intHeight=657;
			//if(Browser.ie8){};
			//if(Browser.ie7){};
			//if(Browser.ie6){};

			objPageElements.eldivwrapperhome.setStyle('height', intHeight);
			objPageElements.eldivwrapperstudent.setStyle('height', intHeight);
			//vicky: est width
			
		}else{
			//objPageElements.eldivwrapperhome.setStyle('height', intHeight+100);
			//objPageElements.eldivwrapperstudent.setStyle('height', intHeight+100);
		}

		//make the student and home divs scrollable
		objPageElements.eldivwrapperhome.setStyle('overflow-x', 'auto');
		objPageElements.eldivwrapperhome.setStyle('overflow-y', 'hidden');

		objPageElements.eldivwrapperstudent.setStyle('overflow-x', 'auto');
		objPageElements.eldivwrapperstudent.setStyle('overflow-y', 'hidden');
	}
	return true;
}

function retrievePageElements(){
	/*
	Find commonly used DOM elements and store them into a global object for re-use later
	*/
	objPageElements.elbody=$$('body')[0];

	//locate the menu elements
	var elDivMenuLevel1=$('menu_level1');
	var elButtonLevel2=$('but_menu_level2');
	var elButtonSearch=$('but_search');
	var elButtonKrooning=$('but_krooning');
	var elDivMenuLevel2Wapper=$('menu_level2_wrapper');
	var elDivMenuLevel2=$('menu_level2');
	var elDivMenuLevel2Content=$('menu_level2_content');
	var elDivSearch=$('menu_search');
	var elDivSearchContent=$('menu_search_content');

	//store the elements in the page elements object for efficient futher usage
	objPageElements.elmenulevel1=elDivMenuLevel1;
	objPageElements.elmenulevel2wrapper=elDivMenuLevel2Wapper;
	objPageElements.elmenulevel2=elDivMenuLevel2;
	objPageElements.elmenulevel2content=elDivMenuLevel2Content;
	objPageElements.elmenusearch=elDivSearch;
	objPageElements.elmenusearchcontent=elDivSearchContent;

	objPageElements.elbuttonlevel2=elButtonLevel2;
	objPageElements.elbuttonsearch=elButtonSearch;
	objPageElements.elbuttonkrooning=elButtonKrooning;

	objPageElements.eldivwrapperhome=objPageElements.elbody.getElement('div.home_wrapper');
	objPageElements.eldivwrapperstudent=objPageElements.elbody.getElement('div.student_wrapper');

	//up down navigation
	objPageElements.eldivnavprev=objPageElements.eldivwrapperstudent.getElement('div.student_content_wrapper div#SlideItMoo_vertical_outer div.back');
	objPageElements.eldivnavnext=objPageElements.eldivwrapperstudent.getElement('div.student_content_wrapper div#SlideItMoo_vertical_outer div.forward');
	objPageElements.elbutnavprev=objPageElements.eldivnavprev.getElement('div');
	objPageElements.elbutnavnext=objPageElements.eldivnavnext.getElement('div');

	objPageElements.arrnavdiv=objPageElements.elbody.getElements('div.content_wrapper div#menu_level2_wrapper div#menu_level2 div#menu_level2_content div.bg_students table.table_standard tr.menu_items td div')

	objPageElements.checkboxshowall=objPageElements.elbody.getElement('div.content_wrapper div#menu_search div#menu_search_content table.table_standard tr td div.search_layer input#showall');

	objPageElements.divslidemooinner=objPageElements.eldivwrapperstudent.getElement('div.student_content_wrapper div#SlideItMoo_vertical_outer div#SlideItMoo_vertical_inner');


	return true;
}

function onResize(){
	/*
	Fired as the page is resized
	*/
	objWindowSize=$(window).getSize();
	resizeColofonView();
	repositionVerticalNavigationElements();
}

function initPageDomReady(){
	/*
	Initiates the page execution
	*/
	var bolDone;

	bolDone=retrievePageElements();
	
	resizeStudentContentDiv.delay(500, null, 0);

	bolDone=setupUiElements();

	bolDone=setupPageEffects();

	bolDone=setupElementEvents();

	bolDone=setupArrowKeysEvents();

	//now slide the menus in and make them visible
	fxMenuLevel2Slide.slideOut().chain(function(){
		//set the opacity for the menu
		if(Browser.ie){
			objPageElements.elmenulevel2.set('opacity', 0.75);
		}
	});
	fxMenuSearchSlide.slideOut().chain(function(){
		//set the opacity for the menu
		if(Browser.ie){
			objPageElements.elmenulevel2content.set('opacity', 0.75);
		}
	});

	//fill the window dimensions
	objWindowSize=$(window).getSize();

	intVerticalNavigationElementWidth=objPageElements.eldivnavprev.getSize().x;
	repositionVerticalNavigationElements();



	//vicky styling tip studyarea
	var myTips = new Tips($$('.toolTipElement'), {
		timeOut: 700,
		maxTitleChars: 50,
		maxOpacity: .9
	});







	/*
	Delay some scripts to prevent overloading the client
	*/
	loadHomePage.delay(1000);


}

//add events
window.addEvent('domready', initPageDomReady);
window.addEvent('resize', onResize);

/*
Global variables
*/
//global object storing the page state
var objPageState={
	idcurrent: null,
	indexcurrent: 0,
	menulevel2open: false,
	menusearchopen: false,
	homevisible: false,
	filteredmenu: false
}

//effects
var fxSlideItMoo;
var fxMenuLevel2Scroller;
var fxMenuLevel2Scroll;
var fxMenuLevel2Slide;
var fxMenuSearchSlide;
var fxHomeScroll;
var fxStudentScroll;

//other global variables
var intMenuLevel1Width=16;
var intMenuLevel2Width=200;
var intSlideEffectDuration=300;	//sets the duration of the sliding effect
var intColofonWidth;
var intVerticalNavigationElementWidth;
var objWindowSize;
var bolHomePageShown=false;

var arrStudentIdFiltered;

//global object storing dom elements
var objPageElements={
	elmenulevel1: null,
	elmenulevel2wrapper: null,
	elmenulevel2: null,
	elmenulevel2content: null,
	elmenusearch: null,
	elmenusearchcontent: null,
	arrimgnavcheckbx: null,
	eldivcolofon: null
}
