var brigaCheckbox = new Class({

    Implements: Options,
    
    initialize: function(options){
        try {
            $defined()
        } 
        catch (err) {
            alert('Mootools needed');
            return
        }
        
        if (!options.mode) {
        
            if (!options.className) {
                alert('manca className');
                return;
            }
            else {
                alert('manca Mode');
                return;
            }
            
        }
        
        
        
        this.setOptions(options);
        
        this.baseClass = 'gnt_checkbox';
        this.labelClass = 'gnt_checklabel';
        this.selectedClass = 'selected';
        this.overClass = 'over';
        
        if (this.options.mode == 'all') {
            this.checkboxes = document.getElements('input[type="checkbox"]');
        }
        else {
            this.checkboxes = document.getElements('input[type="checkbox"][class="' + this.options.className + '"]');
        }
        
        
        this.buildCheckbox();
    },
    
    buildCheckbox: function(){
    
        this.checkboxes.each(function(item, index){
        
            this.checkboxContainer = new Element('div');
            
            this.checkLabel = new Element('div', {
                'class': this.labelClass,
                'html': item.get('label')
            });
            this.checkLabel.setStyle('float', 'right');
            
            this.hiddenInput = new Element('div', {
                'id': 'value_' + item.get('name'),
                'name': item.get('name')
            });
            
            this.newCheck = new Element('div', {
                'class': this.baseClass
            })
            
            this.newCheck.setStyle('float', 'left');
            
            this.newCheck.store('value', item.get('value'));
            
            this.newCheck.addEvents({
                'mouseenter': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    target.addClass(this.overClass);
                    
                }
.bind(this)                ,
                'mouseleave': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    target.removeClass(this.overClass);
                }
.bind(this)                ,
                'click': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    target.addClass(this.selectedClass);
                    
                    this.hiddenInput.set('value', target.retrieve('value'));
                    
                }
.bind(this)
            });
            clearDiv = new Element('div', {
                'class': 'clear'
            });
            this.checkboxContainer.adopt(this.newCheck, this.checkLabel, this.hiddenInput, clearDiv);
            this.checkboxContainer.replaces(item);
            
            
        }
.bind(this));
        
        
        
    }
    
})




var brigaRadio = new Class({

    Implements: Options,
    
    initialize: function(options){
        try {
            $defined()
        } 
        catch (err) {
            alert('Mootools needed');
            return
        }
        
        if (!options.radioName) {
            alert('mancano robe');
            return;
        }
        
        this.setOptions(options);
        
        this.baseClass = 'gnt_radio';
        this.labelClass = 'gnt_radiolabel';
        this.selectedClass = 'selected';
        this.overClass = 'over';
        
        this.radios = document.getElements('input[name="' + this.options.radioName + '"][type="radio"]');
        
        
        this.buildRadio();
    },
    
    buildRadio: function(){
    
        this.hiddenInput = new Element('input', {
            'name': this.options.radioName,
            'id': this.options.radioName,
            'type': 'hidden'
        });
        this.newRadioContainer = new Element('div', {
            'class': 'gnt_radioContainer'
        });
        this.newRadioContainer.adopt(this.hiddenInput);
        
        this.radios.each(function(item, index){
        
		
        
            radioLabel = new Element('div', {
                'class': this.labelClass,
                'html': item.get('label')
            });
            radioLabel.setStyle('float', 'right');
            
            newRadio = new Element('div', {
                'class': this.baseClass
            });
            newRadio.addClass(this.options.radioName);
            
            newRadio.setStyle('float', 'left');
            newRadio.store('value', item.get('value'));
            
			if(item.get('checked')){
				  	document.getElements('div.' + this.baseClass + '.' + this.options.radioName).removeClass(this.selectedClass)

		 			newRadio.addClass(this.selectedClass);
                    this.hiddenInput.set('value', newRadio.retrieve('value'));
			}
			
            newRadio.addEvents({
                'mouseenter': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    target.addClass(this.overClass);
                    
                }
.bind(this)                ,
                'mouseleave': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    target.removeClass(this.overClass);
                }
.bind(this)                ,
                'click': function(ev){
                    el = new Event(ev);
                    target = Element(el.target);
                    document.getElements('div.' + this.baseClass + '.' + this.options.radioName).removeClass(this.selectedClass)
                    
                    target.addClass(this.selectedClass);
                    this.hiddenInput.set('value', target.retrieve('value'));
                    if (this.options.callback) {
                        this.options.callback.apply(null);
                    }
                }
.bind(this)
            });
            
            clearDiv = new Element('div', {
                'class': 'clear'
            });
            
            
            
            /*this.newRadioContainer.adopt(newRadio);
             
             if(index==0){
             this.placeholder=new Element('div',{'id':this.options.radioName});
             
             this.placeholder.replaces(item);
             }else{
             item.destroy();
             }
             this.newRadioContainer.replaces(this.placeholder);
             */
            this.newRadioContainer.adopt(newRadio, radioLabel, clearDiv);
            
            this.newRadioContainer.replaces(item);
            
        }
.bind(this));
        
        //this.newRadioContainer.replaces(this.placeholder);
    
    
    }
    
});












