jQuery(function($){
    Drupal.behaviors.ajax_popup = function(context){
        var ajax_popup = $('a.ajax_popup', context);
        if (!ajax_popup.data('flag')) {
            ajax_popup.data('flag', true).click(function() {
                var _this = $(this);
                ajax_popup_load(_this.attr('href'), {nid: Drupal.settings.ajax_popup_node}, function(_ajax_popup) {
                    if (_ajax_popup.find('form').length) {
                        var _submit = _ajax_popup.find('form');
                        _submit.submit(function(){
                            $('.body', _ajax_popup).append('<div class="overlay"/>');
                            var _form_values = $(this).serialize();
                            _form_values = _form_values +'&op='+ _submit.attr('value');
                            $.post(Drupal.settings.basePath +'ajax_popup_submit', _form_values, function(datas){
                                datas = Drupal.parseJson(datas);
                                if (datas.status) {
                                    if (datas.redirect) {
                                        var _href = window.location.protocol +'//'+ window.location.host + datas.redirect;
                                        window.location.href = _href;
                                    }
                                    if (datas.data) {
                                        ajax_popup_load(null, datas);
                                    }
                                } else {
                                    ajax_popup_hide();
                                }
                            });
                            return false;
                        });
                    }
                });
                return false;
            });
        }
        var ajax_popup_hiding = $('#popup_overlay', context);
        if (!ajax_popup_hiding.data('flag')) {
            ajax_popup_hiding.data('flag', true).click(function() {
                ajax_popup_hide();
                return false;
            });
        }
    }

    Drupal.attachBehaviors($('body'));
});

function ajax_popup_load(url, params, callback) {
    var w = $(window);
    $('#ajax_popup').remove();
    jQuery(Drupal.settings.ajax_popup_box).appendTo('body');
    var a = $('#ajax_popup'),
        b = $('.content', a);
    $('.ajax_popup span.close', a).click(function(){
        ajax_popup_hide();
    });
    a.css({ top: (((w.height()/2)-a.height()/2)), left: w.width() / 2 - (a.width() / 2) })
        .fadeIn('normal');
    popup_view = function (datas){
        if (url) {
            datas = Drupal.parseJson(datas);
        }
        if (datas.status){
            if (datas.title) {
                $('.header h1', a).html(datas.title);
            } else {
                $('.header h1').hide();
            }
            b.append(datas.data);
        }
        if (callback) callback(a);
        b.css({ width: 'auto', height: 'auto'}).removeClass('process_load').fadeIn('fast');
        a.css({ left: w.width() / 2 - b.width() / 2, top: (((w.height()/2)-a.height()/2)) });
    }
    if (url) {
        $.post(Drupal.settings.basePath +'ajax_popup/'+ url.replace(Drupal.settings.basePath, ''), params, popup_view);
    } else {
        popup_view(params);
    }
}

function ajax_popup_hide(){
    $('#ajax_popup').fadeOut('normal');
}
