﻿// Namespace ...
if (RHPConsulting == null)
    var RHPConsulting = {};
       
// Chart Part ...       
RHPConsulting.ChartDataPoint = function(chart, seriesToken, seriesColor, index, X1, Y1, X2, Y2) {
   
    // event arrays ...
    this._animationCompleteHandlers = new Array();
   
    // containers and index ...
    this._parentChart = chart;
    this._seriesToken = seriesToken;
    this._index = index;
   
    // token for animation ...
    this._animToken = null;
   
    // xaml template ...
    var _xamlLine = "";
    _xamlLine += "<Canvas xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='ChartDataPoint{0}' Canvas.Left='0' Canvas.Top='0'>";
    _xamlLine += "<Canvas.Resources>";
    _xamlLine += "<Storyboard x:Name='AnimateChartDataPoint{0}'>";
    _xamlLine += "<DoubleAnimation x:Name='AnimateChartDataDoubleAnimation{0}' Duration='0:00:02.5' Storyboard.TargetName='ChartDataPoint{0}' Storyboard.TargetProperty='(Canvas.Left)' By='-25' />";
    _xamlLine += "</Storyboard>";
    _xamlLine += "</Canvas.Resources>";
    _xamlLine += "<Line X1='{1}' Y1='{2}' X2='{3}' Y2='{4}' Stroke='{5}' StrokeThickness='3' Opacity='0.75' >";
    _xamlLine += "</Line>"; 
    _xamlLine += "</Canvas>";
    
    // format _xaml with values ...
    var _xaml = String.format(_xamlLine, String.format("_{0}_{1}", seriesToken, index), X1, Y1, X2, Y2, seriesColor);
    
    // get host ...
    var _host = this._parentChart.get_XamlContainer().GetHost();
    // create xaml element through host ...
    var _element = _host.content.createFromXaml(_xaml);
    // add to children collection ...
    this._parentChart.get_XamlContainer().children.add(_element);
        
}
RHPConsulting.ChartDataPoint.prototype = {

    // event definition ...
    addEventListener : function(eventName, handler) {
        var handlers = this._get_EventArrayReference(eventName);
        var length   = handlers.push(handler); 
        return (length -1);       
    },
    removeEventListener : function(eventName, token) {
        var handlers  = this._get_EventArrayReference(eventName)
        handlers.splice(token,1); 
    },
    // Events - Private Methods ...
    _raiseEvent : function(eventName, sender, args, ext) {
        var handlers = this._get_EventArrayReference(eventName);
        for(var index in handlers) { handlers[index](sender,args,ext); }  
    },
    // Events - Private Properties ...
    _get_EventArrayReference : function(eventName) {
        var handlers = null;
        switch (eventName)
        {
            case "AnimationComplete"  : handlers = this._animationCompleteHandlers;      break;
        }
        return handlers;
    },
    
    
    
    // private event handlers ...
    _animComplete : function(sender, args) {
        this._raiseEvent("AnimationComplete",this, args);
    },
    
    
    // public properties ...
    get_ScrollInterval : function() {
        return this._scrollInterval;
    },
    set_ScrollInterval : function(value) {
        this._scrollInterval =value;
    },
    // public properties (readonly) ...
    get_SeriesToken : function() {
        return this._seriesToken;
    },
    get_Location : function() {
        var _name = this._parentChart.FormatPartName("ChartDataPoint{0}", this._seriesToken, this._index);
        var _part = this._parentChart.get_XamlContainer().findName(_name);
        return _part["Canvas.Left"];
    },
    get_Index : function() {
        return this._index;
    },
    
    
    // public methods ...
    BeginScroll : function() {
        
        // animation ...
        var _name = this._parentChart.FormatPartName("AnimateChartDataPoint{0}", this._seriesToken, this._index);
        var _anim = this._parentChart.get_XamlContainer().findName(_name);
        
        // associate handler if necessary ...
        if (this._animToken == null)
            this._animToken = _anim.addEventListener("Completed", Function.createDelegate(this, this._animComplete));
        _anim.begin();
        
        
    },
    Dispose : function() {
        
        // remove animation handler ...
        if (this._animToken == null) {
        
            // animation ...
            var _name = this._parentChart.FormatPartName("AnimateChartDataPoint{0}", this._seriesToken, this._index);
            var _anim = this._parentChart.get_XamlContainer().findName(_name);
            
            // remove event listener ...
            _anim.removeEventListener("Completed", this._animToken);
            
        }
        
        // remove from parent element ...
        var _name = this._parentChart.FormatPartName("ChartDataPoint{0}", this._seriesToken, this._index);
        var _part = this._parentChart.get_XamlContainer().findName(_name);
        this._parentChart.get_XamlContainer().children.remove(_part);
        
    }
}



