/* POPUP */

document.popup_show_background = 0;
document.popup_bg_onclick = null;

function showConfirm(text, yesFunction, noFunction) {
	if(document.popup_show_background <= 1) ++document.popup_show_background;

	document.getElementById("popup_bg").style.display = "block";
	document.getElementById("popup_bg").style.height = getDocHeight() + "px";

	if(document.getElementById("popup_bg").onclick) {
		document.popup_bg_onclick = document.getElementById("popup_bg").onclick;
	}
	document.getElementById("popup_bg").onclick = null;

	document.getElementById("popup").style.display = "block";
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight/2)) + 'px';
	} else {
		document.getElementById("popup").style.top = (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight/2) + 'px';
	}

	document.getElementById("popup_text").innerHTML = text;

	document.yesFunction = yesFunction;
	document.noFunction = noFunction;

	document.getElementById("popup_b1").style.display = "";
	document.getElementById("popup_b2").style.display = "";

	if(typeof(noFunction) == 'undefined') {
		document.getElementById("popup_b3").style.display = "none";
	} else {
		document.getElementById("popup_b3").style.display = "";
	}

	document.getElementById("popup_b4").style.display = "none";

}

function showMessage(text) {
	if(document.popup_show_background <= 1) ++document.popup_show_background;
	
	document.getElementById("popup_bg").style.display = "block";
	document.getElementById("popup_bg").style.height = getDocHeight() + "px";

	if(document.getElementById("popup_bg").onclick) {
		document.popup_bg_onclick = document.getElementById("popup_bg").onclick;
	}
	document.getElementById("popup_bg").onclick = null;

	document.getElementById("popup").style.display = "block";
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight/2)) + 'px';
	} else {
		document.getElementById("popup").style.top = (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight/2) + 'px';
	}

	document.getElementById("popup_text").innerHTML = text;

	document.getElementById("popup_b1").style.display = "none";
	document.getElementById("popup_b2").style.display = "none";
	document.getElementById("popup_b3").style.display = "none";
	document.getElementById("popup_b4").style.display = "";
}

function hidePopup() {
	--document.popup_show_background;

	if(document.popup_bg_onclick) {
		document.getElementById("popup_bg").onclick = document.popup_bg_onclick;
	}

	document.getElementById("popup").style.display = "none";
	if(document.popup_show_background <= 0) document.getElementById("popup_bg").style.display = "none";
}

function popupButton(id) {
	hidePopup();
	if(id == 1) eval(document.yesFunction);
	if(id == 2) eval(document.noFunction);
}

/* FILE SELECTOR */

function showFileselector() {
	if(document.popup_show_background <= 1) ++document.popup_show_background;
	
	document.getElementById("popup_bg").style.display = "block";
	document.getElementById("popup_bg").style.height = getDocHeight() + "px";
	
	document.getElementById("fileselector").style.display = "block";
	
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("fileselector").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("fileselector").offsetHeight/2)) + 'px';
	} else {
		document.getElementById("fileselector").style.top = (document.body.clientHeight/2 - document.getElementById("fileselector").offsetHeight/2) + 'px';
	}

	document.getElementById("popup_bg").onclick = hideFileselector;

	getFileselectorMedia();
	setTimeout(initFileselectorSWFUpload, 500);
}

function hideFileselector() {
	if(document.mediamanager_uploading) {
		showMessage(document.mediamanager_wait_text);
	} else if(!document.mediamanager_selecting) {
		--document.popup_show_background;
		
		document.getElementById("fileselector").style.display = "none";
		document.getElementById("popup_bg").onclick = null;

		if(document.popup_show_background <= 0) document.getElementById("popup_bg").style.display = "none";
	}
	document.mediamanager_swfu.cleanUp();
}

function getFileselectorMedia(category) {
	if(!document.mediamanager_uploading) {
		$('fileselector_media').innerHTML = '<div class="loading"><span>Laden..</span></div>';
		
		var select_extra = '?select_filetypes=' + document.mediamanager_select_filetypes;
		if(document.mediamanager_type == 'image') select_extra = '?select_width=' + document.mediamanager_select_width + '&select_height=' + document.mediamanager_select_height;
		
		new Request({url: document.path + "/index.php" + select_extra, onSuccess: function(text){
			var data = text.split('/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/_/-/');
			$('fileselector_trail').innerHTML = data[0];
			$('fileselector_media').innerHTML = data[1];
			eval(data[2]);
		}}).get({
			'module' : 'media',
			'sub' : 'media_frame',
			'tab' : (document.mediamanager_type == 'image' ? 0 : 1),
			'frame_width' : 485,
			'frame_height' : 386,
			'select' : document.mediamanager_select,
			'category': ((isInt(category) && category > 0) ? category : false)
		});
	} else {
		showMessage(document.mediamanager_wait_text);
	}
}

