﻿// Web <form/> 2.0
$(function() {
    var focus = function(e) {
        var sender = $(e.target);
        var pt = sender.offset();
        e.data.css({ top: pt.top, left: pt.left + sender.outerWidth(true) + 1 });
        e.data.fadeIn(300);
    };
    var blur = function(e) {
        e.data.hide();
    };
    function parseTooltip(text) {
        var content = new Array();
        var re = /\*([^*]+)\*/g;
        var lastIndex = 0, m;
        while ((m = re.exec(text)) != null) {
            if (lastIndex < m.index)
                content.push(document.createTextNode(text.substring(lastIndex, m.index)));
            var strong = document.createElement("strong");
            strong.appendChild(document.createTextNode(m[1]));
            content.push(strong);
            lastIndex = re.lastIndex;
        }
        if (lastIndex < text.length)
            content.push(document.createTextNode(text.substring(lastIndex, text.length)));
        return $(content);
    }
    $(":input")
        .not("[type='checkbox'], [type='radio']")
            .focus(function() { $(this).addClass("focus"); })
            .blur(function() { $(this).removeClass("focus"); })
            .filter("[title]")
                .each(function() {
                    var el = $(this);
                    var div = $("<div class='tooltip' style='position:absolute; display:none;'></div>")
                        .append(parseTooltip(el.attr("title")))
                            .appendTo(document.body);
                    el.removeAttr("title");
                    el.bind('focus', div, focus);
                    el.bind('blur', div, blur);
                })
            .end()
        .end()
        .filter("[type='checkbox'], [type='radio']")
            .each(function() {
                var el = $(this.parentNode);
                if (el.is("label[title]")) {
                    var div = $("<div class='tooltip' style='position:absolute; display:none;'></div>")
                        .append(parseTooltip(el.attr("title")))
                            .appendTo(document.body);
                    el.removeAttr("title");
                    el.bind('mouseover', div, focus);
                    el.bind('mouseout', div, blur);
                }
            })
        .end()
        .filter("textarea")
            .filter(function() { return $.mozilla; })
                .resizable({ handles: "se" })
            .end()
        .end()
    ;
});

$(function() {
    $("input.datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yymmdd',
        firstDay: 1,
        nextText: '',
        prevText: '',
        dayNamesMin: ['sö', 'må', 'ti', 'on', 'to', 'fr', 'lö'],
        monthNamesShort: ['jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
        duration: ''
    });
});