var req, X, Y;
var _curImage = 1;
var _curCollection = 1;
var _thumbnailLink, _thumbnail;
var _resizeInterval, _isResizing;

/* AJAX meuk*/

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		var temp;
		req.onreadystatechange = temp = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			var activiteit = document.getElementById('activiteit');
			var activiteitDivs = activiteit.getElementsByTagName("div");
			activiteitDivs[1].innerHTML = req.responseText;		//set de inhoud
			activiteitDivs[0].style.display = 'none';			//loader weg
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}


/*MOUSE XY meuk*/

function mouseXY(e){

var ev=(!e)?window.event:e;//IE:Moz
	if (ev.pageX){//Moz
		X=ev.pageX;//+window.pageXOffset;
		Y=ev.pageY;//+window.pageYOffset;
	}
	else if(ev.clientX){//IE
		if(document.documentElement){//IE 6+ strict mode
		X=ev.clientX+document.documentElement.scrollLeft;
		Y=ev.clientY+document.documentElement.scrollTop;
		}
		else if(document.body){//Other IE
		X=ev.clientX+document.body.scrollLeft;
		Y=ev.clientY+document.body.scrollTop;
		}
	}
	
else{return false}//old browsers

activiteitPopup();
}

function activiteitPopup(){
	var activiteit = document.getElementById('activiteit');
	activiteit.style.top = (Y+3) + 'px';
	activiteit.style.left = (X-parseInt(activiteit.style.width)-3) + 'px';
}

function showPopup(show, date){
	var activiteit = document.getElementById('activiteit');
	var activiteitDivs = activiteit.getElementsByTagName("div");
	
	if(show && activiteit.style.display == 'none'){
		activiteit.style.display = 'block';		//alvast laten zien
		loadXMLDoc("index.php?page=agenda&date=" + date);	//laadt het agenda item
	}
	else if(!show){
		activiteit.style.display = 'none';			//div weghalen
		activiteitDivs[0].style.display = 'block';	//loader voor de volgende keer aanzetten
		activiteitDivs[1].innerHTML = '';			//inhoud leegmaken
	}
}

function plusComment(){
	document.getElementById('author').disabled = false;
	document.getElementById('comment').disabled = false;
	document.getElementById('plusCommentImg').style.display = 'none';
	document.getElementById('pluscomment').style.display = 'block';
}

function evaluateComment(){
	var author = document.getElementById('author');
	var comment = document.getElementById('comment');
	var valid = true;
	
	if(author.value == ''){
		author.style.border = '1px solid #EE0000';
		valid = false;
	}
	else{
		author.style.border = '';
	}
	
	if(comment.value == ''){
		comment.style.border = '1px solid #EE0000';
		valid = false;
	}
	else{
		comment.style.border = '';
	}
	
	return valid;
}

function evaluateMessage(){
	var author = document.getElementById('author');
	var message = document.getElementById('message');
	var valid = true;
	
	if(author.value == ''){
		author.style.border = '1px solid #EE0000';
		valid = false;
	}
	else{
		author.style.border = '';
	}
	
	if(message.value == ''){
		message.style.border = '1px solid #EE0000';
		valid = false;
	}
	else{
		message.style.border = '';
	}
	
	return valid;
}

function showComments(){
	setTimeout("popComments('comments', 0)",40);
}

function popComments(id, curCount){
	var comments = document.getElementById(id);
	var subDivs = comments.getElementsByTagName('div');
	
	if(!subDivs || subDivs.length == 0)
		return;
	
	if(('comment' + curCount) == subDivs[subDivs.length-1].id){
		document.getElementById('commentsButton').style.margin = '0px 0px 0px 0px';
		return;
	}
		
	subDivs[curCount].style.display = 'block';
	
	curCount++;
	
	setTimeout("popComments('"+id+"',"+curCount+")",40);
}

function loadPicture(dir, picture, curImage){
	var imageLoader = document.getElementById('imageLoader');
	imageLoader.style.visibility = 'visible';
	
	var imageDiv = document.getElementById('imageDiv');
	var image = document.getElementById('image');
	if(parseInt(curImage) != 0)
		_curImage = parseInt(curImage);
		
	if(picture == ''){
		var thumbnailDiv = document.getElementById('thumbnail' + _curImage);
		var thumbnail = thumbnailDiv.getElementsByTagName('img')[0];
		picture = thumbnail.title;
		
		//if(thumbnail.src == '')
			//thumbnail.style.visibility = 'hidden';
		//else
			//thumbnail.style.visibility = 'visible';
	}
	
	var imageResizeDiv = document.getElementById('imageResize');
	
	if(imageResizeDiv.style.display == 'block'){
		resizeImage(dir);
	
	}else{
		var img = new Image();
		img.src = 'thumbnail.php?pic=' + dir + picture + '&width=350&co=100';
	
		image.src = img.src;
		image.title = picture;
	}
}

