var mooForms = new Class({

    Implements: [Options],

    options: {
			imageDir: '../images/',
			checkboxImage: {image: 'check.gif', width: 18, height: 18},
			radioImage: {image: 'check.gif', width: 18, height: 18},
			spacer: 'spacer.gif',
			inputs: ['radio','checkbox'],
			labelPosition: 'right'
    },
    
    initialize: function(element, options){
		
		if(options) this.setOptions(options);
		
        this.el = $(element);
        this.elid = element;
		
		this.options.inputs.each(function (input) {
        	this.build(input);
		}.bind(this));
		
		this.divs = this.el.getElements('div');
		
        this.divs.each(function(item){ 
		
            if (item.hasClass('checkbox') || item.hasClass('radio')) {
				
                item.addEvents({
					
					'mousedown': function(event){
                    	new Event(event).stop();
                    	this.effect(item);
                    	return false;
					}.bind(this),
					
	 				'click': function(event){	 
		 				this.handle(item);
					}.bind(this)
					
				});
				
				// ie fix for default drag 'n' drop on image.
				//if (Browser.Engine.trident) {
					
					//item.getElement('img').ondragend = function(event){
						//this.clear(item);
						//return false;
					//}.bind(this);
					
				//}
				
				window.addEvent('mouseup', function(event){
					this.clear(item);
				}.bind(this));
			}
			
		}.bind(this));
	},
 
    build: function(input){
    	
		formElement = this.el.getElements('input[type=' + input + ']');
		
        formElement.each(function (inputElement) {
			
			if (input == 'checkbox') {
				this.image = this.options.imageDir + this.options.checkboxImage.image;
				this.imageWidth = this.options.checkboxImage.width;
				this.imageHeight = this.options.checkboxImage.height;
				
			}
			
			if (input == 'radio') {
				this.image = this.options.imageDir + this.options.radioImage.image;
				this.imageWidth = this.options.radioImage.width;
				this.imageHeight = this.options.radioImage.height;
			}

			this.spacer = new Asset.image(this.options.imageDir + this.options.spacer);
			
			this.spacer.setStyles({
				'width': this.imageWidth,
				'height': this.imageHeight,
				'vertical-align': 'top',
				'background-image': 'url(' + this.image + ')',
				'background-repeat': 'no-repeat',
				'background-position': '0px 0px',
				'margin-right': '3px'				
			});
			
			var wrapper = new Element('div', {
				'class': input,
				'styles': {
					'cursor': 'default',					
					'display': 'inline'
				}
			});
			
			if (this.options.labelPosition == 'left') {
				label = inputElement.getPrevious();
				wrapper.injectBefore(inputElement).
						adopt(label)
						.adopt(this.spacer)
						.adopt(inputElement); 
			} else {
				label = inputElement.getNext();
				wrapper.injectBefore(inputElement)
						.adopt(this.spacer)
						.adopt(inputElement)
						.adopt(label);
			}
			
			label.setStyles({
				'vertical-align': 'middle',
				'display': 'inline'
			});
			
			inputElement.setStyle('display', 'none');
			
			if (inputElement.getAttribute('checked')) {
				wrapper.addClass('selected');
				this.spacer.setStyle('background-position', '0px -' + (this.imageHeight * 2) + 'px');
			}
		}.bind(this)); 
	},
	
	getImageHeight: function(item){
		if (item.hasClass('checkbox')) {
			this.imageHeight = this.options.checkboxImage.height;
		} else {
			this.imageHeight = this.options.radioImage.height;
		}
	},
 	
    effect: function(item){
	  	
		this.getImageHeight(item);
		
        if(item.className == 'checkbox' || item.className == 'radio') {
            item.getElement('img').setStyle('background-position', '0px -' + this.imageHeight + 'px');
		} else {
			item.getElement('img').setStyle('background-position', '0px -' + (this.imageHeight * 3) + 'px');
		}
	},
 
    handle: function(item){
    
        selector = item.getElement('input');
		
        if(item.className == 'checkbox') {
            selector.setProperty('checked', 'checked');
            item.addClass('selected');
			item.getElement('img').setStyle('background-position', '0px -' + (this.options.checkboxImage.height * 2) + 'px');
		} else if (item.className == 'checkbox selected') {
            selector.removeProperty('checked');
            item.removeClass('selected');
			item.getElement('img').setStyle('background-position', '0px 0px');
		} else {
            selector.setProperty('checked', 'checked');
            item.addClass('selected');
			item.getElement('img').setStyle('background-position', '0px -' + (this.options.radioImage.height * 2) + 'px');
            itemName = selector.getProperty('name');
            
			this.inputs = this.el.getElements('input[name=' + itemName + ']');
            
            this.inputs.each(function(radio){
                if (radio != selector) {
                    radio.getParent().removeProperty('checked');
                    radio.getParent().removeClass('selected');			
					radio.getParent().getElement('img').setStyle('background-position', '0px 0px');
				}
			});
		}
	},
    
    clear: function(item){
	
	   	this.getImageHeight(item);
		
        if (item.className == 'checkbox' || item.className == 'radio') {
			item.getElement('img').setStyle('background-position', '0px 0px');
		} else if (item.className == 'checkbox selected' || item.className == 'radio selected') {
			item.getElement('img').setStyle('background-position', '0px -' + (this.imageHeight * 2) + 'px');
		}
	}
});

