var $ = document.getElementById;
window.onload = PageInit;

function handleSilverlightLoad() {
			window.bubblecounter = 1;
			window.shinecounter = 1;
			window.arrowcounter = 1;
			window.lightcounter = 1;
			rootElement.findName('sb_1').begin();


			for (var i=0; i<40; i++){
							setTimeout(BlowBubble, GetRandom(0, 5000));

			}

			for (var i=0; i<10; i++){
							setTimeout(SunShine, GetRandom(0, 5000));
			}	
			for (var i=0; i<1; i++){
							setTimeout(ArrowGenerator, GetRandom(0, 5000));
			}
			for (var i=0; i<10; i++){
							setTimeout(LightSender, GetRandom(0, 5000));
			}



}

function ArrowGenerator(sender) {
	var xpos;
	var top = GetRandom(155,165);
	var velocity;
	var minwidth = 20;
	var maxwidth = 80;
	
	xpos = 35;
	var angle = 0;

if (sender) {
		var canvas = sender.findName(sender.name.split('_')[0]);
		sender.findName('arrow_container').children.remove(canvas);
	} else {
	
	}
	new Arrow(top, xpos, angle);
	
}

var Arrow = function(top, xpos, angle) {
	this.r = top;
	this.x = Math.round(xpos);
	this.t = this.s - GetRandom(-20, 250);
	this.a = Math.round(angle);
	this.n = ++window.arrowcounter;
	this.to = this.v > 0 ? 805 : 0 - (this.w + 5);
	this.d = Math.abs((this.x - this.to) / 810 * 59 / this.v).toFixed(1);
	if (this.d > 59) this.d = 59;
	
	this.Create = function() {		
		var xaml = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  x:Name="arrow' + this.n + '" Canvas.Top="' + this.r + '" Canvas.Left="0">';
xaml += '<Image x:Name="a' + this.n + '" Canvas.Left="156" Canvas.Top="129" Source="media/arrow.png" />';
xaml += '<Canvas.Resources><Storyboard x:Name="arrow' + this.n + '_sb" Completed="ArrowGenerator">';
xaml += '<DoubleAnimation Storyboard.TargetName="a' + this.n + '" Storyboard.TargetProperty="(Canvas.Left)" To="' + this.x + '" Duration="0:0:3.5" />';
xaml += '<DoubleAnimation Storyboard.TargetName="a' + this.n + '" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:3.5" />';

xaml += '</Storyboard></Canvas.Resources>';
		xaml +='</Canvas>';		
		
		

		this.canvas = control.content.createFromXaml(xaml);
		rootElement.findName('arrow_container').children.add(this.canvas);
		rootElement.findName('arrow' + this.n + '_sb').begin();
	
	}

	this.Create();
}

////////////////////////////////////////////////////////////////////////////////////

function LightSender(sender) {
	var height = GetRandom(160, 550);
	var xpos;
	var velocity;
	var width = height / GetRandom(40, 55);
	var minwidth = 20;
	var maxwidth = 80;
	
	xpos = GetRandom(-20, 15);
	var angle = 0;

if (sender) {
		var canvas = sender.findName(sender.name.split('_')[0]);
		sender.findName('light_container').children.remove(canvas);
	} else {
	
	}
	new Light(width, height, xpos, angle);
	
}

