// WEIHONG 7/19/2007 -- JUST PLAYING WITH SILVERLIGHT, PROOF OF CONCEPT ONLY
// NOT SURE IF THIS WILL BE USED IN PGL

// GLOBAL VARIABLES

var mouseDownPosition = 0;
var mouseDownValue = -1;
var thumbCenter = 100;
var minAge = 0;
var maxValue = 200;
var maxColumn = 10;
minAge = minAge +1;
var maxAge = 79;

var maxSlide = 200;
var minSlide = 50;
var factor = [0,0,0,50,50,50,50];
var angle = [0,0,0,0];
var totalfactor = 500;



function onLoaded(sender, eventArgs)
{
        // Retrieve a reference to the control.
        var control = sender.getHost();
               
        var colors = [ 'DarkRed', 'Blue', 'Orange', 'Green' ];
        var display = ["Large Cap Stocks","Mid Cap Stocks","Bonds","Municipals"];

        var slices = [0.05+Math.random()*0.5];
        slices[1] = 0.05+Math.random()*(0.65 - slices[0]);
        slices[2] = 0.05+Math.random()*(0.9 - slices[0] - slices[1]);
        slices[3] = 1.0 - slices[0] - slices[1] - slices[2];
               
        var xy = [[], [], [], []];
        var trans = [[], [], [], []];
        
        var currentAngle = 0;
        for (var i = 0; i < slices.length; ++i) {
            xy[i][0] = (100*Math.sin(currentAngle * 2 * Math.PI)) + ',' + (-100*Math.cos(currentAngle * 2 * Math.PI));
            
            trans[i][0] = (20-slices[i]*10)*Math.sin((currentAngle + slices[i]/2) * 2*Math.PI);
            trans[i][1] = -(20-slices[i]*10)*Math.cos((currentAngle + slices[i]/2) * 2*Math.PI);

            currentAngle += slices[i];
            xy[i][1] = (100*Math.sin(currentAngle * 2 * Math.PI)) + ',' + (-100*Math.cos(currentAngle * 2 * Math.PI));
        }
        


                    
        for (var i = 0; i < 4; ++i) {
            var height = 50 + i * 20;
            angle[i]=slices[i]*2*Math.PI;
            var arc = control.content.createFromXaml(
                '<Path Name="' + i + '" Fill="' + colors[i] + '"'
                    + '    Canvas.Left="350" Canvas.Top="170"'
                    + '    Data="M ' + xy[i][0] + ' A 100,100 ' + (slices[i]*2*Math.PI) + ' ' + (slices[i] > 0.5?1:0) + ' 1 ' + xy[i][1] + ' L 0,0"'
                    + '    MouseEnter="enter" MouseLeave="leave" Stroke="Gray"'
                    + '>'
                    + ' <Path.RenderTransform>'
                    + '   <TransformGroup>'
                    + '     <ScaleTransform Name="a' + i + '" CenterX="0" CenterY="0" ScaleX="0.0" ScaleY="0.0"/>'
                    + '     <RotateTransform Name="r' + i + '" Angle="-90.0"/>'
                    + '     <TranslateTransform Name="t' + i + '" X="0" Y="0"/>'
                    + '   </TransformGroup>'
                    + ' </Path.RenderTransform>'
                    + ' <Path.Resources>'
                    + '     <Storyboard Name="rot' + i + '">'
                    + '         <DoubleAnimation Storyboard.TargetName="r' + i + '" Storyboard.TargetProperty="Angle" To="0" Duration="0:0:0.6" />'
                    + '     </Storyboard>'
                    + '     <Storyboard Name="loademup' + i + '" Completed="loadmeup2">'
                    + '         <DoubleAnimation BeginTime="0:0:0.' + (i*1) + '" Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleX" To="1.1" Duration="0:0:0.3" />'
                    + '         <DoubleAnimation BeginTime="0:0:0.' + (i*1) + '" Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleY" To="1.1" Duration="0:0:0.3" />'
                    + '     </Storyboard>'
                    + '     <Storyboard Name="loademup' + i + '2" Completed="loadmeup3">'
                    + '         <DoubleAnimation Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleX" To="0.9" Duration="0:0:0.2" />'
                    + '         <DoubleAnimation Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleY" To="0.9" Duration="0:0:0.2" />'
                    + '     </Storyboard>'
                    + '     <Storyboard Name="loademup' + i + '23">'
                    + '         <DoubleAnimation Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleX" To="1.0" Duration="0:0:0.1" />'
                    + '         <DoubleAnimation Storyboard.TargetName="a' + i + '" Storyboard.TargetProperty="ScaleY" To="1.0" Duration="0:0:0.1" />'
                    + '     </Storyboard>'
                    + '     <Storyboard Name="enter' + i + '">'
                    + '         <DoubleAnimation Storyboard.TargetName="t' + i + '" Storyboard.TargetProperty="X" To="' + trans[i][0] + '" Duration="0:0:0.1" />'
                    + '         <DoubleAnimation Storyboard.TargetName="t' + i + '" Storyboard.TargetProperty="Y" To="' + trans[i][1] + '" Duration="0:0:0.1" />'
                    //+ '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="(Canvas.Left)" To="480" Duration="0:0:0.07" />'
                    //+ '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="(Canvas.Top)" To="130" Duration="0:0:0.2" />'
                    + '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="FontSize" To="14" Duration="0:0:0.07" />'
                    + '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="Opacity" To="1"  Duration="0:0:0.07" />'
                    + '         <DoubleAnimation Storyboard.TargetName="text' + i + '" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.07" />'                    + '     </Storyboard>'
                    + '     <Storyboard Name="leave' + i + '">'
                    + '         <DoubleAnimation Storyboard.TargetName="t' + i + '" Storyboard.TargetProperty="X" To="0" Duration="0:0:0.1" />'
                    + '         <DoubleAnimation Storyboard.TargetName="t' + i + '" Storyboard.TargetProperty="Y" To="0" Duration="0:0:0.1" />'
                    + '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="FontSize" To="12"  Duration="0:0:0.07" />'                               //+ '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="(Canvas.Top)" To="'+height.toString()+'" Duration="0:0:0.2" />'
                    //+ '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="(Canvas.Left)" To="520" Duration="0:0:0.07" />'
                    + '         <DoubleAnimation Storyboard.TargetName="label' + i + '" Storyboard.TargetProperty="Opacity" To="0.6" Duration="0:0:0.07" />'
                    + '         <DoubleAnimation Storyboard.TargetName="text' + i + '" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.07" />'
                    + '     </Storyboard>'
                    + ' </Path.Resources>'
                    + '</Path>'
                );
            sender.children.add(arc);
            
            // ADD LEGEND COLOR

            var RecBlock = control.content.createFromXaml(
            '<Rectangle Name="Rec'+i+'" Fill="'+colors[i]+'" Canvas.Top="'+(height+5).toString()+'"'
                + '         Canvas.Left = "500" Height="10" Width="10" Stroke="Black" />'
                );
            sender.children.add(RecBlock);
            
            // ADD LEGEND TEXT

            var LabelBlock = control.content.createFromXaml(
                '<TextBlock Name="label' + i + '" FontSize="12" Text="' + display[i] + '" Foreground="#000000"  FontWeight="Bold" '
                + '         Canvas.Left="520" Canvas.Top="'+height.toString()+'"' 
                + '         Opacity="0.6"/>'
                );
            sender.children.add(LabelBlock);
            
            
            // ADD FLY LEGEND TEXT
            var textblock = control.content.createFromXaml(
                '<TextBlock Name="text' + i + '" FontSize="24" FontWeight="ExtraBold" Text="' + parseInt((100*angle[i]/6.28).toString()) + '%" Foreground="'+colors[i]+'"'
                + '         Canvas.Left="500" Canvas.Top="10"'
                + '         Opacity="0"/>'
            );
            sender.children.add(textblock);
        }

        for (var i = 0; i < 4; ++i) {
            sender.findName('loademup' + i).begin();
            sender.findName('rot' + i).begin();            
        }
    }

    function loadmeup2(sender, eventargs) {
        sender.findName(sender.Name + '2').begin();
    }

    function loadmeup3(sender, eventargs) {
        sender.findName(sender.Name + '3').begin();
    }

    function enter(sender, eventargs) {
        sender.findName('enter' + sender.Name).begin();
    }
    function leave(sender, eventargs) {
        sender.findName('leave' + sender.Name).begin();
    }
    
