$(document).ready(function () {
    // Hide messages
    // Messages
    $('a.msg-close').hover(function () {
        $(this).stop().fadeTo(200, .7);
    }, function () {
        $(this).stop().fadeTo(200, 1);
    }).click(function () {$(this).parent().remove();return false});

    // Logo effect
    $('#footer-logo').hover(function () {
        $(this).stop().fadeTo(200, 0.7);
    }, function () {
        $(this).stop().fadeTo(200, 1);
    });

    // Toggle submenu
    $('#main-nav ul li').hover(function () {
        if ($(this).children('ul').length > 0) {
            $(this).children('ul').show();
        }
    }, function () {
        if ($(this).children('ul').length > 0) {
            $(this).children('ul').hide();
        }
    });

    // Form element focus/blur effect
    $('input, textarea').not('.button').focus(function () {
        $(this).addClass('focus');
    });
    $('input, textarea').not('.button').blur(function () {
        $(this).removeClass('focus');
    });

    // Grid hover effect
    $('table.listing tr').hover(function () {
        $(this).addClass('over');
    }, function () {
        $(this).removeClass('over');
    });
    
    // Check All
    $('input.checkall').click(function () {
        $("table.listing td input[type='checkbox']").attr('checked', $(this).is(':checked'));
    });

    // Preloader
    $('form').submit(function () {
        var preloaderWidth = 132;
        var $inputBtns = $('input[type="submit"]');
        $inputBtns.css('disabled', 'disables');
        $inputBtns.each(function () {
            var $elem = $(this);
            $preloader = $('<div class="preloader"></div>');
            var offset = $elem.offset();
            var top = offset.top - 3; // -3 for the extra padding
            var left = 0;
            if ($elem.parent().is('li')) {
                left = offset.left + $elem.totalWidth() - preloaderWidth + 3; // -3 for the extra padding
            } else {
                left = offset.left - 3; // -3 for the extra padding
            }
            $preloader.css({
                top: top + 'px',
                left: left + 'px'
            });
            $('body').append($preloader);
        });
    });

    // Sidebar
    $($('.sidebar ul li a.active').attr('href')).show();
    $('.sidebar ul li a').click(function () {
        $('.sidebar ul li a').removeClass('active');
        $('.tab-content').hide();
        $(this).addClass('active');
        $($(this).attr('href')).show();
        return false;
    });
    
    // Popup window
    $('a.offer_info_btn').click(function () {
        $('#popup_window_content').html($(this).parent().children('.offer_info_content').html());
        $('#popup_window').show();
        return false;
    });
    $('#popup_window_close').click(function () {
        $('#popup_window').hide();
        $('#popup_window_content').html('');
        return false;
    });
    $(document).keyup(function (event) {
        if (event.which == 27) {
            $('#popup_window_close').click();
        }
    });
    // jquery-ui datepicker
    $('.datepicker').datepicker();
    $('#apply_filters').click(function () {
        $(this).parents('form').submit();
        return false;
    });
});
