/* $Id: editor.js 622 2010-03-15 14:58:06Z 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.8);
    var iframe = $(document.createElement("iframe")).addClass("overframe");

    var cookieOption = {
    	expiresAt: new Date(2099, 1, 1)
    };
    $.cookies.setOptions(cookieOption);

    $.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());
    });

    //

    if($.cookies.get('admin_switch') == 'on') {
		$('.edit-links, .overphotokill, .comment-delete').show();
		$('.showeditlinks a').attr('style', 'color: red');
	} else {
		$('.edit-links, .overphotokill, .comment-delete').hide();
		$('.showeditlinks a').removeAttr('style');
	}
    $('.showeditlinks a').click(function(){
    	if($.cookies.get('admin_switch') == 'on') {
    		$('.edit-links, .overphotokill, .comment-delete').hide();
    		$('.showeditlinks a').removeAttr('style');
			$.cookies.del('admin_switch');
    	} else {
			$('.edit-links, .overphotokill, .comment-delete').show();
    		$('.showeditlinks a').attr('style', 'color: red');
			$.cookies.set('admin_switch', 'on');
    	}
		return false;
    });
	$('.nohide').show();

    //

    $(".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();});
                    if (me.hasClass('n-redirect')) {
                        $(".wait").oneTime(150, function(){$(location).attr('href', me.attr('rel'));});
                    }
                } else {
                    $(".wait").css('background-color', '#fdd').css('border-color', '#d77').css('color', '#d00').text(data.error);
                }
                $(".wait").oneTime(500, function(){$(this).remove();});
            });

        }
        return false;
    });

    $(".js-confirm").click(function(){
        if (!confirm(translate_are_you_sure_want+" "+$(this).attr('title')+' "'+$(this).text()+'"?')) {
            return false;
        }
    });

});