var $ = YAHOO.util.Dom.get;
var UCLU = {};

UCLU.util = {
    /* DOM stuff */

    sameElement: function(a, b) {
        a = $(a);
        b = $(b);
        return a && b && a.id && b.id && (a.id == b.id);
    },

    show: function(e) {
        e = $(e);
    
        if (typeof(e.style.display) != 'undefined' && e.style.display == 'none') {
            e.style.display = '';
            return;
        }
    
        var blockElements =
            'address blockquote center dir div dl fieldset form h1 h2 h3 h4 h5 h6 hr noscript ol p pre table ul'.toUpperCase().split(' ');
            
        for (var i=0; i<blockElements.length; i++) {
            if (blockElements[i] == e.tagName) {
                e.style.display = 'block';
                return;
            }
        }
    
        e.style.display = 'inline';
    },

    hide: function(e) {
        $(e).style.display = 'none';
    },

    toggle: function(e) {
        e = $(e);
    
        if (e.style.display == 'none')
            this.show(e);
        else
            this.hide(e);
    },

    switchVisibility: function(ids, id) {
        if (!(ids instanceof Array))
            ids = $(ids).childNodes;

        for (var i=0; i<ids.length; i++) {
            var e = ids[i];
        
            if (this.sameElement(e, id))
                this.show(e);
            else
                this.hide(e);
        }
    },

    switchControls: function(id) {
        this.switchVisibility('page-controls', id);
        this.redraw('middle');
    },

    // Redraw things to flush out IE6 bugs
    redraw: function(id) {
        this.hide(id);
        this.show(id);
    },

    setClass: function(e, className, on) {
        if (on)
            return YAHOO.util.Dom.addClass(e, className);
        else
            return YAHOO.util.Dom.removeClass(e, className);
    },

    remove: function(e) {
        e = $(e);
        return e.parentNode.removeChild(e);
    },
    
    clearChildren: function(e) {
        e = $(e);
        
        while (e.childNodes[0])
            this.remove(e.childNodes[0]);
        
        return e;
    },

    /* String stuff */

    unescapeHTML: function(html) {
        var div = document.createElement('div');
        div.innerHTML = html;
        return div.firstChild ? div.firstChild.nodeValue : '';
    },

    stripBr: function(html) {
        return html.replace(/<br>/g, '\n');
    },

    trim: function(str) {
        return str.replace(/^\s*|\s*$/g, '');
    },
    
    element: function(tagName, id, content, opts) {
        opts = opts || {};
        
        var e = document.createElement(tagName);
        
        if (id)
            e.id = id;
        
        if (typeof(content) == 'string')
            e.innerHTML = content;
        else if (content instanceof Array)
            for (var i=0; i<content.length; i++)
                e.appendChild(content[i]);
        else // assume DOM element
            e.appendChild(content);
        
        if (opts.className)
            e.className = opts.className;
        
        return e;
    },

    module: function(id, cls, content) {
        return this.element('div', id, [
            this.element('div', null, ' ',     { className: 'hd' }),
            this.element('div', null, content, { className: 'bd' }),
            this.element('div', null, ' ',     { className: 'ft' })
        ], { className: 'module ' + cls });
    },

    /* Special stuff */

    // Copied from kitsite-cms/kit/core.js, 27 June 2006
    getQueryParameter: function(name) {
        if (!this._queryParams) {
            this._queryParams = {};
            if (document.location.search && document.location.search.length>1) {
                var q = document.location.search.substr(1).split('&');
                for (var i=0; i < q.length; i++) {  
                    var nv = q[i].split('=');
                    if (nv.length==2)
                        this._queryParams[decodeURIComponent(nv[0])] = decodeURIComponent(nv[1].replace(/\+/g, ' '));
                }
            }
        }
        return this._queryParams[name];
    },

    injectCss: function(css) {
        var style = document.createElement('style');
        style.type = 'text/css';
    
        try {
            style.appendChild(document.createTextNode(css));
        } catch (e) {
            style.text = css; // oh, IE.
        }

        document.getElementsByTagName('head')[0].appendChild(style);
    }
}

