
/*
-----------------------------------------------------------------------------------------------------------------------
    download some stuff
-----------------------------------------------------------------------------------------------------------------------
*/
function AssetLoader( sender )
{
//  this.txt = canvas.findName("progressText");
//  this.gfx = canvas.findName("progressIndicator");
  this.items = [];
  this.totalProgress = 0;
  this.cb;
}

AssetLoader.prototype.addItem = function( _name, _url, _xaml, _cb )
{
  // make a downloader instance
  var loader = agControl.createObject( "Downloader" );
  
  canvas.findName("load_reveal").begin();
  canvas.findName("load_dot_sb").begin();
  
  // Set the downloader's completed event to a local method and start the downloading
//  setCallback( loader, "completed", this.downloadCompleted );
//  setCallback( loader, "downloadProgressChanged", this.getTotalProgress );

  
  // callback for total completion
//  if( _cb )
//  {
//    this.cb = _cb;
//  }
  
  // create the media element
  if ( _xaml )
  {
      var mediaObj = agControl.content.createFromXaml( _xaml );
      canvas.children.add( mediaObj );
      var media = canvas.findName( _name );
  }
  else
  {
      var media = _name;
      loader.addEventListener("downloadProgressChanged", this.getTotalProgress);
//      loader.addEventListener("completed", this.onXamlCompleted);
  }
    
  // keep a reference of the element and loader
  this.items.push( { lIndex:this.items.length, lObj:loader, xObj:media, lCb:_cb } );
  
  // tally all progress
  this.totalProgress = this.items.length;
  
  // start loading
  loader.open( "Get", _url, true );
  loader.send();	

}
AssetLoader.prototype.getTotalProgress = function( sender, args )
{
    var currentProgress = 0;
    var parent = assetLoader;
    
    // get progress of all items
    for( var i in parent.items )
    {
        var item  = parent.items[ i ].lObj;
        var media = parent.items[ i ].xObj;
        var index = parent.items[ i ].lIndex;
        var cb    = parent.items[ i ].lCb;
        
        currentProgress += item.downloadProgress;
        if( item.downloadProgress == 1 && typeof( media ) != "string" && item.status == 200 )
        {
            // its a media element so set the source
            media.setSource( item );
            parent.downloadCompleted( cb );
        }
        else if ( item.status == 200 && item.downloadProgress == 1 && sender.responseText )
        {
            // its xaml so add it to the canvas
            var xamlFragment = agControl.content.createFromXAMLDownloader( sender, "" );
//            canvas.children.add( xamlFragment );

//            var xamlFragment = agControl.content.createFromXaml(sender.responseText);

            // Add the XAML object as a child of the root Canvas object.
            var parentCanvas = agControl.content.findName( media );
            parentCanvas.children.add( xamlFragment );
            
            // remove item from load que
            parent.items.splice( index, 1 );
            
            parent.downloadCompleted( cb );
        }
        else
        {
            // ERROR!!!
//            alert( "ERROR downloading: " + item.status );
        }
        
        
    }
    
    // compare to over all progress
    var currentPercent = parent.totalProgress / currentProgress;
    
    // update progress bar
    if( currentPercent != Infinity )
    {
        parent.updateProgressBar( currentPercent * 100 );
    }
    
    return currentPercent;
}

AssetLoader.prototype.updateProgressBar = function( percent )
{
    // Set the width of the progress bar
//    this.txt.text = percent + "%";
//    this.gfx.width = percent * 2;
}

AssetLoader.prototype.downloadProgressChanged = function( sender, args )
{
    assetLoader.getTotalProgress();
}

AssetLoader.prototype.downloadCompleted = function( cb )
{
  canvas.findName("load_hide").begin();
  canvas.findName("load_dot_sb").stop();

//    if( AssetLoader.getTotalProgress() == 1 )
//    {
//        alert( "loaded" );
        // Set the width of the progress bar
//        this.txt.text = "";
//        this.gfx.width = 0;
        
        if( cb )
        {
            cb.call();
        }
//    }
}

/*
-----------------------------------------------------------------------------------------------------------------------
    Routes a WPF/E event to a local handler instance
-----------------------------------------------------------------------------------------------------------------------
*/
function setCallback(target, eventName, callback) {
	if (!window.methodIndex)
		window.methodIndex = 0;
	
	var callbackName = "uniqueCallback" + (window.methodIndex++);
	var controller = this;
	var func = function() {
		callback.apply(controller, arguments);
	}
	
	eval(callbackName + " = func;");
	target[eventName] = "javascript:" + callbackName;
}

AssetLoader.prototype.onXamlCompleted = function( sender, eventArgs )
{

    // Retrieve the XAML fragment and create an object reference.
    var xamlFragment = agControl.content.createFromXamlDownloader( sender );

    // The CreateFromXamlDownloader method is more efficient than using CreateFromXaml with downloaded content.
    // var xamlFragment = control.createFromXaml(sender.responseText);

    // Add the XAML object as a child of the root Canvas object.
    canvas.children.add( xamlFragment );
}