var brigaSelect = new Class({

    Implements: Options,
    
    
    initialize: function(options){
    
    
        try {
            $defined()
        } 
        catch (err) {
            alert('Mootools needed');
            return
        }
        
        if (!options.id) {
            alert('manca id');
            return;
        }
        this.setOptions(options);
       
        
        this.sysID = 'gnt_cont_' + Math.floor(Math.random() * 3);
        this.targetSelect = document.getElement('#' + this.options.id);
        
        this.baseClass = 'option';
        this.selectedClass = 'selected';
        this.overClass = 'over';
        
		
		this.option_containerClass='gnt_option_container';
        this.optionsEL = this.targetSelect.getElements('option');
        
        this.buildFramework();
        this.buildSelect();
        
        this.finalizeSelect();
    },
    
    buildSelect: function(){
    
        //creo l'input nascosto per tenere il valore della option cliccata
        this.hiddenInput = new Element('input', {
            'name': this.targetSelect.get('name'),
            'id': this.options.id
        });
        this.hiddenInput.set('type', 'hidden');
        //prendo tutti gli input della select
        this.optionsEL.each(function(item, index){
        
            //creo un div per ogni option
            newOption = new Element('div', {
                'class': this.baseClass
            });
            newOption.set('html', item.get('html'));
            
            //salvo il value e lo stato
            newOption.store('value', item.get('value'));
            newOption.store('selected', item.get('selected'));
            newOption.store('html', item.get('html'));
            newOption.addClass((item.hasClass('default') ? 'default' : ''));
           
           
            
            //eventi
            newOption.addEvent('click', function(ev){
            
                el = new Event(ev);
                target = Element(el.target);
                this.selectElement(target);
			
                
            }
.bind(this));
            
            newOption.addEvent('mouseleave', function(ev){
                el = new Event(ev);
                target = Element(el.target);
                target.removeClass(this.overClass);
            }
.bind(this));
            
            newOption.addEvent('mouseenter', function(ev){
                el = new Event(ev);
                target = Element(el.target);
                target.addClass(this.overClass);
            }
.bind(this));
            
            //elemento selezionato
            
            if (item.get('selected')) {
                this.hiddenInput.set('value', item.get('html'));
                if (this.options.maxchars < item.get('html').length) {
                    html = item.get('html').substr(0, this.options.maxchars) + '...'
                }
                else {
                    html = item.get('html');
                }
                
                newOption.addClass('hidden');
                
                this.viewer.set('html', html);
				/*defalt */
					if(item.hasClass('default')){
						this.viewer.addClass('default')
					}
				//;
            }
            else 
                if (index === 0) {
                    this.viewer.set('html', item.get('html'));
					
                }
            
            if (index === 0) {
                this.first = new Element('div', {
                    'class': 'gnt_first'
                });
                this.first.inject(this.option_container);
            }
            
            
            newOption.inject(this.option_container);
            if (index === (this.optionsEL.length - 1)) {
                this.last = new Element('div', {
                    'class': 'gnt_last'
                });
                this.last.inject(this.option_container);
            }
        }
.bind(this));
    },
    buildFramework: function(){
    
        this.currentContainer = new Element('div', {
            'class': 'gnt_currentContainer'
        });
		
		
		
        this.dropDownSwitch = new Element('div', {
            'class': 'gnt_toggler'
        });
        this.dropDownSwitch.setStyle('float', 'right');
        
        this.clear = new Element('div', {
            'class': 'gnt_clear'
        });
        
        this.globalContainer = new Element('div', {
            'class': 'gnt_global_container',
            'id': this.options.additionalId
        });
		
		if(this.targetSelect.getElements('option').length==1)
		{
			this.globalContainer.addClass('disabled');
			this.currentContainer.addClass('disabled');
		}
        

		this.newWidth=this.targetSelect.getComputedSize().totalWidth;
		min=80;
		if(this.options.minwidth){
			min=this.options.minwidth;
		}
		//this.currentContainer.setStyle('width',(min-40));
		if(this.newWidth<min){
		this.newWidth=min;	
		}
        //this.globalContainer.setStyle('position', 'absolute');
        this.globalContainer.setStyle('width',this.newWidth );
        style_arr = this.targetSelect.getStyles('padding', 'margin', 'color', 'border');
        
        
        this.viewer = new Element('div', {
            'class': 'gnt_viewer'
        });
        this.viewer.setStyle('float', 'left');
        /*this.viewer.setStyles('padding',style_arr.padding);
         this.viewer.setStyles('margin',style_arr.margin);
         this.viewer.setStyles('color',style_arr.color);
         this.viewer.setStyles('border',style_arr.border);
         this.viewer.setStyles('background-color','#fff');
         */
        this.viewer.set('id', this.sysID);
		
		// rendo cliccabile tutto il currentContainer
		
		if (!this.currentContainer.hasClass('disabled')) {
			//this.currentContainer.addEvent('click', function(ev){
			this.globalContainer.addEvent('click', function(ev){
			
				evt = new Event(ev);
				target = evt.target;
				//	target_a=target.getParent();
				//target_cont = target.getNext('div.gnt_option_container');
				target_cont = target.getParent().getNext('div.gnt_option_container');
				to_hide = document.getElements('.' + this.option_containerClass).filter(function(item, index){
					return item != target_cont;
				});
				
				to_hide.addClass('hidden');
				to_hide.getParent().setStyle('z-index', '1');
				
				
				this.option_container.toggleClass('hidden');
				if (this.option_container.hasClass('hidden')) {
				
					this.option_container.getParent().setStyle('z-index', '1');
				}
				else {
				
					this.option_container.getParent().setStyle('z-index', '99');
				}
				
				
				
			}
.bind(this));
		}
		
		
		
		
		// solo freccina a dx cliccabile

	/*	
        this.dropDownSwitch.addEvent('click', function(ev){
			
			evt=new Event(ev);
			target=evt.target;
			target_a=target.getParent();
			target_cont=target_a.getNext('div.gnt_option_container');
			to_hide=document.getElements('.'+this.option_containerClass).filter(function(item,index){ return item != target_cont; });
			
			to_hide.addClass('hidden');
			to_hide.getParent().setStyle('z-index','1');
	

            this.option_container.toggleClass('hidden');
			if(this.option_container.hasClass('hidden')){
				
				this.option_container.getParent().setStyle('z-index','1');
			}else{
				
				this.option_container.getParent().setStyle('z-index','99');
			}
	
			
	
        }
.bind(this));
        */
		
		
        this.option_container = new Element('div', {
            'class': this.option_containerClass
        });
        //this.option_container.setStyle('position','absolute');
		
		this.option_container.setStyle('width', this.newWidth-2);
		//this.currentContainer.setStyle('width',(this.newWidth-40));
		option_container_height=(this.targetSelect.getElements('option').length)*25;
		
		if(option_container_height>400)
		{
			option_container_height=200;
		}
		
		this.option_container.setStyle('height', option_container_height);
        this.option_container.addClass('hidden');
        
        //corners
        this.corner_TL = new Element('div', {
            'class': 'gnt_tl'
        });
        this.corner_TL.setStyle('position', 'absolute');
        this.corner_TL.setStyle('top', '0');
        this.corner_TL.setStyle('left', '0');
        this.corner_TR = new Element('div', {
            'class': 'gnt_tr'
        });
        this.corner_TR.setStyle('position', 'absolute');
        this.corner_TR.setStyle('top', '0');
        this.corner_TR.setStyle('right', '0');
        this.corner_BL = new Element('div', {
            'class': 'gnt_bl'
        });
        this.corner_BL.setStyle('position', 'absolute');
        this.corner_BL.setStyle('bottom', '0');
        this.corner_BL.setStyle('left', '0');
        this.corner_BR = new Element('div', {
            'class': 'gnt_tr'
        });
        this.corner_BR.setStyle('position', 'absolute');
        this.corner_BR.setStyle('bottom', '0');
        this.corner_BR.setStyle('right', '0');
    },
    
    finalizeSelect: function(){
        this.corner_TL.inject(this.option_container);
        this.corner_TR.inject(this.option_container);
        this.corner_BL.inject(this.option_container);
        this.corner_BR.inject(this.option_container);
        
        this.currentContainer.adopt(this.viewer);
        this.currentContainer.adopt(this.dropDownSwitch);
        this.currentContainer.adopt(this.clear);
        
        this.globalContainer.adopt(this.currentContainer);
        
        
        this.globalContainer.adopt(this.option_container);
        this.globalContainer.adopt(this.hiddenInput);
        
        this.globalContainer.replaces(this.targetSelect);
    },
    
    selectElement: function(target){
    
        this.hiddenInput.set('value', target.retrieve('value'));
        
        this.option_container.toggleClass('hidden');
        if (this.options.maxchars < target.retrieve('html').length) {
            html = target.retrieve('html').substr(0, this.options.maxchars) + '...'
        }
        else {
            html = target.retrieve('html');
        }
        this.viewer.set('html', html);
        
        document.getElements('.' + this.baseClass).removeClass(this.selectedClass);
        document.getElements('.' + this.baseClass).removeClass('hidden');
        target.addClass('hidden');
        target.removeClass(this.overClass);
        
        if (this.options.callback) {
            this.options.callback.apply(null);
        }
    }
});

