﻿(function ($) {

    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            //alert('ah: ' + ah + ' ph: ' + ph + ' mh: ' + mh);
            $(this).css('margin-top', mh);
        });
    };

})(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");
    });

    /*********************************************************************************/
    //if table, ul or ol has class jq-list-pos: add jq-list-pos class to any eqv children to
    $('table.jq-list-pos, ul.jq-list-pos, ol.jq-list-pos').each(function () {
        $(this).find('table, ul, ol').addClass('jq-list-pos');
    });

    /*********************************************************************************/
    //add position classes (jq-odd, jq-even, jq-first, jq-last, jq-single) to items 
    //in list containers classed 'jq-list-pos', applies to table- and ul child elements
    $('table.jq-list-pos, ul.jq-list-pos, ol.jq-list-pos').each(function () {

        $(this).find('tr:odd, li:odd').addClass('jq-odd');
        $(this).find('tr:even, li:even').addClass('jq-even');
        $(this).find('tr:first, li:first').addClass('jq-first');
        $(this).find('tr:last, li:last').addClass('jq-last');
        $(this).find('tr:only-child, li:only-child').addClass('jq-single');

        /* handle table td element*/
        $(this).find('tr').each(function () {
            $(this).find('td:odd').addClass('jq-odd');
            $(this).find('td:even').addClass('jq-even');
            $(this).find('td:first').addClass('jq-first');
            $(this).find('td:last').addClass('jq-last');
            $(this).find('td:only-child').addClass('jq-single');
        });
    });
});
