/*
    Thanks to Richard Z for his great work and inspiration!
*/         


//----------------------------------------------------------------------------------------------------------------------
//  HELPER METHODS
//----------------------------------------------------------------------------------------------------------------------              
function num2money(n_value,prefix)
 {
	if (isNaN(Number(n_value)))
		return 'ERROR';
	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);	
	var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);    
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value >= 1) {	    
		s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);		
		s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0.' + s_result;
	
	var ret= b_negative
		? '('+prefix+'' + s_result + ')'
		: prefix+'' + s_result;
	// apply formatting and return	
	return  ret;
}
//----------------------------------------------------------------------------------------------------------------------
Silverlight.createDelegate = function(instance, method)
{
	return function()
	{
		return method.apply(instance, arguments);
	}
}
//----------------------------------------------------------------------------------------------------------------------
function GenerateTextInfoBlock(control,text,x,y,fontsize)
{
    return control.content.createFromXaml(
               '<TextBlock Name="Info" FontSize="'+fontsize+'" Text="'+text+'"'
                + '         Canvas.Left="' + x + '" Canvas.Top="' + y + '"'
                + '         Canvas.ZIndex="3" Opacity="0.0" Foreground="#FFFFFF">'              
                  +'<TextBlock.Resources>      '
                  +'<Storyboard Name="InfoRising" Completed="InfoFadingBegin">'
                  +'<DoubleAnimation Storyboard.TargetName="Info" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0:0:1.0" />'
                  +'</Storyboard>'
                  +'<Storyboard Name="InfoFading" Completed="InfoRisingBegin">'
                  +'<DoubleAnimation Storyboard.TargetName="Info" Storyboard.TargetProperty="Opacity" To="0.0" Duration="0:0:1.0" />'
                  +'</Storyboard>'
                  +'</TextBlock.Resources>'          
                + '</TextBlock>'
                );
}
//----------------------------------------------------------------------------------------------------------------------
function GenerateCaptionTextBlock(control,text,x,y,fontsize)
{
    return control.content.createFromXaml(
            '<TextBlock Name="' + GetSegmentID('Caption',0) + '" FontSize="'+fontsize+'" Text="'+text+'"'
            + '         Canvas.Left="' + x + '" Canvas.Top="' + y + '"'
            + '         Canvas.ZIndex="3" Opacity="0.5" Foreground="#FFFFFF">'           
            + '</TextBlock>'
            );    
}
//----------------------------------------------------------------------------------------------------------------------

