﻿function parseQuery(query) {
    var Params = new Object();
    if (!query) return Params; // return empty object
    var Pairs = query.split(/[;&]/);
    for (var i = 0; i < Pairs.length; i++) {
        var KeyVal = Pairs[i].split('=');
        if (!KeyVal || KeyVal.length != 2) continue;
        var key = unescape(KeyVal[0]);
        var val = unescape(KeyVal[1]);
        val = val.replace(/\+/g, ' ');
        Params[key] = val;
    }
    return Params;
}

function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height() + 70;
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}


$(document).ready(function() {
    $("hr").wrap("<div class='hr'></div>");

    // find the div.fade elements and hook the hover event
    $('.leftmenuFader .fader').each(function() {
        $(this).hover(function() {
            // on hovering over find the element we want to fade *up*
            var fade = $(this).find('.hover');
            fade.show();
            fade.addClass('hoverText');


            //            var fade = $(this).find('.hover');
            //            // if the element is currently being animated (to fadeOut)...
            //            if (fade.is(':animated')) {
            //                // ...stop the current animation, and fade it to 1 from current position
            //                fade.stop().fadeTo(100, 1);
            //            } else {
            //                fade.fadeIn(100);
            //            }
        }, function() {
            var fade = $(this).find('.hover');
            fade.hide();
            fade.removeClass('hoverText');

            //            var fade = $(this).find('.hover');
            //            if (fade.is(':animated')) {
            //                fade.stop().fadeTo(2, 1);
            //            } else {
            //                fade.fadeOut(2);
            //            }
        });
    });
    // get rid of the text
    $('.leftmenuFader .fader > .hover').empty();


    $('.leftmenuFader .adminMenu').each(function() {
        $(this).hover(function() {
            $(this).addClass('adminMenuHover');
        }, function() {
            $(this).removeClass('adminMenuHover');
        });
    });



    $('#user').click(function() { if ($(this).val() == 'brukernavn') $(this).val(''); });
    $('#user').blur(function() { if ($(this).val() == '') { $(this).val('brukernavn'); } $('#pass').click(); $('#pass').focus() });
    $('#pass').click(function() {
        if ($(this).val() == 'passord') {
            $('#passwd').html('<input type="password" class="userinput" name="pass" id="pass" size="20" />');
            $('#pass').focus();
        }
    });
//    $('#pass').blur(function() { if ($(this).val() == '') $(this).val('passord'); });

    jQuery.fn.alternateRowColors = function() {
        $('tbody tr:odd', this).removeClass('even').addClass('odd');
        $('tbody tr:even', this).removeClass('odd').addClass('even');
        return this;
    };

    jQuery.fn.alternateDivResultColors = function() {
        $('.result:odd', this).removeClass('even').addClass('odd');
        $('.result:even', this).removeClass('odd').addClass('even');
        return this;
    };

    jQuery.fn.alternateDivRowColors = function() {
        $('.row:odd', this).removeClass('even').addClass('odd');
        $('.row:even', this).removeClass('odd').addClass('even');
        return this;
    };


    jQuery.fn.paginateTable = function() {
        var currentPage = 0;
        var numPerPage = 10;
        var $table = $(this);
        $table.find('tbody tr').show().lt(currentPage * numPerPage).hide().end().gt((currentPage + 1) * numPerPage - 1).hide().end();
        return this;
    };


    /**
    * appendDom - Extremely flexible tool for dynamic dom creation.
    *   http://byron-adams.com/projects/jquery/appendDom
    *
    * Copyright (c) 2007 Byron Adams (http://byron-adams.com)
    * Dual licensed under the MIT (MIT-LICENSE.txt)
    * and GPL (GPL-LICENSE.txt) licenses.
    *
    */
    jQuery.fn.appendDom = function(template) {
        return this.each(function() {
            for (element in template) {
                var el = (typeof (template[element].tagName) === 'string') ?
        document.createElement(template[element].tagName) : document.createTextNode('');
                delete template[element].tagName;
                for (attrib in template[element]) {
                    switch (typeof (template[element][attrib])) {
                        case 'string':
                            if (typeof (el[attrib]) === 'string') {
                                el[attrib] = template[element][attrib];
                            } else {
                                el.setAttribute(attrib, template[element][attrib]);
                            }
                            break;
                        case 'function':
                            el[attrib] = template[element][attrib];
                            break;
                        case 'object':
                            if (attrib === 'childNodes') { $(el).appendDom(template[element][attrib]); }
                            break;
                    }
                }
                this.appendChild(el);
            }
        });
    };
});
