function Schedule ( parent ) 
{	
	/* Class Vars */
	Schedule.prototype.children = 		[];
	Schedule.prototype.rotation = 		[];
	Schedule.prototype.current = 		-1;
	Schedule.prototype.parent =	 		0;
	Schedule.prototype.workQueue = 		[];
				
		
	Schedule.prototype.groupView = 		false;
			
	Schedule.prototype.schedule = 		[];
	Schedule.prototype.nextChange =		false;
	Schedule.prototype.currentTime = 	new Date();
						
	function Schedule ( parent )
	{
		this.parent = parent;
		window.verticalAdRotator = this;
	}

	Schedule.prototype.addChild = function( ad )
	{
		this.children[this.children.length] = ad;		
		this.schedule[this.schedule.length] = ad;
		this.scheduleChange();		
		return true;	
	};
	Schedule.prototype.scheduleChange = function () 
	{
		if ( this.schedule.length == 1 )
			{ return true; }
		this.schedule.sort(function (a,b){ return  b.finalWeight - a.finalWeight; });
		return true;		
	};
	Schedule.prototype.scheduleCheck = function () {};
	Schedule.prototype.getNextUnLoadedNode = function () {
		while ( true )
		{
			replacement = this.getNextAd();
			if ( !replacement )
			{
				this.current = -1;
				replacement = this.getNextAd();
			}
			for ( var i = 0; i < this.parent.slots; i++ )
			{
				if ( replacement.node == this.parent.node.childNodes[i] )
					{ break; }
			}
			if ( i == this.parent.slots )
				{ return replacement; }
		}
	};	
	Schedule.prototype.scheduleNextChild = function ( node ) 
	{	
		obj = window.verticalAdRotator;
		currentNode = obj.schedule[node];
		replacement = obj.getNextUnLoadedNode();
		replacement.currentLoadNode = currentNode.currentLoadNode;
		currentNode.currentLoadNode = false;
		obj.parent.loadNodes[replacement.currentLoadNode] = replacement;
		try
			{ obj.parent.node.replaceChild(replacement.node,currentNode.node); } 
		catch (e)
		{
			replacement = obj.getNextUnLoadedNode();
			obj.parent.node.appendChild(replacement.node);
		}
		if ( obj.parent.node.childNodes.length < obj.parent.slots )
		{
			currentLoadNode = replacement.currentLoadNode;
			replacement = obj.getNextUnLoadedNode();
			replacement.currentLoadNode = currentLoadNode;
			obj.parent.node.appendChild(replacement.node);				
		}
		window.setTimeout("window.verticalAdRotator.scheduleNextChild(" + obj.current + ")",replacement.duration * 1000);
		return true;		
			
	};
	Schedule.prototype.scheduleStart = function () 
	{
		/*If the number of slots = the number of ads, then no rotation is needed*/
		if ( this.parent.children.length <= this.parent.slots )
			{ return true; }		
		for ( var i = 0; i < this.parent.slots; i++ )
			{ window.setTimeout("window.verticalAdRotator.scheduleNextChild(" + i + ")",this.schedule[i].duration * 1000); }
		//window.setInterval(this.scheduleWorker,50);
		return true;
	};
	/* Worker Thread */
	Schedule.prototype.scheduleWorker = function () 
	{
		/* 
		 thread worker has been disabled because some browsers 
		 have a lot of trouble with threading on complex pages	
		*/
		obj = window.verticalAdRotator;
		if ( obj.workQueue.length == 0 )
			{ return true; }
		for ( var i in obj.workQueue )
		{
			currentNode = obj.workQueue[i];
			replacement = obj.getNextUnLoadedNode();
			replacement.currentLoadNode = currentNode.currentLoadNode;
			currentNode.currentLoadNode = false;
			obj.parent.loadNodes[replacement.currentLoadNode] = replacement;
			try
				{ obj.parent.node.replaceChild(replacement.node,currentNode.node); } 
			catch (e)
			{
				replacement = obj.getNextUnLoadedNode();
				obj.parent.node.appendChild(replacement.node);
			}
			/* Recovery Code if there is a failure in swap */
			/*if ( obj.parent.node.childNodes.length < obj.parent.slots )
			{
				currentLoadNode = replacement.currentLoadNode;
				replacement = obj.getNextUnLoadedNode();
				replacement.currentLoadNode = currentLoadNode;
				obj.parent.node.appendChild(replacement.node);				
			}*/
			window.setTimeout("window.verticalAdRotator.scheduleNextChild(" + obj.current + ")",replacement.duration * 1000); 
			delete obj.workQueue[i];	
		}
		return true;	
	};				
	Schedule.prototype.getNextAd = function () 
	{
		this.current++;
		if ( typeof this.schedule[this.current] == "undefined" )
		{
			this.current--;
			return false; 
		}
		thisAd = this.schedule[this.current];
		return thisAd;
	};

	return new Schedule ( parent );
}


