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


document.addEvent('domready', function(){						   
								   
	
		
	if($('home-slider')){ 
		var tabs = $$('#home-slider .tab');
		$('home-slider').addEvents({
			'mouseenter': function(){
				slideshow.pause();
			},
			'mouseleave': function(){
				slideshow.play();
			}
		})
		var slideshow = new SlideShow('home-slider', {
			transition: 'fadeThroughBackground',
			delay: 5000,
			duration: 400,
			autoplay: true,
			selector: '.slide',
			onShow: function(data){
				tabs[data.previous.index].removeClass('current');
				tabs[data.next.index].addClass('current');
			}		
		});
		tabs.each(function(item, index){
			item.adopt(new Element('div', {'class':'arrow'}))
			// click a nav item ...
			if(index==0) item.addClass('current');
			item.addEvent('click', function(e){
				e.stop();
				// pushLeft or pushRight, depending upon where
				// the slideshow already is, and where it's going
				//var transition = (slideshow.index < index) ? 'pushLeft' : 'pushRight';
				var transition = 'fadeThroughBackground';
				// call show method, index of the navigation element matches the slide index
				// on-the-fly transition option
				slideshow.show(index, {transition: transition});
			});
		});
	}
	$$('div.file input[type="file"]').each(function(el){
		if(span = el.getNext('span.input')){
			el.addEvent('change', function(){
				span.set('text', el.get('value').substring(el.get('value').lastIndexOf('\\')+1));
			});
		}
	})
	if($('frm_call_me')){
		var sel = $('frm_call_me').getElement('select');
		var input = $('frm_call_me').getElement('input[name="phone"]');
		var button = $('frm_call_me').getElement('input[type="submit"]');
		
		if(sel) {
			input.set('disabled', true);
			button.set('disabled', true);
		
			sel.addEvent('change', function(){
				input.set('disabled', false);
				button.set('disabled', false);
			});
		}
	}
	/*
	$$('.file-upload').each(function(el){
		var input = el.getElement('input[type="file"]');
		var schim = el.getElement('div.schim');
		var button = el.getElement('a.browse');
		input.setStyles({
		//	'opacity': 0,
		//	'visibility': 'visible'
		}).addEvent('change', function() {
			schim.set('text', this.value.substring(this.value.lastIndexOf('\\')+1));
		});
		button.addEvent('click', function(e) {
			input.click();
		});	
	});	
	*/

	if($('filter_categorieen')){
		
		
		var status = {
			'false': 'SLUIT FILTER ▲',
			'true': 'OPEN FILTER ▼'
		};
		
		if(Browser.ie6 || Browser.ie7){
			slider = {
				element: $('filter_categorieen'),
				open: true,
				hide: function(){
					this.element.setStyle('display','none');
					this.open = false;
				},
				show: function(){
					this.element.setStyle('display','block');
					this.open = true;
				},
				toggle: function(){
					if(this.open){
						this.hide();
					}else{
						this.show();
					}
				}
			};
			slider.hide();
		}else{
			var slider = new Fx.Slide('filter_categorieen');
			slider.hide();
		}

		$('filter_categorieen').setStyle('display','block');
		$('mb_keepintouch').hide();
		$('mb_keepintouch').setStyle('display','block');
		
		if (!Browser.ie6 && !Browser.ie7 && !Browser.ie8 ){
			$$('.scrollerbar').each(function(el){
				new ScrollerBar(el);
			});
		}
		else
		{

			$$('#filter_categorieen #filter_functie').setStyle('overflow-y','scroll');
			$$('#filter_categorieen #filter_locatie').setStyle('overflow-y','scroll');
			$$('#filter_categorieen #filter_sector').setStyle('overflow-y','scroll');
			$$('#kit_filter_functie').setStyle('overflow-y','scroll');
			$$('#kit_filter_locatie').setStyle('overflow-y','scroll');
			$$('#kit_filter_sector').setStyle('overflow-y','scroll');
		}
		$('mb_keepintouch').setStyle('display','none');
			
			//$('filter_categorieen').getParent.setStyle('height','100%');
		
		//var functie_scroll = new ScrollerBar($('filter_functie'));
		//var sector_scroll = new ScrollerBar($('filter_sector'));
		//var locatie_scroll = new ScrollerBar($('filter_locatie'));
		
		$('open_filter').addEvent('click', function(e){//add the click function (i.e on an anchor)
			e = new Event(e);
			slider.toggle();
			e.preventDefault();
			$("toggle_filter").set('text', status[slider.open]);
		});
		if($('filter_categorieen').hasClass('open')){
			slider.show();
		}else{
			slider.hide();
		}
	}
	
	
	if($('filter_categorieen') && !Browser.ie6 && !Browser.ie7)
	{
		$$('label.functie_cat').each(function(el){
					  
			var slider2 = new Fx.Slide(el.getNext('ul'));
			el.addEvent('click', function(e){

				slider2.toggle();
			});
			slider2.hide();
		});		
	}
	

	if($('vacature_list'))
	{
    /*	  
		list_order = new Array();
		i = 0;
		$$('a.vacature_list_item').each(function(list_item){
			list_order[i] = {obj: $(list_item.get('id')), precentage: list_item.getChildren('div.item_precentage').get('precentage')};
			i++;
		});
		if(list_order.length > 0)
		{
			list_ordered = new Array();
			list_ordered = list_order.sort(function(a,b) { return (b.precentage-a.precentage) });
			
			var list_elements = new Array();
			i =0;
			var current_vacature = '';
			list_ordered.each(function(list_item){
				list_elements[i] = list_item.obj;
				//check of er een specifiek item is gesleceteerd
				if(list_item.obj.hasClass('current'))
				{
					current_vacature = list_item.obj.get('entry_id');
				}
				i++;
			});
			
			
			var mySort = new Fx.Sort($$('a.vacature_list_item'));			
			mySort.sortByElements(list_elements, {duration : 10}).chain(function(){
			if(current_vacature == '')
			{
				current_vacature = list_elements[0].get('entry_id');
				list_elements[0].addClass('current');
			}
			
			var req = new Request.HTML({
			  method: 'post',
			  url: '/vacatures/vacature.ajax/'+current_vacature,
			  //onFailure: function(){}
			  onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) { $('vacature').set('html',responseHTML);refresh_js();Mediabox.scanPage();},
			  onComplete: function() {
				  var text = false;
				   $('content_load').setStyle('display','none'); $('content').setStyle('visibility','visible');}
			}).send();
			
																								 });
		}
		else // Niets gevonden
		{
			$('vacature').set('html','<h2>Er zijn geen vacatures gevonden</h2><p>Helaas heeft deze filter geen resultaten opgeleverd.</p>');
			$('content_load').setStyle('display','none'); $('content').setStyle('visibility','visible');
		}
		
		
*/		
		var currentChild = false;
		var currentChildIndex = -1;
		var currentVacature = new URI();
		currentVacature = currentVacature.toString();
		$$('#vacature_list a').each(function(a, i){
			if(!currentChild && a.get('href') == currentVacature){
				currentChild = a;
				currentChildIndex = i;
			}
		});
		
		var childCount = $('vacature_list').getElementsByTagName('a').length;

   		if(childCount > 10 && $('more_results') )
		{
			$('more_results').show();
		}
		var more_results = new Fx.Tween($('vacature_list'),{property: 'height'})
		if($('more_results')){
			$('more_results').addEvent('click', function(e){
				e.preventDefault();
				max_height = childCount*55;
				height = $('vacature_list').getStyle('height').toInt();
				new_height = height + (8*55);
				if (new_height > max_height)
				{
					new_height = max_height;
				}
				more_results.start(height,new_height).chain(function(){
					if(max_height <= new_height)
					{
						$('more_results').fade('out');
					}
				});
			});
		}
		if(currentChildIndex>-1){
			more_results.start($('vacature_list').getStyle('height').toInt(),(Math.max(9,currentChildIndex)+1)*55).chain(function(){;
				if($('more_results') && childCount*55 <= (Math.max(9,currentChildIndex)+1)*55)
				{
					$('more_results').fade('out');
				}
			});
		}
 	}
	
	refresh_js();

	new NS.Placeholder({
		elements: $$('input:not([placeholder=""]), textarea:not([placeholder=""])')
	});
	
	
	
	
});

