Element.implement({
    /**
     * Shows this element.
     */
    show: function() {
        this.setStyle("display", "block");
    },
    /**
     * Hides this element.
     */
    hide: function() {
        this.setStyle("display", "none");
    },
    /**
     * Returns a value indicating if this element is hidden.
     */
    hidden: function() {
        return this.getStyle("display") === "none";
    },
    /**
     * Adds an option to this select element.
     * @param text the text value of the option.
     * @param value the value of the option.
     * @return the added option element.
     */
    addOption: function(text, value) {
        var opt = document.createElement("option");
        opt.text = text;
        opt.value = value;
        try {
            this.add(opt, null);
        } catch(exc) {
            // IE only.
            this.add(opt);
        }
        return opt;
    }
});
String.implement({
    /**
     * Returns the SDBM hash of this instance.
     * @return the SDBM hash of this instance.
     */
    sdbmHash : function() {
        var hash = 0;
        for(var i = 0, l = this.length; i < l; ++i) {
            hash = this.charCodeAt(i) + (hash << 6) + (hash << 16) - hash;
        }
        return hash;
    }
});
/**
 * Moves focus onto the specified element once the DOM has loaded.
 * @param e the element (or element Id).
 */
function autoFocus(e) {
    window.addEvent("domready", function() {
        var element = $(e);
        if(element) {
            element.focus();
            if(element.select) {
                element.select();
            }
        }
    });
}
/**
 * Delegates a return key published by the specified element to the
 * specified button element.
 * @param btn the button element (or element Id).
 * @param publisher the element which publishes the event (defaults to
 * window if not specified).
 */
function delegateReturn(btn, publisher) {
    $($pick(publisher, window)).addEvent("keypress", function(event) {
        if(event.key === "enter") {
            event.stop();
            $(btn).click();
        }
    });
}
/**
 * Alternates the rows as specified by the selector.
 * @param selector the CSS selector.
 */
function alternateRows(selector) {
    window.addEvent("domready", function() {
        $$(selector).each(function(row, index) {
            row.addClass(index % 2 !== 0 ? "alternate" : "");
        });
    });
}
/**
 * Initialises the quick enquiry panel.
 */
function initQuickEnquiryPanel() {
    
    window.addEvent("domready", function() {        
        var fields = $$("#quick-enquiry-panel .panel-body .row input.default");
        fields.each(function(el) {            
            el.store("default", el.get("value"));
            el.addEvent("focus", function() {
                if(this.get("value") === this.retrieve("default")) {
                    this.set("value", "");
                    this.removeClass("default");
                }
            }.bind(el));
            el.addEvent("blur", function() {
                if(this.get("value").trim() === "") {
                    this.set("value", this.retrieve("default"));
                    this.addClass("default");
                }
            }.bind(el));
        });
        $("eq_submit").addEvent("click", function() {
            var query = new Hash();
            fields.each(function(el) {                
                if(el.get("value") !== el.retrieve("default")) {
                    query.set(el.get("id"), el.get("value"));
                }
            });
            document.location.replace($("eq_full_url").get("value") + "&" + query.toQueryString());
        });
    });        
}
function initCollapsiblePanels() {
    $$("#main-column-body .CollapsiblePanel").each(function(panel) {
        new Spry.Widget.CollapsiblePanel(panel, {contentIsOpen:false});
    });
}
function setFontSize(size) {
    Cookie.write("cms_font_size", size, {path: "/"});
    document.location.reload();
}
/*
License:
	http://clientside.cnet.com/wiki/cnet-libraries#license
*/
String.implement({
	parseQuery: function() {
		var rs = {};
		var vars = this.split(/[&;]/);
		if(vars.length) {
            vars.each(function(val) {
                var keys = val.split('=');
                if (keys.length && keys.length === 2) {
                    rs[decodeURIComponent(keys[0])] = decodeURIComponent(keys[1]);
                }
            });
        }
		return rs;
	}
});
var QueryString = {
    /**
     *
     */
    parse: function() {
        return document.location.search.parseQuery();
    }
};
function fixRtfMarkUp() {
    window.addEvent("domready", function() {
        $$(".rtf-container h1 + br", ".rtf-container h2 + br", ".rtf-container h3 + br").each(function(element) {
            element.destroy();        
        });
        $$(".rtf-container table[border=1]").each(function(element) {
            element.addClass("pretty");
            element.set("border", 0);
        });
    });
}

