$(document).ready(function() {
    $(this).run();
});

jQuery.fn.run = function() {
    
    var context = this;
    
    $('.popup', context).click(function(e) {
        var url = $(this).attr('popup');
        if (!url) {
            url = $(this).attr('href');
        }
        if (!url) {
            return false;
        }
        url += (url.indexOf('?') == -1 ? '?' : '&') + 'template=79';
        $('#shadow').height($(document).height());
        $('#popup-m-i').attr('src', url).height($(window).height()-200);
        $('#popup').css({'top':$(window).scrollTop()+50,'left':parseInt(($(window).width()-$('#popup').width())/2)});
        if ($.browser.msie == true) {
            $('#shadow').show();
            $('#popup').show();
        }
        else {
            $('#shadow').fadeIn('fast');
            $('#popup').fadeIn('fast');
        }
        return false;
    });

    $('#popup-m-c', context).click(function(e) {
        if ($.browser.msie == true) {
            $('#shadow').hide();
            $('#popup').hide();
        }
        else {
            $('#shadow').fadeOut('fast');
            $('#popup').fadeOut('fast');
        }
        return false;
    });
    
    $('#popup-m-i form:not([target])', context).ajaxForm({
        'data':{'template':'80'},
        'success':function(data) {
            $('#popup-m-i').html(data);
            $('#popup').run();
        }
    });
    
    $(window, context).scroll(function(e) {
        $('#popup').dequeue().animate({'top':$(window).scrollTop() + 50,'left':parseInt(($(window).width() - $('#popup').width()) / 2)}, 'slow');
    });
    
    $(window, context).resize(function(e) {
        $('#shadow').height($(document).height());
        $('#popup-m-i').height($(window).height() - 200);
        $('#popup').dequeue().animate({'top':$(window).scrollTop() + 50,'left':parseInt(($(window).width() - $('#popup').width()) / 2)}, 'slow');
    });
    
    return this;
    
};

