/**
 * The NEw ShowMore controller
 * 
 * @author Internet Architects BVBA
 * @copyight __copyight__
 * @version __version__
 * @requires IAJS.CONTROLLER.BaseController
 */ 
 /**
  * @constructor
  * @base IAJS.CONTROLLER.BaseController
  * @param {HTMLelement} node The eltype element to create the controller for
  */
IAJS.CONTROLLER.MoreLess = function(node){
	this.node = node;
	this.objectType = "ShowMore";
	this.showNumItems = IAJS.CONFIG.showMoreShowNumItems || 6;
	this.events = new IAJS.EventList(this);
	/**
	 * Fires after the trigger is selected.
	 * @name onChange
	 * @memberOf IAJS.CONTROLLER.Tabpane
	 */	
	this.events.add("onChange");
}
YAHOO.extend(IAJS.CONTROLLER.MoreLess, IAJS.CONTROLLER.BaseController);
/**
 * Initialization of the controller
 */
IAJS.CONTROLLER.MoreLess.prototype.init = function(){	
	this.listElm = $YD.getElementsBy(function(){return true;}, "ul", this.node)[0];
	if( $YD.getChildren(this.listElm).length >  this.showNumItems){

		this.triggerElm = document.createElement("DIV");
		this.triggerElm.innerHTML = '<a href="#">' + LOCALIZE.messages.MORELESS_MORE || "more" + '</a>';
		$YD.addClass(this.triggerElm, "extra");
		$YD.insertAfter(this.triggerElm, this.listElm);
		
		var elm = $YD.getElementsBy(function(){return true;}, "a", this.triggerElm)[0];
		$YE.on(elm, "click", this._ClickHandler, this, true );
		
		this.isClosed = true;
		this._hideListItems();
	}
}
/**
 * 
 * @private
 */
IAJS.CONTROLLER.MoreLess.prototype._ClickHandler = function(e){
	$YE.preventDefault(e);
	this.isClosed = !this.isClosed;
	(this.isClosed) ?  this._hideListItems() : this._showListItems() ;
	this.events.fire('onChange', this );
}
/**
 * 
 * @private
 */
IAJS.CONTROLLER.MoreLess.prototype._hideListItems = function(){
	var items = $YD.getChildren(this.listElm);
	for(var i = this.showNumItems; i < items.length; i++  ){
		YAHOO.util.Dom.setStyle(items[i], 'display', 'none');
	}
	var elm = $YD.getElementsBy(function(){return true;}, "a", this.triggerElm)[0];
	elm.innerHTML = LOCALIZE.messages.MORELESS_MORE || "more";
}
/**
 * 
 * @private
 */
