/***************************************************************************
 *
 *   File      : /js/layout.js
 *   Date      : Saturday, January 27, 2006
 *   Version   : 1.0
 *   Copyright : (C) 2006 AC Creations
 *   Email     : juan@ac-creations.net
 *
 ***************************************************************************
 * Footer
 ***************************************************************************/

function getWindowHeight() {
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function setFooter() {
    if (document.getElementById) {
        var windowHeight = getWindowHeight();

        if (windowHeight > 0) {
            var contentHeight = $('container').offsetHeight;
            var footerElement = $('footer');
            var footerHeight = footerElement.offsetHeight;

            if (windowHeight - (contentHeight + footerHeight) >= 0) {
                footerElement.style.position = 'relative';
                footerElement.style.top = (windowHeight - ((contentHeight + 10) + footerHeight)) + 'px';
            } else {
                footerElement.style.position = 'static';
            }
        }
    }
}

var alignPopup = {
    getScrollY:    false,
    init: function() {
        if( !$('popup')) return;
        alignPopup.el = $('popup');
        alignPopup.elHeight = alignPopup.el.offsetHeight;
        alignPopup.el.style.top = alignPopup.getYpos();
    },
    getYpos: function() {
        alignPopup.scrollPos = 0;
        if (typeof(window.pageYOffset) == 'number') {
            alignPopup.scrollPos = window.pageYOffset;
        } else if (document.body && document.body.scrollTop) {
            alignPopup.scrollPos = document.body.scrollTop;
        } else if ( document.documentElement && document.documentElement.scrollTop) {
            alignPopup.scrollPos = document.documentElement.scrollTop;
        }

        alignPopup.screenSize = getWindowHeight();
        alignPopup.position = (alignPopup.screenSize - alignPopup.elHeight) / 2;
        
        if (alignPopup.position < 0 ) alignPopup.position = 10;

        return  alignPopup.scrollPos + Math.round(alignPopup.position) + 'px';
    }
};

var inputFocus = {
    init: function() {
        inputFocus.input = document.getElementsByTagName('input');
        inputFocus.textArea = document.getElementsByTagName('textarea');

        for (var i = 0; i < inputFocus.input.length; i++) {
            if (HasClass(inputFocus.input[i], 'textfield')) {
                inputFocus.input[i].onfocus = function() {
                    this.style.border = '1px solid #a7a6aa';
                }
                inputFocus.input[i].onblur = function() {
                    this.style.border = '1px solid #dfdfdd';
                }
            }
        }

        for (var i = 0; i < inputFocus.textArea.length; i++) {
            inputFocus.textArea[i].onfocus = function() {
                this.style.border = '1px solid #a7a6aa';
            }
            inputFocus.textArea[i].onblur = function() {
                this.style.border = '1px solid #dfdfdd';
            }
        }
    }
};

var qUrl = {
    control: false,
    target:  false,
    init: function(nameField, urlField) {
        if (!$(nameField) && !$(urlField)) return;
        qUrl.control = $(nameField);
        qUrl.target  = $(urlField);
        
        if (qUrl.control.value.toLowerCase() == 'anonymous') {
            qUrl.target.disabled = true;
            if (qUrl.target.value != '') qUrl.target.value = '';
            qUrl.control.onblur = function(){
                    if (this.value != '' && this.value.toLowerCase() != 'anonymous') {
                        qUrl.target.disabled = false;
                    } else {
                        qUrl.target.disabled = true;
                    }
                };
        } else {
            qUrl.target.disabled = false;
        }
    }
};

var qHover = {
    init: function(qid) {
        if ( !$('quotes')) return;
        qHover.table = $('quotes');
        qHover.tbody = qHover.table.getElementsByTagName('tbody')[0];
        qHover.tr    = qHover.tbody.getElementsByTagName('tr');
        
        for ( var i = 0; i < qHover.tr.length; i++ ) {
            qHover.tr[i].onmouseover = function() {
                qHover.links  = this.getElementsByTagName('a');

                for ( var j = 0; j < qHover.links.length; j++ ) {
                    if (qHover.links[j].getAttribute('title') == 'edit') {
                        qHover.qid   = 'q' + qHover.links[j].getAttribute('rel');
                        qHover.quote = $(qHover.qid);
                    }
                    qHover.links[j].onmouseover = function() {
                        if (this.getAttribute('title') == 'edit' ||
                            this.getAttribute('title') == 'accept' ||
                            this.getAttribute('title') == 'reject') {
                            AddClass(this.parentNode, 'ruled');
                        }
                    };
                    qHover.links[j].onmouseout = function() {
                        KillClass(this.parentNode, 'ruled');
                    };
                }
                
                AddClass(qHover.quote, 'ruled');
            };
            qHover.tr[i].onmouseout = function() {
                KillClass(qHover.quote, 'ruled');
            };
        }
    }
};

addEvent(window, 'load',
function() {
    setFooter();
    inputFocus.init();
    qUrl.init('qname', 'qurl');
    qHover.init();
});
addEvent(window, 'resize',
function() {
    setFooter();
    alignPopup.init();
});