Event.observe(window, 'load', function()
{
    var init = new Common();
});

var Common = Class.create(
{
    initialize: function()
    {
        /* Iterates through all anchor tags on the page */
        $$('a').each(function(a)
            {
                if(a.getAttribute('rel') == 'external')
                {
                    a.target = '_blank';
                }
                
                if(a.getAttribute('href'))
                {
                    //var hashOnly = /^#(.*)$/;
                    if((a.href.indexOf('#') != -1) && (a.getAttribute('rel') == 'self'))
                    //if(a.href.match(hashOnly))
                    {
                        href            = a.href;
                        a.href          = 'javascript:;';
                        this.bindScroll = this.scroll.bindAsEventListener(this, href);
                        
                        Event.observe(a, 'click', this.bindScroll);
                    }
                }
                
                if(a.getAttribute('rel') == 'confirm')
                {
                    if(a.hasAttribute('title'))
                    {
                        confirmText      = a.title;
                        a.title          = '';
                        href             = a.href;
                        a.href           = 'javascript:;';
                        this.bindConfirm = this.confirm.bindAsEventListener(this, confirmText, href);
                        
                        Event.observe(a, 'click', this.bindConfirm); 
                    }
                }
                
            }.bind(this)
        );
        
        $$('ol[class^=start-at]').each(function(ol)
            {
                var className = ol.className;
                var startAt   = className.charAt(className.length - 1);
                
                ol.start = startAt;
            }
        );
    },
    
    confirm: function(event, confirmText, href)
    {
        if(!confirm(confirmText))
        {
            return false;
        }
        else
        {
            window.location = href;
        }
    },
    
    scroll: function(event, href)
    {     
        targetElement = $(href.substr(href.indexOf('#') + 1));
        
        return new Effect.ScrollTo(targetElement);
    } 
});

var Form_Controls = Class.create();
Form_Controls.prototype = 
{
    form: null,
    
    initialize: function(formId, options)
    {
        this.form = formId;
        
        this.setOptions(options);
        Event.observe(window, 'load', this.initialiseFields.bind(this));
        //Event.observe($(this.form), 'submit', this.disabledFields.bind(this))
    },
    
    initialiseFields: function()
    {
        $$('#' + this.form + ' input').each(function(field)
            {
                if(field.hasAttribute('alt') && field.value == '')
                {
                    field.addClassName(this.options.altStateClass);
                    field.value = field.readAttribute('alt');
                    
                    Event.observe(field, 'focus', this.toggleValue.bindAsEventListener(this));
                    Event.observe(field, 'blur', this.toggleValue.bindAsEventListener(this));
                }
            }.bind(this)
        );
    },
    
    disabledFields: function()
    {
        $$('#' + this.form + ' input, #' + this.form + ' select').each(function(field)
            {
                field.disable();
                
            }.bind(this)
        );
    },
    
    toggleValue: function(field)
    {
        var element = Event.element(field);
        
        if(element.readAttribute('alt') == element.value)
        {
            element.removeClassName(this.options.altStateClass);
            element.value = '';
        }
        
        /* Need to check for white space here */
        else if(element.value.replace(/^\s*(.*?)\s*$/, "$1") == '')
        {
            element.addClassName(this.options.altStateClass);
            element.value = element.readAttribute('alt');
        }
    },
    
    setOptions: function(options)
    {
        this.options = 
        {
            altStateClass: 'defaultValue' // The class of input field when the alt text is in the value.
        }
        
        Object.extend(this.options, options || {}); // Overwrites the defaults with those supplied.
    }
};