IAJS.CONTROLLER.MoreLess.prototype._showListItems = function(){
	var items = $YD.getChildren(this.listElm);
	for(var i = this.showNumItems; i < items.length; i++  ){
		YAHOO.util.Dom.setStyle(items[i], 'display', 'block');
	}
	var elm = $YD.getElementsBy(function(){return true;}, "a", this.triggerElm)[0];
	elm.innerHTML = LOCALIZE.messages.MORELESS_LESS || "less";
}
 
 /**
  * The tabpane controller
  * 
  * @author Internet Architects BVBA
  * @copyight __copyight__
  * @version __version__
  * @requires IAJS.CONTROLLER.BaseController
  */ 
  /**
   * @constructor
   * @base IAJS.CONTROLLER.BaseController
   * @param {HTMLelement} node The eltype element to create the controller for
   */
 IAJS.CONTROLLER.Tabpane = function(node) {
 	this.node = node;
 	this.tabView;
 	this.objectType = "Tabpane";
 	
 	this.events = new IAJS.EventList(this);
 	/**
 	 * Fires after the value is changed.
 	 * @name onChange
 	 * @memberOf IAJS.CONTROLLER.Tabpane
 	 */	
 	this.events.add("onChange");
 }
 YAHOO.extend(IAJS.CONTROLLER.Tabpane, IAJS.CONTROLLER.BaseController);
 /**
  * Initialization of the controller
  */
 IAJS.CONTROLLER.Tabpane.prototype.init = function(){	

 	var nav = $YD.getElementsByClassName('tabNav', "ul", this.node);
 	if(nav.length == 0){ return; }
 	//
 	//tabHeading
 	this._hideTabHead();
 	//
 	this.navElm = $YD.getElementsByClassName("tabHandler", "a", nav[0]);
 	var navActive = $YD.getChildrenBy(nav[0], function(elm){ return $YD.hasClass(elm, "active" ); });
 	
 	if(navActive.length > 0){
 		this._setTabs( $YD.getElementsByClassName("tabHandler", "a", navActive[0])[0]  );
 	} else {
 		this._setTabs( this.navElm[0]  );
 	}
 	//
 	for(var i=0, l =this.navElm.length ; i < l ; i++){
 		var tabElmid = this.navElm[i].getAttribute('href').substring(1);
 		var tabElm = document.getElementById(tabElmid);
 		$YE.on(this.navElm[i], "click", this._tabChangeHandler, this, true);
 	}
 }
 IAJS.CONTROLLER.Tabpane.prototype._hideTabHead = function(){
 	var main = $YD.getElementsByClassName('tabMain', "div", this.node);
 	for(var i=0; i < main.length; i++){
 		$YD.getElementsByClassName('tabHeading', null, main[i], function(el){el.style.display = "none";});
 	}
 	
 }
 /**
  * Handler for tab changes
  * @private
  */
 IAJS.CONTROLLER.Tabpane.prototype._tabChangeHandler = function(e){
 	$YE.preventDefault(e);
 	var target = $YE.getTarget(e);
 	var targetNav = $YD.getAncestorByClassName(target, "tabHandler");
 	this._setTabs(targetNav);
 	this.events.fire('onChange', this );
 }

 IAJS.CONTROLLER.Tabpane.prototype._setTabs = function(activeNav){
 	var targetTabId = activeNav.getAttribute('href').substring(1);
 	targetTabId = targetTabId.substring(targetTabId.lastIndexOf("#") + 1); // FF = "#the_href_value", IE = "fullURL/#the_href_value" (is this only with file://url/#the_href_value ?)
 	
 	for(var i=0, l=this.navElm.length ; i < l ; i++){
 		var tabId = this.navElm[i].getAttribute('href').substring(1);
 		tabId = tabId.substring(tabId.lastIndexOf("#") + 1); // FF = "#the_href_value", IE = "fullURL/#the_href_value" (is this only with file://url/#the_href_value ?)
 		var paneElm = document.getElementById(tabId);
 		var navElmLi =  $YD.getAncestorByTagName(this.navElm[i], "li");
 		
 		if( targetTabId == tabId ){
 			$YD.addClass(navElmLi, "active");
 			paneElm.style.display = "block";
 		} else {
 			$YD.removeClass(navElmLi, "active");
 			paneElm.style.display = "none";
 		}
 	}
 }

/**
 * The RadiolistSelect controller
 * 
 * @author Internet Architects BVBA
 * @copyight __copyight__
 * @version __version__
 * @requires IAJS.CONTROLLER.BaseController
 */
  
 /**
  * @constructor
  * @base IAJS.CONTROLLER.BaseController
  * @param {HTMLelement} node The eltype element to create the controller for
  */
 IAJS.CONTROLLER.RadiolistSelect = function(node){
	 this.node = node;
	 this.objectType = "RadiolistSelect";
	 this.events = new IAJS.EventList(this);
	 this.events.add("optionChange");	 
 };
 YAHOO.extend(IAJS.CONTROLLER.RadiolistSelect, IAJS.CONTROLLER.BaseController);
 /**
  * Initialization of the controller
  */
 IAJS.CONTROLLER.RadiolistSelect.prototype.init = function(){
	 
	 $YD.addClass(this.node, "js");
	 var ctrlElm = $YD.getElementsByClassName("inputControl", "div", this.node)[0];
	 var listElmP = $YD.getElementsByClassName("controls", "div", ctrlElm);
	 if(listElmP.length != 1){ return; }
	 this.listElm = $YD.getChildrenBy(listElmP[0], function(el){ return $YD.hasClass(el, "control"); });
	 
	 var firstRadio;
	 for(var i=0;i<this.listElm.length; i++ ){
		 var elm = this.listElm[i];
		 var radioElm = $YD.getElementsBy(function(el){ return el.getAttribute("type") == "radio"; }, "input", elm)[0];
		 if(i==0){firstRadio = radioElm;}
		 if(radioElm.checked){ break;} // found first checked nothing to do, leave
		 else if (i==this.listElm.length-1){
			 // no checkbox checked, select first
			 firstRadio.checked = true; 
		 }
	 }
	
	 for(var i=0;i<this.listElm.length; i++ ){
		 var elm = this.listElm[i];
		 var sub = $YD.getFirstChildBy(elm, function(el){return $YD.hasClass(el, "sub");});
		 var radioElm = $YD.getElementsBy(function(el){ return el.getAttribute("type") == "radio"; }, "input", elm)[0];
		 radioElm._RadiolistSelectSub = sub;
		 sub.style.display = (radioElm.checked ) ? "block" : "none" ;
		 
		 var subSelect = $YD.getFirstChildBy(sub, function(el){return el.tagName == "SELECT"; });
		 if(subSelect){subSelect.disabled = !radioElm.checked;};
		 radioElm._RadiolistSelectSubElm = subSelect;
		 
		 $YE.on(subSelect, "change", this._ChangeHandler, this, true );
		 $YE.on(radioElm, "click", this._ClickHandler, this, true );		 
	 }
	this.events.subscribe('optionChange', this._OptionChangeHandler, this);
	this.events._get('optionChange').fire(firstRadio._RadiolistSelectSubElm);
};
/**
 * 
 */
