﻿var xmlHttp;

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

// Toggling
function toggleBig(sender, args)
{
    sender.Height=15;
    sender.Width=15;
}
function toggleNorm(sender, args)
{
    sender.Height=10;
    sender.Width=10;
}

function mouseIsOverDropTarget(targ, mouseEventArgs)
{
    var targX = mouseEventArgs.GetPosition(targ).x;
    var targY = mouseEventArgs.GetPosition(targ).y;
        
    if (targX >= 0 && targX <= targ.GetValue("Width") && targY >= 0 && targY <= targ.GetValue("Height"))
        return true;
    else
        return false;
}

function findDropLabel(itemlist, mouseEventArgs)
{
    var silverlight = document.getElementById("slIllustration");
    for(var k=0;k<itemlist.length;k++)
    {
        var ellip = silverlight.content.findName('labelA_' + itemlist[k]);
        if (mouseIsOverDropTarget(ellip, mouseEventArgs))
        {
            return ellip;
        }
    }       
    return null;
}

function updateAllDropLabels(itemlist)
{
    var bFoundOne = false;
    var silverlight = document.getElementById("slIllustration");
    if (silverlight == null)
        return;
    for(var k=0;k<itemlist.length;k++)
    {
        try
        {
            var ellip = silverlight.content.findName('labelA_' + itemlist[k]);
            if (ellip.Tag == null || ellip.Tag == '')
            {
                ellip.Opacity = 1;
                
            }
            else
            {
                silverlight.content.findName('labelQ_' + ellip.Tag).Opacity = 1;  
                ellip.Opacity = 0;
            }
        }
        catch (e)
        {
            //silverlight is probably not ready yet
            return;
        }
        
    }  
    
    for (var k=0;k<itemlist.length;k++)
    {
        var myLab = silverlight.content.findName('labelQ_' + itemlist[k]);
        if (askAll)
        {
            myLab.Opacity = 1;
            continue;
        }
        var bShowLab = false;
        for (var x=0;x<itemlist.length;x++)
        {                
            if (myLab.Tag == silverlight.content.findName('labelA_' + itemlist[x]).Tag)
            {
                bShowLab = true;
            }
        }
        
        if (!bShowLab && !bFoundOne)
        {
            bShowLab = true;
            bFoundOne = true;
            
        }
        
        myLab.Opacity = bShowLab ? 1 : 0;
        
        
    }
    
    return null;
}

function clearDropLabel(itemlist, tagToRemove)
{
    var silverlight = document.getElementById("slIllustration");
    for(var k=0;k<itemlist.length;k++)
    {
        var ellip = silverlight.content.findName('labelA_' + itemlist[k]);
        if (ellip.Tag == tagToRemove)
        {
            silverlight.content.findName('tbQ_' + ellip.Tag).Foreground='Green';
            ellip.Tag = null;
            
        }
    }       
    return null;
}

function placeLabel(label, dropTarget)
{
    if (!bRunTest)
        return;

    var silverlight = document.getElementById("slIllustration");
    var Matrix = dropTarget.RenderTransform.Matrix;
    label["Canvas.Left"] = Matrix.OffsetX + dropTarget["Canvas.Left"];
    label["Canvas.Top"] = Matrix.OffsetY + dropTarget["Canvas.Top"];
    dropTarget.Tag = label.Tag;
    silverlight.content.findName('tbQ_' + dropTarget.Tag).Foreground='Blue';
}

function toggleBigNorm(itemlist, mouseEventArgs)
{
    var silverlight = document.getElementById("slIllustration");
    for(var k=0;k<itemlist.length;k++)
    {
        var ellip = silverlight.content.findName('labelA_' + itemlist[k]);
        
        if (mouseIsOverDropTarget(ellip, mouseEventArgs))
            toggleBig(ellip, null);
        else
            toggleNorm(ellip, null);
    }
}

function toggleOpacityElm(element)
{
    var elm = document.getElementById(element);
    
    switch (elm.style.display)
    {
        case "none":
            elm.style.display = "";
            break;
        case "":
            elm.style.display = "none";
            break;
    }
}

function toggleOpacity(sender, args)
{
    switch (sender.Opacity)
    {
        case 1:
            sender.Opacity = 0;
            break;
        case 0:
            sender.Opacity = 1;
            break;
    }
}