var Light = function(width, height, xpos, angle) {
	this.h = height;
	this.w = width;
	this.x = Math.round(xpos);
	this.s = 585 - height;
	this.t = this.s - GetRandom(-20, 250);
	this.a = Math.round(angle);
	this.n = ++window.lightcounter;
	this.to = this.v > 0 ? 805 : 0 - (this.w + 5);
	this.d = Math.abs((this.x - this.to) / 810 * 59 / this.v).toFixed(1);
	if (this.d > 59) this.d = 59;
	
	this.Create = function() {
		var xaml = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="light' + this.n + '" Canvas.Top="' + this.t + '" Canvas.Left="30" Width="250" Height="585" >';
		xaml += '<Ellipse x:Name="l' + this.n + '" Width="' + this.w + '" Height="' + this.h + '">';
		xaml +='<Ellipse.Fill><RadialGradientBrush><GradientStop Color="#FF8453a4" Offset="0.371"/><GradientStop Color="#00FFFFFF" Offset="1"/></RadialGradientBrush></Ellipse.Fill></Ellipse>';
		xaml +='<Canvas.RenderTransform><TransformGroup><RotateTransform Angle="' + this.a + '" /></TransformGroup></Canvas.RenderTransform>';														 
		xaml += '<Canvas.Resources><Storyboard x:Name="light' + this.n + '_sb" Completed="LightSender">';
		xaml += '<DoubleAnimation Storyboard.TargetName="l' + this.n + '" Storyboard.TargetProperty="(Canvas.Left)" To="' + this.x + '" Duration="0:0:3" />';
		xaml += '<DoubleAnimation Storyboard.TargetName="l' + this.n + '" Storyboard.TargetProperty="Opacity" From=".65" To="0" Duration="0:0:2.5" />';
		xaml += '</Storyboard></Canvas.Resources></Canvas>';

		this.canvas = control.content.createFromXaml(xaml);
		rootElement.findName('light_container').children.add(this.canvas);
		rootElement.findName('light' + this.n + '_sb').begin();
	
	}

	this.Create();
}

////////////////////////////////////////////////////////////////////////////////////

function BlowBubble(sender) {
	var height = 8;
	var xpos;
	var velocity;
	var width;
	var minwidth = 20
	var maxwidth = 80;
	var angle;
	
	width = 8;
	xpos = GetRandom(0, 201);
	angle = GetRandom(0, 361);

if (sender) {
		var canvas = sender.findName(sender.name.split('_')[0]);
		sender.findName('container').children.remove(canvas);
	} else {
	
	}
	new Bubble(width, height, xpos, angle);
	
}

var Bubble = function(width, height, xpos, angle) {
	this.h = height;
	this.w = width;
	this.x = Math.round(xpos);
	this.a = Math.round(angle);
	this.n = ++window.bubblecounter;
	this.to = this.v > 0 ? 805 : 0 - (this.w + 5);
	this.d = Math.abs((this.x - this.to) / 810 * 59 / this.v).toFixed(1);
	if (this.d > 59) this.d = 59;
	
	this.Create = function() {
		var xaml = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="bubble' + this.n + '" Canvas.Top="300" Canvas.Left="35" Width="250" Height="585" >';
		xaml += '<Ellipse x:Name="e' + this.n + '" Width="' + this.w + '" Height="' + this.h + '">';
		xaml +='<Ellipse.Fill><RadialGradientBrush><GradientStop Color="#FFFFFFFF" Offset="0.371"/><GradientStop Color="#00FFFFFF" Offset="1"/></RadialGradientBrush></Ellipse.Fill></Ellipse>';
		xaml +='<Canvas.RenderTransform><TransformGroup><RotateTransform Angle="' + this.a + '" /></TransformGroup></Canvas.RenderTransform>';														 
		xaml += '<Canvas.Resources><Storyboard x:Name="bubble' + this.n + '_sb" Completed="BlowBubble">';
		xaml += '<DoubleAnimation Storyboard.TargetName="e' + this.n + '" Storyboard.TargetProperty="(Canvas.Left)" To="' + this.x + '" Duration="0:0:3" />';
		xaml += '<DoubleAnimation Storyboard.TargetName="e' + this.n + '" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:2.25" />';
		xaml += '</Storyboard></Canvas.Resources></Canvas>';

		this.canvas = control.content.createFromXaml(xaml);
		rootElement.findName('container').children.add(this.canvas);
		rootElement.findName('bubble' + this.n + '_sb').begin();
	
	}

	this.Create();
}

/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////

function SunShine(sender) {
	var height = GetRandom(55, 150);
	var xpos;
	var velocity;
	var width;
	var minwidth;
	var maxwidth;
	var angle;
	
	width = height / GetRandom(20, 35);
	xpos = GetRandom(0, 361);
	angle = GetRandom(0, 361);

if (sender) {
		var canvas = sender.findName(sender.name.split('_')[0]);
		sender.findName('shine_container').children.remove(canvas);
	} else {
	
	}
	new Shine(width, height, xpos, angle);
	
}