IAJS.CONTROLLER.RadiolistSelect.prototype._ClickHandler = function(e){
	var t = $YE.getTarget(e);
	for(var i=0;i<this.listElm.length; i++ ){
		$YD.getElementsBy(
				function(el){ return (el.getAttribute("type") == "radio")}, "input", this.listElm[i],
				function(elm){ 
					elm._RadiolistSelectSub.style.display = (elm.id == t.id ) ? "block" : "none" ;
					elm._RadiolistSelectSubElm.disabled = !elm.checked;
				}
		);
	}
	this.events._get('optionChange').fire(t._RadiolistSelectSubElm);
};
/**
 * 
 */
IAJS.CONTROLLER.RadiolistSelect.prototype._ChangeHandler = function(e){
	this.events._get('optionChange').fire($YE.getTarget(e));
};
/**
 * 
 */
IAJS.CONTROLLER.RadiolistSelect.prototype._OptionChangeHandler = function(e){
	var s = arguments[1][0]; // HTML select element
	var frm_s = $YD.getElementsBy(function(el){return el.type == "submit";}, "input", s.form); // HTML submit element
	if(frm_s.length == 1){
		frm_s[0].disabled = s.selectedIndex == 0;
	}
};

IAJS.CONTROLLER.zoeken = function(){
	this.elmGem = $YD.get("frm-gemeente");
	this.elmTref = $YD.get("frm-tref");
	this.form = $YD.get("advancedSearch");
	this.textUpdateTimer = null;
	this.init();
};
IAJS.CONTROLLER.zoeken.prototype = {
	init : function(){
		if( this.elmGem &&  this.elmTref){
			$YE.on(this.elmGem, "change", this.update, this, true);
			$YE.on(this.elmTref, "blur", this.update, this, true);
			$YE.on(this.elmTref, "focus", this.updateTimer, this, true);
			this.update();
		}
	},
	update : function(){
		var frm_s = $YD.getElementsBy(function(el){return el.type == "submit";}, "input", this.form); // HTML submit element
		if(frm_s.length == 1){
			frm_s[0].disabled = (this.elmGem.selectedIndex == 0 && this.elmTref.value == "");
		}
		clearTimeout(this.textUpdateTimer);
	},
	updateTimer : function(){
		var _self = this;
		var f = function(){ _self.check(_self); };
		_self.textUpdateTimer = setInterval( f , 250 );
	},
	check : function(o){
		var frm_s = $YD.getElementsBy(function(el){return el.type == "submit";}, "input", o.form); // HTML submit element
		if(frm_s.length == 1){
			frm_s[0].disabled = (o.elmGem.selectedIndex == 0 && o.elmTref.value == "");
		}		
	}
		
};

//YAHOO.util.Event.onDOMReady(
//	function(){
//		YAHOO.util.Dom.getElementsBy( 
//		  function(e){
//		    if(e.getAttribute('href')){
//		      var h = e.getAttribute('href').toLowerCase();
//		      return (h.substring(0,7) == "http://" && h.indexOf('//provant.be') == -1 && h.indexOf('.provant.be') == -1 );
//		    }
//		    return false;
//		  },
//		  'a', document ,function(el){ YAHOO.util.Dom.addClass(el, "external"); } 
//		)
//	}
//);

YAHOO.util.Event.onDOMReady(
		function(){
			var z = new IAJS.CONTROLLER.zoeken();
		}
)
