﻿// JScript File
var g_sldBackgraph = null;
var g_sldForegraph = null;
var g_sldEdges = null;
var g_sldLabels = null;

var g_MouseX = 0;
var g_MouseY = 0;
        
function InitControls()
{
    var sliderEl = document.getElementById ? document.getElementById("divSldBackgraph") : null;
    var inputEl = document.getElementById("sldBackgraph");
    if (inputEl)
    {
        g_sldBackgraph = new Slider(sliderEl, inputEl);
        g_sldBackgraph.onchange = FadeBackgraph;
        
        g_sldBackgraph.setMinimum(0);
        g_sldBackgraph.setMaximum(100);
        g_sldBackgraph.setValue(50);
    }
    
    sliderEl = document.getElementById ? document.getElementById("divSldForegraph") : null;
    inputEl = document.getElementById("sldForegraph");
    if (inputEl)
    {
        g_sldForegraph = new Slider(sliderEl, inputEl);
        g_sldForegraph.onchange = ScaleForegraph;
        
        g_sldForegraph.setMinimum(0);
        g_sldForegraph.setMaximum(8);
        g_sldForegraph.setValue(1);
    }
    
    sliderEl = document.getElementById ? document.getElementById("divSldEdges") : null;
    inputEl = document.getElementById("sldEdges");
    if (inputEl)
    {
        g_sldEdges = new Slider(sliderEl, inputEl);
        g_sldEdges.onchange = ScaleBackgraph;
        
        g_sldEdges.setMinimum(0);
        g_sldEdges.setMaximum(8);
        g_sldEdges.setValue(1);
    }
    
    sliderEl = document.getElementById ? document.getElementById("divSldLabels") : null;
    inputEl = document.getElementById("sldLabels");
    if (inputEl)
    {
        g_sldLabels = new Slider(sliderEl, inputEl);
        g_sldLabels.onchange = FadeLabels;
        
        g_sldLabels.setMinimum(6);
        g_sldLabels.setMaximum(48);
        g_sldLabels.setValue(12);
    }
    
    document.getElementById("divScene").style.zIndex = 100010;
    
    Bridgeworks.UpdateScene("<AutoInterpolate target='Title' duration='2' opacity='1'><position x='220' y='200'/></AutoInterpolate>");
}
function FadeBackgraph()
{
   if (g_sldBackgraph)
    {
        
        var val = g_sldBackgraph.getValue();
        if (val == 100)
        {
            val = "1";
        }
        else if (val > 0 & val < 100)
        {
            if (val < 10)
            {
                val = "0.0" + val;    
            }
            else
            {
                val = "0." + val;
            }
        }
        var bgEdgeColor = "<Set target='EdgeRenderer'><backGraphEdgeStyle><color r='.9' g='.9' b='.9' a='" + val + "'/></backGraphEdgeStyle></Set>";
        if (Bridgeworks) 
        {
            window.status = bgEdgeColor;
            Bridgeworks.UpdateScene(bgEdgeColor);
        }
    }
}

function FadeLabels()
{
   if (g_sldLabels)
    {
        
        var val = g_sldLabels.getValue();
        
        var bgLabelOpacity = "<Set target='Labels' fontSize='" + val  + "'></Set>";
        if (Bridgeworks) 
        {
            window.status = bgLabelOpacity;
            Bridgeworks.UpdateScene(bgLabelOpacity);
        }
    }
}

function ScaleBackgraph()
{
   if (g_sldEdges)
    {
        var val = g_sldEdges.getValue();
        //var numEdgeLevels = Bridgeworks.Get("EdgeRenderer/edgeStyles/size");
        //numEdgeLevels = numEdgeLevels.substring(numEdgeLevels.indexOf(">")+1, numEdgeLevels.lastIndexOf("<"));
        var update = "<Update>";
        var bgEdgeWidth = "<Set target='EdgeRenderer/backGraphEdgeStyle'><width>" + val + "</width></Set>";
        update += bgEdgeWidth;
        //var defaultEdgeWidth = "<Set target='EdgeRenderer/defaultGraphEdgeStyle'><width>" + val + "</width></Set>";
        //update += defaultEdgeWidth;
        /*
        var strEdgeWidth;
        for (i=0; i < parseInt(numEdgeLevels); i++)
        {
            stEdgeWidth = "<Set target='EdgeRenderer/edgeStyles/level" + i + "'><width>" + val + "</width></Set>";
            update += stEdgeWidth;
        }
        */
        update += "</Update>";
        
        if (Bridgeworks) 
        {
            window.status = update;
            Bridgeworks.UpdateScene(update);
        }
    }
}

function ScaleForegraph()
{
   if (g_sldForegraph)
    {
        var val = g_sldForegraph.getValue();
        var numEdgeLevels = Bridgeworks.Get("EdgeRenderer/edgeStyles/size");
        numEdgeLevels = numEdgeLevels.substring(numEdgeLevels.indexOf(">")+1, numEdgeLevels.lastIndexOf("<"));
        var update = "<Update>";
        
        var strEdgeWidth;
        for (i=0; i < parseInt(numEdgeLevels); i++)
        {
            stEdgeWidth = "<Set target='EdgeRenderer/edgeStyles/level" + i + "'><width>" + val + "</width></Set>";
            update += stEdgeWidth;
        }
        update += "</Update>";
        
        if (Bridgeworks) 
        {
            window.status = update;
            Bridgeworks.UpdateScene(update);
        }
    }
}