var Shine = function(width, height, xpos, angle) {
	this.h = height;
	this.w = width;
	this.x = Math.round(xpos);
	this.a = Math.round(angle);
	this.n = ++window.shinecounter;
	this.to = this.v > 0 ? 805 : 0 - (this.w + 5);
	this.d = Math.abs((this.x - this.to) / 810 * 59 / this.v).toFixed(1);
	if (this.d > 59) this.d = 59;
	
	this.CreateShine = function() {
		var xaml = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="shine' + this.n + '" Canvas.Top="300" Canvas.Left="35" Width="250" Height="585" >';
		xaml += '<Ellipse x:Name="s' + this.n + '" Width="' + this.w + '" Height="' + this.h + '" RenderTransformOrigin="0.488,0.941">';
		xaml +='<Ellipse.Fill><RadialGradientBrush><GradientStop Color="#FFFFFFFF" Offset="0.371"/><GradientStop Color="#00FFFFFF" Offset="1"/></RadialGradientBrush></Ellipse.Fill></Ellipse>';
		xaml +='<Canvas.RenderTransform><TransformGroup><RotateTransform x:Name="rs' + this.n + '" Angle="' + this.a + '" /></TransformGroup></Canvas.RenderTransform>';														 													 
		xaml += '<Canvas.Resources><Storyboard x:Name="shine' + this.n + '_sb" Completed="SunShine">';
		xaml += '<DoubleAnimation Storyboard.TargetName="rs' + this.n + '" Storyboard.TargetProperty="(Angle)" To="' + this.x + '" Duration="0:0:3" />';
		xaml += '<DoubleAnimation Storyboard.TargetName="s' + this.n + '" Storyboard.TargetProperty="(Opacity)" To="0" Duration="0:0:2.5" />';
		xaml += '</Storyboard></Canvas.Resources></Canvas>';

		this.canvas = control.content.createFromXaml(xaml);
		rootElement.findName('shine_container').children.add(this.canvas);
		rootElement.findName('shine' + this.n + '_sb').begin();
	
	}

	this.CreateShine();
}


/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////


function PageInit() {
	//CreateTitle();
	//AddDragToElements();
}

function CreateTitle() {	

	window.titletext = $('titlecontainer').innerHTML.replace(/&amp;/, '&');
	window.title = new Object();
	title.context = new Object();
	title.context.loadfunc = BuildTitle;
	Silverlight.createObjectEx({
		source: 'code/title.xaml',
		parentElement: $('titlecontainer'),
		id: 'titleControl',
		context: title.context,
		properties: {
			isWindowless: 'true',
			background: 'transparent',
			width: '100%',
			height: '100%',
			version: '1.0'
		},
		events: {
			onLoad: handleTitleLoad
		}
	});
	$('titlecontainer').style.visibility = 'visible';
}

function handleTitleLoad(control, userContext, rootElement) {
//alert('HandleTitleLoad');

	var id = control.id.replace(/Control/, '');
	window[id].control = control;
	window[id].context = userContext;
	window[id].root = rootElement;
	var loadfunc;
	if (window[id].context && (loadfunc = window[id].context.loadfunc)) loadfunc();
}

function BuildTitle() {
	//alert(window.titletext);
	title.root.findName('titletext').UnicodeString = window.titletext;
	title.root.findName('titleanim').begin();

}

function BuildSubTitle() {
	//alert(window.titletext);
	rootElement.findName('subtitletext').UnicodeString = window.titletext;
	rootElement.findName('subtitleanim').begin();

}

function TitleAnimComplete(sender, args) {
	BuildSubTitle();
}

function AddDragToElements() {
    var draggableElems = document.getElementsByTagName('a');
    for (var elem in draggableElems) {
        if (draggableElems[elem].tagName) {
            draggableElems[elem].ondrag = draggableElems[elem].ondragenter = draggableElems[elem].ondragend = draggableElems[elem].ondragstart = function() {
                var srcObj = window.event.srcElement;
                var dummyObj = srcObj.outerHTML;
                var dragData = window.event.dataTransfer;
                dragData.effectAllowed = 'none';
                dragData.dropEffect = 'none';
                var oEvent = window.event;
                oEvent.returnValue = false;
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

function GetRandom(a, b) {
	var n = Math.min(a, b), x = Math.max(a, b);
	var d = x - n;
	return Math.random() * d + n;
	
}