﻿(function ($) {

    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {

        //fixed height?
        var pheight = arguments[0];

        return this.each(function (i) {
            var ah = $(this).height();
            var ph = pheight != null ? pheight : $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            //alert('element height: ' + ah + '\nparent height: ' + ph + '\nmargin: ' + mh);
            $(this).css('margin-top', mh);
        });
    };

    // INPUT (text) WATERMARK
    $.fn.Watermark = function (watermarktext) {

        var $$ = $(this);

        $$.val(watermarktext);
        $$.addClass('jq-watermark');

        $$.focusin(function () {
            $$.removeClass('jq-watermark');

            if ($$.val() == watermarktext)
                $$.val('');
        });

        $$.focusout(function () {
            if ($$.val().length == 0) {
                $$.addClass('jq-watermark');
                $$.val(watermarktext);
            }
        });
    }

})(jQuery);

$(document).ready(function () {

    // Find all anchors with the class popup and add the onclick function to open the link in a new window.
    $('a.popup').click(function () {
        window.open($(this).attr('href'));
        return false;
    });

    // Traverse through all links with the class popup and modify the title attribute.
    $('a[class*="popup"]').each(function () {
        $(this).attr('title', $(this).attr('title') + ' (Öppnas i nytt fönster/ny flik)');
    });

    // Find all divs with with answers and hide them
    $('div.answer').hide();

    $('#query').labelize();

    $('.google-translate').translate({ imageUrl: '/images/google-translate.png' });

    // Google Translate
    $('.translate').click(function () {
        if ($('#translate-dialog').length == 0)
            OpenTranslate(this)
        else
            $('#translate-dialog').remove();
        return false;
    });

    $('img').error(function () {
        //$(this).attr("src", "/images/google-translate.png");
    });
});