function printMe(el){
	window.print();
	/*
	if($(el).getParent('.printme')){
		var div = new Element('div', {'html':$(el).getParent('.printme').get('html')});
		div.getElements('.pos_right, a.print, a.download, a.button, a.back-to-overview, iframe').destroy();
		win = window.open('','print','width=10,height=10,scrollbar=0,menubar=0');
		win.document.write(div.get('html'));
		win.print();
		win.close();
	}
	*/
}

function refresh_js()
{
	$$('form.validate').each(function(form){
		var val = new Form.Validator(form);
		val.addEvent('elementFail', function(el, failedValidators) {
			if(el.get('type') == "radio") {
				$$("label[for=" + el.get('name') + "]").addClass('foutevraag');
			}
		});
		val.addEvent('elementPass', function(el) {
			if(el.get('type') == "radio") {
				$$("label[for=" + el.get('name') + "]").removeClass('foutevraag');
			}
		})
	}); 
	
	if($('vacature_reageer'))
	{
		var status_reageer = {
			'false': 'SLUIT ▲',
			'true': 'REAGEER ▼'
		};
		var slider2 = new Fx.Slide('vacature_reageer');	
		slider2.hide();
		$('vacature_reageer').setStyle('display','block');	
		
		$('reageer_button').addEvent('click', function(e){//add the click function (i.e on an anchor)		
			e = new Event(e);
			slider2.toggle();
			e.stop();
			$("reageer_button").set('text', status_reageer[slider2.open]);
		});
	}
	
	
	/*
	$$('form a[rel=submit]').addEvent('click', function(e){
		var form = this.getParent('form');
		if(form.hasClass('validate') && !form.validate()){
			e.stop();
			return false;
		}else{
			form.submit();
		}
	});
	*/
	
	$$('a.print').each(function(el){
		el.addEvent('click', function(e){
			if(e) e.stop();
			printMe(el);
		});
	});
	
	
	// wikiballoon
	var balloon = new Element("div", {
		'class': 'wikiballoon'
	});
	var balloonBody = new Element("div", {
		'class': 'wikiballoon_body'
	});
	
	var balloonContent = new Element("div", {
		'class': 'wikiballoon_content'
	});
	
	balloonBody.grab(balloonContent);
	
	balloon.grab(balloonBody);
	
	balloon.grab(new Element('div', {
		'class': 'wikiballoon_bottom'
	}));
	
	$(document.body).grab(balloon);
	
	$$('a.wiki').each(function(item, index) {
		if(item.get('title') != '') {
			item.set('balloontitle', item.get('title').replace(/\[(.*?)\]/g, "<span class='wikiballoon_keyword'>$1</span>"));
			item.set('title', "");
			
			// de link mag niet worden afgebroken door een regeleinde,
			// omdat het ballonnetje anders niet op een logische plek kan worden neergezet
			item.set('html', item.get('html').replace(/ /g, "\u00A0"));
			
			item.addEvent('mouseenter', function(e) {
				balloonContent.set('html', item.get('balloontitle'));
				var contentSize = balloonContent.measure(function() {
					return this.getDimensions(true);
				});
				var itemCoords = item.getCoordinates($(document.body));
				balloon.setStyles({
					left: (itemCoords.left + (itemCoords.width / 2)) - (balloon.getStyle('width').toInt() / 2),
					top: itemCoords.top - 10 - contentSize.height,
					display: 'block'
				});
			});
			item.addEvent('mouseout', function(e) {
				balloon.setStyle('display', 'none');
			});
		}
	});
	
}


		
