/**
 * General triggered behaviour on document load
 */
var general_rules = {
	'#analytics-placeholder':function(){
		var params = this.getClassParameters();
		if (params.sid) {
			window.pageTracker = _gat._getTracker(params.sid);
			if (params.target) {
				pageTracker._trackPageview(unescape(params.target));
			} else {
				pageTracker._trackPageview();
			}
		}
	},
	'.no-javascript' : function() {
		this.hide();
	},
	'.show-no-javascript' : function() {
		this.show();
	}
};
Event.addBehavior(general_rules);

/**
 * Rules for hyperlink behaviour
 */
var hyperlink_rules = {
	'a.void:click' : function(e) {
		return false;
	},
	'a.popup:click' : function(e) {
		/**
		 * Event:  click
		 * Action: open a popup window
		 */
		var params = this.getClassParameters();
		var width = params.width || 684;
		var height = params.height || 350;
		var top = params.top || 200;
		var left = params.left || '50%';
		window.open(this.href,
			'PopUp',
			'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',scrollbars=0,status=no,resizable=0,toolbar=0,titlebar=0,menubar=0,location=0');
		return false;
	},
	'a.external:click,a.new-window:click' : function(e) {
		/**
		 * Event:  click
		 * Action: open a new window
		 */
		if (!e.ctrlKey && !e.altKey && !e.shiftKey) {
			window.open(this.href);
			return false;
		}
	},
	'a.status:mouseover' : function(e) {
		/**
		 * Event:  mouseover
		 * Action: display the hyperlinks title in the status bar
		 */
		window.status=this.title;
		return true;
	},
	'a.status:mouseout' : function(e) {
		/**
		 * Event:  mouseout
		 * Action: clear the status bar
		 */
		window.status='';
		return true;
	},
	'a.switch:click': function(e) {
		/**
		 * Event:  click
		 * Action: show / hide the tab with the same ID minus "_switch"
		 */
		var c = $(this.id.replace('-switch',''));
		var content = $(c.id + '-content');
		if (c) {c.toggle();}
		return false;
	},
	'a.tabswitch:click' : function(e) {
		/**
		 * Event: click
		 * Action: hide related div, show related div's related element
		 */
		var container = $(this.getAttribute('rel'));
		if(container) {
			var other = $(container.getAttribute('rel'));
			if (other) {
				container.addClassName('hide');
				other.removeClassName('hide');
			}
		}
		return false;
	},
	'a.modal:click' : function(e) {
		/**
		 * Event:  click
		 * Action: open a zoomed in image
		 */
		var image = this.up().next().down('img');
		var dialog = new MUIL.Dialog.Image(image);
		return false;
	},
	'a.submit' : function(e) {
		/**
		 * Event: click
		 * Action: submit the form given as a param. (used by captcha)
		 */
		var params = this.getClassParameters();
		var form = $(params.form);
		if (form){
			form.submit();
		}
		return false;
	}
};
Event.addBehavior(hyperlink_rules);

/**
 * Behaviour rules for form elements
 */

var form_rules = {
	'form.overlabel' : function() {
		this.addClassName('overlabel-apply');
		this.removeClassName('overlabel');
	},
	'form.overlabel-apply label:click' : function(event) {
		// focus input field
		var field = this.getAttribute('for');
		if (field) {
			field.focus();
		}
	},
	'form.overlabel-apply input:focus' : function(event) {
		// hide label
		var label = $(this.form).down('label[for='+this.id+']');
		if (label) {
			label.setStyle({textIndent: '-10000px'});
		}
	},
	'form.overlabel-apply input:blur' : function(event) {
		if ('' == this.getValue()) {
			// show label if no value
			var label = $(this.form).down('label[for='+this.id+']');
			if (label) {
				label.setStyle({textIndent: '0px'});
			}
		}
	},
	'form.auto-submit' : function(e) {
		this.submit();
	},
	'select.auto-submit:change' : function(e) {
		this.form.submit();
	},
	/**
	 * Event:  blur
	 * Action: convert field value to upper case
	 */
	'input.auto-upper:blur' : function(e) {
		this.value = this.value.toUpperCase();
	},
	
	/**
	 * Event:  change
	 * Action: convert field value to upper case
	 */
	'input.auto-upper:change' : function(e) {
		this.value = this.value.toUpperCase();
	},
	'.default-value:blur' : function(e) {
		/**
		 * Event:  blur
		 * Action:
		 *   - when empty set default value
		 */
		if(this.value == ''){
			var params = this.getClassParameters();
			if(params.default_value != ''){
				this.value = params.default_value;
				this.addClassName('auto-clear');
			} 
		}
	},
	'input.auto_blur:focus' : function(e) {
		/**
		 * Event:  focus
		 * Action:
		 *   - replace field's classname from "-off" to "-on"
		 *   - if labeled, replace label's classname from "-off" to "-on"
		 *   - if label is image, replace image with "_hover" version
		 */
		this.className = this.className.replace('_off','_on');
		var fieldLabel = $(this.form).down('[for=' + this.id + ']');
		if (fieldLabel) {
			fieldLabel.className = fieldLabel.className.replace('_off','_on');
			var image = fieldLabel.down('img');
			if (image) {
				image.src = image.src.replace('_normal','_hover');
			}
		}
		// Check for grouped fields
		var fieldSet = this.up('fieldset');
		if (fieldSet && fieldSet.up('fieldset')) {
			var legend = fieldSet.down('legend');
			if (legend) {
				legend.className = legend.className.replace('_off', '_on');
			}
		}
	},
	'input.auto_blur:blur' : function(e) {
	
		/**
		 * Event:  blur
		 * Action:
		 *   - replace field's classname from "-on" to "-off"
		 *   - if labeled, replace label's classname from "-on" to "-off"
		 *   - if label is image, replace image with "_normal" version
		 */
		this.className = this.className.replace('_on','_off');
		var fieldLabel = $(this.form).down('[for=' + this.id + ']');
		if (fieldLabel) {
			fieldLabel.className = fieldLabel.className.replace('_on','_off');
			var image = fieldLabel.down('img');
			if (image) {
				image.src = image.src.replace('_hover','_normal');
			}
		}
		// Check for grouped fields
		var fieldSet = this.up('fieldset');
		if (fieldSet && fieldSet.up('fieldset')) {
			var legend = fieldSet.down('legend');
			if (legend) {
				legend.className = legend.className.replace('_on', '_off');
			}
		}
	},
	'.auto-clear:focus' : function(e) {
	
		/**
		 * Event:  focus
		 * Action:
		 *   - clear value
		 *   - remove auto-clear class
		 */
		if (this.hasClassName('auto-clear')) {
			this.value='';
		}
		this.removeClassName('auto-clear');
	},
	'input.rollover:mouseover' : function(el) {
	
		/**
		 * Event:  mouseover
		 * Action:
		 *   - if not "active", replace classname by "-hover" classname
		 *   - if type is "image", replace image by "_hover" version
		 */
		if (!this.hasClassName('active')) {
			this.className = this.className.replace('-normal','-hover');
			if (this.type == 'image') {
				this.src = this.src.replace('_normal','_hover');
			}
		}
	},
	'input.rollover:mouseout' : function(e) {
		/**
		 * Event:  mouseout
		 * Action:
		 *   - replace classname by "-hover" classname
		 *   - if type is "image", replace image by "_hover" version
		 */
		if (!this.hasClassName('active')) {
			this.className = this.className.replace('-hover','-normal');
			if (this.type == 'image') {
				this.src = this.src.replace('_hover','_normal');
			}
		}
	},
	'select#program_selector:change' : function(e) {
        /**
         * Event:  on change
         * Action: open window for the program 
         */
        window.location = this.getValue();
        return false;
    }
};
Event.addBehavior(form_rules);

var spotlight_main_rules = {
	'table#spotlight-main-nav a:mouseover' : function(el) {
	/**
	 * Event:  mouseover
	 * Action: show hover version
	 */
		var params = Element.getClassParameters(this);
		// remove active state from nav
		if ($$('div.spotlight-main div.active')[0]) {
			var oldactive = $$('div.spotlight-main div.active')[0];
			oldactive.removeClassName('active');
		}
		if(params.linkID == 1) {
			$('mainspotlight_content1').addClassName('active');
			$('spotlight-main-overlay1').style.backgroundPosition = '0 0';
		}
		if(params.linkID == 2) {
			$('mainspotlight_content2').addClassName('active');
			$('spotlight-main-overlay2').style.backgroundPosition = '-141px 0';
		}
		if(params.linkID == 3) {
			$('mainspotlight_content3').addClassName('active');
			$('spotlight-main-overlay3').style.backgroundPosition = '-282px 0';
		}
		if(params.linkID == 4) {
			$('mainspotlight_content4').addClassName('active');
			$('spotlight-main-overlay4').style.backgroundPosition = '-423px 0';
		}
		return false;
	}
}
Event.addBehavior(spotlight_main_rules);

var flash_rules = {
	'#videoplayer' : function(el) {
		var FO = {
			movie:"/flash/videoplayer.swf",
			width: "480",
			height: "390",
			menu:"false",
			xi:"false",
			majorversion:"9",
			build:"28",
			allowfullscreen: "true"
		}
		UFO.create(FO,this.id);
	},
	'#spijkerspel' : function(el) {
		var FO = {
			movie:"/flash/spijkerspel.swf",
			width: "640",
			height: "480",
			menu:"false",
			xi:"false",
			majorversion:"9",
			build:"28",
			allowfullscreen: "true"
		}
		UFO.create(FO,"spijkerspel");
	},
	'#gevangenis' : function(el) {
		var FO = {
			movie:"/flash/gevangenisspel.swf",
			width: "480",
			height: "360",
			menu:"false",
			xi:"false",
			majorversion:"9",
			build:"28",
			allowfullscreen: "true"
		}
		UFO.create(FO,"gevangenis");
	},
	'#kees_boef' : function(el) {
		var FO = {
			movie:"/flash/player.swf",
			width: "470",
			height: "320",
			menu:"false",
			xi:"false",
			majorversion:"9",
			build:"28",
			allowfullscreen: "true",
			flashvars: "&file=/images/filmpjes/Kees_Boef.flv"
		}
		UFO.create(FO,"kees_boef");
	},
	'#learningonline' : function(el) {
		var FO = {
			movie: '/flash/banner_elearning.swf',
			width: '170',
			height: '180',
			menu: 'false',
			xi: 'false',
			majorversion: '9',
			build: '28',
			allowfullscreen: 'false',
			flashvars: "&flashvars=http://www.learningonline.nu/roi/index.asp"
		}
		UFO.create(FO, "learningonline");
	},
	/*
	 * Partner banner Flash versions
	 * zie voor implementatie details: K:\ROI Opleidingen\ROIO0005 (aanpassing banners)\07_development\banner implementatie.txt
	 */
	'#partner_banner' : function(el) {
		var params = Element.getClassParameters(this)
		var FO = {
			movie: params.flashfile,
			width: '234',
			height: '60',
			menu: 'false',
			xi: 'false',
			majorversion: '9',
			build: '28',
			allowfullscreen: 'false'
		}
		UFO.create(FO, 'partner_banner');
	}
}
Event.addBehavior(flash_rules);

/*
var mobile_detection_rules = {
	'body' : function() {
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/blackberry/gi)) || (navigator.userAgent.match(/android/gi))) {
		   location.replace("/mobiel");
		}
	}
};
Event.addBehavior(mobile_detection_rules);
*/
