﻿
var tree1;
var isDeleteting = false;
function initTree()
{
    if(document.getElementById('tree1'))
    {
        tree1 = new tree_component();
        var cookies = { prefix: $("#tree1").attr("name"), expires: 7, path: '/', domain: "coaching.fibaeurope.com", secure: true };
        if($(".ownfolder").length > 0)
            tree1.init($("#tree1"), { dflt: "root", cookies: cookies, ui: { dots: false }, rules: { clickable: "all", deleteable: ["file", "folder"], createable: ["folder"] }, callback: { onchange: treeClick, ondblclk: treeDblClick, onrgtclk: treeRightClick, beforedelete: treeBeforeDelete} });
        else
            tree1.init($("#tree1"), { dflt: "root", ui: { dots: false }, rules: { clickable: "all" }, callback: { onchange: treeClick, ondblclk: treeDblClick} });
        
        // Show menu when #tree1 is clicked (only for user)
        if($(".ownfolder").length > 0)
        {
            $("[rel='file'][id^='play'] > a").contextMenu({menu: 'treemenu_file'},treeContextAction);
            $("[rel='folder'] > a").contextMenu( {menu: 'treemenu_folder'},treeContextAction);
            $("[rel='root'] > a").contextMenu( {menu: 'treemenu_root'},treeContextAction);
        }
    }
}

function treeRightClick(node,tree_obj,ev)
{
    ev.stopPropagation();
    ev.preventDefault();
}

function treeBeforeDelete(node,tree_obj)
{
    isDeleteting = true;
    return true;
}

function treeClick(node,tree_obj)
{
    if(node["rightClick"])
    {
        node["rightClick"] = undefined;
        return;
    }
    
    if(isDeleteting)
    {
        isDeleteting = false;
        return;
    }

    tree_obj.toggle_branch.call(tree_obj, node);
}

function treeDblClick(node, tree_obj)
{
    if (node["rightClick"]) {
        node["rightClick"] = undefined;
        return;
    }

    if (isDeleteting) {
        isDeleteting = false;
        return;
    }

    if (node.firstChild && node.firstChild.href)
        window.location.href = node.firstChild.href;
    else
        $("#treemenu");
}

function treeContextAction(action, el, pos)
{
   // NAVIGATION_COACH_PLAYS_OWN/@value
   var cidPlayOwn = "{15897918-BDCF-4E6B-A177-FC13FEE8BABC}";
   
   // prevent default rightClick action
   $(el).parent()[0]["rightClick"]=true;
   tree1.select_branch($(el));
   
   // edit/show File
   if(action == "edit" && $(el).parent().attr('rel') == "file" && $(el)[0].href)
   {
        window.location.href = $(el)[0].href;
   }
   // rename Folder
   else if(action == "edit" &&  $(el).parent().attr('rel') == "folder")
   {
        var newname = prompt("New name for the Category:", $(el).text());
        if(newname && newname != $(el).text())
        {
            $(el).text(newname);
            $.get(window.location.pathname,
                {cid:cidPlayOwn,
                 "do":"Update",
                 activecat:$(el).parent().attr("id").substr(3),
                 name:newname
                },
                function(returned_data) {
                    
                });
        }
   }
   // delete Folder
   else if (action == "delete" && $(el).parent().attr('rel') == "folder")
   {
        if(confirm('Really delete this category and all plays inside?'))
        {
            tree1.remove();
            $.get(window.location.pathname,
                {cid:cidPlayOwn,
                 "do":"delete",
                 activecat:$(el).parent().attr("id").substr(3)
                },
                function(returned_data) {
                    
                });
        }
   }
   // delete File
   else if (action == "delete" && $(el).parent().attr('rel') == "file")
   {
        if(confirm('Really delete this play?'))
        {
            tree1.remove();
            $.get(window.location.pathname,
                {cid:cidPlayOwn,
                 "do":"deletePlay",
                 playID:$(el).parent().attr("id").substr(4)
                },
                function(returned_data) {
                    
                });
        }
   }
   // new Folder
   else if (action == "newcat")
   {
        var newname = prompt("Name for the new Category:", "New Category");
        if(newname)
        {
            if($(el).parent().attr("id").substr(0,4) == "play")
            {
                //     a   li       ul       li       a
                el = $(el).parent().parent().parent().children("a");
                
                // select
                $(el).parent()[0]["rightClick"]=true;
                tree1.select_branch($(el));
            }
            
            var cat = $(el).parent().attr("id").substr(3);
            tree1.open_branch($(el));
            
            var node = tree1.create("folder",null,newname,"images/ico_cat_u.gif");
            if(node) $(node).children().contextMenu( {menu: 'treemenu_folder'},treeContextAction);
            
            $.get(window.location.pathname,
                {cid:cidPlayOwn,
                 "do":"Create",
                 activecat:cat,
                 name:newname,
                 of:"callback"
                },
                function(returned_data) {
                    $(node).attr("id","cat"+returned_data);
                });
        }
   }
   // move Folder
   else if(action == "moveup" || action == "movedown")
   {
        var url = window.location.pathname;
        url += "?cid="+cidPlayOwn;
        url += "&amp;do=move";
        url += "&amp;activecat=" + $(el).parent().attr("id").substr(3);
        url += "&amp;direction=" + ((action=="moveup")?(1):(-1));
        
        window.location.href = url;
   }
}