//###############################   SLIDER STUFF #############################################


function slider_Loaded(sender, args) {
    slider_SetValue(sender, 0);    
}


function slider_thumb_MouseLeftButtonDown(sender, args) {
    var slider = sender.findName(sender.name.replace("slider_thumb","slider"));
    sender.captureMouse();    
    mouseDownValue = slider_GetValue(sender);
    mouseDownPosition = args.getPosition(null).x;    
}

function slider_thumb_MouseLeftButtonUp(sender, args) {
    var slider = sender.findName(sender.name.replace("slider_thumb","slider"));
    sender.releaseMouseCapture();
    mouseDownValue = -1;
}

function slider_thumb_MouseMove(sender, args) {
    var slider = sender.findName(sender.name.replace("slider_thumb","slider"));
    if (mouseDownValue != -1) {
        //var newValue = mouseDownValue + (args.getPosition(null).x - mouseDownPosition)  ;  
        //slider_SetValue(sender, newValue);
        if (args.getPosition(null).x <= maxSlide && args.getPosition(null).x >= minSlide)
        {
            sender["Canvas.Left"] = args.getPosition(null).x - 50;        
            Text_SetValue(sender,args.getPosition(null).x);
            triangle_SetValue(sender);
            pie_SetValue(sender);
        }
    }   
}

