$(document).ready(function()
    {
        $(document).keydown( function( e ) {
            if( e.which == 27) {  // escape, close box
                $(".bubble_container").remove();
            }
        });

        $(".mvbutton").click( function(e)
        {
            var tomove = $(this).attr('class').split(" ")[1];

            var x = e.pageX;
            var y = e.pageY;

            addBubble(x,y,tomove,"false");
        });

    });

function createBubble(id,content,x,y,corner,element)
{
    if (content == '')
        return false;

    var bubbnum = id;

    if ($("#bn_"+id).is(".bubble_container"))
    {
        $("#bn_"+id).remove();
    }

    // obal bubliny
    var bubble_container = $("<div/>");
    bubble_container.attr("class", "bubble_container");
    bubble_container.attr("id", "bn_" + bubbnum);
    //vlozeni do dokumentu
    if (element) {
        $("#" + element).append(bubble_container);
    }
    else
    {
        $('body').append(bubble_container);
    }
    var width = bubble_container.css("width");
   

    width = width.substr(0, width.length-2);
   
    
    var xz = x;
    var xr = x - width;
    
    
    if (xr < 0)
    {
        x = xz;
    }
    else
    {
        x = xr;
    }


    bubble_container.css("top", y+"px");
    bubble_container.css("left", x+"px");


    /* top part - start*/
    var t_l = $("<div/>");

    var t = $("<div/>");
    t.attr("class", "t");

    var t_r = $("<div/>");
    if (corner == "false")
    {
        if (xr < 0)
        {
            t_l.attr("class", "t_l_c");
            t_r.attr("class", "t_r");
        }
        else
        {
            t_l.attr("class", "t_l");
            t_r.attr("class", "t_r_c");
        }
    }
    else 
    if(corner == "right")
    {
        t_l.attr("class", "t_l");
        t_r.attr("class", "t_r_c");
    }
    else
    if(corner == "left")
    {
        t_l.attr("class", "t_l_c");
        t_r.attr("class", "t_r");
    }
    else
    {
        t_l.attr("class", "t_l");
        t_r.attr("class", "t_r");
    }
    /* top part - end */


    /* mid part - start*/
    // obsah
    var bubble_content = $("<div><div/>");
    bubble_content.attr("class", "bubble_content");
    bubble_content.append(content);
    


    //tlacitko
    var bubble_close_btn = $("<button/>");
    bubble_close_btn.attr("class", "bubble_close_button");
    bubble_close_btn.attr("id", "bt_"+ bubbnum);
    bubble_close_btn.attr("onlickc", "this.cancelBubble=true; return true;");

    //obal tlacitka
    var bubble_close = $("<div/>");
    bubble_close.attr("class", "bubble_close");
    bubble_close.append(bubble_close_btn);


    var r = $("<div/>");
    r.attr("class", "r");
    r.append(bubble_content);
    r.append(bubble_close);
    
    var l = $("<div/>");
    l.attr("class", "l");
    l.append(r);
    /* top part - end */

    /* bott part - start*/
    

    var b_l = $("<div/>");
    b_l.attr("class", "b_l");

    var b = $("<div/>");
    b.attr("class", "b");

    var b_r = $("<div/>");
    b_r.attr("class", "b_r");

    var bubble = $("<div/>");
    bubble.attr("class", "bubble");
    bubble.append(t_l);
    bubble.append(t);
    bubble.append(t_r);
    bubble.append(l);
    bubble.append(b_l);
    bubble.append(b);
    bubble.append(b_r);
    /* bott part - end*/

    //vlozeni obsahu do obalu
    bubble_container.append(bubble);

    var height = bubble_content.height();
    if (height > 400)
    {
        bubble_content.css("height", "400px");
        bubble_content.css("overflow", "auto");
    }

    $(".bubble_close_button").click( function(e)
    {
        bubble_container.remove();
    });


/*$(".send_event").click( function(e)
    { 
       $("#form_"+e.id).trigger("submit")
       
    });*/


}
/*
$('body').bind('addBubble', function (x,y,content_name)
{
        var tomove = $(".data_"+content_name).get(0).innerHTML;
        createBubble(content_name, tomove, x, y);
});
*/
function addBubble(x,y,content_name, corner)
{
    var tomove = $(".data_"+content_name).get(0).innerHTML;
    createBubble(content_name, tomove, x, y, corner);
}