function initFileselectorSWFUpload() {
	if(!document.mediamanager_swfu) {
		var settings = {
			flash_url : document.path + "/includes/swfupload/swfupload.swf",
			flash9_url : document.path + "/includes/swfupload/swfupload_fp9.swf",
			upload_url: document.path + "/modules/media/custom/media_actions.php?action=upload&type=" + document.mediamanager_type,
			post_params: {"session_id" : document.mediamanager_session_id },
			file_size_limit : "10 MB",
			file_types : (document.mediamanager_type == 'image' ? '*.jpg;*.jpeg;*.png;*.gif' : '*.pdf;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.zip;*.rar;*.eps;*.wav;*.aac;*.mp3;*.mp4;*.swf;*.avi;*.mpg;*.mpeg;*.flv;*.vcf'),
			file_types_description : (document.mediamanager_type == 'image' ? 'Afbeeldingen' : 'Documenten'),
			file_upload_limit : 100,
			file_queue_limit : 0,
			custom_settings : {
				type: document.mediamanager_type,
				position: 0,
				resize_width: document.mediamanager_scale_width,
				resize_height: document.mediamanager_scale_height
			},
			debug: false,

			button_width: "41",
			button_height: "17",
			button_placeholder_id: "fileselector_swfu_browse",
			button_text: '<span class="sfwu_font">+ item</span>',
			button_text_style: ".sfwu_font { font-family: verdana; font-size: 10; color: #ffffff; }",
			button_text_left_padding: 0,
			button_text_top_padding: 0,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			swfupload_preload_handler : preLoad,
			swfupload_load_failed_handler : loadFailed,
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			queue_complete_handler : queueComplete
		};
		
		document.mediamanager_swfu = new SWFUpload(settings);
	} else {
		var fileTypes = document.mediamanager_type == 'image' ? '*.jpg;*.jpeg;*.png;*.gif' : '*.pdf;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.zip;*.rar;*.eps;*.wav;*.aac;*.mp3;*.mp4;*.swf;*.avi;*.mpg;*.mpeg;*.flv;*.vcf';
		document.mediamanager_swfu.setUploadURL(document.path + "/modules/media/custom/media_actions.php?action=upload&type=" + document.mediamanager_type);
		document.mediamanager_swfu.setFileTypes(fileTypes, (document.mediamanager_type == 'image' ? 'Afbeeldingen (' + fileTypes + ')' : 'Documenten (' + fileTypes + ')'));
		document.mediamanager_swfu.customSettings = {
				type: document.mediamanager_type,
				resize_width: document.mediamanager_scale_width,
				resize_height: document.mediamanager_scale_height
			};
	}
}

/* GENERAL */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function goTo(url) {
	if(typeof(goToExtended) == 'function') {
		goToExtended(url);
	} else {
		window.location = url;
	}
}

function setObjectOpacity(object, value) {
	object.style.opacity = value/10;
	object.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

function isInt(x) {
	var y=parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
}

function trim(value) {
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}

function in_array(needle, haystack, argStrict) {
	var key = '', strict = !!argStrict;
	if (strict) {
		for (key in haystack) {
			if (haystack[key] === needle) {
				return true;
			}
		}
	} else {
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		}
	}
	return false;
}

function removeHTMLTags(value){
	strInputCode = value.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return trim(strTagStrippedText);
}

function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');

	var counter = document.createElement('div');
	counter.className = 'counter';
	counter.style.display = 'block';

	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	this.changed = (this.startValue != escape(this.value));

	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'counter_toomuch';
		this.value = this.value.substring(0, maxLength);
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
}

function textareaTab(e,el) {
	//9 is the tab key, except maybe it's 25 in Safari?
	if(e.keyCode==9){
		var oldscroll = el.scrollTop;
		e.returnValue = false;
		//Check if we're in a firefox deal
		if (el.setSelectionRange) {
			var pos_to_leave_caret=el.selectionStart + 1;
			el.value = el.value.substring(0, el.selectionStart) + '\t' + el.value.substring(el.selectionEnd, el.value.length);
			setTimeout("var t=document.getElementById('" + el.id + "'); t.focus(); t.setSelectionRange(" + pos_to_leave_caret + ", " + pos_to_leave_caret + ");", 0);
		}
		//Handle IE
		else {
			document.selection.createRange().text='\t';
		}
	}
}

function checkVersion() {
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 7) {
		var old_version = document.createElement('div');
		old_version.setAttribute('id', 'old_version');
		old_version.setAttribute('class', 'old_version');
		old_version.className = 'old_version';
		
		old_version.innerHTML = '<div class="info"></div><span>U maakt gebruik van een verouderde browser. Voor een optimale weergave raden wij u aan uw browser te updaten. <a class=\"icon_firefox\" target="_blank" href="http://www.mozilla.com/firefox"></a><a class=\"icon_ie\" target="_blank" href="http://www.microsoft.com/netherlands/windows/internet-explorer/"></a></span>';
		
		document.body.insertBefore(old_version,document.body.firstChild);
	}
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
	{
		string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		prop: window.opera,
		identity: "Opera"
	},
	{
		string: navigator.vendor,
		subString: "iCab",
		identity: "iCab"
	},
	{
		string: navigator.vendor,
		subString: "KDE",
		identity: "Konqueror"
	},
	{
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	},
	{
		string: navigator.vendor,
		subString: "Camino",
		identity: "Camino"
	},
	{	// for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	},
	{
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	},
	{
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	},
	{ 	// for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}
	],
	dataOS : [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
	]

};
BrowserDetect.init();
