/**
 * The 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";
}