// ###############  CHANGE PIE #####################
function pie_SetValue(sender)
{
        totalfactor = totalfactor / 1000.0;
        var slices = [0.05+totalfactor*0.5];
        slices[1] = 0.05+totalfactor*(0.65 - slices[0]);
        slices[2] = 0.05+totalfactor*(0.9 - slices[0] - slices[1]);
        slices[3] = 1.0 - slices[0] - slices[1] - slices[2];
               
        var xy = [[], [], [], []];
        var trans = [[], [], [], []];
        
        var currentAngle = 0;
        for (var i = 0; i < slices.length; ++i) {
            xy[i][0] = (100*Math.sin(currentAngle * 2 * Math.PI)) + ',' + (-100*Math.cos(currentAngle * 2 * Math.PI));
            
            trans[i][0] = (20-slices[i]*10)*Math.sin((currentAngle + slices[i]/2) * 2*Math.PI);
            trans[i][1] = -(20-slices[i]*10)*Math.cos((currentAngle + slices[i]/2) * 2*Math.PI);

            currentAngle += slices[i];
            xy[i][1] = (100*Math.sin(currentAngle * 2 * Math.PI)) + ',' + (-100*Math.cos(currentAngle * 2 * Math.PI));
        }
        for (var i = 0; i < 4; i++)
        {
            var pie=sender.findName(i.toString());
            pie["Data"]="M " + xy[i][0] + " A 100,100 " + (slices[i]*2*Math.PI) + " " + (slices[i] > 0.5?1:0) + " 1 " + xy[i][1] + " L 0,0";
            var percent = sender.findName("text"+i.toString());
            angle[i]=slices[i]*2*Math.PI;
            percent["Text"] = parseInt((100*angle[i]/6.28).toString()) + "%"
        }
}


function slider_GetValue(sender) {
    var thumb = sender.findName(sender.name.replace("slider","slider_thumb"));
    return sender["Canvas.Left"] ; 
}


// ################### SLIDING BAR TEXT ###############################
function Text_SetValue(sender, newValue) {

    var text = sender.findName(sender.name.replace("slider_thumb","txt"));   
    if (sender.name == "slider_thumb1")
    {
        text["Text"] = parseInt(((newValue - 50)/1.5).toString()).toString();
        factor[1] = 200 - newValue;
    }
    else if (sender.name == "slider_thumb4")
    {
        var str = ((newValue - 50)/1.5).toString();
        text["Text"] = parseInt(str).toString() + " %";
        factor[4]  = 200 -  newValue;
    }
    else if (sender.name == "slider_thumb6")
    {
        text["Text"] = "$"+((newValue - 50)*1000).toString() + " K";
        factor[6] = newValue;
    }
    else if (sender.name == "slider_thumb2")
    {
        if (newValue < 75)
            text["Text"] = "Very Low";
        else if (newValue < 100 && newValue >= 75)
            text["Text"] = "Low";
        else if (newValue < 125 && newValue >= 100)
            text["Text"] = "Medium";
        else if (newValue >= 125 && newValue < 150)
            text["Text"] = "High";
        else if (newValue >= 150)
            text["Text"] = "Very High";   
        factor[2] = newValue*2;     
    }
    else if (sender.name == "slider_thumb3")
    {
        if (newValue < 75)
            text["Text"] = "Very Low";
        else if (newValue < 100 && newValue >= 75)
            text["Text"] = "Low";
        else if (newValue < 125 && newValue >= 100)
            text["Text"] = "Medium";
        else if (newValue >= 125 && newValue < 150)
            text["Text"] = "High";
        else if (newValue >= 150)
            text["Text"] = "Very High";    
        factor[3] = 200 - newValue;     
    }
    else if (sender.name == "slider_thumb5")
    {
        if (newValue < 75)
            text["Text"] = "Very Poor";
        else if (newValue < 100 && newValue >= 75)
            text["Text"] = "Poor";
        else if (newValue < 125 && newValue >= 100)
            text["Text"] = "Medium";
        else if (newValue >= 125 && newValue < 150)
            text["Text"] = "Good";
        else if (newValue >= 150)
            text["Text"] = "Very Good"; 
        factor[5] = newValue;       
    }
    
}

