var messageBoxOverlay;
var allIframes = new Array();

function doRowColoring(){
	$$('.clsTableRowColoring').each(function(elem){
		var allTRElements = elem.getElements('tr');
		for(j=0; j<allTRElements.length; j++){
			if(j%2 == 0){
				allTRElements[j].addClass('clsOddRowColoring');
			}
		}
	});
}

function openOverlay(zIndex){
	if(zIndex == null){
		zIndex = 100;
	}
	if(messageBoxOverlay == undefined || messageBoxOverlay == null){
		messageBoxOverlay = new Overlay(document.body, { 
			duration: 150,
			color	: '#000',
			opacity	: 0.6
		});
		
	}
	messageBoxOverlay.open();
	
	messageBoxOverlay.setZIndex(zIndex);
}
function setOverlayZIndex(zIndex){
	if(messageBoxOverlay == undefined || messageBoxOverlay == null){
		return;
	}
	messageBoxOverlay.setZIndex(zIndex);
}
function closeOverlay(){
	
	if(messageBoxOverlay == undefined || messageBoxOverlay == null){
		return;
	}
	if( (typeOf(popupArrow) == 'object' || typeOf(popupArrow) == 'element') ){
		if($(popupArrow)){
			$(popupArrow).destroy();
			popupArrow = null;
		}
	}
	messageBoxOverlay.close();
}

var redBoxPopup; //reference to active red box popup, so it can be closed before opening the next

var popupBox;
var activeCalendar;
function doAtoZFilter(strSearch){
	if(strSearch.clean().length >= 3){
		var strData = 'strAtoZSearch='+strSearch.clean();
		var myRequest = new Request.JSON({
    		onSuccess: function(arrReturn){
    			$('idSearchBar').setStyle('display','block');
    			$('idSearchTerm').setProperty('html',strSearch);
    			var allULs = $('idInfoAZList').getElements('ul');
    			var blnAllExit = true;
    			for(var i=0; i<allULs.length; i++){
    				var strHName = allULs[i].id.replace('id_ul_','id_h2_');
    				var allLists = allULs[i].getElements('li');
    				var blnDisplay = false;
    				for(var j=0; j<allLists.length; j++){
    					if(arrReturn.contains(allLists[j].id.replace('id_','')) == false){
    						allLists[j].setStyle('display','none');
    					} else {
    						allLists[j].setStyle('display','');
    						blnDisplay = true;
    					}
    				}
    				if(blnDisplay == false){
    					allULs[i].setStyle('display','none');
    					$(strHName).setStyle('display','none');
    				} else {
    					blnAllExit = false
    					allULs[i].setStyle('display','');
    					$(strHName).setStyle('display','');
    				}
    				
    			}  
    			if(blnAllExit === true){
    				$('idAZGradient').setStyle('top','0px');
    			} else {
    				$('idAZGradient').setStyle('top','-10px');
    			}
    		}, url: '/script/getAtoZFiltered/'});
		myRequest.send({ method: 'post', data: strData	});
	} else {
		// turn everything back on
		var allULs = $('idInfoAZList').getElements('ul');
		for(var i=0; i<allULs.length; i++){
			var strHName = allULs[i].id.replace('id_ul_','id_h2_');
			var allLists = allULs[i].getElements('li');
			var blnDisplay = true;
			for(var j=0; j<allLists.length; j++){
				allLists[j].setStyle('display','');
			}
			allULs[i].setStyle('display','');
			$(strHName).setStyle('display','');
		}    			
		$('idSearchBar').setStyle('display','none');
		$('idSearchTerm').setProperty('html','');
	}
}
function getAtoZItem(intAtoZId){
	openOverlay();
	var strData = 'intAtoZId='+intAtoZId;
    var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null && objReturn.arrFiles != null && objReturn.objAtoZ != null){
    				createAtoZPopup( 	objReturn.objAtoZ.strTitle, 
    									objReturn.objAtoZ.strSubTitle,
    									objReturn.objAtoZ.strContent, 
    									objReturn.arrFiles);
    				doRowColoring();
    				setOverlayZIndex(104);
    			}
    		}, url: '/script/getAtoZItem/'});
    myRequest.send({ method: 'post', data: strData	});
}
function getNewsItem(intNewsId){
	openOverlay();
	var strHref = false;
	if($('newsLinkId'+intNewsId)){
		strHref = $('newsLinkId'+intNewsId).get('href');
	}
	var strData = 'intNewsId='+intNewsId;
    var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null && objReturn.arrFiles != null && objReturn.objNews != null){
    				var strDeepLink = (strHref != null && strHref != false) ? strHref:objReturn.objNews.strDeepLink;
					createNewsPopup(strDeepLink,
									objReturn.objNews.strTitle,
								    objReturn.objNews.strSummary, 
								    objReturn.objNews.strContent, 
								    objReturn.objNews.strMediaType,
								    objReturn.objNews.strMediaUrl,
								    objReturn.arrFiles,
								    objReturn.arrMedia );
					doRowColoring();
					setOverlayZIndex(104);
    			}
    		}, url: '/script/getNewsItem/'});
    myRequest.send({ method: 'post', data: strData	});
}

function getAnnouncementItem(intAnnouncementId, intPosition){
	openOverlay();
	var strData = 'intAnnouncementId='+intAnnouncementId;
    var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null && objReturn.arrFiles != null && objReturn.objAnnouncement != null){
    				if( (typeOf(redBoxPopup) == 'object' || typeOf(redBoxPopup) == 'element') ){
    					if($(redBoxPopup)){
    						$(redBoxPopup).destroy();
    						redBoxPopup = null;
    					}
    				}
    				redBoxPopup = createNotificationPopup(  objReturn.objAnnouncement.strTitle, 
    										  objReturn.objAnnouncement.strSubTitle,
    										  objReturn.objAnnouncement.strContent, 
    										  objReturn.arrFiles,
    										  objReturn.objAnnouncement.strIcon,
    										  intPosition);
    				doRowColoring();
    				setOverlayZIndex(100);
    			}
    		}, url: '/script/getAnnouncementItem/'});
    myRequest.send({ method: 'post', data: strData	});
}

function getCalendarItem(intCalendarId){
	openOverlay();
	var strData = 'intCalendarId='+intCalendarId;
    var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null && objReturn.arrFiles != null && objReturn.objCalendarItem != null){
    				createCalendarPopup(  objReturn.objCalendarItem.strTitle, 
    									  objReturn.objCalendarItem.strSubTitle,
    									  objReturn.objCalendarItem.strContent, 
    									  objReturn.objCalendarItem.intIsChanged,
    									  objReturn.arrFiles
    									  );
    				doRowColoring();
    				setOverlayZIndex(108);
    			}
    		}, url: '/script/getCalendarItem/'});
    myRequest.send({ method: 'post', data: strData	});
}

function getRoosterWijzigingen(strCategory, intPosition){
	openOverlay();
	var strData = 'function=roosterwijzigingen&sector='+strCategory;
    var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null){
    				if( (typeOf(redBoxPopup) == 'object' || typeOf(redBoxPopup) == 'element') ){
    					if($(redBoxPopup)){
    						$(redBoxPopup).destroy();
    						redBoxPopup = null;
    					}
    				}
    				redBoxPopup = createScheduleChangesPopup(  
    						 'Roosterwijzigingen',
							 strCategory,
							 intPosition,
		  					 objReturn,
		  					 (58+(intPosition*60)));
    				setOverlayZIndex(100);
    			}
    		}, 
    		url: '/roosterweb/frontend/functions_json.php'});
    myRequest.send({ method: 'get', data: strData });
}


var arrCategory;
var arrKlas;
var arrLeerling;
var objRooster;

