﻿var minusImgName = "App_Themes/Default/Images/minusLex.gif";
var plusImgName = "App_Themes/Default/Images/plusLex.gif";
var bulletImgName = "App_Themes/Default/Images/bulletLex2.png";

var subId = 0;

$(document).ready(function()
{
    var $regControl = $("#TflControl");
    if ($("#tree-container", $regControl).length > 0 && $("#list-container", $regControl).length > 0)
    {
        $("#tree-container ul li a").click(function() { MarkBranchSelected(this); });

        var selectedId = $.query.get("expid");
        if (selectedId === "") { $.query.get("expId"); } else { SyncTree(selectedId); }

        subId = $.query.get("sub");
        if (subId === "") { subId = 0; }
    }
});

function SyncTree(selectedNodeId)
{
    var elId = "#br_" + selectedNodeId;
    var $el = $(elId);

    if ($el[0].tagName === "A")
    {
        MarkBranchSelected($el[0]);
        eval($el.attr("href"));
    }
    else if ($el[0].tagName === "UL")
    {
        //alert("list");

        var $li = $($el[0]).parent();
        var $a = $li.children("a");

        MarkBranchSelected($a);
    }

    var path = GetPathElements($el);
    ExpandTreeToElement(path);
}

function ToggleBranch(nodeId)
{
    var selector = "ul#br_" + nodeId;

    var $ul = $(selector);
    var $img = $ul.parent().children("img");
    if ($img.attr("src") === plusImgName)
    {
        $img.attr("src", minusImgName);
        $img.attr("title", "Skrij področje");
    }
    else
    {
        $img.attr("src", plusImgName);
        $img.attr("title", "Prikaži področje");
    }

    $ul.slideToggle("fast");
}

function MarkBranchSelected(linkElement)
{
    //reset tree, list
    $("#tree-container ul li a.selected").removeClass("selected");
    ResetList();

    var $link = $(linkElement);
    $link.addClass("selected");

    //show path
    var path = GetPathElements($link);
    ShowPath(path);
}

function GetPathElements(startElement)
{
    var $el = $(startElement);
    var path = new Array();

    while ($el.attr("class") != "top-level")
    {
        var $a = null;
        var $ul = null;

        if ($el.tagName === "A")
        {
            $a = $el;
            $ul = $el.parent().parent();
        }
        else if ($el[0].tagName === "LI")
        {
            var $ela = $el.children("a");
            if ($ela.length > 0)
            {
                $a = $ela;
                $ul = $ela.parent().parent();
            }
        }

        if ($a != null && $ul != null)
        {
            var nodeId = $ul.attr("id");
            var name = $a.attr("title");

            var pi = new PathItem(nodeId, name);
            path.push(pi);
        }

        $el = $el.parent();
    }

    //path.push(new PathItem("reg", "Register"));

    return path;
}

function ShowPath(pathArray)
{
    var path = "<div class='nfo'>Pot iskanja:</div><p> ";

    for (i = (pathArray.length - 1); i >= 0; i--)
    {
        var pi = pathArray[i];
        path += pi.Name;
        if (i > 0) { path += " <b>/</b> "; }
    }

    path += "</p>";

    var $pathContainer = $("#result-info div.path");
    $pathContainer.fadeOut("fast", function()
    {
        $pathContainer.html(path);
        $pathContainer.fadeIn("fast");
    });
}

function ExpandTreeToElement(pathArray)
{
    var $classLink = $("#" + pathArray[0].NodeId);

    //ShowDebug(pathArray);
    
    for (i = (pathArray.length - 2); i >= 0; i--)
    {
        var pi = pathArray[i];
        var brId = pi.NodeId.toString().substring(3);
        
        if (brId.length > 0)
        {
            ToggleBranch(brId);
        }
    }
}

function ToggleTree(action)
{
    var imgs = $("#tree-container img");
    for (i = 0; i < $(imgs).length; i++)
    {
        if ($(imgs[i]).attr("src") === plusImgName && action === 1)
        {
            $(imgs[i]).attr("src", minusImgName);
            $(imgs[i]).attr("title", "Skrij področje");
        }
        else if ($(imgs[i]).attr("src") === minusImgName && action === 0)
        {
            $(imgs[i]).attr("src", plusImgName);
            $(imgs[i]).attr("title", "Prikaži področje");
        }
    }

    var selector = "#tree-container ul.top-level ul ul";
    action === 0 ? $(selector).hide() : $(selector).show();
}

function ResetList()
{
    $("#result-info div.count").html("&nbsp;").hide();
    $("#result-info div.list-control").hide();
    //$("#list-title").html("");
    $("#list-container").html("");
}