function toggleAnswers(sender, args) {
    var aLabelName = new Array();
    aLabelName = sender.name.split('_');
    var iLabelID = aLabelName[1];

    var silverlight = document.getElementById("slIllustration");
    var correctA = silverlight.content.findName('label_' + iLabelID);
    var myA = silverlight.content.findName('ans_' + iLabelID);

    switch (myA.Opacity) {
        case 1:
            myA.Opacity = 0;
            correctA.Opacity = 1;
            break;
        case 0:
            myA.Opacity = 1;
            correctA.Opacity = 0;
            break;
    }
}
function toggleAnswersOn(itemlist) {
    var silverlight = document.getElementById("slIllustration");
    if (itemlist.length == 0) {
        itemlist = labels;
    }
    for (var k = 0; k < itemlist.length; k++) {
        silverlight.content.findName("label_" + itemlist[k]).Opacity = 0;
        silverlight.content.findName("ans_" + itemlist[k]).Opacity = 1;
    }
}
function toggleAnswersOff(itemlist) {
    var silverlight = document.getElementById("slIllustration");
    if (itemlist.length == 0) {
        itemlist = labels;
    }
    for (var k = 0; k < itemlist.length; k++) {
        silverlight.content.findName("label_" + itemlist[k]).Opacity = 1;
        silverlight.content.findName("ans_" + itemlist[k]).Opacity = 0;
    }
}

function toggleOff(itemlist,type)
{
    var silverlight = document.getElementById("slIllustration");
    if (itemlist.length == 0) {
        itemlist = labels;
    }
        for(var k=0;k<itemlist.length;k++)
        {
            silverlight.content.findName(type+itemlist[k]).Opacity=0;
        }
}
function toggleOn(itemlist,type)
{
        var silverlight = document.getElementById("slIllustration");
        if (itemlist.length == 0) {
            itemlist = labels;
        }
        for (var k = 0; k < itemlist.length; k++)
        {
            silverlight.content.findName(type+itemlist[k]).Opacity=1;
        }
}

function resetUnplacedLabels(allLabels)
{
    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    var main = silverlight.content.findName("mainCanvas");
    var sgwidth = main["width"];
    var leftOffset = (sgwidth - (300 / currZoom)) +50;
    var topOffset = 10;
    var bFoundAny = 0;
    
    for(var x=0; x<allLabels.length;x++)
    {
        var bFound = 0;
        var myLabel = silverlight.content.findName('labelQ_' + allLabels[x]);
    
        for(var k=0;k<allLabels.length;k++)
        {
            var ellip = silverlight.content.findName('labelA_' + allLabels[k]);
            if (ellip.Tag == myLabel.Tag)
            {
                bFound = 1;
                break;
            }
        }
        
        if(bFound == 0)
        {
            bFoundAny = 1;
              myLabel["Canvas.Left"] = leftOffset;
              myLabel["Canvas.Top"] = topOffset;
              topOffset += 10;
        }
    }
    
    if (bFoundAny == 0)
    {
        doSubmitTest(allLabels);
    }
    
}

function resetImage(leaders, labels)
{
    //var leader_rb = document.getElementById("leader_on");
    var label_rb = document.getElementById("label_on");
    //leader_rb.checked = true;
    label_rb.checked = true;
    //toggleOn(leaders,"leader_");
    toggleOff(labels,"label_");

    
    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    var main = silverlight.content.findName("mainCanvas");
    var leftOffset = (silverlightWidth - (zoomLevel * main["Width"])) / 2;
    var topOffset = (650 - (zoomLevel * main["Height"])) / 2;
    main["Canvas.Left"] = leftOffset;
    main["Canvas.Top"] = topOffset;
    scale.ScaleX = zoomLevel;
    scale.ScaleY = zoomLevel;

    sliderZoom.setValue(zoomLevel);
}
function resetResultsImage(leaders, labels) {
    var label_rb = document.getElementById("label_on");
    label_rb.checked = true;
    toggleAnswersOn(labels);


    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    var main = silverlight.content.findName("mainCanvas");
    var leftOffset = (silverlightWidth - (zoomLevel * main["Width"])) / 2;
    var topOffset = (650 - (zoomLevel * main["Height"])) / 2;
    main["Canvas.Left"] = leftOffset;
    main["Canvas.Top"] = topOffset;
    scale.ScaleX = zoomLevel;
    scale.ScaleY = zoomLevel;

    sliderZoom.setValue(zoomLevel);
}


