/*
    FormStyle
    ----------
    FormStyle has been collected/developed by
    Gabriel Kaufmann for LinkInnovation
    
    This library provides mootools-support for advanced form-styling
    capabilities using CSS.

    ComBoo
    ----------
    ComBoo is basing code of the following author(s). The code has been
    rewritten/fixed partly.
    
    code-base:
	Copyright:
	Copyright (c) 2009 Dipl.-Ing. (FH) André Fiedler <kontakt@visualdrugs.net>
	License:
	MIT-style license
	Credits:
	Based on ComboBoo by Bruno Torrinha - www.torrinha.com
*/

var checkMee = new Class({
    Implements: [Events, Options],
    options: {
        className: 'checkMee',
	container: null
    },

    initialize: function(el, options){
	this.markObjects = Object;
	this.setOptions(options);
	this.options.container = this.options.container || document.body;
	this.oldCheck = $(el);
	var posLink = this.oldCheck.getCoordinates(this.options.container);
	
	this.checkMee = new Element('div',{
	    'class':(this.options.className+'-checkbox')
	});
	
	this.checkMee.setStyles({
	    'cursor':'pointer',
	    'width':'18px',
	    'height':'18px'
	}).injectBefore(this.oldCheck);
	
	// set initial check-state
	if(this.oldCheck.checked) {
	    this.setChecked(true);
	}
	
//	this.oldCheck.fade('hide');
	this.oldCheck.setStyles({
	    'display':'none',
	    'visibility':'hidden',
	    'margin':'0px',
	    'overflow':'hidden'
	});
	this.addCheckEvents(this.checkMee);
        this.oldCheck.store(this.options.className, this);


	Object = this.markObjects;
    },
    
    setChecked: function(flag) {
	if(flag) {
	    this.checkMee.checked = true;
	    this.checkMee.set('checked','checked');
	    this.checkMee.addClass('checked');
	} else {
	    this.checkMee.checked = false;
	    this.checkMee.set('checked','');
	    this.checkMee.removeClass('checked');
	}

	this.oldCheck.fireEvent('blur',{target:this.oldCheck});
	Object = this.markObjects;
    },

    addCheckEvents: function(el){
	// bind form:reset to this element
	$$(el).getParent('form').addEvent('reset',this.reset.bind(this));
	
        return el.addEvents({
            click: this.click.bind(this, [el]),
            select: this.disabledEvents(this, [el]),
            mouseover: this.mouseover.bind(this, [el]),
            mouseleave: this.mouseleave.bind(this, [el])
        });
    },
    
    disabledEvents: function(el) {
	Object = this.markObjects;
	return false;
    },
    
    mouseover: function(el) {
	this.oldCheck.fireEvent('mouseover');
	Object = this.markObjects;
    },
    
    mouseleave: function(el) {
	this.oldCheck.fireEvent('mouseleave');
    },
    
    click: function(el) {
	this.checkMee.toggleClass('checked');
	var oldValue = this.oldCheck.checked;
	this.oldCheck.checked = !this.oldCheck.checked;
	this.oldCheck.fireEvent('click');
	
	this.setChecked(this.oldCheck.checked);
	if(oldValue != this.oldCheck.checked) {
	    this.oldCheck.fireEvent('change');
	}

	el.blur();
	Object = this.markObjects;
    },
    
    reset: function(el) {
	this.setChecked(false);
    }
});

