/**
* @file elSelect.js
* @downloaded from http://www.cult-f.net/2007/12/14/elselect/
* @author Sergey Korzhov aka elPas0
* @site  http://www.cult-f.net
* @date December 14, 2007
* 
* elSelect is made with MooTools Framework.
* You need following components:
*
    * core ( core )
    * class ( all )
    * native ( all )
    * element ( event, filters, selectors )
    * window ( domready )
*/


var Checkbox = new Class({
	options: {
		baseclass: 'elCheckbox',
		onclass: 'checked',
		suffix: '_image',
		onStart: Class.empty,
		onComplete: Class.empty,
		trueAjax: '',
		falseAjax: ''
	},
	initialize: function(sourceid, options) {
		this.setOptions(options);
		var source = $(sourceid);
		this.options.sourceid = sourceid;
		this.status = false;
		this.destination = new Element('input', {
			'type': 'hidden',
			'name': source.name,
			'id': source.id,
			'value': source.checked
		});
		this.checkbox = new Element('div', {
			'class': this.options.baseclass,
			'id': source.id + this.options.suffix
		});
		if (source.checked) {
			this.checkbox.addClass(this.options.onclass);
			this.status = true;
		}
		var self = this;
		this.checkbox.addEvent('click', function(e){
			self.fireEvent('onStart');
 
			this.toggleClass(self.options.onclass);
			self.status = !self.status;
			$(self.options.sourceid).value = self.status;
 
			switch (true) {
				case (self.options.trueAjax && self.status):
					self.ajax = new Ajax(self.options.trueAjax, {onComplete: function() {
						self.fireEvent('onComplete');
					}}).request();
					break;
				case (self.options.falseAjax && !self.status):
					self.ajax = new Ajax(self.options.falseAjax, {onComplete: function() {
						self.fireEvent('onComplete');
					}}).request();
					break;
				default:
					self.fireEvent('onComplete');
					break;
			}
		});
		source.replaceWith(this.destination);
		this.checkbox.injectAfter(this.destination);
	}
});
Checkbox.implement(new Events);
Checkbox.implement(new Options);

var elSelect = new Class({
	options: {
		container: false,
		baseClass : 'elSelect'
	},
	source : false,
	selected : false,
	_select : false,
	current : false,
	selectedOption : false,
	dropDown : false,
	optionsContainer : false,
	hiddenInput : false,
	/*
	pass the options,
	create html and inject into container
	*/
	initialize: function(options){
		
		this.setOptions(options)
		
		if ( !this.options.container ) return
		
		this.selected = false
		this.source = $(this.options.container).getElement('select')
		this.buildFrameWork()
		
		$(this.source).getElements('option').each( this.addOption, this )
		$(this.options.container).setHTML('')
		this._select.injectInside($(this.options.container))
		
		this.bindEvents()
		
	},
	
	buildFrameWork : function() {
		this._select = new Element('div').addClass( this.options.baseClass )
		this.current = new Element('div').addClass('selected').injectInside($(this._select))
		this.selectedOption = new Element('div').addClass('selectedOption').injectInside($(this.current))
		this.dropDown = new Element('div').addClass('dropDown').injectInside($(this.current))
		new Element('div').addClass('clear').injectInside($(this._select))
		this.optionsContainer = new Element('div').addClass('optionsContainer').injectInside($(this._select))
		
		var t = new Element('div').addClass('optionsContainerTop').injectInside($(this.optionsContainer))
		var o = new Element('div').injectInside($(t))
		var p = new Element('div').injectInside($(o))
		var t = new Element('div').addClass('optionsContainerBottom').injectInside($(this.optionsContainer))
		var o = new Element('div').injectInside($(t))
		var p = new Element('div').injectInside($(o))

		this.hiddenInput = new Element('input').setProperties({
		type : 'hidden',
		name : this.source.getProperty('name'),
		id   : this.source.getProperty('name')
		}).injectInside($(this.optionsContainer))
	},
	
	bindEvents : function() {
		document.addEvent('click', function() { 
				if ( this.optionsContainer.getStyle('display') == 'block') 
					this.onDropDown()
			}.bind(this));
			
		$(this.options.container).addEvent( 'click', function(e) { new Event(e).stop(); } )		
		this.current.addEvent('click', this.onDropDown.bindWithEvent(this) )
		
	},
	
	//add single option to select
	addOption: function( option ){
    	var o = new Element('div').addClass('option').setProperty('value',option.value)
		
		
		if ( option.disabled ) { o.addClass('disabled') } else {
			o.addEvents( {
				'click': this.onOptionClick.bindWithEvent(this),
				'mouseout': this.onOptionMouseout.bindWithEvent(this),
				'mouseover': this.onOptionMouseover.bindWithEvent(this)
			})
		}
		
		if ( $defined(option.getProperty('class')) && $chk(option.getProperty('class')) ) 
			o.addClass(option.getProperty('class'))
	
		if ( option.selected ) { 
			if ( this.selected) this.selected.removeClass('selected');
			this.selected = o
			o.addClass('selected')
			this.selectedOption.setText(option.text);
			this.hiddenInput.setProperty('value',option.value);
			doSelectValue(option.value);
			this.selected.setStyle('display', 'none');
		}
		o.setText(option.text)		
		o.injectBefore($(this.optionsContainer).getLast())
	},
	
	onDropDown : function( e ) {
			
			if ( this.optionsContainer.getStyle('display') == 'block') {
				this.optionsContainer.setStyle('display','none')
			} else {
				this.optionsContainer.setStyle('display','block')
				this.selected.addClass('selected')
				//needed to fix min-width in ie6
				var width =  this.optionsContainer.getStyle('width').toInt() > this._select.getStyle('width').toInt() ?
															this.optionsContainer.getStyle('width')
															:
															this._select.getStyle('width')
															
				this.optionsContainer.setStyle('width', width)
				this.optionsContainer.getFirst().setStyle('width', width)
				this.optionsContainer.getLast().setStyle('width', width)
			}						
	},
	onOptionClick : function(e) {
		var event = new Event(e)
		if ( this.selected != event.target ) {
			
			var strValue = this.selected.getProperty('value');
			var separator = strValue.lastIndexOf('_');
			var varName = strValue.substr(0, separator);
			var numValue = parseInt(strValue.substr(separator + 1));
			if (numValue != 0) {
				this.selected.setStyle('display', 'block');
			}
			
			this.selected.removeClass('selected')
			event.target.addClass('selected')
			this.selected = event.target
			
			//var strValue = this.selected.getProperty('value');
			//var separator = strValue.lastIndexOf('_');
			//var varName = strValue.substr(0, separator);
			//var numValue = parseInt(strValue.substr(separator + 1));
			//if (numValue == 0) {
				this.selected.setStyle('display', 'none');
			//}
			
			this.selectedOption.setText(this.selected.getText());
			this.hiddenInput.setProperty('value',this.selected.getProperty('value'));
			doSelectValue(this.hiddenInput.value);
		}
		this.onDropDown()
	},
	onOptionMouseover : function(e){
		var event = new Event(e)
		this.selected.removeClass('selected')
		event.target.addClass('selected')
	},
	onOptionMouseout : function(e){
		var event = new Event(e)
		event.target.removeClass('selected')
	}
	
});

elSelect.implement(new Events);
elSelect.implement(new Options);