function getLesrooster(strCategory, intPosition, strRoosterNaam, strIngangsDatum, strKlas, strLeerling){
	openOverlay();
	var action;
	var roosterNaam;
	var ingangsDatum;

	var objResult;
	
	if(strRoosterNaam != null && strIngangsDatum != null){	
		if(strLeerling != null && strLeerling != '') {
			var strData = 'function=rooster&ll_nr='+strLeerling;
			var myRequest = new Request.JSON({
				async : false,
	    		onSuccess: function(objReturn){
	    			objRooster = objReturn;
	    		}, 
	    		url: '/roosterweb/frontend/functions_json.php'});
			myRequest.send({ method: 'get', data: strData});
			action = 'rooster';
		} else if(strKlas != null && strKlas != '') {
			var objKlas;
			/** 
			 * to check for the cluster, select the right class from our array
			 */
			if(arrKlas.length > 0){
				for(j=0; j<arrKlas.length; j++){
					if(arrKlas[j].value == strKlas){
						objKlas = arrKlas[j];
						break;
					}
					
				}
			}
			
			if(objKlas != null && objKlas.cluster == 0){
				var strData = 'function=rooster&klas='+strKlas;
				var myRequest = new Request.JSON({
					async : false,
		    		onSuccess: function(objReturn){
		    			objRooster = objReturn;
		    		}, 
		    		url: '/roosterweb/frontend/functions_json.php'});
				myRequest.send({ method: 'get', data: strData});
				arrLeerling = null;
				action = 'rooster';
			} else if(objKlas != null && objKlas.cluster == 1){
				var strData = 'function=leerlingen&klas='+strKlas;
				arrLeerling = new Array();
				var myRequest = new Request.JSON({
					async : false,
		    		onSuccess: function(arrReturn){
		    			if(arrReturn != null){
		    				arrLeerling[0] = {name:'selecteer naam', value:'', selected:0};
		    				for(j=0; j<arrReturn.length; j++){
		    					arrLeerling[(j+1)] = {name:(arrReturn[j].achternaam+", "+arrReturn[j].voornaam), value:arrReturn[j].ll_nr, selected:((arrReturn[j].ll_nr == strLeerling)? 1:0 )};  				
		    				}
		    			}
		    			objResult = null;
		    		}, 
		    		url: '/roosterweb/frontend/functions_json.php'});
				myRequest.send({ method: 'get', data: strData});				
				action = 'leerling';
			}
		} else if (strCategory != null && strCategory != "") {
			var strData = 'function=klassen&sector='+strCategory;
			arrKlas = new Array();
			var myRequest = new Request.JSON({
				async : false,
	    		onSuccess: function(arrReturn){
	    			if(arrReturn != null){
	    				arrKlas[0] = {cluster:0, name:'selecteer klas', value:'', selected:0};
	    				for(j=0; j<arrReturn.length; j++){
	    					arrKlas[(j+1)] = {cluster:arrReturn[j].cluster, name:arrReturn[j].klas, value:arrReturn[j].klas, selected:((arrReturn[j].klas == strKlas)? 1:0 )};  				
	    				}
	    			}
	    			arrLeerling = null;
	    			objResult = null;
	    		}, 
	    		url: '/roosterweb/frontend/functions_json.php'});
			myRequest.send({ method: 'get', data: strData});
			action = 'klassen';
		} else {
			var strData = 'function=klassen';
			arrKlas = new Array();
			var myRequest = new Request.JSON({
				async : false,
	    		onSuccess: function(arrReturn){
	    			if(arrReturn != null){
	    				arrKlas[0] = {cluster:0, name:'selecteer klas', value:'', selected:0};
	    				for(j=0; j<arrReturn.length; j++){
	    					arrKlas[(j+1)] = {cluster:arrReturn[j].cluster, name:arrReturn[j].klas, value:arrReturn[j].klas, selected:((arrReturn[j].klas == strKlas)? 1:0 )};  				
	    				}
	    			}
	    			arrLeerling = null;
	    			objResult = null;
	    		}, 
	    		url: '/roosterweb/frontend/functions_json.php'});
			myRequest.send({ method: 'get', data: strData});
			action = 'klassen';
		}
	} else {
		var strDataA = 'function=roosterNaam'; 
		var myRequestA = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null){
    				roosterNaam = objReturn;
    				if(ingangsDatum != null){
    					getLesrooster(strCategory, intPosition, roosterNaam, ingangsDatum);
    				}    				
    			}
    		}, 
    		url: '/roosterweb/frontend/functions_json.php'});
		myRequestA.send({ method: 'get', data: strDataA });
		
		var strDataB = 'function=roosterIngangsDatum';
		var myRequestB = new Request.JSON({
			onSuccess: function(objReturn){
    			if(objReturn != null){
    				ingangsDatum = objReturn;
    				if(roosterNaam != null){
    					getLesrooster(strCategory, intPosition, roosterNaam, ingangsDatum);
    				}
    			}
    		}, 
    		url: '/roosterweb/frontend/functions_json.php'});
		myRequestB.send({ method: 'get', data: strDataB });
		return;
	}
	
	
	/**
	 * Assume we're complete
	 */
	arrCategory = new Array(
			{name:' alle ', value:'',     selected:((strCategory == null)   ? 1 : 0)},
			{name:' vwo ' , value:'vwo',  selected:((strCategory == 'vwo')  ? 1 : 0)},
			{name:' havo ', value:'havo', selected:((strCategory == 'havo') ? 1 : 0)},
			{name:' mavo ', value:'mavo', selected:((strCategory == 'mavo') ? 1 : 0)},
			{name:' vmbo ', value:'vmbo', selected:((strCategory == 'vmbo') ? 1 : 0)}
			);
	
	if(arrKlas != null){
		for(var i=0; i < arrKlas.length; i++){
			if(arrKlas[i].value == strKlas)
				arrKlas[i].selected = 1;
			else 
				arrKlas[i].selected = 0;
		}
	}
	
	if(arrLeerling != null){
		for(var i=0; i < arrLeerling.length; i++){
			if(arrLeerling[i].value == strLeerling)
				arrLeerling[i].selected = 1;
			else 
				arrLeerling[i].selected = 0;
		}
	}
	
	if( (typeOf(redBoxPopup) == 'object' || typeOf(redBoxPopup) == 'element') ){
		if($(redBoxPopup)){
			$(redBoxPopup).destroy();
			redBoxPopup = null;
		}
	}
    switch(action) {
	    case 'klassen' :
    		redBoxPopup = createLesRoosterPopup(strRoosterNaam, intPosition, strIngangsDatum, arrCategory, arrKlas);
    		createArrowAtRightMenuPos(intPosition);
    		setOverlayZIndex(100);
        	break;
    	case 'leerling' : 
    		redBoxPopup = createLesRoosterPopup(strRoosterNaam, intPosition, strIngangsDatum, arrCategory, arrKlas, arrLeerling);
    		createArrowAtRightMenuPos(intPosition);
    		setOverlayZIndex(100);
        	break;
    	case 'rooster' :
    		redBoxPopup = createLesRoosterPopup(strRoosterNaam, intPosition, strIngangsDatum, arrCategory, arrKlas, arrLeerling, objRooster);
    		createArrowAtRightMenuPos(intPosition);
    		setOverlayZIndex(100);
        	break;
    }
    
}

function getCalendarMonth(strYear, strMonth, intCategory){
	openOverlay();
	var strData = 'strYear='+strYear+'&strMonth='+strMonth;
	if(intCategory != null && ''+intCategory.clean() != ''){
		strData = strData+'&intCategory='+intCategory;
	}
	var myRequest = new Request.JSON({
    		onSuccess: function(objReturn){
    			if(objReturn != null){
    				createCalendarMonthPopup(  
    						 'Kalender',
							 objReturn.configData,
							 objReturn.allDates,
							 strYear,
							 strMonth);
    			}
    		}, 
    		url: '/script/getCalendarByMonth/'});
    myRequest.send({ method: 'get', data: strData });
}


function getHumanSize(filesize){
	var sizeUnit = 'Byte';
    if(filesize > 100){
	    filesize = (filesize / 1024);
	    sizeUnit = 'Kb';
    } 
    if(filesize > 100){
	    filesize = (filesize / 1024);
	    sizeUnit = 'Mb';
    } 
    return (Math.round(filesize*100)/100)+' '+sizeUnit;
}

function createRowForSchedule(arrCols, rowSet, blnAllHeaders, blnFirstHeader){
	if(arrCols != null) {
		for (var i=0;i<arrCols.length;i++){
			var objCol = arrCols[i];
			var dataCell = new Element('td');
			if(blnAllHeaders){
				dataCell = new Element('th');
			} else if(blnFirstHeader && i == 0){
				dataCell = new Element('th');	
			}
			var strHtml = '<div class="clsScheduleCell">';
			
			if(objCol.arrEntries != null && objCol.arrEntries.length > 0){
				for(var j=0; j<objCol.arrEntries.length; j++){
					strHtml += '<div>';
					strHtml += '<span>'+objCol.arrEntries[j].vak+'</span> ';
					strHtml += '<span>'+objCol.arrEntries[j].docent+'</span> ';
					strHtml += '<span>'+objCol.arrEntries[j].lokaal+'</span>';
					strHtml += '</div>';
				}
			} else if(objCol.strText != null) {
				strHtml += '<span>'+objCol.strText.clean()+'</span>';
			} else {
				strHtml += '<span>&nbsp;</span>';
			}
			strHtml += '</div>';
			
			dataCell.setProperty('html',strHtml);
			dataCell.inject(rowSet, 'bottom');
		}
	} 	
}

