/**
 * Projekt PVM - js/pvm.js
 * (c) 2011 datahouse AG
 */

$(function() {
        $('div#jumper a[href*=#]').click(function() {
                return jumpto(this.hash.slice(1));
        });
});

var jumpto = function(name) {
    var target = $('[name=' + name +']');
    if (target.length) {
        var offset = target.offset().top;
        $('html,body').animate({scrollTop: offset}, 10);
        return false;
    } 
};

function sure(name) {
    return confirm("Wollen sie\n" + name + "\n wirklich loeschen ?");
}

needToConfirm = false;
function confirm_leave() {
    if (needToConfirm) {
        return "Damit werden die Veränderungen nicht gespeichert!";
    }
}
function confirm_leave2() {
    if (needToConfirm) {
        return "Damit werden die letzten Veränderungen nicht mitgeschickt!";
    }
}

/*
the same in JQuery (not working with IE7)
$(window).bind('beforeunload', function() {
    if (needToConfirm) {
        return 'Damit werden die Veränderungen nicht gespeichert!';
    }
}); */

function change_caption(id,newtext) {
     document.getElementById(id).value = newtext;
 }

$(document).ready( function(){
    
    // bind "click" event for links with title="submit"
    $('a#form_send').click( function(){
        // it submits the form it is contained within
        $(this).parents("form").submit();
    });
    

    // Dialog boxes 

    // sidebar-delete-confirmation
    if ($("dialog").length) {
        $("#dialog").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 150,
                modal: true
        })
    }
        
    $('a.opener').click(function(e) {
        e.preventDefault();
        url=$(this).attr("href");
        
        $("#dialog").dialog({
            buttons: {
                OK: function() {
                    $(this).dialog('close');
                    $(window).unload();
                    location = url;
                },
                Abbrechen: function() {
                    $(this).dialog('close');
                }
            }
        });
        
        $('#dialog').dialog('open');
	});
    
    // file-delete-confirmation
    if  ($('#dialog2').length) {
        $("#dialog2").dialog({
            bgiframe: true,
            autoOpen: false,
            height: 150,
            modal: true,
            buttons: {
                OK: function() {
                    $('#dialog2 form').submit();
                    $(this).dialog('close');
                },
                Abbrechen: function() {
                    $(this).dialog('close');
                }
            }
        });
    }
    
    $('#submit_delete').click(function(e) {
        e.preventDefault();
        $('#dialog2').dialog('open')
    })
    
    // if 'Abschicken' changed to 'Vorschau', don't send emails
    $('#submit_newsletter').click(function(e) {
        e.preventDefault();
        if (e.target.value == 'Vorschau') {
           $('#newsletter_edit_form').submit();
            return;
        } else {
            $('#dialog2').dialog('open');
        }
    })
    
     $('#submit_test_newsletter').click(function(e) {
        if (e.target.value == 'Vorschau') {
            e.preventDefault();            
           $('#newsletter_edit_form').submit();
            return;
        } 
    })
    
    // acknowledgement of add/delete file/sidebar
    if  ($('#dialog3').length) {
        $("#dialog3").dialog({
            bgiframe: true,
            autoOpen: true,
            height: 150,
            modal: false,
            buttons: {
                OK: function() {
                    $('#dialog3 form').submit();
                    $(this).dialog('close');
                }
            }
        });
    }
    
    // newsletter registration confirmation
     if  ($('#dialog4').length) {
        $("#dialog4").dialog({
            bgiframe: true,
            autoOpen: false,
            height: 150,
            modal: true,
            buttons: {
                OK: function() {
                    $(this).dialog('close');
                    $('#newsletter_reg').submit();
                },
                Abbrechen: function() {
                    $(this).dialog('close');
                }
            }
        });
    }
    
    $('#newsletter_reg #activate').click(function(e) {
        //e.preventDefault();
        $('#dialog4').dialog('open');
    })
    
    
});
