
(function(jQuery){
    var dialog
    function onFormSubmit(ev){
        ev.preventDefault();
        var elems = jQuery(".popup_form INPUT[type='text']").add( jQuery(".popup_form textarea") )
        var valid = true;
        elems.each( function(){ 
            if(jQuery.trim(this.value) == ''){
                jQuery(this).addClass("validation_failed")
                valid = false;
            }
        })
        if(valid){
            var params = jQuery(".popup_form FORM").serializeArray()
            params.push({name:"_", value: session})
            jQuery.ajax({
                url: ev.data.url,
                data: jQuery.param(params),
                type: "POST",
                success: function(r){ 
                    if(r.success){
                        dialog.html(ev.data.successMsg)
                        window.setTimeout(function(){
                            dialog.dialog("close") 
                        },2500);
                    }
                    else{
                        jQuery(".popup_form .info").html("Verifikation falsch.")
                        var img = jQuery(".popup_form #captcha_img")[0]
                        img.src = img.src + "a"
                    }
                }
            })
        }
    }

    function main(){
        jQuery('#comment_add').click(function(ev){
            ev.preventDefault();
            jQuery.ajax({
                url: this.href + "&ajax=1",
                success: function(r){
                    dialog = jQuery('<div id="comment_form" class="content popup_form"></div>')
                          .html(r.form)
                          .dialog({ title: r.head,
                                    modal: true,  
                                    resizable: false,
                                    width: 490
                                    });
                }
            });
        });

        jQuery('#comment_show').click(function(ev){
            ev.preventDefault()
            jQuery.ajax({ 
                url: this.href + "&ajax=1",
                success: function(r){
                    jQuery('#comment_list').html(r);
                    jQuery('.comment a').click(function(ev){
                        ev.preventDefault();
                        jQuery.ajax({
                            url: this.href + "&ajax=1",
                            success: function(r){
                                dialog = jQuery('<div id="comment_form" class="content popup_form"></div>')
                                      .html(r.form)
                                      .dialog({ title: r.head,
                                                modal: true,  
                                                resizable: false,
                                                width: 490
                                                });
                            }
                        });
                    });  

                }
            })
        });

        jQuery('#recommend A').click(function(ev){
            ev.preventDefault();
            jQuery.ajax({
                url: this.href + "&ajax=1&_=" + session,
                success: function(r){
                    dialog = jQuery('<div id="recommend_form" class="content popup_form"></div>')
                          .html(r.form)
                          .dialog({ title: r.head,
                                    modal: true,  
                                    resizable: false,
                                    width: 490
                                    });
                    jQuery("#recommend_form FORM").bind("submit", {url: "/xd/public/comment/recommend_send.html",
                                                              successMsg: "Ihre Empfehlung wurde verschickt."},
                                                              onFormSubmit)
                }
            });
        });
    }

    jQuery(document).ready(main)

})(jQuery)