function GenerateBubble(controlP,i,text,x,y,fontsize)
{
    var control = controlP.canvas.getHost();
    return control.content.createFromXaml(
            '<TextBlock Name="' + GetSegmentID('bubbleText',i) + '" FontSize="'+fontsize+'" Text="'+text+'"'
            + '         Canvas.Left="' + x + '" Canvas.Top="' + y + '"'
            + '         Canvas.ZIndex="3" Opacity="0" Visibility="Collapsed" Foreground="#FFFFFF">'           
            + '</TextBlock>'
            );
}
//----------------------------------------------------------------------------------------------------------------------
function GenerateXLabel(controlP,i,text,x,y,fontsize,rotated)
{
    var control = controlP.canvas.getHost();
    var rotateangle = 0;
    
    if(rotated==1)
        rotateangle=270;
        
    return   control.content.createFromXaml(
            '<TextBlock Name="' + GetSegmentID('bubbleText2',i) + '" FontSize="'+fontsize+'" Text="'+text+'"'
            + '         Canvas.Left="' + x +'" Canvas.Top="' + y + '"'
            + '         Canvas.ZIndex="3" Opacity="1" Foreground="#f1f1f1">'
            + ' <TextBlock.RenderTransform>'
            + '     <RotateTransform Angle="'+rotateangle+'" />'
            + ' </TextBlock.RenderTransform>'          
            + '</TextBlock>'
            );            
}
//----------------------------------------------------------------------------------------------------------------------
function GenerateBarBRect(controlP,i,x,y,width,height,color,opacity)
{ 
    var control = controlP.canvas.getHost();     
    return control.content.createFromXaml(
            '<Rectangle Name="' + GetSegmentID('barB',i) + '" Canvas.Left="' + x + '" Width="'+width+'"'
          + '           Canvas.Top="' + y + '" Height="' + (height +5)+ '"'
          + '           RadiusX="2" RadiusY="2"  StrokeThickness="0" '+opacity+' >'
          + '   <Rectangle.Fill>'
          + '       <SolidColorBrush Name="' + GetSegmentID('brushB',i) + '" Color="'+color+'" />'
          + '   </Rectangle.Fill>'          
          + '   <Rectangle.RenderTransform>'
          + '       <ScaleTransform Name="' + GetSegmentID('barscaleB',i) + '" CenterY="' + (height) + '" ScaleY="0.0" />'
          + '   </Rectangle.RenderTransform>'
          + '</Rectangle>'
        );  
}
//----------------------------------------------------------------------------------------------------------------------
function GenerateBarRect(controlP,i,x,y,width,height,cb)
{ 
    var control = controlP.canvas.getHost();     
    return control.content.createFromXaml(
            '<Rectangle Name="' + GetSegmentID('bar',i) + '" Canvas.Left="' + x + '" Width="'+width+'"'
          + '           Canvas.Top="' + y + '" Height="' + (height+5) + '"'
          + '           StrokeThickness="1" RadiusX="2" RadiusY="2"'
          + '           Loaded="loadbar">'
          
          + '   <Rectangle.RenderTransform>'
          + '     <TransformGroup>'
          + '       <ScaleTransform Name="' + GetSegmentID('barscale',i) + '" CenterY="' + height + '" ScaleY="0.0" />'
          + '       <SkewTransform Name="' + GetSegmentID('barskew',i) + '" CenterX="0" />'
          + '     </TransformGroup>'
          + '   </Rectangle.RenderTransform>'

          + '   <Rectangle.Stroke>'
          + '       <SolidColorBrush Name="' + GetSegmentID('barStroke',i) + '" Color="'+cb.colorBarStroke+'" />'
          + '   </Rectangle.Stroke>'
          
          + '   <Rectangle.Fill>'
          + '       <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">'
          + '           <GradientStop Name="' + GetSegmentID('grad1',i) + '" Color="'+cb.colorBarGrad1+'" Offset="0.0" />'
          + '           <GradientStop Name="' + GetSegmentID('grad2',i) + '" Color="'+cb.colorBarGrad2+'" Offset="1.0" />'
          + '       </LinearGradientBrush>'
          + '   </Rectangle.Fill>'
          //////////////////////
          // Animations
          + '  <Rectangle.Resources>'
          // Loaded animation (rise from the bottom)
          + '    <Storyboard Name="' + GetSegmentID('load',i) + '" Completed="load2">'
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscale', i),
                      'targetProperty': 'ScaleY',
                      'beginTime':      i/50,
                      'duration':       0.4,
                      'to':             1.1
                    })
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscaleB', i),
                      'targetProperty': 'ScaleY',
                      'beginTime':      i/50,
                      'duration':       0.3,
                      'to':             1.1
                    })
          + '    </Storyboard>'

          // Loaded animation 2 (first bounce)
          + '    <Storyboard Name="' + GetSegmentID('load2',i) + '" Completed="load3">'
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscale', i),
                      'targetProperty': 'ScaleY',
                      'duration':       0.2,
                      'to':             0.9
                    })
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscaleB', i),
                      'targetProperty': 'ScaleY',
                      'duration':       0.2,
                      'to':             0.9
                    })
          + '    </Storyboard>'
          // Loaded animation 3 (last bounce)
          + '    <Storyboard Name="' + GetSegmentID('load3',i) + '">'
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscale', i),
                      'targetProperty': 'ScaleY',
                      'duration':       0.06,
                      'to':             1.0
                    })
          +         DoubleAnimation({
                      'targetName':     GetSegmentID('barscaleB', i),
                      'targetProperty': 'ScaleY',
                      'duration':       0.06,
                      'to':             1.0
                    })
          + '    </Storyboard>'
          // Growing animation
          + '    <Storyboard Name="' + GetSegmentID('esg',i) + '">'
          + '      <DoubleAnimation Storyboard.TargetName="' + GetSegmentID('bubbleText',i) + '" Storyboard.TargetProperty="Opacity"'
          + '                       To="1" Duration="0:0:0.00" />'
