$(document).ready( function () {
    
    this.pathname = window.location.pathname;
    
    if( $.cookie('cutted') ){
        setInsZone('Coller ici.');
    }
    
    if( $.cookie('copy') ){
        setInsZone('Ins&eacute;rer ici.');
    }
    
    
    $(".bsEdit div").css({cursor:'pointer'}).hover(
        function () {

            $(".tools").remove()
            var spl = $(this).attr('title').split("-")
            this.ht = $(this).html();
            
            
            
            $(this).html( this.ht + '<div class="tools"><span title="' + spl[1] + '" class="paraProp"></span><span title="' + spl[1] + '" class="paraRemove"></span><span title="' + spl[1] + '" class="paraCut"></span><span title="paragraph-' + spl[1] + '-' + spl[2]+ '-' + spl[3] + '" class="paraEdit"></span></div>');
        
            $(".paraEdit").click( function (){
                var url;
                
                $.cookie('cutted', "", { path: "/" } );
                var spl = $(this).attr('title').split("-")
                if( spl.length > 1 ){
                    url = '/fr/' + spl[0] + '/bsForm.html?id=' + spl[1] + '&language=' + spl[2] + '&from=' + spl[3] ;
                }
                else {
                    url = '/fr/' + spl[0] + '/bsList.html' ;
                }
                window.open(url,'_self')
            })
            
            
            $(".paraCut").click( function (){
                
                $(".bsEdit>div").fadeTo(100, 1);
                $(this).parent().parent().fadeTo(200, .4);
                var temp = $(this).parent().parent().attr('id').split("-");
                var id = temp[1];
                $.cookie('cutted', id + '-' + $(this).parent().parent().parent().attr('title') + '-' + window.location.pathname , { path: "/" } );
                setInsZone('Coller ici.');
                
            })
            
            $(".paraRemove").click( function (){
                var id = $(this).attr('title')
                var col =$(this).parent().parent().parent().attr('title');
                window.open(window.location.pathname + "?action=page.removeParagraph&form[id]=" + id + "&form[col]=" + $(this).parent().parent().parent().attr('title') + "&form[url]=" + window.location.pathname ,"_self")
            })
            
        },
        function () {
            $(this).html( this.ht );
        }
    )
    
    
    
    
    function setInsZone(txt){
        
        $(".paste").remove();
        $.each( $(".bsEdit").children("div"), function(){
            var temp = $(this).attr('id').split("-");
            id = temp[1];
            if( $.cookie('cutted') ){
                $(this).after('<a class="paste" href="?action=page.cutParagraph&form[para]=' + $.cookie('cutted') + '&form[after]=' + id + '&form[page]=' + window.location.pathname + '&form[col]=' + $(this).parent().attr('title') + '">' + txt + '</a>');
            }
            else{
                $(this).after('<a class="paste" href="?action=page.pasteParagraph&form[para]=' + $.cookie('copy') + '&form[after]=' + id + '&form[page]=' + window.location.pathname + '&form[col]=' + $(this).parent().attr('title') + '">' + txt + '</a>');
            }
        })
        
        $.each( $(".bsEdit"), function(){
            if( $.cookie('cutted') ){
                $(this).prepend('<a class="paste" href="?action=page.cutParagraph&form[para]=' + $.cookie('cutted') + '&form[after]=0&form[page]=' + window.location.pathname + '&form[col]=' + $(this).attr('title') + '">' + txt + '</a>');
            }
            else{
                $(this).prepend('<a class="paste" href="?action=page.pasteParagraph&form[para]=' + $.cookie('copy') + '&form[after]=0&form[page]=' + window.location.pathname + '&form[col]=' + $(this).attr('title') + '">' + txt + '</a>');
            }
        })
    }
})
