/*! Copyright 2008 360-Systems
 * $Id: utils.js 201 2010-08-26 15:05:16Z darreng $ */
/*jslint strict: true, undef: true, eqeqeq: true, immed: true, browser: true */
/*globals $, $$, $uid, $extend, $pick, $defined, Cookie, Element, Hash */
/*globals window, Class, Spry */
/* ----------------------------------------------------------------------------
 * @author  Andy Kernahan
 * @created 18/11/2008
 * @depends MooTools Core (>=1.2).
 * ------------------------------------------------------------------------- */
/* --------------------------------- Debug --------------------------------- */
/* A utility class that provides Firebug logging facility. This class can be
 * used with the MooTools Implements class mutator. */
var Debug = new Class({
    /* Logs a debug message to the Firebug console. */
    debug: function() {
        if(Debug.enabled && window.console && window.console.debug) {
            console.debug.apply(console, arguments);
        }
    }
});
$extend(Debug, {
    /* Indicates whether debugging is globally enabled / disabled. */
    enabled: false
});
/* ------------------------------- Extensions ------------------------------ */
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;
    }
});
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 fixRtfMarkUp() {
    window.addEvent("domready", function() {
        $$(".rtf-container h1", ".rtf-container h2", ".rtf-container h3").each(function(element) {
            var breaks = [];
            for(element = element.nextSibling; element && element.tagName === "BR"; element = element.nextSibling) {
                breaks.push($(element));
            }
            breaks.each(function(br) { br.destroy(); });
        });
        $$(".rtf-container table[border=1]").each(function(element) {
            element.addClass("pretty");
            element.set("border", 0);
        });
    });
}
function initialiseSpry() {
        $$(".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);
        });
}
var _preloadedImages = [];
function preloadImages() {
    var image;
    var images = $$('img');
	for(var i = 0; i < images.length; i++) {
        image = new Image();
        image.src = images[i].get("src");
		_preloadedImages.push(image);
	}
}
/*
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();
    }
};