function createSelectBox(strName, arrOptions, fnChangeHandler){
	var selectSection = new Element('select');
	selectSection.name = strName;
	selectSection.id = strName;
	for(var i=0;i<arrOptions.length;i++){
		var objSection = arrOptions[i];
		var optionSection = new Element('option');
		optionSection.value = objSection.value;
		optionSection.setProperty('text',objSection.name);
		if(objSection.selected === 1){
			optionSection.selected = "selected";
		}
		optionSection.inject(selectSection,'bottom');
	}
	
	if(fnChangeHandler != null){
		selectSection.addEvent('change',fnChangeHandler);
	}
	
	return selectSection;
	
}

function createAtoZPopup(strTitle, strSubTitle, strContent, arrFiles){ 
	var popupBox = createGeneralPopup(strTitle, 'big', strSubTitle, 'italic');
	popupBox.addClass('clsPopupMedium').setStyles({'z-index':'220', 'width':'650px'});
	
	var popupText		= new Element('div').addClass('clsPopupTextP');
	popupText.setProperty('html',strContent);
	popupText.inject(popupBox,'bottom');
	
	var popupIconInfo	= new Element('div').addClass('clsIconInfo').addClass('clsIconInPopupPos');
	popupIconInfo.inject(popupBox,'top');
	
	var attachmentUL = createAttachmentsList(arrFiles);
	attachmentUL.inject(popupBox,'bottom');
	
	popupBox.set('morph', {duration: 350});
	popupBox.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ($(window).getScroll().y.toInt() + ($(window).getSize().y.toInt() / 2) - (popupBox.getSize().y.toInt() / 2)) - 50));
		popupBox.setStyle('top', newPos+"px");
		popupBox.morph({'opacity':1});
	}
	setPosition.delay(120);	
	
}

var popupNotification;

var popupArrow;
function createArrowAtRightMenuPos(intArrowPos){
	if( (typeOf(popupArrow) == 'object' || typeOf(popupArrow) == 'element') ){
		if($(popupArrow)){
			$(popupArrow).destroy();
			popupArrow = null;
		}
	}
	if(intArrowPos !== false){
		popupArrow  = new Element('div').addClass('clsPopupArrow');
		popupArrow.addClass('clsPopupArrowPosCommunication').setStyles({'top':intArrowPos+(180)+"px",'left':"716px"});
		popupArrow.inject($(document.body),'bottom');
	}
}

function createScheduleChangesPopup(strTitle, strSelectedCategory, intPosition, objInfo, intArrowPos){
	if( (typeOf(popupNotification) == 'object' || typeOf(popupNotification) == 'element') ){
		if($(popupNotification)){
			$(popupNotification).destroy();
			popupNotification = null;
		}
	}
	popupNotification = createGeneralPopup(strTitle, 'normal', false, false);
		
	
	if(typeOf(objInfo) == 'string'){
		
		var popupScheduleBox = new Element('div').addClass('clsPopupScheduleBlock');
		popupScheduleBox.setProperty('html','<div class="clsIconCalendarSmall clsIconInPopupSchedule"></div><div class="clsPopupScheduleDate" style="font-weight:normal;">Nog geen wijziging bekend</div>');
		popupScheduleBox.inject(popupNotification,'bottom');
		
	} else {
		var popupScheduleBox = new Element('div').addClass('clsPopupScheduleBlock');
		popupScheduleBox.setProperty('html','<div class="clsIconCalendarSmall clsIconInPopupSchedule"></div><div class="clsPopupScheduleDate" style="font-weight:normal;">'+objInfo.datum+'</div>');
		popupScheduleBox.inject(popupNotification,'bottom');
	
		var popupScheduleBox2 = new Element('div').addClass('clsPopupScheduleBlock');
		popupScheduleBox2.setProperty('html', '<div class="clsIconInfoSmall clsPropertiesOfIconToTxt"></div>'+objInfo.opmerkingen.replace("\n","<br/>"));
		popupScheduleBox2.inject(popupNotification,'bottom');
	}	
	var popupScheduleBox1 = new Element('div').addClass('clsPopupScheduleBlock');
	popupScheduleBox1.setProperty('html', '<div class="clsIconUserSmall clsPropertiesOfIconToTxt"></div>');
	popupScheduleBox1.inject(popupNotification,'bottom');

	selectSection = createSelectBox( 'roosterInfoCategorie',
									 new Array(	
											 	{name: 'Vwo', value:'vwo', selected:( strSelectedCategory == 'vwo' ? 1: 0)},
											 	{name: 'Havo', value:'havo', selected:( strSelectedCategory == 'havo' ? 1: 0)},
											 	{name: 'Mavo', value:'mavo', selected:( strSelectedCategory == 'mavo' ? 1: 0)},
											 	{name: 'Vmbo', value:'vmbo', selected:( strSelectedCategory == 'vmbo' ? 1: 0)}
											 	),
									  function(){getRoosterWijzigingen(this.options[this.selectedIndex].value, intPosition);});
	//selectSection.addClass('clsFloatLeft');
	selectSection.inject(popupScheduleBox1,'bottom');
	
	
	var popupClear  = new Element('div').addClass('clsClear');
	popupClear.inject(popupScheduleBox1,'bottom');
	
	
	var popupIconInfo	= new Element('div').addClass('clsIconClock').addClass('clsIconInPopupPos');
	popupIconInfo.inject(popupNotification,'top');
	
	if(typeOf(objInfo) != 'string'){
		var scheduleTable = createScheduleChangesTable(objInfo.wijzigingen);
		scheduleTable.inject(popupNotification,'bottom');
	} else {
		var popupNoMessageBox = new Element('div').addClass('clsScheduleTable').set('html','<div style="padding:5px;">Er zijn voor de aankomende dag nog geen wijzigingen bekend. Blijf het rooster in de gaten houden.</div>');
		popupNoMessageBox.inject(popupNotification,'bottom');
	}
	
	createArrowAtRightMenuPos(intArrowPos);
	
	popupNotification.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ((intArrowPos+200) - (popupNotification.getSize().y.toInt() / 2)) ));
		popupNotification.setStyle('top', newPos+"px");
		popupNotification.setStyle('opacity', 1);
	}
	setPosition.delay(120);	
	
	return popupNotification;
}

function createScheduleChangesTable(arrChanges){
	var objTable = new Element('table').addClass('clsScheduleTable');
	var objTbody = new Element('tbody');
	var objRow = new Element('tr').addClass('clsScheduleRowOdd');
	
	new Element('th').setProperty('html','Klas').inject(objRow, 'bottom');
	new Element('th').setProperty('html','Uur').inject(objRow, 'bottom');
	new Element('th').setProperty('html','Vak').inject(objRow, 'bottom');
	new Element('th').setProperty('html','Vervanger').inject(objRow, 'bottom');
	new Element('th').setProperty('html','Docent').inject(objRow, 'bottom');
	new Element('th').setProperty('html','Lokaal').inject(objRow, 'bottom');
	
	objRow.inject(objTbody, 'top');
	
	for(var i=0; i<arrChanges.length; i++){
		var objRow = new Element('tr');
		if(i%2 == 1)
			objRow.addClass('clsScheduleRowOdd');
		
		new Element('td').setProperty('html',arrChanges[i].klas).inject(objRow, 'bottom');
		new Element('td').setProperty('html',arrChanges[i].uur).inject(objRow, 'bottom');
		new Element('td').setProperty('html',arrChanges[i].vak).inject(objRow, 'bottom');
		
		var objCellVervanger = new Element('td').setProperty('html',arrChanges[i].vervanger).inject(objRow, 'bottom');
		
		if(arrChanges[i].vervanger.toLowerCase().clean() == 'verdelen')
			objCellVervanger.addClass('clsScheduleItemBlue');
		else if(arrChanges[i].vervanger.toLowerCase().clean() == 'geen les')
			objCellVervanger.addClass('clsScheduleItemRed');
		
		new Element('td').setProperty('html',arrChanges[i].docent).inject(objRow, 'bottom');
		new Element('td').setProperty('html',arrChanges[i].lokaal).inject(objRow, 'bottom');
		objRow.inject(objTbody,'bottom');
	}
	
	objTbody.inject(objTable, 'top');
	return objTable;
}