function AdGroup ( data )
{

	AdGroup.prototype.data = 			[];
	AdGroup.prototype.children = 		[];
	AdGroup.prototype.currentNodes = 	[];
	AdGroup.prototype.loadNodes = 		[];	
	AdGroup.prototype.schedule =		false;
		
	AdGroup.prototype.node = 			false;
	AdGroup.prototype.slots = 			2;
	AdGroup.prototype.host = 			false;	
		
	AdGroup.prototype.html = 			false;
	AdGroup.prototype.displayFunction = 
	{
		"mlgpro" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 2
			},
		"gameroom" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 1
			},
		"gamebattles" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 2
			},
		"gotfrag" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 1
			},
		"smashboards" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 3
			},
		"mmo-champion" : {
				display : function( obj ) { obj.defaultDisplay() },
				slots : 2
			}																				
	};
	
	function AdGroup( data ) 
	{
		this.data = data;
		this.schedule = new Schedule( this );
		this.siteInit();
		for ( var i in this.data )
		{
			if ( typeof this.data[i].sites == "undefined" )
				{ continue; }
			if ( this.data[i].sites.toUpperCase().search(this.host.toUpperCase()) == -1 ) 
			{
				delete this.data[i];
				continue;
			}
			this.addChild( new AdView( this, data[i] ) );
		}
		if ( this.data.length < this.slots )
			{ this.slots = this.data.length }		
		this.display( this );
	}
	
	AdGroup.prototype.getHost = function ()
	{
		thisHost = location.host.toLowerCase().split(".");
		for ( var i = 0; i < thisHost.length; i++ )
		{
			if ( thisHost[i] == false )
				{ return false; }
			if ( typeof this.displayFunction[thisHost[i]] != "undefined" )
				{ return thisHost[i]; }
		}
		return false;	
	};
		
	AdGroup.prototype.siteInit = function ()
	{
		this.host = this.getHost();
		this.display = this.displayFunction[this.host].display;
		this.slots = this.displayFunction[this.host].slots;
	};

	AdGroup.prototype.addChild = function ( ad ) 
	{
		this.children[this.children.length] = ad;
		this.schedule.addChild(ad);
		return true;			
	};	

	AdGroup.prototype.removeChild = function ( ad ) {};
	AdGroup.prototype.swapChild = function ( ad ) {};
	
	AdGroup.prototype.appendChildren = function ( node ) 
	{
		currentAd = this.schedule.getNextAd();
		for ( var i = 0; i < this.slots && currentAd; i++ )
		{
			node.appendChild(currentAd.node);
			currentAd.currentLoadNode = i;
			this.loadNodes[this.loadNodes.length] = currentAd;
			currentAd = this.schedule.getNextAd();
		}			
	};
	
	
	/* Specific Display Logic */	
	AdGroup.prototype.defaultDisplay = function ()
	{
		this.node = window.document.getElementById("vert-ads");
		this.appendChildren(this.node);
		this.schedule.scheduleStart();  //Starting Schedule Routine
		return true;		
	};
		
	AdGroup.prototype.gameroomDisplay = function () 
	{
		this.node = window.document.getElementById("vert-ads");
		currentAd = this.schedule.getNextAd();
		for ( var i = 0; i < this.slots && currentAd; i++ )
		{
			this.node.appendChild(currentAd.node);
			currentAd.currentLoadNode = i;
			this.loadNodes[this.loadNodes.length] = currentAd;
			currentAd = this.schedule.getNextAd();
		}
		this.schedule.scheduleStart();  //Starting Schedule Routine
		return true;					
	};
		
	return new AdGroup ( data );
}



