///////////////////////////////////////////////////////////////////////////////
//
//  CreateSilverlight.js   version 0.9
//
//  This file is provided by Microsoft as a helper file for websites that
//  incorporate Silverlight Objects. The default parameters below comply with 
//  Silverlight v1.0 Beta, which exposes 0.9 as its version number.     
//  This file is provided as is.
// 
// © 2007 Microsoft Corporation. All Rights Reserved.
//
// This file is licensed as part of the Silverlight 1.0 SDK, 
// for details look here: http://go.microsoft.com/fwlink/?LinkID=89144&clcid=0x409
//
///////////////////////////////////////////////////////////////////////////////


// JScript source code

//contains calls to silverlight.js, examples are below

function createSilverlight()
{  
                                                               
    Silverlight.createObjectEx(
        {source: 'SimpleXaml.xml'
            , parentElement:  document.getElementById('theDiv')
            , id:'theHost'
            , properties:{width:'100%'
                    , height:'100%'
                    , framerate:'24'
                    , version:'1.0'}
            , events: {onError:null, onLoad: handleLoad}
            , context:null
         });
}

// variables ...
var _imagesInfo = new Array();
var _imagesLoaded = 0;
var _images = new Array();
var _mainCanvas = null;

function handleLoad(sender, args, root)
{
    // get reference to main canvas ...
    _mainCanvas = root.findName("CanvasMain");
    // get reference to host ...
    var _host = root.getHost();   
    
    // add resize event listener, and call method to initialize size ...
    _host.content.onResize = handleResize;
    handleResize();
    
    // add elements to image array ...
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image001.JPG", 432, 576)); 
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image002.JPG", 576, 432));
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image003.JPG", 576, 432));
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image004.JPG", 576, 432));
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image005.JPG", 432, 576));
    _imagesInfo.push(new RHPConsulting.ImageInfo("Photos/Image006.JPG", 576, 432));
    
    // resize to browser ...
     
    // display loader ...
    DisplayLoader(true);
    DisplayLoaderMessage("Download of Images started...");
     
    // download images ...
    for(var index in _imagesInfo) 
    {
        // create downloader object and add and event listener ...
        var _downloader = _host.createObject("Downloader");
        _downloader.addEventListener("Completed", handleDownloadCompleted);
        _downloader.open("GET", _imagesInfo[index].get_Path());
        _downloader.send();
    }
}
//window.onload    = function() { handleResize(); }
//window.onresize  = function() { handleResize(); }
function handleResize(sender, args) {

    // resize main canvas ...
    var _host = _mainCanvas.GetHost();
    _mainCanvas.Width  = _host.content.ActualWidth;
    _mainCanvas.Height = _host.content.ActualHeight;
    
    // resize loader ...
    var _loader = _mainCanvas.findName("CanvasLoader");
    _loader.Width = _host.content.ActualWidth;
    _loader.Height = _host.content.ActualHeight;
    
    // resize loader elements (rectangle, textblock) ...
    var _rect = _mainCanvas.findName("RectangleLoader");
    _rect.Width = _host.content.ActualWidth;
    _rect.Height = _host.content.ActualHeight;
    var _text = _mainCanvas.findName("TextBlockLoader");
    _text["Canvas.Top"] = ((_host.content.ActualHeight - _text.ActualHeight) / 2);
    _text["Canvas.Left"] = ((_host.content.ActualWidth - _text.ActualWidth) / 2) ;
    
    // randomize position of items in canvas ...
    for(var index in _images) {
        _images[index].RamdomizeTranslateRotate();
        _images[index].Tumble();
    }
    
}
function handleDownloadCompleted(sender, args) {

    // find image reference ...
    for(var index in _imagesInfo) {
        if (_imagesInfo[index].get_Path() == sender.uri) {
            // build an image ...
            var _image = new RHPConsulting.ImageSurfaceExtender(_mainCanvas, index, _imagesInfo[index].get_Width(),  _imagesInfo[index].get_Height());
            _image.set_ImageSource_Downloader(sender, "");
            _image.addEventListener("Selected", handleImageSelected);
            _image.RamdomizeTranslateRotate();
            _image.set_Visible("Visible");
//            _image.Tumble();
            _images.push(_image);
            
            // increment images loaded ...
            _imagesLoaded++;
            
            // display message accordingly ...
            switch (_imagesLoaded) {
                case _imagesInfo.length :
                    DisplayLoaderMessage("All Images Loaded.");
                    DisplayLoader(false);
                    break;
                default : 
                    DisplayLoaderMessage("Succesfully Loaded Image (" + _imagesLoaded + " Of " + _imagesInfo.length + ")");
                    break;
            }
            
            // exit for ...
            break;
            
        }
    }
}
function handleImageSelected(sender, args) {
    for(var index in _images) {
        if (_images[index] == sender)
            _images[index].set_ZIndex(1);
        else
            _images[index].set_ZIndex(0);
    }
}

// Methods (Loader) ...
function DisplayLoader(action) {    

    var _loader = _mainCanvas.findName("CanvasLoader");
    _loader.Visibility = (action == true) ? "Visible" : "Collapsed";
}
function DisplayLoaderMessage(message) {
    
    // set text ...
    var _loaderTextBlock = _mainCanvas.findName("TextBlockLoader");
    _loaderTextBlock.Text = message;
    
    // center text ...
    var _host = _loaderTextBlock.GetHost();
    _loaderTextBlock["Canvas.Top"] = ((_host.content.ActualHeight - _loaderTextBlock.ActualHeight) / 2);
    _loaderTextBlock["Canvas.Left"] = ((_host.content.ActualWidth - _loaderTextBlock.ActualWidth) / 2) ;
    
}


if (!window.Silverlight) 
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
        return method.apply(instance, arguments);
    }
}