﻿if (!window.skyscraper_ad)
	window.skyscraper_ad = {};

skyscraper_ad.Page = function() 
{
}

//base prototype
skyscraper_ad.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		//element variables
		this.control = control;
		var intro = this.control.content.findName("startScraper");
		this.types = this.control.content.findName("typeLoop");
		this.platinum = {
			"storyboard" : this.control.content.findName("platinumLoop"),
			"index" : 0,
			"preload" : new Image(),
			"data" : new Array()
		};
		this.gold = {
			"storyboard" : this.control.content.findName("goldLoop"),
			"index" : 0,
			"preload" : new Image(),
			"data" : new Array()
		};
		this.silver = {
			"storyboard" : this.control.content.findName("silverLoop"),
			"index" : 0,
			"preload" : new Image(),
			"data" : new Array()
		};
		this.bronze = {
			"storyboard" : this.control.content.findName("bronzeLoop"),
			"index" : 0,
			"preload" : new Image(),
			"data" : new Array()
		};
		
		//set paths for images and data so it works when moved...
		this.includeDepth = window.location.protocol + "//" + window.location.hostname + "/dynamics/convergence/includes/copenhagen/2008/silverlight/skyscraper/";
		this.setBaseLocation();
		
		//set company data object and remove elements without images
		var data = companies.companies;
		this.cleanArray(data);
		
		//this.data split into sponsorType arrays
		this.splitArray(data);
		
		//shuffle split data so that it displays in a new order per page load
		this.platinum.data = this.shuffleArray(this.platinum.data);
		this.gold.data = this.shuffleArray(this.gold.data);
		this.silver.data = this.shuffleArray(this.silver.data);
		this.bronze.data = this.shuffleArray(this.bronze.data);
		
		//preload first images
		this.platinum.preload.src = this.includeDepth + "resources/" + this.platinum.data[this.platinum.index].logoUrl;
		this.gold.preload.src = this.includeDepth + "resources/" + this.gold.data[this.gold.index].logoUrl;
		this.silver.preload.src = this.includeDepth + "resources/" + this.silver.data[this.silver.index].logoUrl;
		this.bronze.preload.src = this.includeDepth + "resources/" + this.bronze.data[this.bronze.index].logoUrl;
		
		//start "startScraper" animation storyboard
		intro.Begin();
		
		//storyboard completion events
		intro.addEventListener("Completed", Silverlight.createDelegate(this, this.loopTypes));
		this.types.addEventListener("Completed", Silverlight.createDelegate(this, this.loopFirst));
		this.platinumEvent = this.platinum.storyboard.addEventListener("Completed", Silverlight.createDelegate(this, this.loopAll));
		this.goldEvent = this.gold.storyboard.addEventListener("Completed", Silverlight.createDelegate(this, this.loopAll));
		this.silverEvent = this.silver.storyboard.addEventListener("Completed", Silverlight.createDelegate(this, this.loopAll));
		this.bronzeEvent = this.bronze.storyboard.addEventListener("Completed", Silverlight.createDelegate(this, this.loopAll));
	},
	
	loopTypes: function(sender, eventArgs)
	{
		this.types.Begin();
	},
	
	loopFirst: function(sender, eventArgs)
	{
		//alert("first happened");
		
		this.loopAll(this.platinum.storyboard);
		this.loopAll(this.gold.storyboard);
		this.loopAll(this.silver.storyboard);
		this.loopAll(this.bronze.storyboard);
	},
	
	//set new data and runLoop
	loopAll: function(sender, eventArgs)
	{
		var element = sender.getValue("Name").split("Loop")[0];
		
		var logo = this.control.content.findName(element+"Logo");
		logo.source = this.includeDepth + "resources/" + eval("this."+ element +".data[this."+ element +".index].logoUrl");
		
	    //if at the end of the object, restart the index.
		eval("this."+ element +".index = (this."+ element +".index >= this."+ element +".data.length-1) ? 0 : this."+ element +".index+1");
		
		//preload next images while animation is running
		eval("this."+ element +".preload.src = this.includeDepth + 'resources/' + this."+ element +".data[this."+ element +".index].logoUrl");
		
		//run loop and increment index
		eval("this."+ element +".storyboard.Begin()");
	},
	
	setBaseLocation: function()
	{
		/*
		//the way it should work!
		images = this.control.content.getElementsByTagName('IMAGE');
		for(var i = images.length; i; i--) images[i].source = siteBase + this.includeDepth + images[i].source;
		*/
		
		//the way it has to work...
		this.control.content.findName("headerAccent").source = this.includeDepth + this.control.content.findName("headerAccent").source;
		this.control.content.findName("adOverlay").source = this.includeDepth + this.control.content.findName("adOverlay").source;
		//this.control.content.findName("platinumType").source = this.includeDepth + this.control.content.findName("platinumType").source;
		//this.control.content.findName("silverType").source = this.includeDepth + this.control.content.findName("silverType").source;
		//this.control.content.findName("goldType").source = this.includeDepth + this.control.content.findName("goldType").source;
		//this.control.content.findName("bronzeType").source = this.includeDepth + this.control.content.findName("bronzeType").source;
	},
	
	//Thanks to http://jsfromhell.com/array/shuffle
	shuffleArray: function(v)
	{
		for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
		return v;
	},
	
	cleanArray: function(inArr)
	{
		var pattern = new RegExp("logos08/");
		for (var x=inArr.length-1; x >= 0; x--) {
			if (!pattern.test(inArr[x].logoUrl))
			{
				inArr.splice(x,1);
			}
		}
	},
	
	splitArray: function(inArr)
	{
		for(var i = inArr.length-1; i >= 0; i--)
		{
			switch(inArr[i].partnerType)
			{
				case "platinum":
					this.platinum.data.push(inArr[i]);
					break;
				case "gold":
					this.gold.data.push(inArr[i]);
					break;
				case "silver":
					this.silver.data.push(inArr[i]);
					break;
				case "bronze":
					this.bronze.data.push(inArr[i]);
					break;
			}
		}
	}
}