var ComBoo = new Class({

    Implements: [Events, Options],
    options: {
        className: 'comBoo',
	container: null
    },
    
    initialize: function(el, options){
	// disabl for IE6!
	if(Browser.Engine.trident4) return false;
	
	this.markObjects = Object;
        this.setOptions(options);
	this.options.container = this.options.container || document.body;
        this.oldCombo = $(el);
        this.bOpen = false;
        var posLink = this.oldCombo.getCoordinates(this.options.container);
//-	var posList = this.oldCombo.getCoordinates();
	
	this.comboContainer = new Element('div',{
	    'class':(this.options.className+'-container')
	}).setStyles({
            'position':'relative',
            'width':posLink.width
	}).injectAfter(this.oldCombo);
	this.comboContainerSize = this.comboContainer.getSize();
//-	var posCombo = this.comboContainer.getCoordinates();
	
        this.comboLink = new Element('a', {
            'class': (this.options.className+'-label'),
            'id': this.oldCombo.get('name')
        }).setStyles({
            'width':(this.comboContainerSize.x)+'px'
	}).set('text', this.oldCombo.options[this.oldCombo.options.selectedIndex].text);
        this.comboLink.injectInside(this.comboContainer);
	
        this.comboList = new Element('ul', {
            'class': this.options.className+'-list',
            'id': 'choices-'+ this.oldCombo.name
        }).setStyles({
            'display':'none',
            'position':'absolute',
            'zIndex':'8',
            'width':(this.comboContainerSize.x-2)+'px'
        }).injectInside(this.comboContainer);

        this.comboList.fade('hide');
        this.oldCombo.setStyles({
	    'width':'0px',
	    'height':'0px',
	    'margin':'0px',
	    'display':'none',
	    'overflow':'hidden'
	});
        this.addComboLinkEvents(this.comboLink);

        this.build();
        this.comboList.setStyle('display', 'block');
        this.oldCombo.store(this.options.className, this);
    },
    
    build: function(){
        this.comboList.empty();
        for (i = 0; i < this.oldCombo.length; i++) {
            var el2 = new Element('li').addClass(this.oldCombo.options[i].className).store('index', i).set('text', this.oldCombo.options[i].text);
	    if(i == this.oldCombo.options.selectedIndex) {
		el2.addClass('selected');
	    }
            this.addChoiceEvents(el2).inject(this.comboList);
        }
        this.comboLink.set('text', this.oldCombo.options[this.oldCombo.options.selectedIndex].text);
	    this.items = this.comboList.getElements('li');
	    
	if(this.oldCombo.selectedIndex==0) {
	    this.comboLink.addClass('first-index');
	}

    },
    
    click: function(el){
        if (this.bOpen) {
            this.close();
        }
        else {
            this.open(true);
        }
        this.oldCombo.fireEvent('click');
	Object = this.markObjects;
    },

    close: function() {
	$(document.body).removeEvent('click', this.checkClickOutside);
	if (this.bOpen) {
	    this.bOpen = false;
	    this.comboList.fade('out');
	}
    },
    
    reset: function() {
        this.oldCombo.selectedIndex = 0;
        var defaultOpt = this.oldCombo.getElement('option[default],option:first-child');
        this.choiceSelect(defaultOpt);
    },
    
    blur: function() {
        this.oldCombo.fireEvent('blur');
    },
    
    open: function(closeOnClickOutside) {
        if (!this.bOpen) {
	    this.bOpen = true;
	    this.comboList.fade('in');
	}
	
	// Check if User clicked Outside the Combobox
	if (closeOnClickOutside) {
	    this.checkClickOutside = function(e){
	    var clickedCombo = false;
	    if (this.comboLink == e.target) 
		clickedCombo = true;
	    if (this.comboList == e.target) 
		clickedCombo = true;
	    this.items.each(function(li){
		if (li == e.target) 
		    clickedCombo = true;
	    });
	    if (!clickedCombo) 
		this.close();
	    }.bind(this);
	    
	    $(document.body).addEvent('click', this.checkClickOutside);
	}
    },
    
    comboOver: function(){
        this.oldCombo.fireEvent('mouseover');
	Object = this.markObjects;
    },
    
    comboOut: function(el){
        this.oldCombo.fireEvent('mouseout');
	Object = this.markObjects;
    },
    
    choiceOver: function(el){
        if(this.selected) {
            this.selected.removeClass('choice-selected');
            this.comboLink.removeClass('choice-selected');
        }
        this.selected = el.addClass('choice-selected');
        this.comboLink.addClass('choice-selected');
	Object = this.markObjects;
    },
    
    choiceSelect: function(el){
        this.bOpen = false;
        this.comboList.fade('hide');
        this.comboLink.set('text', el.get('text'));
	var oldIndex = this.oldCombo.selectedIndex;
        this.oldCombo.selectedIndex = el.retrieve('index');
	
        if(this.oldCombo.selectedIndex != oldIndex) {
	    this.oldCombo.fireEvent('change');
	    
	    if(this.oldCombo.selectedIndex==0) {
		this.comboLink.addClass('first-index');
	    } else {
		this.comboLink.removeClass('first-index');
	    }
	}
	if(this.oldCombo.get('class').match('.url-redirect')) {
	    var url = this.oldCombo.options[ this.oldCombo.selectedIndex ].value;
	    if(url!='') document.location = url;
	}
	
	// Redirect some functions to the new dropDown
	this.oldCombo.addClass.bind(this.comboContainer);
	this.oldCombo.removeClass.bind(this.comboContainer);
	this.oldCombo.toggleClass.bind(this.comboContainer);
	this.oldCombo.setStyle.bind(this.comboContainer);
	this.oldCombo.getStyle.bind(this.comboContainer);
	this.oldCombo.setStyles.bind(this.comboContainer);
	this.oldCombo.getStyles.bind(this.comboContainer);
	this.oldCombo.fireEvent('blur',{target:this.oldCombo});
	
	this.items.removeClass('selected');
	el.addClass('selected');
	Object = this.markObjects;
    },
    
    addComboLinkEvents: function(el){
	// bind form:reset to this element
	$$(el).getParent('form').addEvent('reset',this.reset.bind(this));
	
        return el.addEvents({
	    blur: this.blur.bind(this, [el]),
            click: this.click.bind(this, [el]),
            mouseover: this.comboOver.bind(this, [el]),
            mouseleave: this.comboOut.bind(this, [el])
        });
    },
    
    addChoiceEvents: function(el){
        return el.addEvents({
            mouseover: this.choiceOver.bind(this, [el]),
            mousedown: this.choiceSelect.bind(this, [el])
        });
	Object = this.markObjects;
    },
    
    getElement: function(){
	return this.comboLink;
    },
    
    destroy: function() {
	this.close();
	this.comboLink.destroy();
	this.comboList.destroy();
    }
});


/* EOF */
