function reportBrokenVideoclip() {
	this.a = null;
	this.tooltip = null;
	this.dialogid = 'dialog';
	this.urlformpart = null;
	this.connectorid = null;
	this.target = null;
	this.requestTimestamp = null;
	this.faderuntime = 1500;
	this.showHint = function(e) {
		e.preventDefault();
		this.createToolTipDialog();
		dijit.popup.open({ around: this.a, popup: this.tooltip });
		this.connectorid = dojo.connect(document, 'onclick', this, "close");
	}
	this.hideHint = function(e) {
		dijit.popup.close(this.tooltip);
	}
	this.close = function(e) {
		id = e.target && e.target.id ? e.target.id : null;		
		if (id != this.a.id && !(dojo.isDescendant(e.target, dojo.byId(this.dialogid)))) {
			this.hideHint();
			dojo.disconnect(this.connectorid);
		}
	}
	this.postForm = function() {
		query = dojo.formToQuery(dojo.byId('reportbroken'));
		dojo.xhrPost( { // 
	        url: dijit.byId('reportbroken').action, 
	        postData: query,
	        handleAs: "text",
	        timeout: 5000,
	        targetId: this.target,
	        requestTimestamp: this.requestTimestamp,
	        parent: this,
	        load: function(response,ioArgs) {
   	          	this.parent.handleResponse(response);
	          return response;
	        },
	        error: function(response, ioArgs) { // 
	          console.error("HTTP status code: ", ioArgs.xhr.status);
	          return response; 
	          }
	        });
	}
	this.handleResponse = function(response) {
      	iTimeout = 1;
     	iDelta = new Date() - this.requestTimestamp;
     	if (iDelta < this.faderuntime) iTimeout = this.faderuntime - iDelta;
     	this.showLink(iTimeout, response);
	}
	this.hideLink = function() {
		dojo.fadeOut({
			node: this.target,
			duration: this.faderuntime
		}).play();
		this.requestTimestamp = new Date();
	}
	this.showLink = function(iTimeout, response) {
		animation =	dojo.fadeIn({
			node: this.target,
			duration: this.faderuntime,
			onBegin: function() {
						dojo.byId(this.node).innerHTML = response;
					}
				});
		setTimeout(dojo.hitch(animation, "play"), iTimeout); 
	}
	this.createToolTipDialog = function() {
		if (!this.tooltip) {
			this.tooltip = new dijit.TooltipDialog({}, this.dialogid);
		}
	}
	this.submit = function(e) {
		e.preventDefault();
		if (dijit.byId('reportbroken').validate()) {
			this.hideLink();
			this.postForm();
			this.hideHint();
			dojo.disconnect(this.connectorid);
		}
	}
	this.init = function(a) {
		this.a = dojo.byId(a);
		if (!this.a) return false;
		this.target = this.a.parentNode;
		submitbutton = dojo.byId('reportbrokenSubmit');
		dojo.connect(submitbutton, 'onclick', this, "submit");
		if (dojo.isIE) {
		 	submitbutton.value = 'Rapporteer';
		}
		dojo.connect(this.a, 'onclick',  this, "showHint");
		return true;
	}
}
function generalForm() {
	this.a = null;
	this.aCache = null;
	this.tooltip = null;
	this.dialogid = null;
	this.formid = null;
	this.submitbutton = '';
	this.submitValue = null;
	this.form = null;
	this.urlformpart = null;
	this.connectorid = null;
	this.aConnectors = new Array();
	this.target = null;
	this.requestTimestamp = null;
	this.faderuntime = 1500;
	// to actually post form
	this.performPost = true;
	// to make link disappear after attempt at form submit
	this.performHidelink = true;
	this.setPerformPost = function(b) {
		this.performPost = b ? true : false;
	}
	this.getPerformPost = function() {
		return this.performPost ? true : false;
	}
	this.setPerformHidelink = function(b) {
		this.performHidelink = b ? true : false;
	}
	this.getPerformHidelink = function() {
		return this.performHidelink ? true : false;
	}
	this.showHint = function(e) {
		e.preventDefault();
		this.createToolTipDialog();
		dijit.popup.open({ around: this.a, popup: this.tooltip, orient: ['TR'] });
		if (this.connectorid) dojo.disconnect(this.connectorid);
		this.connectorid = dojo.connect(document, 'onclick', this, "close");
	}
	this.hideHint = function(e) {
		dijit.popup.close(this.tooltip);
	}
	this.close = function(e) {
		id = e.target && e.target.id ? e.target.id : null;		
		if (id == this.formid) {
			if (this.getPerformHidelink()) {
				this.hideLink();
			}
			if (this.getPerformPost()) {
				this.postForm();
			}
			this.hideHint();
			dojo.disconnect(this.connectorid);
		} else if (id != this.a.id && !(dojo.isDescendant(e.target, dojo.byId(this.dialogid)))) {
			this.hideHint();
			dojo.disconnect(this.connectorid);
		}
	}
	this.processForm = function(e) {
		e.preventDefault();
		if (dijit.byId(this.formid).isValid()) {
			this.close(e);
		} else {
			dijit.byId(this.formid).validate();
		}
	}
	this.postForm = function() {
		query = dojo.formToQuery(this.form);
		dojo.xhrPost( { // 
	        url: this.form.action+'/format/html', 
	        postData: query,
	        handleAs: "text",
	        timeout: 5000,
	        targetId: this.target,
	        requestTimestamp: this.requestTimestamp,
	        parent: this,
	        load: function(response,ioArgs) {
   	          	this.parent.handleResponse(response);
	          return response;
	        },
	        error: function(response, ioArgs) { // 
	          console.error("HTTP status code: ", ioArgs.xhr.status);
	          return response; 
	          }
	        });
	}
	this.handleResponse = function(response) {
		iTimeout = this.calcFaderTimeout();
     	this.showLink(iTimeout, response);
	}
	this.calcFaderTimeout = function() {
      	iTimeout = 1;
     	iDelta = new Date() - this.requestTimestamp;
     	if (iDelta < this.faderuntime) iTimeout = this.faderuntime - iDelta;
		return iTimeout;	
	}
	this.hideLink = function() {
		dojo.fadeOut({
			node: this.target,
			duration: this.faderuntime
		}).play();
		this.requestTimestamp = new Date();
	}
	this.showLink = function(iTimeout, response) {
		animation =	dojo.fadeIn({
			node: this.target,
			duration: this.faderuntime,
			ref: this,
			onBegin: function() {
						dojo.byId(this.node).innerHTML = response;
					},
			onEnd: function() {
					setTimeout(dojo.hitch(this.ref, "resetLink"),5);
				}
				});
		setTimeout(dojo.hitch(animation, "play"), iTimeout); 
	}
	this.resetLink = function() {
		this.hideLink();
		iTimeout = this.calcFaderTimeout();
		animation =	dojo.fadeIn({
		node: this.target,
		duration: this.faderuntime,
		value: this.aCache,
		ref: this,
		onBegin: function() {
					dojo.byId(this.node).innerHTML = this.value;
				},
		onEnd: function() { 
					setTimeout(dojo.hitch(this.ref, "init"),5);
				}
			});
		setTimeout(dojo.hitch(animation, "play"), iTimeout);
	}
	this.createToolTipDialog = function() {
		if (!this.tooltip) {
			this.tooltip = new dijit.TooltipDialog({}, this.dialogid);
		}
	}
	this.init = function(a, dialogid, formid, sbmvalue) {
		// re init from within, use already set values
		if (typeof a !='object' && this.a) {
			a = this.a.id;
			dialogid = this.dialogid;
			formid = this.formid;
			sbmvalue = this.submitValue;
		}
		this.a = dojo.byId(a);
		this.dialogid = dialogid;
		this.formid = formid;
		this.submitValue = sbmvalue;
		this.aCache = this.a.parentNode.innerHTML;
		if (!this.a) return false;
		this.form = dojo.byId(this.formid);
		this.target = this.a.parentNode;
		this.submitbutton = dojo.byId(this.formid+'Submit');
		if (dojo.isIE) {
		 	this.submitbutton.value = this.submitValue;
		}
		this.form.reset();
		if (this.aConnectors.length > 0) {
			for (i = 0, iLen = this.aConnectors.length; i < iLen; i++) {
				dojo.disconnect(this.aConnectors[i]);
			}
		}
		this.aConnectors[0] = dojo.connect(this.form, 'onsubmit', dojo.hitch(this, 'processForm'));
		this.aConnectors[1] = dojo.connect(this.a, 'onclick',  dojo.hitch(this, "showHint"));
		return true;
	}
}
function downloadVideoclip() {
	this.aConnectors = new Array();
	this.formId = null;
	this.loadingIconId = null;
	this.connectorId = null;
	this.quality = new Array();
	this.init = function(formId,loadingIconId) {
		this.formId = formId;
		this.loadingIconId = loadingIconId;
		this.aConnectors[0] = dojo.connect(dojo.byId('download'), 'onclick',  dojo.hitch(this, "getAvailableQualities"));
		this.aConnectors[1] = dojo.connect(dojo.byId(formId), 'onsubmit',  dojo.hitch(this, "processForm"));
	}
	this.prepareLoad = function() {
		form = dojo.byId(this.formId);
		for (i = 0, iLen = form.elements.length; i < iLen; i++) {
			e = dijit.byId(form.elements[i].id);
			e.attr('disabled', true);
		}
		dojo.disconnect(this.aConnectors[0]);
	}
	this.getAvailableQualities = function() {
		tracker.trackPageView(dojo.byId(this.formId).action); 
		this.prepareLoad();
		dojo.xhrGet( { // 
	        url: dojo.byId(this.formId).action+'/format/json', 
	        handleAs: "json",
	        timeout: 60000,
	        parent: this,
	        load: function(response,ioArgs) {
			      	this.parent.handleLoaded(response);
	          return response;
	        },
	        error: function(response, ioArgs) { // 
	          console.error("HTTP status code: ", ioArgs.xhr.status);
	          return response; 
	          }
	    });
	}
	this.processForm = function() {
		form = dojo.byId(this.formId);
		sQuality = null;
		for (i = 0, iLen = form.elements.length; i < iLen; i++) {
			e = form.elements[i];
			e = dijit.byId(e.id);
			if (e.type != 'radio' || !e.checked) continue;
			sQuality = e.value;
		}
		if (typeof sQuality != 'null' && this.quality[sQuality]) {
			ifrm = document.createElement('iframe');
			ifrm.src = this.quality[sQuality];
			ifrm.style.display = 'none';
			document.body.appendChild(ifrm);
		}
	}
	this.handleLoaded = function(response) {
		//dojo.byId(this.loadingIconId).style.display = 'none';
		form = dojo.byId(this.formId);
		this.quality = response;
		qualityStr = 'downloadQuality-';
		qA = qualityStr+'720p';
		qB = qualityStr+'mp4';
		qC = qualityStr+'flv';
		for (i = 0, iLen = form.elements.length; i < iLen; i++) {
			element = dijit.byId(form.elements[i].id);
			id = element.id;
			switch (id) {
				case qA:
					if (false != this.quality['720p']) {
						element.attr('disabled', false);
					}
				break;
				case qB:
					if (false != this.quality['mp4']) {
						element.disabled = false;
						element.attr('disabled', false);
					}
				break;
				case qC:
					if (false != this.quality['flv']) {
						element.disabled = false;
						element.attr('disabled', false);
					}
				break;
				default:
					element.disabled = false;
					element.attr('disabled', false);
			}
		}
	}
}
function initFocus() {
	this.id = null;
	this.init = function(id) {
		if (id) {
			this.id = id;
			this.initFocus();
		} else {
			this.firstFocussableField();
		}
	}
	this.initFocus = function() {
		dojo.addOnLoad(dojo.hitch(this, 'focus'));	
	}
	this.focus = function() {
		el = dojo.byId(this.id);
		el.focus();
	}
	this.firstFocussableField = function() {
		for (var x = 0, xLen = document.forms.length; x < xLen; x++) {
			for (var i = 0, iLen = document.forms[x].elements.length; i < iLen; i++) {
				el = document.forms[x].elements[i];
				if (el.id && el.focus) {
					this.id = el.id;
					this.initFocus();
					return;
				}
			}
		}
	}
}
function submitFormOnEnterPress(event, sForm) {
	if (event.keyCode == 13) {
		if (dijit.byId(sForm).isValid()) {
			dijit.byId(sForm).submit();
		}
	}
}
function autoPlay() {
	this.id = null;
	this.uri = null;
	this.init = function(id) {
		this.id = id;
		el = dojo.byId(this.id);
		this.uri = el.href.replace(/\/auto\/[on|off]+/, '');
		dojo.connect(el, 'onclick',  dojo.hitch(this, "request"));
	}
	this.request = function(e) {
		e.preventDefault();
		s = dojo.byId(this.id).href;
		dojo.xhrGet( { // 
	        url: s, 
	        handleAs: "json",
	        timeout: 10000,
	        caller: this,
	        load: function(response,ioArgs) {
			      	this.caller.toggle();
	          return response;
	        },
	        error: function(response, ioArgs) { // 
	          console.error("HTTP status code: ", ioArgs.xhr.status);
	          return response; 
	          }
	    });
	}
	this.toggle = function() {
		re = /off/;
		if (re.test(dojo.byId(this.id).href)) {
			this.setAutoplayOff();
		} else {
			this.setAutoplayOn();
		}
	}
	this.setAutoplayOn = function() {
		uri = this.uri + '/auto/off';
		el = dojo.byId(this.id);
		el.innerHTML = 'Autoplay uit';
		el.href = uri;
	}
	this.setAutoplayOff = function() {
		uri = this.uri + '/auto/on';
		el = dojo.byId(this.id);
		el.innerHTML = 'Autoplay aan';
		el.href = uri;
	}
}
function googleTranslate(srcEl, destEl) {
	var text = dojo.byId(srcEl).value;
	var magicSep = '__333__';
	text = text.replace(/\n/g, "<br>");
	text = wordwrap(text, 1000, magicSep, false);
	aText = text.split(magicSep);
	
	google.language.detect(aText[0], function(result) {
	    if (!result.error && result.language) {
	    	for (i = 0, iLen = aText.length; i < iLen; i++) {
	      google.language.translate(aText[i], result.language, "nl",
	                                function(result) {
	        var translated = dojo.byId(destEl);
	        if (result.translation) {
	          text = result.translation.replace(/<br><br>/g, "\n\n");
	          text = text.replace(/ <br> /g, "\n");
	          text = text.replace(/<br> /g, "\n");
	          text = text.replace(/\n /g, "\n");
	          text = decodeURIComponent(text);
	          
	          text = text.replace(/&#(\d+);/g,
	        		 function(wholematch, parenmatch1) {
	        		  return String.fromCharCode(+parenmatch1);
	        		  });
	          text = unescapeHtml(text);
	          translated.value += text;
	         // translated.innerHTML = text;
	        }
	      });
	    	} // for
	    }
	});	
}
function unescapeHtml(s) {
	var namedEntities= {amp:'&',lt:'<',gt:'>',quot:'"',euro:String.fromCharCode(8364)};
	return s.replace(/&(amp|lt|gt|quot|euro|#\d+);/g,function(c,n){
		return namedEntities[n]||String.fromCharCode(n.substr(1))}
	);
}
function wordwrap(str, width, brk, cut ) {
    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;
    if (!str) { return str; }
    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
    return str.match( RegExp(regex, 'g') ).join( brk );
 
}