function openUserPage(userId)
{
    window.open(g_Url_Delicious + userId);
}

function openUserPageAtTag(userId, tag)
{
    window.open(g_Url_Delicious + userId + "/" + tag);
}

function getUserFeed(userId)
{
    var url = g_Url_RSS + userId;
    window.status = url;
    window.open(url);
}


function showTags(userId)
{
    var url = g_Url_TagCloud + "?userid=" + userId + "&min=10";
    var frame = document.getElementById("FrameTagCloud");
    if (frame)
    {
        frame.setAttribute("src", url);
        frame.setAttribute("visible", true);
        frame.src = url;
        frame.visible = true;
        frame.style.zIndex = 100010;
        document.getElementById("divScene").style.zIndex = 100000;		        
    }
}

// This function is here b/c it also manipulates the layers on the page
function Locate(userId, closeness)
{

    var frame = document.getElementById("FrameTagCloud");
    if (frame)
    {
        frame.setAttribute("visible", false);
        frame.visible = false;
        frame.style.zIndex = 100000;
        document.getElementById("divScene").style.zIndex = 100010;		        
    }
    
    var c = "100";
    if (closeness)
    {
        c = closeness;
    }
    
    Bridgeworks.UpdateScene("<Locate closeness='" + c + "' target='" + userId + "'/>");
}




/**
 * Shows the Help window 
 */
function helpPop(url)
{
    var helpWindow = open(url,"",'scrollbars=no,resizable=yes,width=250,height=300,screenX=50,screenY=50,top=170,left=300');
    if (helpWindow.opener == null)
        helpWindow.opener = self;
}


/**
 * Highlights the given aTag (html element)
 */
function Highlight(aTag)
{
	if (aTag && aTag.style)
	{
		  // save the current colors is user attributes
		  aTag.style.baseColor = aTag.style.color;
		  aTag.style.baseBgColor = aTag.style.backgroundColor;

		  // TODO:  remove style from code (change to classes?)
		  aTag.style.color = "blue";
		  aTag.style.backgroundColor = "#D1D2FF";
	}
}

/**
 * Unhighlights the given aTag (html element)
 */
function Unhighlight(aTag)
{
	if (aTag && aTag.style)
	{
		// restore the saved colors from user attributes
		aTag.className = aTag.base_class;
		aTag.style.color = aTag.style.baseColor;
		aTag.style.backgroundColor = aTag.style.baseBgColor;
	}
}

function ToggleArtifact(Artifact, checkbox)
{
    if (checkbox)
    {
        var update = "";
        switch (checkbox.checked)
        {
            case true:
                update = "<Set target='" + Artifact + "' enabled='true'/>";
            break;
            case false:
                update = "<Set target='" + Artifact + "' enabled='false'/>";
            break;
        }
        if (Bridgeworks)
        {
            Bridgeworks.UpdateScene(update);
        }
    }
}

function ToggleEdgeArrows(checkbox)
{
    var update = "<Update>";
    var endStyle;
    switch (checkbox.checked)
    {
        case true:
            endStyle = 1;
        break;
        case false:
            endStyle = 0;
        break;
    }
    var numEdgeLevels = Bridgeworks.Get("EdgeRenderer/edgeStyles/size");
    numEdgeLevels = numEdgeLevels.substring(numEdgeLevels.indexOf(">")+1, numEdgeLevels.lastIndexOf("<"));
    
    var bgEdgeEndStyle = "<Set target='EdgeRenderer'><backGraphEdgeStyle><p1_endStyle>" + endStyle + "</p1_endStyle></backGraphEdgeStyle></Set>";
    var defaultEdgeEndStyle = "<Set target='EdgeRenderer'><defaultGraphEdgeStyle><p1_endStyle>" + endStyle + "</p1_endStyle></defaultGraphEdgeStyle></Set>";
    update += bgEdgeEndStyle;
    update += defaultEdgeEndStyle;
    var strEdgeEndStyle;
    for (i=0; i < parseInt(numEdgeLevels); i++)
    {
        stEdgeEndStyle = "<Set target='EdgeRenderer/edgeStyles/level" + i + "'><p1_endStyle>" + endStyle + "</p1_endStyle></Set>";
        update += stEdgeEndStyle;
    }
    update += "</Update>";

    if (Bridgeworks)
    {
        //window.alert(update);
        window.staus = update;
        Bridgeworks.UpdateScene(update);
    }
}

function ShowMenu(name)
{
    var menu = document.getElementById("RightClickMenuWindow");
    if (menu)
    {
        //alert (name);
        menu.style.display = "block";
        menu.style.left = g_MouseX;
        menu.style.top = g_MouseY;
        //document.getElementById("divScene").style.zIndex = -1;;
        menu.style.zIndex = 100050;
        menu.focus();
    }
    
}

function HideMenu()
{
   var menu = document.getElementById("RightClickMenuWindow");
    if (menu)
    {
        menu.style.display = "none";
    } 
}

// TODO: localize to specific divs?
document.onmousemove = getMouseXY;
document.onkeyup = keyActionReceived;
function getMouseXY()
{
    g_MouseX = event.clientX + document.body.scrollLeft
    g_MouseY = event.clientY + document.body.scrollTop
}
function keyActionReceived()
{
    switch (event.keyCode)
    {
        // ESC
        case 27:
        {
            HideMenu();
        }
        break;
    }
}