/**
 * LBI.common (subset)
 * @version 1.0
 * @author LBi - http://www.lbi.com/en
 * @requires jQuery Core 1.4.1 - http://www.jquery.com/
 */
var LBI = window.LBI || {};
/**
 * @namespace Common re-used functionality.
 */
LBI.common = {
    version: 1.0,
    dynamicInputSelector: 'input.autoclear',
    dynamicInputFocusClass: 'focused',
    ajaxQueryParam: 'rc=1',
    newWindowClass: 'newwin',
    newWindowText: 'This link will open a new window',

    /**
     * New window/popup function.
     * <p>
     * Add the openInNewWindow function to the onclick event of links with a specified class name.
     *
     * @return	{Boolean} Returns false if window not open
     */
    getNewWindowLinks: function () {
        $('a').each(function (i) {
            if ($(this).hasClass(LBI.common.newWindowClass)) {
                $(this).title = $(this).title + " " + LBI.common.newWindowText;
                $(this).click(function () {
                    var newWindow = window.open(this.getAttribute('href'), '_blank');
                    if (newWindow) {
                        if (newWindow.focus) {
                            newWindow.focus();
                        }
                    }
                    return false;
                });
            }
        });
    },

    /**
     * Input field text population.
     * <p>
     * Takes current value of form inputs and handles clearing and repopulating on focus.
     */
    dynamicInputText: function () {
        $(LBI.common.dynamicInputSelector).focus(function () {
            this.value = jQuery.trim(this.value);
            if (this.value === this.defaultValue) {
                this.value = "";
            }
            $(this).addClass(LBI.common.dynamicInputFocusClass);
        }).blur(function () {
            this.value = jQuery.trim(this.value);
            if (this.value === "") {
                this.value = this.defaultValue;
            }
            $(this).removeClass(LBI.common.dynamicInputFocusClass);
        });
    },

    /**
     * If within a frame kill the frame and reload at top
     */
    frameKill: function () {
        var top;
        var self;
        if (top !== self) {
            top.location.href = self.location.href;
        }
    },
	
    /**
     * @namespace Common AJAX functionality.
     */
    ajax: {
        bufferFieldID: 'ajaxVirtualBufferUpdate',
        /**
         * Insert form field for buffer update
         */
        prepareBuffer: function () {
            var objHidden = document.createElement('input');

            objHidden.setAttribute('type', 'hidden');
            objHidden.setAttribute('value', '1');
            objHidden.setAttribute('id', LBI.common.ajax.bufferFieldID);
            objHidden.setAttribute('name', LBI.common.ajax.bufferFieldID);

            document.body.appendChild(objHidden);
        },
        /**
         * Updates value of hidden form field to nudge screen readers into recognising page state change.
         *
         * @see LBI.common.ajax.prepareBuffer()
         */
        updateBuffer: function () {
            var objHidden = document.getElementById(LBI.common.ajax.bufferFieldID);
            if (objHidden) {
                if (objHidden.getAttribute('value') === '1') {
                    objHidden.setAttribute('value', '0');
                } else {
                    objHidden.setAttribute('value', '1');
                }
            } else {
                LBI.common.ajax.prepareBuffer();
                LBI.common.ajax.updateBuffer();
            }
        },
        /**
         * Takes a string (URL expected) and appends parameter to identify as AJAX request for backend processing.
         *
         * @param {String}	URL
         * @return {String}	An AJAXed URL
         */
        tagUrl: function (url) {
            var prefix = (url.indexOf('?') < 0) ? "?" : "&";
            var ajaxURL = url + prefix + LBI.common.ajaxQueryParam;
            return ajaxURL;
        },

        /**
         * Update the page buffer for assistive tech and focus on optional element.
         *
         * @param {Object} focusEL
         * @param {Boolean} timeout
         */
        pageUpdated: function (focusEL, timeout) {
            // Update page 'buffer'
            LBI.common.ajax.updateBuffer();
            // If an element is provided focus it
            if (focusEL) {
                if ($.isArray(focusEL) || (focusEL.length > 0)) {
                    // If focusEL is an array object
                    focusEL[0].focus();
                } else {
                    // If focusEL is a single object
                    focusEL.focus();
                }
            }
        }
    }

};