function previousPic(dir){
	if(_curImage == 1 && _curCollection == 1)
		return;
	
	_curImage--;
	
	if(_curImage < 1){
		previousCollection(dir);
	}else{
		var thumbnailDiv = document.getElementById('thumbnail' + (_curImage + (_curCollection * 20)));
		if(!thumbnailDiv){
			_curImage++;
			return;
		}
		var thumbnail = thumbnailDiv.getElementsByTagName('img')[0];
		loadPicture(dir, thumbnail.title, _curImage);
	}
}

function nextPic(dir){	
	_curImage++;
	
	if(_curImage > 20){
		nextCollection(dir);
	}else{
		var thumbnailDiv = document.getElementById('thumbnail' + (_curImage + (_curCollection * 20)));
		if(!thumbnailDiv){
			_curImage--;
			return;
		}
		var thumbnail = thumbnailDiv.getElementsByTagName('img')[0];
		loadPicture(dir, thumbnail.title, _curImage);
	}
}

function nextCollection(dir){
	_curCollection++;
	var thumbnailDiv = document.getElementById('thumbnail' + ((_curCollection * 20) + 1));
	if(!thumbnailDiv){
		_curCollection--;
		return;
	}
	_curImage = 1;
	_flushCollectionLinks();
	setTimeout("_flushImages(1,'" + dir + "',true);",50);
}

function previousCollection(dir){
	_curCollection--;
	if(_curCollection == 0){
		_curCollection++;
		return;
	}
	_curImage = 20;
	_flushCollectionLinks();
	setTimeout("_flushImages(1,'" + dir + "',false);",50);
}

function goToCollection(dir,collection){
	_curCollection = collection;
	_curImage = 1;
	_flushCollectionLinks();
	setTimeout("_flushImages(1,'" + dir + "',true);",50);
}

function _flushCollectionLinks(){
	for(i=0; i < document.getElementById('numberedLinks').getElementsByTagName('a').length; i++){
		document.getElementById('numberedLinks').getElementsByTagName('a')[i].style.backgroundColor = '#FF7800';
		
		if(document.getElementById('numberedLinks').getElementsByTagName('a')[i].innerHTML == _curCollection)
			document.getElementById('numberedLinks').getElementsByTagName('a')[i].style.backgroundColor = '#FF9F55';
	}
}

function _clearAll(){
	for(i = 1; i <= 20; i++){
		var thumbDiv		= document.getElementById('thumbnail' + i);
		var thumbnail 		= document.getElementById('thumbnail' + i).getElementsByTagName('img')[0];
		var thumbnailLink	= document.getElementById('thumbnail' + i).getElementsByTagName('a')[0];
		
		thumbDiv.style.background = '#FF7800';
		thumbnail.src = '';
		thumbnailLink.href = '';
	}
}

function _flushRest(thumbnailCounter){
	
	var thumbnailDiv	= document.getElementById('thumbnail' + thumbnailCounter);
	var thumbnail 		= thumbnailDiv.getElementsByTagName('img')[0];
	var thumbnailLink	= thumbnailDiv.getElementsByTagName('a')[0];
	
	thumbnailDiv.getElementsByTagName("img")[0].style.visibility = 'hidden';
	
	
	if((thumbnailCounter + 1) <= 20){
		var nextThumbnailDiv = document.getElementById('thumbnail' + (thumbnailCounter + 1));
		nextThumbnailDiv.style.background = '#ffffff';
		nextThumbnailDiv.getElementsByTagName('img')[0].style.visibility = 'hidden';
	}
	
	thumbnailDiv.style.background = '#FF7800';
	thumbnail.src = '';
	thumbnailLink.href = '';
	
	thumbnailCounter++;
	
	if(thumbnailCounter > 20)
		return;
	
	setTimeout("_flushRest(" + thumbnailCounter + ")", 50);
}

