/* $Id: editor.js 330 2009-12-08 15:00:19Z tigra $ */
$(document).ready(function(){
    var wait = $(document.createElement("div")).addClass("wait").text(translate_wait);
    var xButton = $(document.createElement("div")).addClass("overclose").append($(document.createElement("span")).text("close"));
    var overlay = $(document.createElement("div")).addClass("overlay").css("opacity", 0.5);
    var iframe = $(document.createElement("iframe")).addClass("overframe");

    $.fn.generateEditorOverlay = function(href, id){
        $("body").prepend(overlay.click(function(){$.fn.destroyOverlay(href, id);}).clone(true));
        $(".overlay").after(iframe.attr('src', href+'edit/'+id).clone(true)).after(xButton.click(function(){$.fn.destroyOverlay(href, id);}).clone(true));
    };

    $.fn.generateOverlay = function(href){
        $("body").prepend(overlay.click(function(){$.fn.destroyOverlay(href);}).clone(true));
        $(".overlay").after(iframe.attr('src', href).clone(true)).after(xButton.click(function(){$.fn.destroyOverlay(href);}).clone(true));
    };

    $.fn.destroyOverlay = function(href, id){
        $(".overlay, .overframe, .overclose").remove();
        location.reload();
    };

    $.each($('.link-add, .link-edit, .link-delete'),function(){
        $(this).attr('title', $(this).find('span').text());
    });
    $('.edit-links, .overphotokill, .comment-delete').hide();
    $('.showeditlinks a').click(function(){
        $('.edit-links, .overphotokill, .comment-delete').removeAttr('style');
        return false;
    });
    $(".overeditor").click(function(){
        $.fn.generateEditorOverlay($(this).attr('href'), $(this).attr('id'));
        return false;
    });
    $(".overlink").click(function(){
        $.fn.generateOverlay($(this).attr('href'));
        return false;
    });
    $(".overkill").click(function(){
        var me = $(this);
        if (confirm(delete_confirm)) {
            $("body").prepend($(wait).clone(true));
            $.getJSON(me.attr('href'), function(data){
                if (data.ok) {
                    $(".wait").css('background-color', '#efe').css('border-color', '#0a0').css('color', '#0a0').text(data.ok);
                    me.closest(".killme").animate({backgroundColor: "#fdd" }, 150).animate({opacity: 0 }, 500, function() {$(this).remove();});
                } else {
                    $(".wait").css('background-color', '#fdd').css('border-color', '#d77').css('color', '#d00').text(data.error);
                }
                $(".wait").oneTime(1500, function(){$(this).remove();});
            });
            if (me.hasClass('n-redirect')) {
                $(location).attr('href', me.attr('rel'));
            }
        }
        return false;
    });

    $(".js-confirm").click(function(){
        if (!confirm(translate_are_you_sure_want+" "+$(this).attr('title')+' "'+$(this).text()+'"?')) {
            return false;
        }
    });
    
    var multifieldName = '';
    var multifieldCounter = 0;
    var kill = $(document.createElement("a")).text('remove').addClass("multifilekill").click(function(){
            $(this).closest("div").animate({backgroundColor: "#fdd" }, 150).animate({opacity: 0 }, 500, function() {$(this).remove();});
        });
    
    $(".multifile").css('cursor', 'pointer').click(function(){
        if (multifieldName == '') {
            multifieldName = $(this).closest('div').find('.file-input:first').find('input').attr('name');
            $(this).closest('div').find('.file-input:first').find('input').attr('name', multifieldName+'['+(multifieldCounter++)+']').after(kill);
        }
       
        var newinput = $(this).closest('div').find('.file-input:first').clone(true).hide();
        newinput.find('input').attr('name', multifieldName+'['+(multifieldCounter++)+']');
        newinput.insertBefore(this).show(500);
        return false;
    });
});