//          + '      <DoubleAnimation Storyboard.TargetName="' + id('bubble',i) + '" Storyboard.TargetProperty="Opacity"'
//          + '                       To="0.5" Duration="0:0:0.00" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('grad2',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+ cb.colorBarGrad2Growing+'" Duration="0:0:0.0" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('grad1',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+cb.colorBarGrad1Growing +'" Duration="0:0:0.0" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('barStroke',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+ cb.colorBarStrokeGrowing+'" Duration="0:0:0.0" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('brushB',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+cb.colorBarBackFillGrowing +'" Duration="0:0:0.0" />'
          + '    </Storyboard>'

          // Shrinking animation
          + '    <Storyboard Name="' + GetSegmentID('ess',i) + '" Completed="doneShrink">'
          + '      <DoubleAnimation Storyboard.TargetName="' + GetSegmentID('bubbleText',i) + '" Storyboard.TargetProperty="Opacity"'
          + '                       To="0" Duration="0:0:0.0" />'
//          + '      <DoubleAnimation Storyboard.TargetName="' + id('bubble',i) + '" Storyboard.TargetProperty="Opacity"'
//          + '                       To="0" Duration="0:0:0.0" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('grad1',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+ cb.colorBarGrad1 +'" Duration="0:0:0.1" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('grad2',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+cb.colorBarGrad2 +'" Duration="0:0:0.1" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('barStroke',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+ cb.colorBarStroke+'" Duration="0:0:0.0" />'
          + '      <ColorAnimation Storyboard.TargetName="' + GetSegmentID('brushB',i) + '" Storyboard.TargetProperty="Color"'
          + '                      To="'+cb.colorBarBackFill +'" Duration="0:0:0.1" />'
          + '    </Storyboard>'

          + '  </Rectangle.Resources>'

          ///////////////////////
          + '</Rectangle>'
        );
}
//----------------------------------------------------------------------------------------------------------------------
function GetSegmentID(name, id) 
{ return name + '_' + 'gr' + '_' + id; }

//----------------------------------------------------------------------------------------------------------------------
function Animation(type, data) {
    if (data.targetName && data.targetProperty && data.to != null && data.duration) {
        var properties = '';
        
        properties += ' Storyboard.TargetName="' + data.targetName + '"';
        properties += ' Storyboard.TargetProperty="' + data.targetProperty + '"';
        
        if (data.beginTime)
            properties += ' BeginTime="0:0:' + data.beginTime + '"';

        if (data.from)
            properties += ' From="' + data.from + '"';

        properties += ' To="' + data.to + '"';
        properties += ' Duration="0:0:' + data.duration + '"';
        return '<' + type + 'Animation' + properties + ' />';
    }
    return '';
}
//----------------------------------------------------------------------------------------------------------------------
function DoubleAnimation(data) {
    return Animation('Double', data);
}
//----------------------------------------------------------------------------------------------------------------------
function PointAnimation(data) {
    return Animation('Point', data);
}
//----------------------------------------------------------------------------------------------------------------------
function RemoveSpaceOnTheEnd(inputstring)
{
    if(inputstring.charAt(inputstring.length-1)==' ') //spacja na ostatniej pozycji
    {
        return inputstring.substr(0,inputstring.length-1);
    }
    else
        return inputstring;
}
//----------------------------------------------------------------------------------------------------------------------
function InfoRisingBegin(sender, eventArgs)
 {
 var targetMovie =  sender.findName('InfoRising');
   if(targetMovie!=null)
    targetMovie.begin();  
 }
 //----------------------------------------------------------------------------------------------------------------------
 function InfoFadingBegin(sender, eventArgs)
 {
 var targetMovie =  sender.findName('InfoFading');
   if(targetMovie!=null)
    targetMovie.begin();  
 }
//----------------------------------------------------------------------------------------------------------------------
function loadbar(sender, eventArgs)
{
   // alert('loadbar');
   var targetMovie =  sender.findName('load'+sender.Name.substr(3));
   if(targetMovie!=null)
    targetMovie.begin();
}
//----------------------------------------------------------------------------------------------------------------------
function load2(sender, eventArgs)
{
    //alert('laod2');
   // sender.findName('load2'+sender.Name.substr(4)).begin();
   var targetMovie =  sender.findName('load2'+sender.Name.substr(4));
   if(targetMovie!=null)
    targetMovie.begin();
}
//----------------------------------------------------------------------------------------------------------------------
function load3(sender, eventArgs)
{
   // alert('load3');
   // sender.findName('load3'+sender.Name.substr(5)).begin();
   var targetMovie =  sender.findName('load3'+sender.Name.substr(5));
   if(targetMovie!=null)
    targetMovie.begin();
}
//----------------------------------------------------------------------------------------------------------------------
      