function _flushImages(thumbnailCounter, dir){
	
	if(thumbnailCounter > 20){
		loadPicture(dir, '', _curImage);
		return;
	}
	
	var thumbnailDiv = document.getElementById('thumbnail' + thumbnailCounter);
	var thumbnailLink = thumbnailDiv.getElementsByTagName('a')[0];
	var thumbnail = thumbnailDiv.getElementsByTagName('img')[0];
	
		
	var hiddenThumbnailCounter = (_curCollection * 20) + thumbnailCounter;
	var hiddenThumbnailDiv = document.getElementById('thumbnail' + hiddenThumbnailCounter);
	
	if((thumbnailCounter + 1) <= 20){
		var nextThumbnailDiv = document.getElementById('thumbnail' + (thumbnailCounter + 1));
		nextThumbnailDiv.style.background = '#ffffff';
		nextThumbnailDiv.getElementsByTagName('img')[0].style.visibility = 'hidden';
	}
	
	if(!hiddenThumbnailDiv){
		loadPicture(dir, '', _curImage);
		setTimeout("_flushRest(" + thumbnailCounter + ")", 50);
		return;
	}
	
	
	thumbnailLink.href = hiddenThumbnailDiv.getElementsByTagName("a")[0].href;
	thumbnailDiv.style.background = '#FF7800';
	thumbnail.src = hiddenThumbnailDiv.getElementsByTagName('img')[0].src;
	thumbnail.title = hiddenThumbnailDiv.getElementsByTagName('img')[0].title;
	
	thumbnailCounter++;
	
	setTimeout("_flushImages(" + thumbnailCounter + ",'" + dir + "', " + nextCollection + ")", 50);
}

function resizeImage(dir){
	var thumbnailDiv = document.getElementById('thumbnail' + _curImage);
	var thumbnail = thumbnailDiv.getElementsByTagName('img')[0];
	
	var prev_sidebar   = document.getElementById('prev_sidebar');
	var next_sidebar   = document.getElementById('next_sidebar');
	
	prev_sidebar.style.visibility = 'hidden';
	next_sidebar.style.visibility = 'hidden';
	
	var imageLink = dir + thumbnail.title;
	
	var imageResizeDiv = document.getElementById('imageResize');
	var imageResizeImg = document.getElementById('imageResizeImg');
	imageResizeDiv.style.backgroundImage = 'url(images/loader-purple.gif)';
	imageResizeDiv.style.backgroundRepeat = 'no-repeat';
	imageResizeDiv.style.backgroundPosition = 'center';
	
	imageResizeImg.style.visibility = 'hidden';
	imageResizeImg.src = 'thumbnail.php?pic=' + imageLink + '&width=600&co=100'
	
	
	if(imageResizeDiv.style.display != 'block'){
		var image = document.getElementById('image');
		imageResizeDiv.style.width = parseInt(image.width) + 'px';
		imageResizeDiv.style.height = parseInt(image.height) + 'px';
		imageResizeDiv.style.display = 'block';
		imageResizeDiv.style.top = '420px';
		imageResizeDiv.style.left = parseInt(_clientWidth() / 2 - parseInt(image.width) / 2 - 11) + 'px';
		var subDivs = imageResizeDiv.getElementsByTagName('div');
		subDivs[0].style.visibility = 'hidden';
	}
	
	_isResizing = true;
}