function LoadList(entityId, listTitle)
{
    ShowLoader();

    var ssid = $.query.get("ssid");
    if (ssid === "") { ssid = "lex"; }

    var datastring = "classRootEntityId: '" + entityId + "', ssid: '" + ssid + "', subId: " + subId;

    //alert("loading list " + listTitle + "\ndatastring: " + datastring);

    GetData(
        "GetDocumentList",
        datastring,
        function(data)
        {
            //document list
            var html = data.d;
            $("#list-container").html(html);

            //result count
            var allLinks = $("#list-container ul li a").length;
            var expandLinks = $("#list-container ul li div.child-count a").length;
            var documentLinks = allLinks - expandLinks;

            $("#result-info div.count").html(
                "<div class='nfo'>Prikazani rezultati:</div>Število dokumentov: <b>" + documentLinks + "</b>"
            );

            $("#result-info div.count").show();
            $("#result-info div.list-control").show();

            window.scrollTo(0, 200);
        },
        function(data)
        {
            $("#list-container").html(data.responseText);
        }
    );
}

function ToggleListItem(nodeId)
{
    var selector = "#li_" + nodeId;

    var $ul = $(selector);
    var imgs = $ul.parent().children("img");
    var $img = null;
    for (i = 0; i < imgs.length; i++)
    {
        if ($(imgs[i]).attr("src") != bulletImgName) { $img = $(imgs[i]); break; }
    }

    if ($img === null) { alert("list item not found!"); return; }
    
    var $expLinks = $("div.child-count > a", $ul.parent());
    var $a = $($expLinks[0]);

    var childHtml = $a.html();

    if ($img.attr("src") === plusImgName)
    {
        $img.attr("src", minusImgName);
        $img.attr("title", "Skrij podzakonske predpise");

        childHtml = childHtml.toString().replace("Prikaži", "Skrij");
    }
    else
    {
        $img.attr("src", plusImgName);
        $img.attr("title", "Prikaži podzakonske predpise");

        childHtml = childHtml.toString().replace("Skrij", "Prikaži");
    }
    $a.html(childHtml);

    
    $ul.slideToggle("fast");
}

function ToggleList(action)
{
    var imgs = $("#list-container img");
    for (i = 0; i < $(imgs).length; i++)
    {
        var $img = $(imgs[i]);
        var $childLink = $($("div.child-count > a", $(imgs[i]).parent())[0]);
        var childHtml = $childLink.html();

        if ($img.attr("src") === plusImgName && action === 1)
        {
            $img.attr("src", minusImgName);
            $img.attr("title", "Skrij podzakonske predpise");

            $childLink.html(childHtml.replace("Prikaži", "Skrij"));
        }
        else if ($(imgs[i]).attr("src") === minusImgName && action === 0)
        {
            $img.attr("src", plusImgName);
            $img.attr("title", "Prikaži podzakonske predpise");

            $childLink.html(childHtml.replace("Skrij", "Prikaži"));
        }
    }

    var selector = "#list-container ul.top-level ul ul";
    action === 0 ? $(selector).hide() : $(selector).show();
}

function ShowLoader()
{
    var html = "<div class='ajax-loader'>";
    html += "<img src='Images/ajax-loader.gif' />";
    html += "Nalaganje seznama dokumentov...";
    html += "</div>"

    var $listContainer = $("#list-container");
//    $listContainer.fadeOut("fast", function()
//    {
        $listContainer.html(html);
        //$listContainer.show();
//    });
    
}

function ShowDebug(pathArray)
{
    var html = "<ul>";
    for (i = 0; i < pathArray.length; i++)
    {
        html += "<li>" + i + ". " + pathArray[i].NodeId + " :: " + pathArray[i].Name + "</li>";
    }
    html += "</ul>";

    $("#debug-output").html(html);
}

function GetElementCounts()
{
    var treeElementsCount = $("#tree-container *").length;
    var listElementsCount = $("#list-container *").length;

    var message = "Tree: " + treeElementsCount + "; List: " + listElementsCount;
    alert(message);
}

/* ajax-y sh*t... */
function GetData(method, datastring, successCallback, errorCallback)
{
    //alert("ajax call " + datastring);

    $.ajax({
        type: "POST",
        url: "WebSvc/Register.asmx/" + method,
        data: "{ " + datastring + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(data, textstatus)
        {
            if (successCallback) { successCallback(data); }
        },
        error: function(request, textstatus, exception)
        {
            if (errorCallback)
            {
                errorCallback(request, textstatus, exception);
            }
            else
            {
                alert(request.responseText);
            }
        }
    });
}

/* objects */
function PathItem(nodeId, name)
{
    this.NodeId = nodeId;
    this.Name = name;
};