// Zooming
function zoomIn()
{
    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    scale.ScaleX += .1;
    scale.ScaleY += .1;
}
function zoomOut()
{
    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    scale.ScaleX -= .1;
    scale.ScaleY -= .1;
}
function zoomSlide(target)
{
    var silverlight = document.getElementById("slIllustration");
    var scale = silverlight.content.findName("zoomLevel");
    scale.ScaleX = target;
    scale.ScaleY = target;
    var main = silverlight.content.findName("mainCanvas");
    var leftOffset = (silverlightWidth - (target * main["Width"])) / 2;
    var topOffset = (650 - (target * main["Height"])) / 2;
    main["Canvas.Left"] = leftOffset;
    main["Canvas.Top"] = topOffset;
    currZoom = target;
}

// Panning
function panHorizontal(target)
{
    var silverlight = document.getElementById("slIllustration");
    var main = silverlight.content.findName("zoomLevel");
    main.CenterX=target;
}
function panVertical(target)
{
    var silverlight = document.getElementById("slIllustration");
    var main = silverlight.content.findName("zoomLevel");
    main.CenterY=target;
}

var beginX;
var beginY;
var isMouseDown = false;
var fRedraw = 0;
var nElements = 3;

function onMouseDown(sender, mouseEventArgs)
{
   // Ensure this object is the only one receiving mouse events.   
   sender.captureMouse();
   fRedraw = 0;    
   // Set the beginning position of the mouse.    
   beginX = mouseEventArgs.getPosition(null).x;    
   beginY = mouseEventArgs.getPosition(null).y;    
   isMouseDown = true;
}
// Stop drag and drop operation.
function onMouseUp(sender, mouseEventArgs)
{
    isMouseDown = false;  
    // Allow all objects to receive mouse events.    
    sender.releaseMouseCapture();
}
// Reposition object during drag and drop operation.
function onMouseMove(sender, mouseEventArgs)
{
    // Determine whether the mouse button is down.    
    // If so, move the object.    
    if (isMouseDown == true && isQMouseDown == false)    
    {        
        // Retrieve the current position of the mouse.        
        var currX = mouseEventArgs.getPosition(null).x;        
        var currY = mouseEventArgs.getPosition(null).y;        
        // Reset the location of the object.        
        sender["Canvas.Left"] += currX - beginX;        
        sender["Canvas.Top"] += currY - beginY;        
        // Update the beginning position of the mouse.        
        beginX = currX;        
        beginY = currY;        
    }
    
   
}


// Code for Question/Answer Drag/Drop
var qbeginX;
var qbeginY;
var isQMouseDown = false;
function onQMouseDown(sender, mouseEventArgs)
{
   // Ensure this object is the only one receiving mouse events.   
   sender.captureMouse();
   fRedraw = 0;    
   // Set the beginning position of the mouse.    
   qbeginX = mouseEventArgs.getPosition(null).x ;    
   qbeginY = mouseEventArgs.getPosition(null).y ;    
   isQMouseDown = true;
   isMouseDown = false;
   clearDropLabel(labels, sender.Tag);
   //updateAllDropLabels(labels);
}
// Stop drag and drop operation.
function onQMouseUp(sender, mouseEventArgs)
{
    isQMouseDown = false;  
    // Allow all objects to receive mouse events.    
    sender.releaseMouseCapture();
    // Code here to place the answer!!!
    var targ = findDropLabel(labels,mouseEventArgs);
    if (targ != null)
    {
        placeLabel(sender, targ);
    }
    resetUnplacedLabels(labels);
    updateAllDropLabels(labels);
}
function onQuestionMove(sender, mouseEventArgs)
{
    // Determine whether the mouse button is down.    
    // If so, move the object.    
        if (isQMouseDown == true)    
    {        
        // Retrieve the current position of the mouse.        
        var currX = mouseEventArgs.getPosition(null).x ;        
        var currY = mouseEventArgs.getPosition(null).y ;        
        // Reset the location of the object.        
        sender["Canvas.Left"] += (currX - qbeginX) / currZoom; 
        sender["Canvas.Top"] += (currY - qbeginY) / currZoom;        
        // Update the beginning position of the mouse.        
        qbeginX = currX;        
        qbeginY = currY;     
               
    }
    toggleBigNorm(labels, mouseEventArgs);
}