function _resizeImageTo(){
	var imageResizeDiv = document.getElementById('imageResize');
	var imageResizeImg = document.getElementById('imageResizeImg');
	var prev_sidebar   = document.getElementById('prev_sidebar');
	var next_sidebar   = document.getElementById('next_sidebar');
	
	
	var widthInPlus = parseInt(imageResizeDiv.style.width) < parseInt(imageResizeImg.width) ? true : false;
	var heightInPlus = parseInt(imageResizeDiv.style.height) < parseInt(imageResizeImg.height) ? true : false;
	
	var width = parseInt(imageResizeImg.width);
	var height = parseInt(imageResizeImg.height);
	var oldHeight = parseInt(imageResizeDiv.style.height);
	
	
	if(parseInt(imageResizeDiv.style.width) == width && parseInt(imageResizeDiv.style.height) == height){
		var subDivs = imageResizeDiv.getElementsByTagName('div');
		subDivs[0].style.visibility = 'visible';
		//subDivs[1].style.visibility = 'visible';
		//subDivs[2].style.visibility = 'visible';
		
		prev_sidebar.style.visibility = 'visible';
		next_sidebar.style.visibility = 'visible';
		
		imageResizeImg.style.visibility = 'visible';
		clearInterval(_resizeInterval);
		_isResizing = false;
		return;
	}
		
	if(widthInPlus){
		var newWidth = parseInt(imageResizeDiv.style.width) + 40;
		if(newWidth > width)
			newWidth = width;
	}
	else{
		var newWidth = parseInt(imageResizeDiv.style.width) - 40;
		if(newWidth < width)
			newWidth = width;
	}
	
	if(heightInPlus){
		var newHeight = parseInt(imageResizeDiv.style.height) + 40;
		if(newHeight > height)
			newHeight = height;
	}
	else{
		var newHeight = parseInt(imageResizeDiv.style.height) - 40;
		if(newHeight < height)
			newHeight = height;
	}
	
	var heightDiff = newHeight - oldHeight;
	

	imageResizeDiv.style.left = (parseInt(_clientWidth() / 2) - parseInt(newWidth / 2)) + 'px';
	imageResizeDiv.style.width = newWidth + 'px';
	prev_sidebar.style.width = parseInt(newWidth / 2) + 'px';
	next_sidebar.style.width = parseInt(newWidth / 2) + 'px';
	

	imageResizeDiv.style.height = newHeight + 'px';
	imageResizeDiv.style.top = (parseInt(imageResizeDiv.style.top) - heightDiff / 2) + 'px';
	prev_sidebar.style.height = newHeight + 'px';
	next_sidebar.style.height = newHeight + 'px';	
}

function closeResizedImage(){
	var imageResizeDiv = document.getElementById('imageResize');
	
	if(!_isResizing){
		imageResizeDiv.style.background = '#FF7800';
		imageResizeDiv.style.display = 'none';
	}
}

function _clientWidth(){
	if (document.all){
		return document.body.clientWidth;
	}else{
		return window.innerWidth;
	}
}

function _clientHeight(){
	if (document.all){
		return document.body.clientHeight;
	}else{
		return window.innerHeight;
	}
}

function selectsmile(n){
	document.getElementById('smileselector1').className='';
	document.getElementById('smileselector2').className='';
	document.getElementById('smileselector3').className='';
	document.getElementById('smileselector4').className='';
	document.getElementById('smileselector5').className='';
	document.getElementById('smileselector6').className='';
	document.getElementById('smileselector' + n).className='selected';
	document.getElementById('commentsmile').value=n;
}

function updateExampleMessage(sourceInput){
	return false;
	var exampleMessageDiv = document.getElementById('exampleMessageDiv');
	var exampleName = document.getElementById('exampleName');
	var exampleMessage = document.getElementById('exampleMessage');
	var exampleDate = document.getElementById('exampleDate');
	
	exampleMessageDiv.style.display = 'block';
	
	if(sourceInput.type == 'text'){
		exampleName.innerHTML = sourceInput.value;
	}
	else{
		var text = sourceInput.value;
		exampleMessage.innerHTML = text.replace(/(\r\n|[\r\n])/g, "<br />");
	}
	
	var months = new Array("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December");
	
	var today=new Date();
	var hour=today.getHours();
	var minutes=today.getMinutes();
	var day = today.getDate();
	var month = months[today.getMonth()];
	var year = today.getFullYear();
	
	exampleDate.innerHTML = day + " " + month + " " + year + " om " + hour + ":" + minutes;
}

function selectAgendaItem(tdValue){
	var tdElem = document.getElementById('td' + tdValue);
	
	tdElem.style.backgroundColor = '#FF7966';
}

function updateDepTime(){
	var depTimeHour = document.getElementById('depTimeHour');
	var depTimeMin = document.getElementById('depTimeMin');
	var deptime = document.getElementById('deptime');
	
	deptime.value = depTimeHour.value + ':' + depTimeMin.value;
}


	
function makeFolder(naam, year){
	
	var folder = prompt('Kies een naam voor de activiteit', naam);
	
	if(folder!=null && folder!=''){
		var alphaExp = /^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*[^!@#$%\^&*()-_=+{}\[\]?`~"']$/;
		if(folder.match(alphaExp)){
			window.location = '?page=pictures&action=upload&year=' + year + '&activity=' + folder;
		}else{
			alert('Naam bevat ongeldige tekens');
			makeFolder(folder, year);
		}
	}
}


document.onmousemove=mouseXY;