function createNotificationPopup(strTitle, strSubTitle, strContent, arrFiles, strIcon, intArrowPos){
	if( (typeOf(popupNotification) == 'object' || typeOf(popupNotification) == 'element') ){
		if($(popupNotification)){
			$(popupNotification).destroy();
			popupNotification = null;
		}
	}
	popupNotification = createGeneralPopup(strTitle, 'normal', strSubTitle, 'normal');
	
	popupNotification.addClass('clsPopupCommunications');
	
	var popupTextBox    = new Element('div').addClass('clsPopupCommunicationsTxt');
	popupTextBox.setProperty('html',strContent);
	popupTextBox.inject(popupNotification,'bottom');

	if(strIcon != null && strIcon == 'info'){
		var popupIcon  = new Element('div').addClass('clsIconPopupInfo').addClass('clsIconInPopupPos');
	} else if(strIcon != null && strIcon == 'notice'){
		var popupIcon  = new Element('div').addClass('clsIconPopupNotice').addClass('clsIconInPopupPos');
	} else if(strIcon != null && strIcon == 'agenda'){
		var popupIcon  = new Element('div').addClass('clsIconPopupCalendar').addClass('clsIconInPopupPos');			
	} else if(strIcon != null && strIcon == 'arrow'){
		var popupIcon  = new Element('div').addClass('clsIconPopupArrow').addClass('clsIconInPopupPos');
	} else if(strIcon != null && strIcon == 'toets'){
		var popupIcon = new Element('div').addClass('clsIconTag').addClass('clsIconInPopupPos');
	} else {
		var popupIcon = new Element('div').addClass('clsIconPopupInfo').addClass('clsIconInPopupPos');
	}
	if(popupIcon != undefined){
		popupIcon.inject(popupNotification,'top');
	}
	
	var attachmentUL = createAttachmentsList(arrFiles);
	attachmentUL.inject(popupTextBox,'bottom');	
	
	createArrowAtRightMenuPos(intArrowPos);
	
	popupNotification.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ((intArrowPos+200) - (popupNotification.getSize().y.toInt() / 2)) ));
		popupNotification.setStyle('top', newPos+"px");
		popupNotification.setStyle('opacity', 1);
	}
	setPosition.delay(120);	
	
	return popupNotification;
}

var popupCalendarItem;
function createCalendarPopup(strTitle, strSubTitle, strContent, intIsChanged,  arrFiles){
	
	if( (typeOf(popupCalendarItem) == 'object' || typeOf(popupCalendarItem) == 'element') ){
		if($(popupCalendarItem)){
			$(popupCalendarItem).destroy();
			popupCalendarItem = null;
		}
	}
	popupCalendarItem = createGeneralPopup(strTitle, 'big', strSubTitle, 'bold', true);
	popupCalendarItem.addClass('clsPopupCommunications');
	
	if(intIsChanged == 1){
		var popupScheduleBox = new Element('div').addClass('clsPopupScheduleBlock');
		popupScheduleBox.setProperty('html','<div class="clsIconChanged clsPropertiesOfIconToTxt"></div><strong>gewijzigd</strong>');
		popupScheduleBox.inject(popupCalendarItem,'bottom');
	}
	
	var popupTextBox    = new Element('div').addClass('clsPopupTextP');
	popupTextBox.setProperty('html',strContent);
	popupTextBox.inject(popupCalendarItem,'bottom');
	
	var popupIcon  = new Element('div').addClass('clsIconPopupCalendar').addClass('clsIconInPopupPos');			
	popupIcon.inject(popupCalendarItem,'top');
	
	var attachmentUL = createAttachmentsList(arrFiles);
	attachmentUL.inject(popupCalendarItem,'bottom');	
	
	popupCalendarItem.set('morph',{duration:350});
	popupCalendarItem.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ($(window).getScroll().y.toInt() + ($(window).getSize().y.toInt() / 2) - (popupCalendarItem.getSize().y.toInt() / 2)) - 50));
		popupCalendarItem.setStyle('top', newPos+"px");
		popupCalendarItem.morph({'opacity':1});
	}
	setPosition.delay(120);	
	
}

var popupCalendar;
function createCalendarMonthPopup(strTitle, objGeneral, arrDates, strYear, strMonth){
	if( (typeOf(popupCalendar) == 'object' || typeOf(popupCalendar) == 'element') ){
		if($(popupCalendar)){
			$(popupCalendar).destroy();
			popupCalendar = null;
		}
	}
	popupCalendar = createGeneralPopup(strTitle, 'big', false, false);
	popupCalendar.addClass('clsPopupCommunications').setStyle('width','990px');
	
	var popupIcon  = new Element('div').addClass('clsIconPopupCalendar').addClass('clsIconInPopupPos');			
	popupIcon.inject(popupCalendar,'top');
	
	var arrDagNamen = new Array("Zondag","Maandag","Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag")
	
	var selectCategories = createSelectBox('idCalendarCategory', objGeneral.arrCategories, function(){
																						getCalendarMonth(strYear, 
																										 strMonth,
																										 this.options[this.selectedIndex].value)});
	/* creating the selectbox */
	var selectBoxContainer = new Element('div').addClass('clsCalendarCatSelector');
	var selectIconPrefix = new Element('div').addClass('clsIconUserSmall').addClass('clsPropertiesOfIconToTxt');
	selectIconPrefix.inject(selectBoxContainer, 'top');
	selectCategories.inject(selectBoxContainer, 'bottom').setStyles({'position':'relative','top':'-5px'});
	selectBoxContainer.inject(popupCalendar,'bottom');
	new Element('div').addClass('clsClear').inject(popupCalendar,'bottom');
	
	var prevMonth = new Element('div').addClass('clsCalendarPrevMonth').addEvent('click',function(){
																						getCalendarMonth(objGeneral.strPrevYear, 
																										 objGeneral.strPrevMonth,
																										 selectCategories.options[selectCategories.selectedIndex].value)});
	var nextMonth = new Element('div').addClass('clsCalendarNextMonth').addEvent('click',function(){
																						getCalendarMonth(objGeneral.strNextYear, 
																										 objGeneral.strNextMonth,
																										 selectCategories.options[selectCategories.selectedIndex].value)});
	var monthTitle = new Element('div').addClass('clsCalendarMonthTitle');
	monthTitle.setProperty('html',objGeneral.strCurrDate);
	monthTitle.inject(popupCalendar, 'bottom');
	prevMonth.inject(monthTitle, 'top');
	nextMonth.inject(monthTitle, 'top');
	
	var dateTable = new Element('table').addClass('clsCalendarBigTable');
	var dateTbody = new Element('tbody');
	for(var i=0; i<arrDates.length; i++){
		var strDay = '<span class="'+arrDates[i].objDate.strClass+'">'+arrDates[i].objDate.strDay+'</span>';
		// add day names to first row
		if(i<7){ strDay = arrDagNamen[i]+' '+strDay; }
		// alternating column coloring
		if(i%7 == 0 || i == 0){	var tr = new Element('tr').inject(dateTbody,'bottom'); }
		
		// create and inject table cells
		var td = new Element('td').setStyle('cursor','pointer');
		if( ((i%7) % 2) == 0){ td.addClass('clsCalendarRowOdd'); }
		if(arrDates[i].objDate.strClass == 'clsToday'){ td.setProperty('html', '<div class="clsToday" style="width:100%;">'+strDay+'</div>'); } 
		else { td.setProperty('html', strDay); }
		td.inject(tr,'bottom');
		
		var popupContainer = new Element('div').setStyle('position','relative');
		popupContainer.inject(td,'top');
		
		var popupDiv = new Element('div').addClass('clsPopupSchedule').addClass('clsPopupScheduleItem').setStyles({'min-height':'120px','display':'none','cursor':'pointer'});
		popupDiv.setProperty('html','<strong>'+arrDates[i].objDate.strDay+'</strong>');
		
		popupDiv.addEvent('click',function(elem){$(this).fade('out'); return false;});
		
		td.store('popupDiv', popupDiv);
		
		td.addEvent('click', function(){
			var popupDiv = $(this).retrieve('popupDiv');	
			if(activeCalendar != null){ 
					activeCalendar.set('tween', {duration: 250});
					activeCalendar.fade('out');
			}
			activeCalendar = popupDiv;
			popupDiv.set('tween', {duration: 250});
			popupDiv.setStyles({'display':'block','opacity':'0'});
			popupDiv.fade('in');
		});
		
		var ulAppointmentsLarge = createListScheduleItems(arrDates[i].arrSchedule, true);
		ulAppointmentsLarge.inject(popupDiv, 'bottom');
		
		popupDiv.inject(popupContainer,'top');
		
		var ulAppointments = createListScheduleItems(arrDates[i].arrSchedule, false);
		ulAppointments.inject(td, 'bottom');
	}
	dateTbody.inject(dateTable,'top');
	dateTable.inject(popupCalendar,'bottom');
	
	popupCalendar.set('morph',{duration:350});
	popupCalendar.setStyle('opacity',0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ($(window).getScroll().y.toInt() + ($(window).getSize().y.toInt() / 2) - (popupCalendar.getSize().y.toInt() / 2)) - 50));
		popupCalendar.setStyle('top', newPos+"px");
		popupCalendar.morph({'opacity':1});
	}
	setPosition.delay(120);
	
}

