$(function() {
    $('a#toollink').toggle(function() {
        $('#toolbox').slideDown("slow");
        return false;
    }, function() {
        $('#toolbox').slideUp("slow");
        return false;
    });
    $('a[rel=external]').click(function() { window.open(this.href); return false; });
    $('.viewmore').each(function() {
        var me = this;
        $(this).slideUp('normal', function() {
            $(this).after('<a href="#">View More</a>').next('a').click(function() {
                var lnk = this;
                $(me).slideToggle('normal', function() {
                    var txt = ($(lnk).text() == 'View More') ? 'Hide' : 'View More';
                    $(lnk).text(txt);
                });
                return false;
            });
        });

    });
    //feedback
    $('#feedbacklink a').feedback();
});
(function($) {
    $.fn.feedback = function() {
        return this.each(function() {
            var popup = $('#feedback').jqm(),
                txtarea = popup.find('textarea:first'),
                postUrl = popup.find('form:first').attr('action');

            popup.find('#submit').click(function() {
                var btn = this, reHTML = /<\S[^><]*>/g,
                feedback = $.trim(txtarea.val().replace(reHTML, ''));

                if (feedback.length > 0) {
                    //valid feedback. Show indication then post it
                    $(btn).text('Thanks..').attr('disabled', 'disabled');
                    $.post(postUrl, { 'feedback': feedback }, function() {
                        $(btn).text('Submit').removeAttr('disabled');
                        txtarea.val('');
                        popup.jqmHide();
                    });
                }

                return false;
            });
            $(this).click(function() { popup.jqmShow(); return false; });
        });
    };
    $.fn.compactlabels = function() {
        return this.each(function() {
            var me = this;
            var elfor = $(this).attr('for');
            if (elfor) {
                var field = $('#' + elfor);
                if (field.length > 0) {
                    //setup label
                    $(this).addClass('over-label').click(function() { field.focus(); });
                    // Hide any fields having an initial value.
                    if (field.val() !== '') {
                        $(me).css('text-indent', '-1000px');
                    }
                    //input focus event
                    field.focus(function() {
                        $(me).css('text-indent', '-1000px');
                    }).blur(function() {
                        if ($(this).val() === '') {
                            $(me).css('text-indent', '0px');
                        }
                    });
                }
            }
        });
    };
    $.fn.helpmodal = function(options) {
        var settings = { title: 'Help using this page', text: '' };
        if (options) { $.extend(settings, options); }
        if (!$.helpmodal) {
            $.helpmodal = $('<div class="jqmWindow" id="popuphelp"><div class="pophead"><h2><\/h2><a class="jqmClose" href="#">Close<\/a><\/div><div class="jqmTarget"><\/div><\/div>').appendTo('body').jqm();
        }
        return this.each(function() {
            $(this).click(function() {
                $.helpmodal.find('h2:first').html(settings.title).end().find('.jqmTarget').html(settings.text).end().jqmShow();
                return false;
            });
        });
    };

    $.fn.confirmmodal = function(options) {
        var settings = { text: 'Are you sure you want to do this?', action: function() { $.confirmmodal.jqmHide(); } };
        if (options) { $.extend(settings, options); }
        if (!$.confirmmodal) {
            $.confirmmodal = $('<div class="jqmWindow" id="confirmpopup"><div class="pophead"><h2>Are You Sure?<\/h2><a class="jqmClose" href="#">Close<\/a><\/div><div class="jqmTarget"><p class="warning"><\/p><a id="ok">Ok<\/a><button type="button" id="cancel" class="jqmClose">Cancel<\/button><\/div><\/div>').appendTo('body').jqm();
        }
        return this.each(function() {
            var lnk = this;
            $(this).click(function() {
                $.confirmmodal.find('.warning').html(settings.text).end().find('#ok').unbind().click(function() { settings.action.apply(lnk); return false;}).end().jqmShow();
                return false;
            });
        });
    };

})(jQuery);


