if (!window.CslaFactory)
	window.CslaFactory = {};

CslaFactory.Page = function() 
{
}

CslaFactory.Page.prototype =
{
	testMe: function(msg)
	{
		alert("Test: " + msg);
	},

	//-------------------------------------------------------------------------
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.textFxTextbox = rootElement.findName("textFxTextbox");
		this.textFx = rootElement.findName("MyTextFxLine");
		if(this.textFx != null)
		{
			this.textFx.addEventListener("Completed", Silverlight.createDelegate(this, this.handleTextFx_completed));
			
			textFxUrl = "textFx.xml";
			if(this.textFxUrl != null)
			{
				textFxUrl = this.textFxUrl;
			}
			this.initTextFxDownloader(rootElement, textFxUrl);
		}
				
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));

	},
	
	//-------------------------------------------------------------------------
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("Timeline1").Begin();
	},
	
	//-------------------------------------------------------------------------
	initTextFxDownloader: function(sender, url)
	{
		slPlugin = sender.getHost();
		downloader = slPlugin.createObject("downloader");

		downloader.addEventListener("completed", Silverlight.createDelegate(this, 
		this.handleTextFxDownloader_completed));
		downloader.addEventListener("DownloadFailed", Silverlight.createDelegate(this, 
		this.handleTextFxDownloader_failed));
		downloader.open("GET", url);
		downloader.send();
	},
	
	//-------------------------------------------------------------------------
	handleTextFxDownloader_completed: function (sender, eventArgs)
	{
		this.textFxTexts = Array();
		
		if(typeof document.implementation != 'undefined' && typeof document.implementation.createDocument != 'undefined')
		{
			parser = new DOMParser();
			doc = parser.parseFromString(sender.ResponseText,"text/xml");
		}
		else if(typeof window.ActiveXObject != 'undefined')
		{
			doc = new ActiveXObject("Microsoft.XMLDOM");
			doc.async = false;
			doc.loadXML(sender.ResponseText);
		}
		
		entries = doc.getElementsByTagName("item");
		for(i=0; i<entries.length; i++)
		{
			entry = entries[i].getElementsByTagName("description");
			if(entry != null)
			{
				this.textFxTexts[this.textFxTexts.length] = entry[0].text;
			}
		}
		
		if(!document.cookie)
		{
			document.cookie = "textFx=0;";
			this.textFxIndex = 0;
		}
		this.handleTextFx_completed(sender, null);
	},
	
	//-------------------------------------------------------------------------
	handleTextFx_completed: function(sender, eventArgs)
	{
		if(navigator.cookieEnabled)
		{
			c = document.cookie;
			nvs = c.split(";")
			for(i=0; i<nvs.length; i++)
			{
				nv = nvs[i].split("=");
				if(nv[0] == "textFx")
				{
					this.textFxIndex = nv[1];
					break;
				}
			}
		}
		
		if(this.textFxIndex >= this.textFxTexts.length)
		{
			this.textFxIndex = 0;
		}
		
		tb = this.textFxTextbox;
		tbCanvas = this.textFxTextbox.getParent();
		this.textFxTextbox.Inlines.GetItem(0).Text = this.textFxTexts[this.textFxIndex];
		this.textFxTextbox["Canvas.Left"] = tbCanvas.Width - tb.ActualWidth - 8;
		this.textFx.Begin();
		this.textFxIndex++;
		if(navigator.cookieEnabled)
		{
			document.cookie = "textFx="+this.textFxIndex+";";
		}
	},
	
	//-------------------------------------------------------------------------
	handleTextFxDownloader_failed: function (sender, eventArgs)
	{
//		alert("Unable to load texts: " + eventArgs.errorMessage);
	},
	
	//-------------------------------------------------------------------------
	initXmlFxSource: function()
	{
		if(typeof document.implementation != 'undefined' && typeof document.implementation.createDocument != 'undefined')
		{
			this.xml_source = document.implementation.createDocument("","",null);
			this.xml_source.loaded = Silverlight.createDelegate(this, this.handleXmlSourceLoaded);
			this.xml_source.load("test.xml");
		}
		else if(typeof window.ActiveXObject != 'undefined')
		{
			this.xml_source = new ActiveXObject("Microsoft.XMLDOM");
			this.xml_source.async = false;
			this.xml_source.onreadystatechange = Silverlight.createDelegate(this, this.handleXmlSourceLoaded);
			this.xml_source.load("test.xml");
		}
		else
		{
			alert("No XML");
			return;
		}
		
	},
	
	//-------------------------------------------------------------------------
	handleXmlSourceLoaded: function(sender, eventArgs)
	{
		if(this.xml_source.readyState != 'undefined' && this.xml_source.readyState != 4)
		{
			return false;
		}
		
		this.textFxTexts = Array();
		entries = this.xml_source.getElementsByTagName("Entry");
		for(i=0; i<entries.length; i++)
		{
			atext = entries[i].getElementsByTagName("Text");
			if(atext != null)
			{
				this.textFxTexts[this.textFxTexts.length] = atext.nodeValue;
			}
		}
	}
}