var popupLesrooster;
function createLesRoosterPopup(strRoosterNaam, intPosition, strIngangsDatum, arrCategories, arrKlassen, arrLeerlingen, objRooster){
	
	if( (typeOf(popupLesrooster) == 'object' || typeOf(popupLesrooster) == 'element') ){
		if($(popupLesrooster)){
			$(popupLesrooster).destroy();
			popupLesrooster = null;
		}
	}

	popupLesrooster = createGeneralPopup('Lesrooster', 'big', false, false);
	popupLesrooster.addClass('clsPopupSchedule').addClass('clsPopupClassSchedule').setStyles({'z-index':'103'});

	
	var popupIcon  = new Element('div').addClass('clsIconClipBoard').addClass('clsIconInPopupPos');			
	popupIcon.inject(popupLesrooster,'top');
	
	var popupScheduleBox = new Element('div').addClass('clsPopupScheduleBlock');
	popupScheduleBox.setProperty('html','<div class="clsIconCalendarSmall clsIconInPopupSchedule"></div><div class="clsPopupScheduleDate">'+strRoosterNaam+'</div>'); // vanaf '+strIngangsDatum+'</div>');
	popupScheduleBox.inject(popupLesrooster,'bottom');
	
	var popupScheduleBox1 = new Element('div').addClass('clsPopupScheduleBlock');
	popupScheduleBox1.setProperty('html', '<div class="clsIconUserSmall clsPropertiesOfIconToTxt clsPropertiesOfIconUser" ></div>');
	popupScheduleBox1.inject(popupLesrooster,'bottom');
	
	var selectSection = createSelectBox('idLesRoosterCategory', arrCategories, function(){ getLesrooster($(this).options[$(this).selectedIndex].value, intPosition, strRoosterNaam,strIngangsDatum);});
	//selectSection.addClass('clsFloatLeft');
	selectSection.inject(popupScheduleBox1,'bottom');
	
	new Element('div').addClass("clsIconArrowInlineRightGray").setStyles({'margin-left':'5px'}).inject(popupScheduleBox1,'bottom');
	
	var selectKlas = createSelectBox('idLesRoosterKlas', arrKlassen, function(){ getLesrooster($('idLesRoosterCategory').options[$('idLesRoosterCategory').selectedIndex].value, intPosition, strRoosterNaam,strIngangsDatum, $(this).options[$(this).selectedIndex].value);});
	//selectKlas.addClass('clsFloatLeft');
	selectKlas.inject(popupScheduleBox1,'bottom');
	
	if(arrLeerlingen != null){
		new Element('div').addClass("clsIconArrowInlineRightGray").setStyles({'margin-left':'5px'}).inject(popupScheduleBox1,'bottom');
		var selectLeerling = createSelectBox('idLesRoosterLeerlingen', arrLeerlingen, function(){ getLesrooster($('idLesRoosterCategory').options[$('idLesRoosterCategory').selectedIndex].value, intPosition, strRoosterNaam,strIngangsDatum, $('idLesRoosterKlas').options[$('idLesRoosterKlas').selectedIndex].value, $(this).options[$(this).selectedIndex].value );});
		//selectLeerling.addClass('clsFloatLeft');
		selectLeerling.inject(popupScheduleBox1,'bottom');
	}
	
	new Element('div').addClass('clsClear').inject(popupScheduleBox1,'bottom');
	
	if(objRooster != null){
		var strRoosterTitel = 'Lesrooster van ';
				
		if(objRooster.header.voornaam){
			strRoosterTitel = strRoosterTitel + objRooster.header.voornaam;
		}
		if(objRooster.header.achternaam){
			strRoosterTitel = strRoosterTitel + " "+objRooster.header.achternaam+", ";
		}
		if(objRooster.header.klas){
			strRoosterTitel = strRoosterTitel + " "+objRooster.header.klas;
		}
		
		if(objRooster.rooster){
			var arrAllData 	 = new Array();
			arrAllData[0] = new Array(
								{strText:'Uur'},
								{strText:'Maandag'},
								{strText:'Dinsdag'},
								{strText:'Woensdag'},
								{strText:'Donderdag'},
								{strText:'Vrijdag'}		
							);
			
			for(var i=1; i<10; i++){
				arrAllData[i] = new Array(
						{strText:''+i},
						{arrEntries: objRooster.rooster['1'][''+i]},
						{arrEntries: objRooster.rooster['2'][''+i]},
						{arrEntries: objRooster.rooster['3'][''+i]},
						{arrEntries: objRooster.rooster['4'][''+i]},
						{arrEntries: objRooster.rooster['5'][''+i]}		
				);
			}
		}
		var strMessage = '';
		if(objRooster.messages){
			for(var i=0; i<objRooster.messages.length; i++){
				for(property in objRooster.messages[i]){
					strMessage = strMessage + '<strong>'+property+':</strong>';
					strMessage = strMessage + '<div style="font-size:0.9em; padding-bottom:5px;">'+objRooster.messages[i][''+property]+'</div>';
				}
			}
			
		}

		new Element('div').addClass('clsClear').inject(popupScheduleBox1,'bottom');
		
		var popupScheduleBox2 = new Element('div').addClass('clsPopupScheduleBlock');
		popupScheduleBox2.setProperty('html','<div class="clsArrowGreenMedium"></div><div class="clsScheduleOwner">'+strRoosterTitel+'</div><div class="clsClear"></div>');
		popupScheduleBox2.inject(popupLesrooster, 'bottom');
		
		var roosterContainer = new Element('div').setStyles({'height':'0px','overflow':'hidden'});
		var tableSchedule = new Element('table').addClass('clsScheduleTable').addClass('clsClassScheduleTable');
		var tbodyElement = new Element('tbody').inject(tableSchedule,'top');
		
		for(var i=0; i<arrAllData.length; i++){
			if(i%2 == 0){
				var tblRow = new Element('tr').addClass('clsScheduleRowOdd');
			} else {
				var tblRow = new Element('tr');
			}
			createRowForSchedule( arrAllData[i], tblRow, (i==0 ? true : false), (i > 0 ? true : false));
			tblRow.inject(tbodyElement, 'bottom');
		}
		tableSchedule.inject(roosterContainer,'bottom');
		roosterContainer.inject(popupLesrooster,'bottom');
		
		roosterContainer.set('morph',{duration:350, onComplete:function(){roosterContainer.setStyle('height','auto');}});
		var scrollInRooster = function(){
			roosterContainer.morph({'height':tableSchedule.getSize().y.toInt()});
			
		}
		scrollInRooster.delay(120);
		
		if(strMessage.clean() != ''){
			new Element('div').addClass('clsClear').inject(popupScheduleBox1,'bottom');
			var popupScheduleBox3 = new Element('div').addClass('clsPopupScheduleBlock');
			popupScheduleBox3.setProperty('html',''+strMessage+'');
			popupScheduleBox3.inject(popupLesrooster, 'bottom');
			new Element('div').addClass('clsClear').inject(popupScheduleBox3, 'bottom');
		}
		
		var popupScheduleBox4 = new Element('div').addClass('clsPopupScheduleBlock');
		
		
		
		
		var linkContainer = new Element('div');
		
		var emailContainer = new Element('div').setStyles({'float':'right','width':'300px'});
		var inputEmailAdres = new Element('input').addClass('clsInput').setStyle('color','#666666').set('id','idEmailField').set('name','idEmailField');
		inputEmailAdres.value = 'je emailadres';
		
		inputEmailAdres.addEvent('focus', function(){
			if(this.value == 'je emailadres'){
				this.value = '';
				this.setStyle('color','#000000');
			} 
		});
		
		inputEmailAdres.addEvent('blur', function(){
			if(this.value == ''){
				this.value = 'je emailadres';
				this.setStyle('color','#666666');
			} 
		});
		
		inputEmailAdres.inject(emailContainer,'bottom');
		new Element('div').addClass('clsIconMailSmall').addClass('clsIconInPopupPos').inject(emailContainer, 'bottom').addEvent('click',function(){mailRooster();return false;});
		new Element('a').addClass('clsLinkUnderSchedule').set('href','#').setProperty('text', 'Rooster e-mailen').inject(emailContainer, 'bottom').addEvent('click',function(){mailRooster();return false;});
		
		emailContainer.inject(linkContainer);
		
		if(typeOf($('idLesRoosterLeerlingen')) == 'element'){
			var strParams = '&ll_nr='+$('idLesRoosterLeerlingen').options[$('idLesRoosterLeerlingen').selectedIndex].value;
		} else {
			var strParams = '&klas='+$('idLesRoosterKlas').options[$('idLesRoosterKlas').selectedIndex].value;
		}
		new Element('div').addClass('clsIconNewIcon').addClass('clsIconInPopupPos').inject(linkContainer, 'bottom').addEvent('click',function(){self.location.href='/dynamic_pdf/?type=pdf'+strParams;});
		new Element('a').addClass('clsLinkUnderSchedule').set('href','/dynamic_pdf/?type=pdf'+strParams).setProperty('text', 'Rooster afdrukken').inject(linkContainer, 'bottom');
		
		new Element('div').addClass('clsClear').inject(linkContainer, 'bottom');
		linkContainer.inject(popupScheduleBox4, 'bottom');
		
		popupScheduleBox4.inject(popupLesrooster, 'bottom');
		
		new Element('div').addClass('clsClear').inject(popupScheduleBox4, 'bottom');
		
	}
	
	popupLesrooster.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ((intPosition+200) - (popupLesrooster.getSize().y.toInt() / 2)) ));
		popupLesrooster.setStyle('top', newPos+"px");
		popupLesrooster.setStyle('opacity', 1);
	}
	setPosition.delay(120);	
	
	return popupLesrooster;
}