function AdView ( parent, data ) 
{

	AdView.prototype.data = 			[];
	AdView.prototype.view = 			false;
	AdView.prototype.parent = 			false;
	AdView.prototype.node = 			false;		
		
	AdView.prototype.title = 			false;
	AdView.prototype.link = 			false;
	AdView.prototype.copy = 			false;
	AdView.prototype.image = 			false;
	AdView.prototype.duration = 		false;
	AdView.prototype.weight = 			false;
	AdView.prototype.finalWeight = 		false;
	AdView.prototype.currentLoadNode = 	false;	
	

	AdView.prototype.displayFunction = 
	{
		"mlgpro" : {
				display : function( obj ) { obj.defaultDisplay() }
			},
		"gameroom" : {
				display : function( obj ) { obj.defaultDisplay() }
			},
		"gamebattles" : {
				display : function( obj ) { obj.defaultDisplay() }
			},
		"gotfrag" : {
				display : function( obj ) { obj.defaultDisplay() }
			},
		"smashboards" : {
				display : function( obj ) { obj.defaultDisplay() }
			},
		"mmo-champion" : {
				display : function( obj ) { obj.defaultDisplay() }
			}																			
	};
		
	function AdView ( parent, data ) {
		this.parent = parent;
		this.data = data;
		this.copy = this.data.copy;
		this.title = this.data.title;
		this.link = this.data.link;
		this.image = this.data.image;
		this.duration = this.data.duration;
		this.weight = this.data.weight;
		this.finalWeight = Math.random() * this.weight;
		this.siteInit();
		this.display( this );	
	}
	
	AdView.prototype.getHost = function ()
	{
		thisHost = location.host.toLowerCase().split(".");
		for ( var i = 0; i < thisHost.length; i++ )
		{
			if ( thisHost[i] == false )
				{ return false; }
			if ( typeof this.displayFunction[thisHost[i]] != "undefined" )
				{ return thisHost[i]; }
		}

		return false;	
	};
	
	
	AdView.prototype.siteInit = function ()
	{
		host = this.getHost();
		this.display = this.displayFunction[host].display;
	};	
	
	
	/* Specific Display Logic */
	AdView.prototype.defaultDisplay = function ()
	{
		this.node = document.createElement("DIV");
		this.node.className = "vert-ad";
		this.node.id = "vert-ad";
		
		bgDiv = document.createElement("DIV");
		bgDiv.className = "bg";
		this.node.appendChild(bgDiv);
		
		link = document.createElement("A");
		link.href = this.link;
		
		if ( this.image )
		{
			padDiv = document.createElement("DIV");
			padDiv.className = "padding";
			bgDiv.appendChild(padDiv);
			
			imgDiv = document.createElement("DIV");
			imgDiv.className = "img-border";
			padDiv.appendChild(imgDiv);
			
			imgLink = link.cloneNode(true);
			imgDiv.appendChild(imgLink);
			img = document.createElement("IMG");
			img.src = this.image;
			imgLink.appendChild(img);
		}
		
		textDiv = document.createElement("DIV");
		textDiv.className = "text-content";
		bgDiv.appendChild(textDiv);
		
		hdrSpan = document.createElement("SPAN");
		hdrSpan.className = "hdr";
		hdrSpan.id = "hdr";
		textDiv.appendChild(hdrSpan);
		
		titleLink = link.cloneNode(true);
		hdrSpan.appendChild(titleLink);
		titleLink.appendChild(document.createTextNode(this.title));
		
		hdrSpan.appendChild(document.createElement("BR"));
		
		featureSpan = document.createElement("SPAN");
		featureSpan.className = "feature";
		featureSpan.appendChild(document.createTextNode(this.copy));
		hdrSpan.appendChild(featureSpan);
	};	
		

	
	return new AdView ( parent, data );
}

