/* <gslb_version version="V1.3.3" /> */
/* Copyright © GalaSoft Laurent Bugnion 2007 */

//*****************************************************************************
//* gslb.sl.SilverlightLogo.js
//*****************************************************************************
//* Control name            : SilverlightLogo
//* Target                  : Silverlight-enabled web browsers
//* Language/Compiler       : JavaScript
//* Author                  : Laurent Bugnion (LBu)
//* Created                 : 07.10.2007
//*****************************************************************************
//* Last Base Level: BL0002
//*****************************************************************************

//*****************************************************************************
//* Imports *******************************************************************
//*****************************************************************************

// using Silverlight

// Create namespace
if (!window.gslb)
    gslb = {};

if (!gslb.sl)
    gslb.sl = {};

//*****************************************************************************
//* Constructor ***************************************************************
//*****************************************************************************

gslb.sl.SilverlightLogo = function(resourcesPath, hostID) 
{
    this._resourcesPath = resourcesPath;
    this._divContainer = document.getElementById(hostID);
}

//*****************************************************************************
//* Static methods ************************************************************
//*****************************************************************************

gslb.sl.SilverlightLogo.createPage = function(pagePath, hostID, controlID, resourcesPath)
{
	var page = new gslb.sl.SilverlightLogo(resourcesPath, hostID);
	Silverlight.createObjectEx({
		source: pagePath,
		parentElement: page._divContainer,
		id: controlID,
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0",
			isWindowless: "True",
			background: "#00FFFFFF"
		},
		events: {
			onLoad: gslb.sl.SilverlightLogo.createDelegate(page, page.page_Load)
		}
	});
	
	return page;
}

// TODO Pass in gslb.Object
gslb.sl.SilverlightLogo.createDelegate = function(instance, method)
{
    return function()
    {
        return method.apply(instance, arguments);
    }
}

gslb.sl.SilverlightLogo.prototype =
{
    //*************************************************************************
    //* Properties ************************************************************
    //*************************************************************************

    // Public interface
    
    MediaPlayer : null,
    RootElement : null,

    //*************************************************************************
    //* Methods ***************************************************************
    //*************************************************************************

	// TO BE PASSED IN gslb.sl.SilverlightBase
	
	addListenerToElement: function(element, eventName, handler)
	{
	    if (this._loaded)
	    {
	        if (typeof(element) == "string")
	        {
	            element = this.RootElement.FindName(element);
	        }
    	    
	        if (element === this)
	        {
	            element = this.RootElement;
	        }

	        if (!element)
	        {
	            throw "Cannot add event " + eventName;
	        }
	        
  	        element.AddEventListener(eventName, handler);
	    }
	    else
	    {
	        if (!this._savedHandlers)
	        {
	            this._savedHandlers = new Array();
	        }
	        var newHandler =
	        {
	            element : element,
	            eventName : eventName,
	            handler : handler
	         };
	         this._savedHandlers[this._savedHandlers.length] = newHandler;
	    }
	},
	
	findName: function(name)
	{
	    if (!this.RootElement)
	    {
	        return null;	    
	    }
	    return this.RootElement.FindName(name);
	},
	
    //*************************************************************************
    //* Privates **************************************************************
    //*************************************************************************

    //*************************************************************************
    // The methods and attributes under this line are "private" and should not be used by external users.
    // Using these methods and attributes may result in unexpected effect.

    // Attributes -------------------------------------------------------------

    _savedHandlers: null,
    _resourcesPath: "",
    _divContainer: null,
    _loaded: false,

    // Event handlers ---------------------------------------------------------

	page_Load: function(control, userContext, rootElement) 
	{
        this.MediaPlayer = control.content.FindName("SilverlightLogoMedia");
        
        if (this.MediaPlayer)
        {
            this.MediaPlayer.Source = this._resourcesPath + "SilverlightLogo.wmv";
		    this.MediaPlayer.AddEventListener("MediaEnded",
		        gslb.sl.SilverlightLogo.createDelegate(this, this.media_Ended));
        }
		
		this.RootElement = rootElement;
		
		var backgroundImage = control.content.FindName("BackgroundImage");
		if (backgroundImage)
		{
        	backgroundImage.Source = this._resourcesPath + "Silverlight.png";
		}

		this._loaded = true;
		
		if (this._savedHandlers)
		{
		    for (var index = 0; index < this._savedHandlers.length; index++)
		    {
		        this.addListenerToElement(this._savedHandlers[index].element,
		            this._savedHandlers[index].eventName,
		            this._savedHandlers[index].handler);
		    }
		}
		
		var animation = control.content.FindName("FadeInAnimation");
		if (animation)
		{
    		animation.Begin();
		}
		
		if (this._divContainer)
		{
		    var canvas = control.content.FindName("Page");
		    this._divContainer.style.width = canvas.Width + "px";
		    this._divContainer.style.height = canvas.Height + "px";
		}
	},
	
	media_Ended: function(sender, eventArgs)
	{
	    sender.Stop();
	    sender.Play();
	}
}
