/**
 * Called by the bt_mini and bt_full swf objects
 * for web events tracking
 */
var BTCalculator_Widget_Tracking = function() {

    this.unloadEventAttached = false;
    this.hits=0;

    this.values = {
        interest : { min:null, max:null, last:null },
        transfer : { min:null, max:null, last:null },
        monthly  : { min:null, max:null, last:null }
    };
    
    this.setInterest = function(value) {
        //alert('interest:'+value);
        this.values.interest.last = value;
        this.updateMaxMin('interest');
    }
    
    this.setTransfer = function(value) {
        //alert('transfer:'+value);
        this.values.transfer.last = value;
        this.updateMaxMin('transfer');
    }
    
    this.setMonthly = function(value) {
        //alert('monthly:'+value);
        this.values.monthly.last = value;
        this.updateMaxMin('monthly');
    }
    
    this.updateMaxMin = function(target)
    {
        if( this.hits<3 ) {
            this.hits++;
            return;
        }
        this.attachUnloadEvent();
        if( this.values[target].last > this.values[target].max || this.values[target].max === null )
            this.values[target].max = this.values[target].last;
        if( this.values[target].last < this.values[target].min || this.values[target].min === null )
            this.values[target].min = this.values[target].last;
    }
    
    this.attachUnloadEvent = function()
    {
        if( ! this.unloadEventAttached )
        {
            var here = this;
            $(window).unload(function(){here.unloadEvent()});
            this.unloadEventAttached = true;
        }
    }
    
    this.unloadEvent = function()
    {
        if( this.values.interest.max || this.values.transfer.max || this.values.monthly.max )
            for( v in this.values )
                WebTracker.send('BTCalc.'+v+':max='+this.values[v].max+':min='+this.values[v].min+':last='+this.values[v].last,'end','slider');
    }
}

var BTCalculator_Widget_Tracking = new BTCalculator_Widget_Tracking();