UCLU.login = {
    init: function() {
        YAHOO.util.Event.addListener('login-link', 'click', function(){
            UCLU.util.hide('login');
            UCLU.util.show('login-form');
        });
    
        YAHOO.util.Event.addListener('login-cancel-link', 'click', function(){
            UCLU.util.hide('login-form');
            UCLU.util.show('login');
        });
    }
}

UCLU.login.init();

UCLU.form = {
    tagFields: {},

    init: function() {
        YAHOO.util.Event.addListener(window, 'load', function() {
            var forms = document.getElementsByTagName('form');
        
            for (var i=0; i<forms.length; i++) {
                var form = forms[i];
                
                if (form.id) UCLU.form.tagFields[form.id] = {};
                
                var inputs = UCLU.form.getRequiredInputs(form);
                
                form.allRequiredInputs = inputs;
            
                for (var j=0; j<inputs.length; j++) {
                    var input = inputs[j];
                
                    YAHOO.util.Event.addListener(input, 'keypress', function() {
                        var e = this;
                    
                        setTimeout(function() {
                            UCLU.form.checkInput(e);
                        }, 10);
                    });
                
                    UCLU.form.checkInput(input);
                }

                inputs = UCLU.form.getTagInputs(form);
            
                for (var j=0; j<inputs.length; j++)
                    if (form.id)
                        UCLU.form.tagFields[form.id][inputs[j].name] = new KitTagField(inputs[j]);
            }
        });
    },

    validate: function(form) {
        for (var i=0; i<form.allRequiredInputs.length; i++)
            this.checkInput(form.allRequiredInputs[i]);
    
        if (this.getRequiredInputs(form).length) {
            YAHOO.util.Dom.addClass(form, 'submit-failed');
            return false;
        }
    
        return true;
    },

    getRequiredInputs: function(form) {
        return YAHOO.util.Dom.getElementsByClassName('required', null, form);
    },

    getTagInputs: function(form) {
        return YAHOO.util.Dom.getElementsByClassName('tags-input', 'input', form);
    },

    checkInput: function(e) {
        UCLU.util.setClass(e, 'required', !e.value);
    },

    getRadioValue: function(form, field) {
        var inputs = $(form)[field];
    
        for (var i=0; i<inputs.length; i++)
            if (inputs[i].checked)
                return inputs[i].value;
    
        return false;
    },
    
    getTagXml: function(form, field) {
        var tags = this.tagFields[$(form).id][field].getTags();
        
        xml = '';
        
        for (var i=0; i<tags.length; i++)
            xml += '<tag>' + tags[i] + '</tag>';
        
        return xml;
    }
}

UCLU.form.init();

UCLU.election = {
    msInAnHour: 60 * 60 * 1000,
    msInADay:   24 * 60 * 60 * 1000,
    
    init: function() {
        YAHOO.util.Event.onAvailable('election-countdown', function() {
            var date = this.innerHTML.match(/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/);
            
            if (!date) return;
            
            UCLU.election.closingDate = new Date(date[1], date[2]-1, date[3], date[4], date[5], 0);
            UCLU.election.tick();
            $('election-countdown').style.display = '';
        });
    },
    
    tick: function() {
        timeLeft = this.closingDate - new Date();
        
        days  = Math.floor(timeLeft / this.msInADay);
    
        timeLeftInDay = timeLeft - (days*this.msInADay);
    
        hours = Math.floor(timeLeftInDay / this.msInAnHour);
        
        if (days > 0 || hours > 0) {
            str = 'Only';
        
            if (days == 1)
                str += ' 1 day';
            else if (days > 1)
                str += ' ' + days + ' days';
        
            if (days > 0 && hours > 0)
                str += ' and';
        
            if (hours == 1)
                str += ' 1 hour';
            else if (hours > 1)
                str += ' ' + hours + ' hours';
        
            str += ' left';
    
            $('election-countdown').innerHTML = str;
        } else {
            $('election-banner').style.display = 'none';
        }
    }
}

UCLU.election.init();