var schools = new Array();

function updateSchoolOptions () {
	var schoolOptions = new Array();
	var k = 0;
	for (var i = 0; i < schools.length; i++) {
		if (schoolInLevel(schools[i])) {
			schoolOptions[k] = schools[i];
			k++;
		}
	}
	setSchoolOptions(schoolOptions);
}

function schoolInLevel (school) {
	activeLevel = document.getElementById('niveau').options[document.getElementById('niveau').selectedIndex].value;
	if (activeLevel == 0) {
		return true;
	}
	if (activeLevel) {
		for (var i = 0; i < school[3].length; i++) {
			if (school[3][i] == activeLevel) {
				//	This school is in clicked level
				return true;
			}
		}
	}
	return false;
}

function setSchoolOptions (schoolOptions) {
	document.getElementById('school').length = 0;
	var elOptNew = document.createElement('option');
	elOptNew.text = 'Kies je vestiging';
	try {
		document.getElementById('school').add(elOptNew, null);
	}
	catch(ex) {
		document.getElementById('school').add(elOptNew);
	}
	for (i=0; i<schoolOptions.length; i++) {
		var elOptNew = document.createElement('option');
		elOptNew.text = schoolOptions[i][1];
		elOptNew.value = schoolOptions[i][2];
		try {
			document.getElementById('school').add(elOptNew, null);
		}
		catch(ex) {
			document.getElementById('school').add(elOptNew);
		}
	}
	$('#school').resetSS();
}

function goTo() {
	var url = document.getElementById('school').options[document.getElementById('school').selectedIndex].value;
	if (url != 'Kies je vestiging') {
		document.location = url;
	}
}


$(document).ready(function(){
	var niveauSelect = $('#niveau').sSelect();
	var schoolSelect = $('#school').sSelect();

	niveauSelect.change(function(){
		hoveringSchoolsDiv = false;
		updateSchoolOptions();
	});
	schoolSelect.change(function(){
		hoveringSchoolsDiv = false;
	});
	
	setSchoolOptions(schools);
});

//	--------------------------------------------------------------------
//	Sliding methods
//	--------------------------------------------------------------------

var animating = false;	//	True when in an animation
var slidingUp = false;	//	True when sliding up and when stationary up.

//	--------------------------

var hoveringSchoolsDiv = false;

function setHoveringSchoolsDiv (value) {
	if (value != hoveringSchoolsDiv) {
		hoveringSchoolsDiv = value;
		processHoveringSchoolsDiv();
	}
}

function processHoveringSchoolsDiv () {
	updateSlide();
}

//	--------------------------

function updateSlide () {
	if (!animating) {
		if (hoveringSchoolsDiv && !slidingUp) {
			//	Currently hovering but not sliding up
			slidingUp = true;
			slideSchoolsDivUp();
			return;
		}
		if (!hoveringSchoolsDiv && slidingUp) {
			//	Currenly not hovering but not sliding down
			slidingUp = false;
			slideSchoolsDivDown();
			return;
		}
	}
}

//	--------------------------

function slideSchoolsDivUp () {
	animating = true;
	$('#schools').animate({
	top: '-=195',
	height: '+=195'
	}, 1000, function() {
		$('#schools').css('overflow', 'visible');
		animating = false;
		updateSlide();
	});
}

function slideSchoolsDivDown () {
	animating = true;
	$('#schools').css('overflow', 'hidden');
	$('.newList').hide();
	$('#schools').animate({
		top: '+=195',
		height: '-=195'
	}, 1000, function() {
		animating = false;
		updateSlide();
	});
}

//	--------------------------

function schoolsDivMouseEnter () {
	setHoveringSchoolsDiv(true);
}

function schoolsDivMouseMove () {
	setHoveringSchoolsDiv(true);
}

function schoolsDivMouseLeave () {
	setHoveringSchoolsDiv(false);
}

$(document).ready(function() {
	$('#schools').bind('mouseenter', schoolsDivMouseEnter);
	$('#schools').bind('mouseleave', schoolsDivMouseLeave);
	$('#schools').bind('mousemove', schoolsDivMouseMove);
});