// ################### TRIANGLE POSITION ###############################
// 
function triangle_SetValue(sender)
{
    var triangle = sender.findName("triangle");
    for (var i = 0; i < 7; i++)
    {
        totalfactor += factor[i];
    }
    triangle["Canvas.Left"] = 210 + parseInt((totalfactor / 5.1).toString());
    var txtResult = sender.findName("txtResult");
    txtResult["Text"] = "Score "+parseInt(totalfactor.toString());
    var txtResultSummary = sender.findName("txtResultSummary");
    if (totalfactor < 300)
    {
        txtResultSummary["Text"] = "Extremely Conservative";
    }
    else if (totalfactor >= 300 && totalfactor < 400)
    {
        txtResultSummary["Text"] = "Very Conservative";
    }
        else if (totalfactor >= 400 && totalfactor < 500)
    {
        txtResultSummary["Text"] = "Moderate Conservative";
    }
        else if (totalfactor >= 500 && totalfactor < 600)
    {
        txtResultSummary["Text"] = "Conservative";
    }
        else if (totalfactor >= 600 && totalfactor < 700)
    {
        txtResultSummary["Text"] = "Moderate Aggressive";
    }
        else if (totalfactor >= 700 && totalfactor < 800)
    {
        txtResultSummary["Text"] = "Aggressive";
    }
        else if (totalfactor >= 800 && totalfactor < 900)
    {
        txtResultSummary["Text"] = "Very Aggressive";
    }
        else if (totalfactor >= 900)
    {
        txtResultSummary["Text"] = "Extremely Aggressive";
    }
    
    
}
function barHeight_SetValue(sender,newValue)
{
//    if (maxValue == null || maxValue < 10000) maxValue = 500000;
//    
//    for (var i = 0; i < maxColumn; i++)
//    {
//        try
//        {
//            var bar = sender.findName("ValueBar_"+i);
//            bar["Canvas.Top"] = 250 - getData(i,newValue) * (250 /maxValue);
//            bar["Height"] = 250*(getData(i,newValue)/ maxValue);
//            //bar["Canvas.Top"] = newValue;
//            var dollar = sender.findName("Dollar_"+i);
//            dollar["Text"]=Currency(getData(i,newValue));
//        }
//        catch(err)
//        {
//        
//        }

//    }
}



function createMySilverlightControl()
{  
    Silverlight.createObject(
        "AssetAllocator.xaml",                  // Source property value.
        parentElement,                  // DOM reference to hosting DIV tag.
        "mySilverlightControl",         // Unique control ID value.
        {                               // Control properties.
            width:'700',                // Width of rectangular region of 
                                        // control in pixels.
            height:'300',               // Height of rectangular region of 
                                        // control in pixels.
            inplaceInstallPrompt:false, // Determines whether to display 
                                        // in-place install prompt if 
                                        // invalid version detected.
            background:'#ffffff',       // Background color of control.
            isWindowless:'false',       // Determines whether to display control 
                                        // in Windowless mode.
            framerate:'24',             // MaxFrameRate property value.
            version:'0.9'               // Control version to use.
            
        },
        {
            onError:null,               // OnError property value -- 
                                        // event handler function name.
            onLoad:null                 // OnLoad property value -- 
                                        // event handler function name.
        },
        null);                          // Context value -- event handler function name.
        
        
}