function createListScheduleItems(arrSchedule, popup){
	if(popup === false){
		var appointmentUL = new Element('ul').addClass('clsScheduleBigItemList');
		if (arrSchedule != null) {
			for (var i=0;i<arrSchedule.length;i++){
				var objSchedule = arrSchedule[i];
				var size = 128;
				var timeDiv = '';
				var scheduleRow = new Element('li').setStyle('clear','both');
				if(objSchedule.strStartTime != null){
					size = 100;
					timeDiv = '<div style="float:right;color:#59391a;">'+objSchedule.strStartTime+'</div>';
				} 
				scheduleRow.setProperty('html','<div style="width:129px;">'+timeDiv+'<div style="width:'+size+'px; height:18px; overflow:hidden; float:left;">'+objSchedule.strTitle+'</div></div>');
				scheduleRow.inject(appointmentUL,'bottom');
			}
		}
		return appointmentUL;
	} else {
		var appointmentUL = new Element('ul').addClass('clsScheduleBigItemListPopup');
		if (arrSchedule != null) {
			for (var i=0;i<arrSchedule.length;i++){
				var objSchedule = arrSchedule[i];
				
				var divAdditions = new Element('div');
				
				var size = 220;
				var subSize = 60;
				if(objSchedule.strContent.clean() != ''){
					var divIconInfo = new Element('div').addClass("clsIconInfoSmallest").addEvent('click',function(){getCalendarItem($(this).retrieve('intCalendarId'));return false;});
					divIconInfo.store('intCalendarId',objSchedule.intId);
					divIconInfo.inject(divAdditions,'top');
				}
				if(objSchedule.intIsChanged == 1){
					var divIconChanged = new Element('div').addClass("clsIconChangedSmallest");
					divIconChanged.inject(divAdditions,'bottom');
				}
				var timeDiv = new Element('div').setStyles({'color':'#59391a','position':'relative','float':'left','height':'15px','width':'33px'});
				if(objSchedule.strStartTime != null){
					timeDiv.setProperty('html',objSchedule.strStartTime);
				}
				timeDiv.inject(divAdditions,'top');
				divAdditions.setStyles({'position':'absolute','right':'0px','width':subSize+'px'});
				
				var divContainer = new Element('div').setStyles({'width':'288px','position':'relative'});
				var scheduleRow = new Element('li').setStyles({'clear':'both', 'position':'relative'});
				var clsClass = '';
				if (i%2 == 0) {
					divContainer.addClass("clsScheduleRowOdd")
				}
				
				divAdditions.inject(divContainer, 'top');
				
				new Element('div').setStyles({'width':size+'px','overflow':'hidden','float':'left'}).setProperty('html',objSchedule.strTitle).inject(divContainer,'bottom');
				new Element('div').addClass("clsClear").inject(divContainer, 'bottom');
				divContainer.inject(scheduleRow,'top');
				scheduleRow.inject(appointmentUL,'bottom');
			}
		}
		return appointmentUL;
	}
}
function createAttachmentsList(arrFiles){
	// start attachments
	var attachmentUL = new Element('ul').addClass('clsScheduleFilesList');
	if (arrFiles != null) {
		for (var i=0;i<arrFiles.length;i++){
			var objFile = arrFiles[i];
			var fileRow = new Element('li');
			if (i%2 == 0) {
				fileRow.addClass('clsScheduleRowOdd');
			}
			if(objFile.filedesc.clean().length > 0){
				objFile.filedesc = '<br/><div class="clsFileComment">'+objFile.filedesc.clean()+'</div>';
			}
			fileRow.setProperty('html','<div class="clsPosIconFileIcon clsIcon_'+objFile.filetype+'"></div><a href="'+objFile.fileurl+'">'+objFile.filename+'</a><span>&nbsp;&nbsp;'+getHumanSize(objFile.filesize)+' ('+objFile.filetype+')</span>'+objFile.filedesc);
			fileRow.inject(attachmentUL,'bottom');
		}
	}
	return attachmentUL;
}

function createGeneralPopup(strTitle, strTitleType, strSubTitle, strSubTitleType, blnForceNew){
	
	var	popupBox   = new Element('div').addClass('clsPopup');

	var popupClose = new Element('div', {events:{click: function(){ closeOverlay();$(this).getParent().destroy();}}}).addClass('clsIconClose').addClass('clsIconCloseInPopupPos');

	if(strTitle !== false && strTitle.clean() != ''){
		if(strTitleType == 'normal')
			var popupTitle	= new Element('div').addClass('clsPopupSchemaTitle');
		else 
			var popupTitle	= new Element('div').addClass('clsPopupSchemaTitleBig');
		popupTitle.setProperty('html',strTitle);
		popupTitle.inject(popupBox,'top');
	}	
	
	if(strSubTitle !== false && strSubTitle.clean() != ''){
		if(strSubTitleType == 'bold')
			var popupSubTitle	= new Element('div').addClass('clsSubTitleInPopup');
		else
			var popupSubTitle	= new Element('div').addClass('clsSubTitleItalicInPopup');
		popupSubTitle.setProperty('html',strSubTitle);
		popupSubTitle.inject(popupBox,'bottom');
	}
	
	popupClose.inject(popupBox,'top');
    
	var elemBody = $(document.body);
	popupBox.inject(elemBody, 'bottom');
	
	return popupBox;
}

