/*
 * Select Class
 * 
 * @require 	mootools javascript framework
 * @todo 		check obj available (error handling)
 * 
 * @author 		Steffen Maechtel <s.maechtel@netzbewegung.com>
 * @version 	1.01
 */

Select = new Class({
    initialize: function(objRoot) {
        this.objRoot    = objRoot;
        this.objBody    = $$('body')[0];
        this.objCurrent = this.objRoot.getChildren('.current')[0];
        this.objValue   = this.objCurrent.getChildren('.value')[0];
        this.objList    = this.objRoot.getChildren('.list')[0];
        this.arrOption  = this.objList.getChildren('.option');
        
        /*
         * add "this" as attribute "obj" to all objects with events
         */
        this.objCurrent.obj = this;
        
        for (intIndex in this.arrOption) {
            if (parseInt(intIndex) || intIndex == 0) {
                this.arrOption[intIndex].obj = this;
            }
        }
        
        /*
         * add events
         */
        this.objCurrent.addEvent('click', function (objEvent) {
            // current select has changed
            if (this.obj !== this.obj.objBody.obj) {
                // fire event
                this.obj.objBody.fireEvent('click');
                this.obj.objBody.removeEvents('click');
                // add event on body from current select
                this.obj.objBody.obj = this.obj;
                this.obj.objBody.addEvent('click', function (objEvent) {
                    this.obj.hideList();
                });
            }
            
            // show list with options
            objEvent.stop();
            this.obj.showList();
        });
        
        this.arrOption.each(function (objOption) {
            objOption.addEvent('click', function (objEvent) {
                objEvent.stop();
				
				if (objOption.hasClass('href')) {
					location.href = this.get('rel');
					
				} else {
					this.obj.selectOption(this);
                	this.obj.hideList();
				}
				
                
            });
        });
        
        /*
         * check if we have to update hidden input field
         */
        if (this.strTarget = this.objValue.get('rel')) {
            this.objForm = this.objRoot.getParent('form');
        }
        
        /*
         * init current active value
         */
        this.intCurrent = 0;
        
        for (intIndex in this.arrOption) {
            if (parseInt(intIndex) || intIndex == 0) {
                if (this.arrOption[intIndex].hasClass('active')) {
                    this.intCurrent = intIndex;
                }
            }
        }
        
        this.selectOption(this.arrOption[this.intCurrent]);
        
        /*
         * width current = width list
         */
		
		if (this.objList.getStyle('position') == 'absolute') {
			this.objList.setStyle('width', this.objCurrent.getStyle('width'));
		}
        
    }
});

Select.implement({
    showList: function ()
    {
        if (this.objList.getStyle('display') == 'block') {
            this.objList.setStyle('display', 'none');
        } else {
            this.objList.setStyle('display', 'block');
        }
        
    },
    
    hideList: function ()
    {
        this.objList.setStyle('display', 'none');
    },
    
    selectOption: function (objOption)
    {
        /*
         * reset all to default
         */
        this.arrOption.each(function (objOption) {
            objOption.removeClass('active');
        });
        
        /*
         * set current active
         */
        objOption.addClass('active');
        
        /*
         * set current value
         */
        this.objValue.set('text', objOption.get('text'));
        
        if (this.arrOption[0] === objOption) {
            if (this.objValue.hasClass('active')) {
                this.objValue.removeClass('active');
            }
        } else {
            if (!this.objValue.hasClass('active')) {
                this.objValue.addClass('active');
            }
        }
        
        /*
         * set hidden input value
         */
        if (this.strTarget) {
            this.objForm.elements[this.strTarget].value = objOption.get('rel');
        }
    }
});



