/**
 * IgnitionWeb 4.0
 * 
 * Content Management System and Web Site Development Framework
 * 
 * Copyright 2007 by High-Touch Communications Inc.
 * 
 * This software was developed internally by High-Touch Communications Inc.
 */
 
 /**
  * A supporting class for selection lists
  */
 function SelectionList(id, fieldName) {
    this.available = $(id + '-available');
    this.selected = $(id + '-selected');
    Element.extend(this.available);
    Element.extend(this.selected);
    this.fieldName = fieldName;
    
    Event.observe($(id + '-select'), 'click', this.select.bindAsEventListener(this));
    Event.observe($(id + '-deselect'), 'click', this.deselect.bindAsEventListener(this));
    Event.observe(this.available.form, 'submit', this.submit.bindAsEventListener(this));
    
 }
 
 SelectionList.prototype = {
    initialize : function() {},
    /**
     * Moves an item from the "available" list to the "selected" list.
     */
    select : function() {
        this.moveSelected(this.available, this.selected);
    },       
    /**
     * Moves an item from the "selected" list to the "available" list.
     */    
    deselect : function() {
        this.moveSelected(this.selected, this.available);
    },
    
    /**
    * Moves all selected items from the specified source
    * to the specified destination
    *
    * @param HTMLElement source         the source list
    * @param HTMLElement destination    the destination list
    */
    moveSelected: function(source, destination) {
        $A(source.options)
        .findAll(
            function(option){ 
                return option.selected; 
            }
        ).each(
            function(option) {
                var option = Element.remove(option);
                destination.appendChild(option);
            }
        );
        
    },
    
    
    /**
     * Adds the selected items to the form fields
     */
    submit: function() {
        var form = this.available.form;
        this.selected.multiple = true;
        for (var i=0; i < this.selected.options.length;i++) {
            this.selected.options[i].selected = true;
        }
    }
 }
 
Callout = Class.create();
Callout.DEFAULT_OPACITY = 0.8;

Callout.prototype = {    
    initialize: function(element, options) {
        this.element = $(element);
        this.options = options || {};
        if (!this.options.opacity) {
            this.options.opacity = Callout.DEFAULT_OPACITY;
        }
        if (!this.options.text) {
            this.options.text = '';
        }
        this.element.callout = this;
        Event.observe(window, 'load',
            this.create.bindAsEventListener(this));
    },
    
    setText : function(text) {
        this.options.text = text;
        Element.update(this.callout, text);
    },
    
    create: function() {
        var element = document.createElement('div');
        Element.extend(this.element);
        var elementId = this.element.id + '-callout';                
        $(element).id = elementId;                
        $(element).style.backgroundImage =  "url('" + Paths.www + '/img/bg_callout.gif' + "')";
        $(element).style.backgroundRepeat = 'no-repeat';        
        $(element).className = 'callout';                             
        $(element).update(this.options.text);               
        
        this.element.parentNode.insertBefore(element, this.element);
        Position.absolutize(element); 
               
        
        $(element).hide();
        this.callout = $(elementId);
    },
    
    show : function() {
        this.callout.show();
        var width = this.element.getWidth();
        var offset = Position.cumulativeOffset(this.element);
        new Effect.Move(this.callout, {x: offset[0]-10, y: offset[1]-10, 'duration' : 0.0, mode:'absolute', queue:'front'});
        
        new Effect.Parallel([
               new Effect.Move(this.callout, {x: offset[0], y: offset[1] - this.callout.getHeight(), mode:'absolute'}),
               new Effect.Fade(this.callout, {from:0.0, to: this.options.opacity})
        ], {queue: 'end'});
    },
    
    hide: function() {
        this.callout.hide();
    }
}
    
Widget = Class.create();
Widget.createOption = function(value, label) {
    return new Option(label, value)
};

Widget.Methods = {

    addOption : function(list, option) {
        try {
            $(list).add(option, null);
        } catch(e) {
            $(list).add(option);
        }
    },
    updateOptions : function(list, options) {
        $(list).immediateDescendants().each(
            function(element) {
                $(element).remove();
            }
        );
        if (options.length) {
            for (var i=0;i<options.length;i++) {
                $(list).addOption(Widget.createOption(i, options[i]));
            }
        } else {
            $H(options).each(function(item) {
                $(list).addOption(Widget.createOption(item.key, item.value));
            });
        
        }
    }
}

Element.addMethods(Widget.Methods);
