// ### GENERAL ### //

// From http://www.dustindiaz.com/top-ten-javascript/
// Get classes searchClass in id searchId
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



// ### CLIENT-SERVER ### //

// Code from http://developer.apple.com/internet/webcontent/iframe.html
// Checks if iframe exists. If not, tries to create it. If it does, loads URL in it.

var IFrameObj; // our IFrame object
function callToServer(pageID, eventID, wbURL) {
	if (!document.createElement) {return true};
	var IFrameDoc;
	var URL = wbURL+'/templates/sg_stuff/load.php?page_id='+pageID+'&event_id='+eventID;
	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
	 try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='\<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer()',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	
	IFrameDoc.location.replace(URL);
	//return false;
}

function handleResponse(id) {
	if (IFrameObj.contentDocument) {
		// For NS6
		var contentVar = IFrameObj.contentDocument.getElementById('content');
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		var contentVar = IFrameObj.contentWindow.document.getElementById('content');
	} else if (IFrameObj.document) {
		// For IE5
		var contentVar = IFrameObj.document.getElementById('content');
	} else {
		return true;
	}
	
	//var targetVar = new getObj('content_'+1);
	//targetVar.obj.innerHTML = contentVar.innerHTML;
	document.getElementById('content_'+id).innerHTML = contentVar.innerHTML;

	// Makes links in content work
	checkLink();
	// Change buttons
	itemStyle(id, 10)
	// Reset loading var
	document.itemLoading = false;
}



// ### DISPLAY ### //

function agendaStatus(pageID, eventID, wbURL) {
	itemStatus(pageID, eventID, wbURL, true);
}

function infoStatus(eventID) {
	itemStatus('', eventID, '', false);
}

// Open or close right item.
// Status is FALSE (not yet set), open or closed.
// If loadStuff is true, stuff needs to be loaded, and another margin is used (bit dirty, I know...)
function itemStatus(pageID, eventID, wbURL, loadStuff) {

	// Change status
	if (eval("document.itemStatus" + eventID) == 'closed' || !eval("document.itemStatus" + eventID)) {

		eval("document.itemStatus" + eventID + "= 'open'");
		
		if (loadStuff) {
			// If another item is loading, close it
			if (document.itemLoading) {
				eval("document.itemStatus" + document.itemLoading + "= 'closed'");
				itemStyle(document.itemLoading)
			}

			// Start loading
			document.itemLoading = eventID;
			callToServer(pageID, eventID, wbURL);
		} else {
			// Change buttons
			itemStyle(eventID, 20);
			// Makes links in content work
			checkLink();
		}
		
	} else if (! document.checkLinkVar || document.checkLinkVar == false) { // checkLink: only if not al link is clicked

		eval("document.itemStatus" + eventID + "= 'closed'");


		if (loadStuff) {
			// Change buttons
			itemStyle(eventID, 10);

			// If this item is loading, stop loading
			if (document.itemLoading == eventID) document.itemLoading = false;

			// Clear content
			document.getElementById('content_'+eventID).innerHTML = '';
		} else {
			// Change buttons
			itemStyle(eventID, 20);
		}
	}

}

// (Margin diffres between info and agenda items)
function itemStyle(id, margin) {

	if (eval("document.itemStatus" + id) == 'open') {
		styleOpen = 'block';
		styleClosed = 'none';
		styleMargin = margin+'px';
	} else {
		styleOpen = 'none';
		styleClosed = 'block';
		styleMargin = (margin-10)+'px';
	}
	
	document.getElementById("item_"+id).style.marginBottom = styleMargin;
	document.getElementById('content_'+id).style.display = styleOpen;

	var showOpen = getElementsByClass("show_open", document.getElementById("item_"+id), "a");
	var showClosed = getElementsByClass("show_closed", document.getElementById("item_"+id), "a");
	for (i = 0; i < showOpen.length; i++) {
		showOpen[i].style.display = styleOpen;
	}
	for (i = 0; i < showClosed.length; i++) {
			showClosed[i].style.display = styleClosed;
	}
}

function itemOver(id, time, mouse) {
	if (time == 'future') {
		if (mouse == 'over') {
			col = '#999999';
			imgMore = meerImgOfuture.src;
			imgClose = sluitImgOfuture.src;
		} else {
			col = '#BBBBBB';
			imgMore = meerImgfuture.src;
			imgClose = sluitImgfuture.src;
		}
	} else {
		if (mouse == 'over') {
			col = '#DDDDDD';
			imgMore = meerImgOpast.src;
			imgClose = sluitImgOpast.src;
		} else {
			col = '#EEEEEE';
			imgMore = meerImgpast.src;
			imgClose = sluitImgpast.src;
		}
	}

	var butMore = getElementsByClass("but_more", document.getElementById("item_"+id), "img");
	butMore[0].src = imgMore;
	var butClose = getElementsByClass("but_close", document.getElementById("item_"+id), "img");
	butClose[0].src = imgClose;
	itemVar = document.getElementById('item_'+id);
	itemVar.style.backgroundColor = col;
}



// ### LINKS ### //

// Make a link in content work

// Original script by Peter-Paul Koch (www.xs4all.nl/~ppk)
// Dit script zorgt ervoor dat een link in het uitklap gedeelte blijft werken.
// D.w.z. dat als je op een link klikt de tekst niet inklapt,
// maar je naar de link gaat.
// Toevoeging 031204: werkt nu ook voor "input" (formulier)
function checkLink() {
	x = new Array();
	if (document.getElementsByTagName) {
		x[0] = document.getElementsByTagName('a');
		x[1] = document.getElementsByTagName('input');
	} else if (document.all) {
		x[0] = document.all.tags('a');
		x[1] = document.all.tags('input');
	}
	// twee arrays in elkaar (concat werkt niet...)
	for (var a=0; a<x.length; a++) {
		for (var b=0; b<x[a].length; b++) {
			// alleen als link nog geen onclick aktie heeft
			if (! x[a][b].onclick) {
				// LET OP: checkLinkVar ZONDER haakjes v. funktie!
				x[a][b].onclick = checkLinkSetVar;
			}
		}
	}
}

function checkLinkSetVar() {
	document.checkLinkVar = true;
	setTimeout('document.checkLinkVar = false;', 500);
}



// ### VARIOUS ### //

// Mail a friend
function mailaFriend(subject, body, imagepath, state) {
	subject = escape (subject);
	body = escape (body);
	document.write('<a href=\"mailto:?subject=' + subject + '&body=' + body + '\" class=\"show_open\"><img src=\"'+imagepath+'/b_mail_'+state+'.gif\" alt="mail" class=\"but_mail\" onMouseOver=\"javascript:this.src=mailImgO'+state+'.src;\" onMouseOut=\"javascript:this.src=mailImg'+state+'.src;\" /></a>');
}

function printPop(url) {
	newwindow = window.open(url,"name","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, height=420, width=390, top=90, left=15");
	if (window.focus) {
		newwindow.focus();
	}
	checkLinkSetVar();
}

// Image popup
// Original script by Peter Todorov (http://www.sitepoint.com/article/1022)
function popToImage(imageURL) {
	window.open("../templates/sg_stuff/image_popup.html?"+imageURL, "","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=200, height=200, top=90, left=15");
	checkLinkSetVar();
}

function popToImageBig(imageURL) {
	window.open("../templates/sg_stuff/image_popup_big.html?"+imageURL, "","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=200, height=200, top=90, left=15");
	checkLinkSetVar();
}