
(function ($) {
 
    /**
	 * Initialise input hints on all matched inputs.
	 *
	 * Usage examples:
	 *
	 * Add hints to all inputs with the 'title' attribute set:
	 *   $('input[title],textarea[title]').inputHint();
     *
     * Add hints to all matched elements, grabbing the hint text from each element's
     * adjacent <kbd/> tag:
     *   $('input').inputHint({using: '+ kbd'});
	 *
	 * Options keys:
	 *  using: jQuery selector locating element containing hint text, relative to
	 *         the input currently being considered.
	 *  hintAttr - tag attribute containing hint text. Default: 'title'
	 *  hintClass - CSS class to apply to inputs with active hints. Default: 'hint'
	 */
    $.fn.inputHint = function(options) {
 
        options = $.extend({
            hintClass: 'hint',
            hintAttr: 'title'
        }, options || {});
 
        function hintFor(element) {
            var h;
            if (options.using && (h = $(options.using, element)).length > 0) {
                return h.text();
            } else {
                return $(element).attr(options.hintAttr) || '';
            }
        }
 
        function showHint() {
            if ($(this).val() == '') {
                $(this).addClass(options.hintClass).val(hintFor(this));
            }
        }
 
        function removeHint() {
            if ($(this).hasClass(options.hintClass)) $(this).removeClass(options.hintClass).val('');
        }
 
        this.filter(function() {
            return !!hintFor(this);
        })
        .focus(removeHint).blur(showHint).blur();
 
        this.each(function() {
            var self = this;
            $(this).parents('form').submit(function() { 
                removeHint.apply(self);
            });
        });
 
        return this.end(); // undo filter
 
    };
 
})(jQuery);

jQuery.exists = function(selector) {
    return ($(selector).length > 0);
};
    
//extends jQuery Tools Overlay Effect
jQuery.ajaxOverlay = function(p) {
    //p.selector, p.openNow, p.gmapContent, p.centered
    if(!p) p = {};
    var topPos = 'center';
    var wasClosed = false;
    var topPosVariableContent = '15%';
    if(!p.gmapContent && !p.centered) topPos = topPosVariableContent;
    if(!p.selector)p.selector = "a[rel]";
    var wrap = null;
    var ajaxOverlayApi = $(p.selector).overlay({
        onBeforeLoad: function() {
            wrap = this.getContent().find(".contentWrap");
            if(p.gmapContent){
                if(!($.browser.msie && $.browser.version < '7'))
                    $(wrap).parent().css('display', 'none').fadeTo(300,1);
                createMap(this.getTrigger().attr("address"), wrap[0]);
            }
            if($.browser.msie && $.browser.version < '7')
                $('select').css('visibility','hidden');
        },
        onLoad: function() {
            if(!p.gmapContent){

                var urlLoad = null;
                urlLoad = (p.loadPage && !wasClosed ? p.loadPage : this.getTrigger().attr("href"));

                if(urlLoad != null) {
                    $(wrap).parent().fadeTo(1, 0);
                }
                $(wrap).load(urlLoad, function() {
                    $(wrap).parent().fadeTo(300, 1);
                });
            }
        },
        onClose: function() {
            if($.browser.msie && $.browser.version < '7')
                $('select').css('visibility','');
            $(wrap).empty();
            wrap = null;
            wasClosed = true;
        },
        top: topPos,
        left: 'center',
        api:true,
        expose: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.7
        }
    });
    $("div.overlay").appendTo("body");
    if(p.openNow) ajaxOverlayApi.load();
};

jQuery.getHashParams = function(setUrl) {

    var hashParams = {};
    var urlHashParams = null;
    var urlHashQuery = null;
    
    if(setUrl) {
        urlHashParams = setUrl;
        urlHashParams = urlHashParams.split('#');
        hashParams.urlFile = decodeURIComponent(urlHashParams[0]);
    } else {
        urlHashParams = window.location.toString();
        urlHashParams = urlHashParams.split('#');
        hashParams.urlFile = decodeURIComponent(window.location.pathname);
        hashParams.urlQuery = decodeURIComponent(window.location.search);

        if(hashParams.urlQuery != '') {
            urlHashQuery = hashParams.urlQuery.split('?');
            urlHashQuery = (urlHashQuery[1] ? urlHashQuery[1].split('&') : null);

            if(urlHashQuery != null) {
                hashParams.urlQuery = {};
                for(i=0; i<urlHashQuery.length; i++) {
                    quer = urlHashQuery[i].split('=');
                    hashParams.urlQuery[decodeURIComponent(quer[0])] = decodeURIComponent(quer[1]);
                }
            }
        }
    }
    
    urlHashParams = ( urlHashParams[1] ? urlHashParams[1].split('?') : null);

    if(urlHashParams != null) {
        hashParams.urlHash = decodeURIComponent(urlHashParams[0]);
        urlHashParams = (urlHashParams[1] ? urlHashParams[1].split('&') : null);

        if(urlHashParams != null) {
            for(i=0; i<urlHashParams.length; i++) {
                param = urlHashParams[i].split('=');
                hashParams[decodeURIComponent(param[0])] = decodeURIComponent(param[1]);
            }
        }
    }
    return hashParams;
};