var popupNews;
function createNewsPopup(strDeepLink, strTitle, strSummary, strContent, strMediaType, strMediaUrl, arrFiles, arrMedia){
	if( (typeOf(popupNews) == 'object' || typeOf(popupNews) == 'element') ){
		if($(popupNews)){
			$(popupNews).destroy();
			popupNews = null;
		}
	}
	
	popupNews = new Element('div').addClass('clsPopup').addClass('clsPopupMedia');
	
	popupTitleBig		= new Element('div').addClass('clsPopupSchemaTitleBig');
	popupTitleBig.setProperty('html',strTitle);
	
	var popupClose = new Element('div', {events:{click: function(){ closeOverlay();$(this).getParent().destroy();}}}).addClass('clsIconClose').addClass('clsIconCloseInPopupPos');
	
	popupTitleBig.inject(popupNews,'top');
	popupClose.inject(popupNews,'top');
	
	if (strMediaType == 'movie' || strMediaType == 'image' || strMediaType == 'youtube'){
		var popupBackground = new Element('div').addClass('clsPopupBackgroundMedia');
		var popupMediaInner = new Element('div').addClass('clsPopupMediaInner');
		var popupMedia = new Element('div').addClass('clsPopupMediaPlayer');
		
		if(strMediaType == 'youtube'){
			var objPlayer = new Swiff('http://www.youtube.com/v/'+strMediaUrl+'?hl=nl_NL&fs=1&rel=0&showinfo=0', {
		    	id		  : 'youtubeVideoPlayer',
		    	container : popupMedia,
		        width	  : 282, 
		        height	  : 205, 
		        params	  : {wMode: 'transparent',
		        			allowFullScreen:'true',
		        			allowScriptAccess:'always'}
		    });
			//var strFile = '<embed src="http://www.youtube.com/v/'+strMediaUrl+'?hl=en&fs=1&rel=0&showinfo=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="280" height="183">'+'</embed>';
		} else if(strMediaType == 'movie'){
			var strFile = '<iframe style="background-color:#dbdbdb;overflow:hidden;" allowtransparency="true" class="hvc-player"'+' type="text/html"'+' width="282"'+' height="205"'+' scrolling="no" src="/video.php?autoStart=false&videoURL='+strMediaUrl+'" frameborder="0"'+'>'+
							'</iframe>';
			popupMedia.setProperty('html',strFile);
		} else if(strMediaType == 'image'){
			var strFile = '<img src="'+strMediaUrl+'" style="width:280px;" />';
			popupMedia.setProperty('html',strFile);
		}
		
		popupMedia.inject(popupBackground,'bottom');
		
		//var popupArrow  = new Element('div').addClass('clsPopupArrow');
		//var popupBGMedia = new Element('div').addClass('clsPopupBackgroundMedia');
		//popupBGMedia.inject(popupNews,'bottom');
		
	} else if(strMediaType == 'gallery'){
		popupNews.setStyle('width','940px');
		var popupMediaInner = new Element('div').setStyles({'width':'260px','float':'right'});
		
		var popupBackground = new Element('div');
		
		new Element('div').addClass("clsPopupMediaTabSelected").set('id','idFilmStripText').setProperty('html','Filmstrip').setStyle('cursor','pointer').inject(popupNews,'bottom').addEvent('click',function(){
								$('idOverzichtTab').setStyle('display','none');
								$('idFilmStripTab').setStyle('display','block');
								$('idOverzichtText').removeClass('clsPopupMediaTabSelected').addClass('clsPopupMediaTab');
								$('idFilmStripText').removeClass('clsPopupMediaTab').addClass('clsPopupMediaTabSelected');
								
							});
		new Element('div').addClass("clsPopupMediaTab").set('id','idOverzichtText').setProperty('html','Overzicht').setStyle('cursor','pointer').inject(popupNews,'bottom').addEvent('click',function(){
								$('idOverzichtTab').setStyle('display','block');
								$('idFilmStripTab').setStyle('display','none');
								$('idOverzichtText').removeClass('clsPopupMediaTab').addClass('clsPopupMediaTabSelected');
								$('idFilmStripText').removeClass('clsPopupMediaTabSelected').addClass('clsPopupMediaTab');
							});
							 
		
		new Element('div').addClass("clsClear").inject(popupNews,'bottom');
		
		var divMediaTab   = new Element('div').addClass('clsPopupMediaTabContent').set('id','idFilmStripTab');
		var divGradient   = new Element('div').addClass('clsPopupMediaGradientTab').inject(divMediaTab, 'top');
		var divArrowLeft  = new Element('div').addClass('clsArrowLeftGray').addClass('clsPosArrowInFilmStripLeft').set('title','Vorige').inject(divMediaTab,'bottom');
		var divArrowRight = new Element('div').addClass('clsIconArrowRightGray').addClass('clsPosArrowInFilmStripRight').set('title','Volgende').inject(divMediaTab,'bottom');
		
		
		var divOverzichtTab   = new Element('div').addClass('clsPopupMediaTabContent').set('id','idOverzichtTab');//.setStyle('display','none');
		var divGradientOverzicht   = new Element('div').addClass('clsPopupMediaGradientTab').inject(divOverzichtTab, 'top');
		var divArrowLeftOverzicht  = new Element('div').addClass('clsArrowLeftGray').addClass('clsPosArrowInFilmStripLeft').setStyle('top','300px').set('title','Vorige').inject(divOverzichtTab,'bottom');
		var divArrowRightOverzicht = new Element('div').addClass('clsIconArrowRightGray').addClass('clsPosArrowInFilmStripRight').setStyle('top','300px').set('title','Volgende').inject(divOverzichtTab,'bottom');
		
		var divOverzichtCont = new Element('div').setStyles({'position':'relative','width':'610px','height':'480px','top':'5px','left':'8px','overflow':'hidden', 'z-index':'1'}).inject(divOverzichtTab,'bottom');
		var aantalPerRegel   = Math.max(Math.ceil(arrMedia.length / 4), 5);
		var divOverzichtScrl = new Element('div').setStyles({'position':'absolute','left':'0px','top':'0px','width':((aantalPerRegel * 120)+10)+'px','height':'480px'}).inject(divOverzichtCont,'top');
		
		
		var divBGItems       = new Element('div').addClass('clsMediaFilmStripBackgroundItems').inject(divMediaTab,'bottom');
		var divArrowLeftWh   = new Element('div').addClass('clsArrowLeftWhite').addClass('clsMediaFilmStripItemsArrowLeftPos').inject(divBGItems,'top');
		var divThumbnailCont = new Element('div').addClass('clsMediaFilmStripBackgroundInner').setStyles({'overflow':'hidden'}).inject(divBGItems,'bottom');
		
		
		
		var divArrowRightWh  = new Element('div').addClass('clsArrowRightWhite').addClass('clsMediaFilmStripItemsArrowRightPos').inject(divBGItems,'bottom');
		
		
		divMediaTab.inject(popupNews,'bottom');
		
		var allImages = arrMedia;
		var arrImagesToLoad = new Array();
		var arrImageElements = new Array();
		var arrImageOverzicht = new Array();
		var arrLargeImageElements = new Array();
		var intCurrentItem = 0;
		var intActiveItem = 0;
		
		var intCurrentOverzichtItem = 0;
		
		/* Other Alignment if there are less items*/
		if(allImages.length <= 10) {
			var divAllImagesScrl = new Element('div').setStyles({'margin-left':'auto','margin-right':'auto','width':((58*arrMedia.length)+4)+'px'});
		} else {
			var divAllImagesScrl = new Element('div').setStyles({'position':'absolute','top':'0','left':'0','width':((58*arrMedia.length)+4)+'px'});
		}
		divAllImagesScrl.inject(divThumbnailCont,'top');
		
		
		for(var i=0; i<allImages.length; i++){
			arrImagesToLoad[i] = allImages[i].strFileUrl;
			arrImageElements[i] = new Element('img').store('index',i).set('src','/images/loader.gif').setStyles({'float':'left','width':'32px','height':'32px','padding':'9px','margin-right':'8px'}).inject(divAllImagesScrl, 'bottom');

			arrImageOverzicht[i] = new Element('img').store('index',i).set('src','/images/loader.gif').setStyles({'float':'left','width':'32px','height':'32px','padding':'34px','margin':'10px'}).inject(divOverzichtScrl, 'bottom');
			if(((i+1)%aantalPerRegel) == 0 && i != 0){
				new Element('div').addClass('clsClear').inject(divOverzichtScrl,'bottom');
			}
			
			arrImageElements[i].addEvent('click',function(){
				
				arrImageElements[intActiveItem].setStyle('border','');
				arrLargeImageElements[intActiveItem].morph({'opacity':0});
				
				intActiveItem = this.retrieve('index');
				arrLargeImageElements[intActiveItem].morph({'opacity':1});
				arrImageElements[intActiveItem].setStyle('border','1px solid #FFF');
				
				
				doFadeIn(divArrowLeft);
				doFadeIn(divArrowRight);
				if(intActiveItem == 0){
					doFadeOut(divArrowLeft);

				} else if(intActiveItem == (arrImageElements.length - 1)) {
					doFadeOut(divArrowRight);
				}
				
			});
			
			
			arrImageOverzicht[i].addEvent('click',function(){
				
				$('idOverzichtTab').setStyle('display','none');
				$('idFilmStripTab').setStyle('display','block');				
				$('idOverzichtText').removeClass('clsPopupMediaTabSelected').addClass('clsPopupMediaTab');
				$('idFilmStripText').removeClass('clsPopupMediaTab').addClass('clsPopupMediaTabSelected');
				
				arrImageElements[intActiveItem].setStyle('border','');
				arrLargeImageElements[intActiveItem].morph({'opacity':0});
				
				intActiveItem = this.retrieve('index');
				arrLargeImageElements[intActiveItem].morph({'opacity':1});
				arrImageElements[intActiveItem].setStyle('border','1px solid #FFF');
								
				doFadeIn(divArrowLeft);
				doFadeIn(divArrowRight);
				if(intActiveItem == 0){
					doFadeOut(divArrowLeft);
				} else if(intActiveItem == (arrImageElements.length - 1)) {
					doFadeOut(divArrowRight);
				}				
			});
			
		}
		var divImgFader = new Element('div').addClass('clsMediaImageFader');
		divImgFader.inject(divMediaTab,'bottom');
		var objLoader = new Asset.images(arrImagesToLoad, {
	 			onProgress: function(counter,index) {
	 				$(arrImageElements[index]).setProperty('src', arrMedia[index].strFileThumbUrl).setStyles({'width':'50px','cursor':'pointer','height':'50px','padding':'0px'});
	 				$(arrImageOverzicht[index]).setProperty('src', arrMedia[index].strFileThumbUrl).setStyles({'width':'100px','cursor':'pointer','height':'100px','padding':'0px'});
	 				if(index == 0){
	 					arrLargeImageElements[index] = new Element('div');
	 					new Element('img').setProperty('src', arrMedia[index].strFileUrl).setStyle('position','static').inject(arrLargeImageElements[index]);
	 					arrImageElements[index].setStyle('border','1px solid #FFF');
	 				} else {
	 					arrLargeImageElements[index] = new Element('div');
	 					new Element('img').setProperty('src', arrMedia[index].strFileUrl).setStyle('position','static').inject(arrLargeImageElements[index]);
	 				}
	 				arrLargeImageElements[index].setStyles({'position':'absolute','top':(((460 - arrMedia[index].intHeight) / 2)+10)+'px','left':((640 - arrMedia[index].intWidth) / 2)+'px'}).inject(divImgFader,'bottom');
	 				if(index == 0){
	 					arrLargeImageElements[index].setStyles({'opacity':'1'});
	 				} else {
	 					arrLargeImageElements[index].setStyles({'opacity':'0'});
	 				}
	 				arrLargeImageElements[index].set('morph', {duration: 300});
	 				
	 			},
				onComplete: function() {
					
				}
		}); 
		
		
		divOverzichtTab.setStyle('display','none');
		divOverzichtTab.inject(popupNews,'bottom');
		/*if there is no posibility to navigate*/
		if(allImages.length <= 11) {
			divArrowRightWh.setStyle('display','none');
			divArrowLeftWh.setStyle('display','none');
		}
		
		divAllImagesScrl.set('morph', {duration: 300});

		divArrowLeftWh.addEvent('click',function(){
			if(intCurrentItem > 0){
				var intNrOfElements = Math.min(intCurrentItem, 10);
				
				divAllImagesScrl.morph({'left':(-1*(intCurrentItem-intNrOfElements)*58)});
				intCurrentItem = intCurrentItem - intNrOfElements;
				doFadeIn(divArrowRightWh);
				if(intCurrentItem == 0){
					doFadeOut(divArrowLeftWh);
				} else {
					doFadeIn(divArrowLeftWh);
				}
			}
		});
		doFadeOut(divArrowLeftWh);
		
		divArrowRightWh.addEvent('click',function(){
			if((arrMedia.length - intCurrentItem) > 11){
				var intNrOfElements = Math.min((arrMedia.length - intCurrentItem - 11), 10);
				
				divAllImagesScrl.morph({'left':(-1 * (intCurrentItem+intNrOfElements)*58)});
				intCurrentItem = intCurrentItem + intNrOfElements;
				doFadeIn(divArrowLeftWh);
				if(intCurrentItem == (arrMedia.length - 11)){
					doFadeOut(divArrowRightWh);
				} else {
					doFadeIn(divArrowRightWh);
				}
			} 
		});
		
		
		
		divArrowLeftOverzicht.addEvent('click',function(){
			if(intCurrentOverzichtItem > 0){
				
				var aantalPerKeer = Math.min(intCurrentOverzichtItem, 4);
				
				divOverzichtScrl.morph({'left':(-1*(intCurrentOverzichtItem-aantalPerKeer)*120)});
				intCurrentOverzichtItem = intCurrentOverzichtItem - aantalPerKeer;
				doFadeIn(divArrowRightOverzicht);
				if(intCurrentOverzichtItem == 0){
					doFadeOut(divArrowLeftOverzicht);
				} else {
					doFadeIn(divArrowLeftOverzicht);
				}
			}
		});
		doFadeOut(divArrowLeftOverzicht);
		
		
		divArrowRightOverzicht.addEvent('click',function(){
			if((aantalPerRegel - intCurrentOverzichtItem) > 5){
				var aantalPerKeer = Math.min((aantalPerRegel - intCurrentOverzichtItem), 4);
				
				divOverzichtScrl.morph({'left':(-1*(intCurrentOverzichtItem+aantalPerKeer)*120)});
				intCurrentOverzichtItem = intCurrentOverzichtItem + aantalPerKeer;
				doFadeIn(divArrowLeftOverzicht);
				if(intCurrentOverzichtItem == (aantalPerRegel - 5)){
					doFadeOut(divArrowRightOverzicht);
				} else {
					doFadeIn(divArrowRightOverzicht);
				}
			}
		});
		
		
		
		divArrowLeft.addEvent('click',function(){
			if(intActiveItem > 0){
				arrImageElements[intActiveItem].setStyle('border','');
				arrLargeImageElements[intActiveItem].morph({'opacity':0});
				arrLargeImageElements[(intActiveItem - 1)].morph({'opacity':1});
				arrImageElements[(intActiveItem - 1)].setStyle('border','1px solid white');
				doFadeIn(divArrowRight);
				intActiveItem = intActiveItem - 1;
				if(intActiveItem == 0){
					doFadeOut(divArrowLeft);
				} else {
					doFadeIn(divArrowLeft);
				}
			}
		});
		doFadeOut(divArrowLeft);
		
		divArrowRight.addEvent('click',function(){
			if((arrMedia.length - intActiveItem) > 1){
				arrLargeImageElements[intActiveItem].morph({'opacity':0});
				arrImageElements[intActiveItem].setStyle('border','');
				arrLargeImageElements[(intActiveItem + 1)].morph({'opacity':1});
				arrImageElements[(intActiveItem + 1)].setStyle('border','1px solid white');
				doFadeIn(divArrowLeft);
				intActiveItem = intActiveItem + 1;
				if(intActiveItem == (arrMedia.length - 1)){
					doFadeOut(divArrowRight);
				} else {
					doFadeIn(divArrowRight);
				}
			}
		});
		
	}
	

	
	var elemBody = $(document.body);
	popupNews.inject(elemBody, 'bottom');
	
	var allContent = new Element('div').setStyles({'position':'relative','padding-bottom':'50px','margin-bottom':'10px'});
	allContent.setProperty('html',strContent).inject(popupMediaInner,'bottom');
	
	if(arrFiles != null && arrFiles.length > 0){
		var ulAttachments = createAttachmentsList(arrFiles); 
		ulAttachments.inject(allContent, 'bottom');
	}
		
	allContent.inject(popupMediaInner, 'bottom');
	
	var socialBoxItems = new Element('div').addClass('clsMediaSocialIconsPosWide');
	if(strMediaType == 'gallery'){
		socialBoxItems.setStyle('width','250px');
	}
	var strHref = encodeURI(strDeepLink);

	var intFacebookWidth = 360;
	if(strMediaType == 'gallery'){
		intFacebookWidth = 260;
	}
	
	var hyves = new Element('iframe').setProperty('src','http://www.hyves.nl/respect/button?url='+encodeURI(strDeepLink)+'');
	hyves.setProperty('scrolling','no').setProperty('frameborder','0').setStyles({'border':'none','overflow':'hidden','width':intFacebookWidth+'px','height':'21px'}).set('allowTransparency','true');
	hyves.inject(socialBoxItems, 'bottom');
	
	var facebook = new Element('iframe').setProperty('src','http://www.facebook.com/plugins/like.php?href='+encodeURI(strDeepLink)+'&amp;layout=button_count&amp;show_faces=false&amp;width='+intFacebookWidth+'&amp;action=like&amp;colorscheme=light&amp;height=27');
	facebook.setProperty('scrolling','no').setProperty('frameborder','0').setStyles({'border':'none','overflow':'hidden','width':intFacebookWidth+'px','height':'27px'}).set('allowTransparency','true');
	facebook.inject(socialBoxItems, 'bottom');
	
	socialBoxItems.inject(popupMediaInner,'bottom');

	popupMediaInner.inject(popupBackground,'bottom');
	new Element('div').addClass('clsClear').inject(popupBackground,'bottom');
	
	popupBackground.inject(popupNews,'bottom');
	
	popupNews.set('morph', {duration: 350});
	popupNews.setStyle('opacity', 0);
	var setPosition = function(){
		var newPos = Math.round(Math.max(180, ($(window).getScroll().y.toInt() + ($(window).getSize().y.toInt() / 2) - (popupNews.getSize().y.toInt() / 2)) - 50));
		popupNews.setStyle('top', newPos+"px");
		popupNews.morph({'opacity':1});
	}
	setPosition.delay(120);	
	
}
function doFadeOut(elem){
	elem.setStyle('opacity',0.3);
	if(Browser.ie6 || Browser.ie7 || Browser.ie8){
		elem.setStyle('display','none');
	}
}
function doFadeIn(elem){
	if(Browser.ie6 || Browser.ie7 || Browser.ie8){
		elem.setStyle('display','block');
	}
	elem.setStyle('opacity',1);
}
function mailRooster(){
	if($('idEmailField').value.indexOf('@') != -1 && 
			$('idEmailField').value.indexOf('.') != -1 ){
		if(typeOf($('idLesRoosterLeerlingen')) == 'element'){
			var strParams = 'type=email&ll_nr='+$('idLesRoosterLeerlingen').options[$('idLesRoosterLeerlingen').selectedIndex].value+'&email='+$('idEmailField').value;
		} else {
			var strParams = 'type=email&klas='+$('idLesRoosterKlas').options[$('idLesRoosterKlas').selectedIndex].value+'&email='+$('idEmailField').value;
		}
		var myRequest = new Request.JSON({
	    		onSuccess: function(objReturn){
	    			if(objReturn != null){
	    				alert(''+objReturn);
	    			}
	    		}, url: '/dynamic_pdf/'});
	    myRequest.send({ method: 'get', data: strParams	});
	} else {
		alert('Dit lijkt een ongeldig email adres te zijn');
	}
}

