/*
    JavaScript used to make DirectoryBrowser.aspx work.
    Mikael Jönsson Avantime AB - 20080916
*/

//Fetches directories in supplied path.
function fetchDirectoryChildren(webpath)
{
    Avantime.Pages.Edit.DirectoryBrowser.GetChildren(webpath, renderDirectoryChildren);
}

//Fetches all root directories
function fetchRootDirectories()
{
    Avantime.Pages.Edit.DirectoryBrowser.GetRootDirectories(renderDirectoryChildren);
}

//Renders directorylist
function renderDirectoryChildren(response)
{
    if( response && response.error )
        handleAjaxError(response);
    else if( response && response.value )
    {
        var list = response.value;
        for( var i=0; i < list.length; i++ )
        {
            if( list[i] )
                renderDirectory(list[i]);
        }
    }
}

//Renders directory
function renderDirectory(dir)
{
    var parentDiv = document.getElementById(dir.ParentIDPath);
    var div = document.createElement("div");
    div.style.clear = "left";
    parentDiv.appendChild(div);
    
    //setup icon.    
    var icon_span = document.createElement("span");
    icon_span.id = "icon_" + dir.IDPath;
    icon_span.className = "icon";
    icon_span.className += dir.HasChildren ? " icon_plus" : "";
    icon_span.onclick = function() { ExpandCollapse(dir.VirtualPath, dir.IDPath); };
    div.appendChild(icon_span);

    //setup item.
    var item_span = document.createElement("span");
    item_span.id = "item_" + dir.IDPath;
    item_span.innerText = dir.Name;
    item_span.className = "item_selectable";
    item_span.onclick = function() { SelectDirectory(dir); };
    div.appendChild(item_span);
    
    //setup child container.
    var childDiv = document.createElement("div");
    childDiv.id = dir.IDPath;
    childDiv.style.paddingLeft = "10px";
    childDiv.style.display = "none";
    div.appendChild(childDiv);
}

function SetClass(icon_item, className)
{
    if( icon_item )
        icon_item.className = className;
}

function SelectDirectory( dir )
{
    var hiddenField = document.getElementById("webpath_hidden");
    var selectedIdField = document.getElementById("selectedId_hidden");
    if( hiddenField && selectedIdField)
    {
        //normalize last select's text.
        if( selectedIdField.value.length > 0 )
        {
            document.getElementById("item_" + selectedIdField.value).style.fontWeight = "normal";
        }
        
        //set new path.
        hiddenField.value = dir.VirtualPath;
        selectedIdField.value = dir.IDPath;
    }
    
    //make selected text bold.
    document.getElementById("item_" + dir.IDPath).style.fontWeight = "bold";
}

function ExpandCollapse(webpath, childDivId)
{
    var div = document.getElementById(childDivId);
    if( div )
    {
        if( div.innerText.length == 0 )
        {
            div.style.display = "block";
            SetClass(document.getElementById("icon_" + childDivId), "icon icon_minus");
            fetchDirectoryChildren(webpath);
        }
        else
        {
            div.innerHTML = "";
            div.style.display= "none";
            SetClass(document.getElementById("icon_" + childDivId), "icon icon_plus");
        }
    }
}

//Displays error message in an appropiet manar.
function handleAjaxError(response)
{
    alert(response.error);
}

//Opens fileExplorer, used by property.
function OpenFileExplorer(url, valueholderId)
{
    var returnValue = window.showModalDialog(url, null, 'dialogHeight:540px;dialogWidth:340px;')
    
    if( returnValue && returnValue.length > 0 )
        document.getElementById(valueholderId).value = returnValue;
}