$(document).ready(function() {
    
    $(this).run();
    
    var context = this;
    
    var hr = $('.td-r .content-m-b', context).height();
    var hc = $('.td-c .content-m-b', context).height();
    var hl = $('.td-l .links-m-m', context).height();
    if (hc) {
        if (hr + hc + 22 < hl) {
            $('.td-c .content-m-b', context).height(hl - hr - 22).css({'minHeight':(hl - hr - 22)});
        }
        else {
            $('.td-l .links-m-m', context).height(hr + hc + 22).css({'minHeight':hr + hc + 22});
        }
    }
    else {
        if (hr < hl) {
            $('.td-r .content-m-b', context).height(hl).css({'minHeight':hl});
        }
        else {
            $('.td-l .links-m-m', context).height(hr).css({'minHeight':hr});
        }
    }
    
    /*setInterval(function() {
        var hr = $('.td-r .content-m-b', context).height();
        var hc = $('.td-c .content-m-b', context).height();
        var hl = $('.td-l .links-m-m', context).height();
        if (hr < hl) {
            $('.td-r .content-m-b', context).height(hl);
        }
        else {
            $('.td-l .links-m-m', context).height(hr);
        }
    }, 1);*/
    
});

(function($) {

jQuery.fn.run = function() {
    
    var context = this;
    
    $('.content-menu-item', context).hover(
        function() {
            $('.content-menu-item-select-submenu:hidden', this).fadeIn('slow');
        },
        function() {
            $('.content-menu-item-select-submenu:visible', this).fadeOut('slow');
        }
    );
    
    //$('.faq-a', context).hide();
    
    $('.faq-q a', context).click(
        function() {
            $(this).parent().next().toggle('blind', {}, 'slow');
            return false;
        }
    );
    
    $('.popup-close', context).click(
        function(e) {
            $('#popup').fadeOut('slow');
        }
    );
    
    $('.popup-show', context).click(
        function(e) {
            var popup = $('#popup');
            var url = $(this).attr('popup');
            var tooltip = $(this).parents('.tooltip');
            var offset = tooltip.offset();
            popup.fadeOut('slow', function() {
                $.get(url, {}, function(data) {
                    $('.popup-m-m', popup).html(data);
                    var h = popup.height();
                    var w = popup.width();
                    var x = offset.left - 8;
                    var y = offset.top - h + 8 - 14 + tooltip.height();
                    var ww = $(document).width(); 
                    var wh = $(document).height();
                    if (ww - x - w < 10) {
                        x = ww - w - 10;
                    }
                    if (y < 10) {
                        y = 10;
                    }
                    popup.css({'top':y,'left':x}).fadeIn('slow');
                    bind(popup.get(0));
                });
            });
            return false;
        }
    );
    
    $('.popup-m-m form', context).submit(
        function() {
            $(this).ajaxSubmit({
                'target':$('.popup-m-m', context),
                'beforeSubmit':function(formData, jqForm, options) {
                    //alert($.param(formData));
                },
                'success':function(responseText, statusText) {
                    bind(context);
                    //alert(responseText);
                },
                'iframe':true
            });
            return false;
        }
    );
    
    $('.popup-m-m .sublinks a', context).click(
        function() {
            $(this).parents('form').attr('action', $(this).attr('href')).submit();
            return false;
        }
    );
    
    return this;
    
};

})(jQuery);
