/** $Id: core.lib.js 2274 2006-05-25 23:35:46Z jrust $ */
/** core JS functions included on all pages */

var FastFrame = {
    // These are set by PHP
    app: null,
    module: null,
    userId: null,

    fadeColor: '#eecc77',
    // {{{ updateInfoSnippets()

    updateInfoSnippets: function()
    {
        document.getElementsByClassName('infoBoxSnippet').each(function(node) {
            var el = node.parentNode.parentNode.nextSibling;
            if (!Element.visible(el)) node.innerHTML = el.firstChild.innerHTML.stripTags();
        });
    },

    // }}}
    // {{{ toggleInfoBoxes()

    toggleInfoBoxes: function(el, container) {
        var dir = el.firstChild.src.indexOf('arrow_down.gif') != -1;
        var boxes = document.getElementsByClassName('infoBoxHeader', $(container));
        var tmp_dir;
        boxes.each(function(node, i) {
            // Last one is always open
            tmp_dir = boxes.length - 1 == i ? true : dir;
            this.toggleInfoBox(node, null, tmp_dir);
        }.bind(this));
        el.firstChild.src = 'graphics/mini/' + (dir ? 'arrow_up.gif' : 'arrow_down.gif');
        el.firstChild.nextSibling.firstChild.nodeValue = dir ? 'Collapse All' : 'Expand All';
        return false;
    },

    // }}}
    // {{{ toggleInfoBox()
    
    toggleInfoBox: function(el, e, force) {
        // Don't continue if they're clicking on a link
        if (e && (Event.element(e).tagName.toLowerCase() == 'a' || Event.element(e).parentNode.tagName.toLowerCase() == 'a')) return;
        if (force === true) {
            if (Element.visible(el.nextSibling)) return;
            Effect.BlindDown(el.nextSibling, {duration: .2});
        }
        else if (force === false) {
            if (!Element.visible(el.nextSibling)) return;
            Effect.BlindUp(el.nextSibling, {duration: .2});
        }
        else {
            Effect.toggle(el.nextSibling, 'blind', {duration: .2});
        }

        document.getElementsByClassName('infoBoxSnippet', el).each(function(node) {
            node.innerHTML = Element.visible(el.nextSibling) ? el.nextSibling.firstChild.innerHTML.stripTags() : '';
        });
        $A(el.getElementsByTagName('img')).each(function(node) {
            if (node.src.indexOf('triangle.gif') != -1) {
                node.src = 'graphics/arrows/' + (Element.visible(el.nextSibling) ? 'triangle.gif' : 'opentriangle.gif');
            }
        });
    },

    // }}}
    // {{{ selfURL()

    selfURL: function(params) {
        var current = this.cleanURL();
        current = current.substring(current.indexOf('?'), current.length).toQueryParams();
        this.params = {
            app: this.app,
            module: this.module
        };
        if (current['FF_SESSID']) this.params['FF_SESSID'] = current['FF_SESSID'];
        Object.extend(this.params, params || {});
        return 'index.php?' + $H(this.params).toQueryString();
    },

    // }}}
    // {{{ cleanURL()

    cleanURL: function(url) {
        url = url || document.location.href;
        return url.substr(url.length - 1, 1) == '#' ? url.substring(0, url.length - 1) : url;
    },

    // }}}
    // {{{ processMessages()

    processMessages: function(xmlObj) {
        FastFrame.clearAllMessages();
        if (xmlObj.getElementsByTagName('msgs')) {
            $A(xmlObj.getElementsByTagName('msgs')[0].childNodes).each(function(node) {
                FastFrame.setMessage(node.getElementsByTagName('txt')[0].firstChild.nodeValue,
                                     node.getElementsByTagName('type')[0].firstChild.nodeValue);
            });
        }
    },

    // }}}
    // {{{ doAJAXTimeout()

    doAJAXTimeout: function(request) {
        if (this.callInProgress(request.transport)) {
            request.transport.abort();
            this.clearAllMessages();
            this.setFailureMessage();
            if (request.options['onFailure']) { 
                request.options['onFailure'](request.transport, request.json);
            }
        }
    },

    // }}}
    // {{{ clearAllMessages()

    clearAllMessages: function() {
        Element.update('messageWrap', '');
    },

    // }}}
    // {{{ setFailureMessage()

    setFailureMessage: function() {
        this.setMessage('Uh oh, your action could not be completed.  Please try again shortly.', FastFrame['FASTFRAME_ERROR_MESSAGE']);
    },

    // }}}
    // {{{ callInProgress()

    callInProgress: function(xmlhttp) {
            switch (xmlhttp.readyState) {
                case 1: case 2: case 3:
                    return true;
                break;
                // Case 4 and 0
                default:
                    return false;
                break;
            }
    },
    
    // }}}
    // {{{ setMessage()

    FASTFRAME_NORMAL_MESSAGE: 'info.gif',
    FASTFRAME_ERROR_MESSAGE: 'error.gif',
    FASTFRAME_WARNING_MESSAGE: 'warning.gif',
    FASTFRAME_SUCCESS_MESSAGE: 'success.gif',
    setMessage: function(message, type) {
        new Insertion.Bottom('messageWrap', '<div class="color1 roundCorner2"><!-- //--></div><div class="color1 message"><img src="graphics/mini/' + type + '" /> ' + message + '</div><div class="color1 roundCorner2 messageBottom"><!-- //--></div>');
    },

    // }}}
    // {{{ popup()

    popup: function(href, name, params) {
        var options = [], param;
        options.push('status=yes,scrollbars=yes,resizable=yes,left=50,screenX=50,top=50,screenY=50');
        options.push('menubar=' + (params.menubar ? params.menubar : 'no'));
        options.push('width=' + (params.width ? params.width : '650'));
        options.push('height=' + (params.height ? params.height : '500'));
        win = window.open(href, name, options.join(','));
        win.focus();
    },

     // }}}
     // {{{ doRowClick()

     doRowClick: function(e) {
        // There are some cases (like checkout item list) where there are tables cells within cells or form buttons
        // where we do nott want the row click action to happen
        if (Event.element(e).nodeName.toLowerCase() != 'input' &&
            ((Event.element(e).parentNode.nodeName.toLowerCase() == 'tr' && Event.element(e).parentNode.id) ||
             (Event.element(e).parentNode.parentNode.nodeName.toLowerCase() == 'tr' && Event.element(e).parentNode.parentNode.id))) {
            if (Event.element(e).parentNode.nodeName.toLowerCase() == 'tr') {
                window.location.href = Event.element(e).parentNode.id;
            }
            else {
                window.location.href = Event.element(e).parentNode.parentNode.id;
            }
        }
    },

    // }}}
    // {{{ addHover()

    addHover: function() { this.className += " hover"; },

    // }}}
    // {{{ removeHover()

    removeHover: function() { this.className = this.className.replace(" hover", ""); }

    // }}}
}

// {{{ AJAX timeouts

Ajax.Responders.register({
    onCreate: function(request) {
        request['timeoutId'] = window.setTimeout(function() { FastFrame.doAJAXTimeout(request) }, 5000);
    },
    onComplete: function(request) {
        window.clearTimeout(request['timeoutId']);
    }
});

// }}}
// {{{ Make the row on list pages clickable

Event.observe(window, 'load', function() {
    if (!$('listTableHighlight')) return;
    var e = document.getElementsByClassName('primaryRow', 'listTableHighlight');
    document.getElementsByClassName('secondaryRow', 'listTableHighlight').each(function(node) { e.push(node); });
    e.each(function(node) {
        Event.observe(node, 'click', FastFrame.doRowClick);
        // Cannot get these to reliably work with Event.observe()
        if (document.all) {
            node.onmouseover = FastFrame.addHover;
            node.onmouseout = FastFrame.removeHover;
        }
    });
});

// }}}
// {{{ Add highlight effect

Event.observe(window, 'load', function() {
    document.getElementsByClassName('fade').each(function(node) {
        new Effect.Highlight(node, { duration: 2.0, startcolor: FastFrame.fadeColor });
    });
});

// }}}
