/*! Copyright 2008 360-Systems */
/*jslint undef: true, eqeqeq: true, browser: true */
/*globals $, $$, $pick, $defined, Cookie, Element, Hash, window, $uid, Spry */
/* ----------------------------------------------------------------------------
 * @author  Andy Kernahan
 * @created 18/11/2008
 * @depends MooTools Core (>=1.2).
 * ------------------------------------------------------------------------- */
Element.implement({
    /**
     * Shows this element.
     */
    show: function() {
        this.setStyle("display", "");
    },
    /**
     * Hides this element.
     */
    hide: function() {
        this.setStyle("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;
    },
    /**
     * Automatically restores the default value on blur when no change has
     * been made.
     */
     autoDefault: function() {
        var el = this;
        el.store("default", el.get("value"));
        el.addEvent("focus", function() {
            if(el.get("value") === el.retrieve("default")) {
                el.set("value", "");
                el.removeClass("default");
            }
        });
        el.addEvent("blur", function() {
            if(el.get("value").trim() === "") {
                el.set("value", el.retrieve("default"));
                el.addClass("default");
            }
        });
     },
     /**
      * Returns a value indicating if the default value is being used.
      */
     isDefault: function() {
        return this.get("value") === this.retrieve("default");
     }
});
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 .row input.default");
        fields.each(function(el) {
            el.autoDefault();
        });
        $("eq_submit").addEvent("click", function() {
            var query = new Hash();
            fields.each(function(el) {
                if(!el.isDefault()) {
                    query.set(el.get("id"), el.get("value"));
                }
            });
            document.location.replace($("eq_full_url").get("value") + "&" + query.toQueryString());
        });
    });
}
function initialisePage() {
    window.addEvent("domready", function() {
        $$(".CollapsiblePanel").each(function(el) {
            el = $(el);
            window["CollapsiblePanel_" + $uid(el)] = new Spry.Widget.CollapsiblePanel(el, {contentIsOpen:false});
        });
        $$(".MenuBarHorizontal").each(function(el) {
            el = $(el);
            window["MenuBarHorizontal_" + $uid(el)] = new Spry.Widget.MenuBar(el);
        });
        $$(".TabbedPanels").each(function(el) {
            el = $(el);
            window["TabbedPanels_" + $uid(el)] = new Spry.Widget.TabbedPanels(el);
        });
        $$(".SlidingPanels").each(function(el) {
            el = $(el);
            window[el.get("id")] = new Spry.Widget.SlidingPanels(el, {
                enableAutoAnimate: false,
                panelSelectedCallback: function(panel) {
                    var callback = window[el.get("id") + "PanelSelectedCallback"];
                    if(callback) {
                        callback(panel);
                    }
                }
            });           
        });
    });
}
/*
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();
    }
};
