/*
Copyright 2006-2007 Wellspring Technologies, LLC
All Rights Reserved
Unauthorized Use Prohibited
*/
var adDirRoot = 'ads/';
var requiredVersion = 8;

//Flash initialization for IE, writes vbscript required to check for plugin
if(navigator.appVersion.indexOf("MSIE") > 0 && navigator.appVersion.indexOf("Windows") > 0) {
	document.writeln('<scr' + 'ipt language="VBscript">');
	document.writeln('on error resume next');
	document.writeln('flashInstalled = false');
	document.writeln('flashInstalled = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + requiredVersion + '"))');
	document.writeln('</scr' + 'ipt>');
}
//Call the server-fed ad initialization routine
if(window.attachEvent) {
	window.attachEvent('onload',initializeAdArrays);
} else if(window.addEventListener) {
	window.addEventListener('load',initializeAdArrays, false);
}

//This gets called from initializeAdArrays and sets other local variables
function continueAdInitialization() {
	rotateIt();
	/*var topRight = document.getElementById('topRightAd');
	(topRight)?topRight.innerHTML = topRightAd:null;
	var right = document.getElementById('rightAds');
	(right)?right.innerHTML = rightAds:null;
	var left = document.getElementById('leftAd');
	if(left) left.innerHTML = leftAds;*/
}

function rotateIt(){
	var currKey, currAdObject, targetNode, amountToWrite, sToWrite;
	if(ads && ads.keys && ads.keys.length) {
		for(var i = 0; i < ads.keys.length; i ++) {
			currKey = ads.keys[i]
			if(ads[currKey]) {
				currAdObject = ads[currKey];
				targetNode = document.getElementById(currKey + "Ads");
				amountToWrite = (currAdObject.length >= currAdObject.count)?currAdObject.count:currAdObject.length;
				if(targetNode && currAdObject.length > 0 & amountToWrite > 0) {
					sToWrite = '';
					for(var j = 0; j < amountToWrite; j ++) {
						currAdObject.next();
						sToWrite += currAdObject[currAdObject.index].html;
						targetNode.innerHTML = sToWrite;
					}
				}
			}//valid ad group object check
		}//ad type loop
	}//valid ad object check
	var newTimeout = 12000;
	if(ads && ads.timeout) newTimeout = ads.timeout;
	setTimeout("rotateIt()", newTimeout);
}

//Ad object
function adArray() {
	for (var i = 0; i < adArray.arguments.length; i++) {
		this[i] = new Object();
		this[i].html = arguments[i];
		}
	this.length = i;
}

adArray.prototype.randomize = function() {
	var randNum = new Date();
	var adPosition;
	var divisor = (this.length > 0)?this.length:1;
	adPosition = Math.round(Math.random()*10) % divisor;
	this.index = (this.length > 0)?adPosition:0;
}

adArray.prototype.next = function() {
	if(!this.index) this.index = 0;
	this.index ++;
	if (this.index > this.length - 1){
		this.index = 0;
	}
}

function getDefaultAds(node, location) {
	if(node.src != adDirRoot + location + '/default/default.gif') {
		node.src = adDirRoot + location + '/default/default.gif';
	} else {
		node.src = '';
	}
}

function checkFlash() {
	if(navigator.appVersion.indexOf("MSIE") > 0 && navigator.appVersion.indexOf("Windows") > 0) {
		return eval("flashInstalled");
	}
	else if(navigator.plugins) {
		
		/*
		for(var i = 0; i < navigator.plugins.length; i++)
		{
			document.write(navigator.plugins[i].description + "<br>");
		}
		*/
		
		if(navigator.plugins["Shockwave Flash"]) {
			var flashDescription = navigator.plugins["Shockwave Flash"].description;
			var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
			
			if(flashVersion < requiredVersion) {
				return false;
			} else {
				return true;
			}
		}
	}
	return false;
}

function LoadFlash() {
	//this function gets called once when after the adarrays are filled
	//the first chunk shows the flash ads and hides the images in the asp generated code
	//the second chunk loops through the adarrays and replaced the appropriate
	//text to show the flash ad and hide the images
	
	//load into the asp generated ads
	var EmbedTags = document.getElementsByTagName('embed');
	for(var i = 0; i < EmbedTags.length; i++) {
		var currentParent = EmbedTags[i].parentNode;
		EmbedTags[i].style.display = 'block';
		for(var j = 0; j < currentParent.childNodes.length; j++) {
			if(currentParent.childNodes[j].id == 'altFlashImage') {
				currentParent.removeChild(currentParent.childNodes[j])
			}
		}
	}
	
	//load into the adArrays javascript will rotate
	for(var i = 0; i < ads.keys.length; i++) {
		var currentKey = ads.keys[i];
		for(var j = 0; j < ads[currentKey].length; j++) {
			if(ads[currentKey][j].html.indexOf('embed') > 0) {
				ads[currentKey][j].html = ads[currentKey][j].html.replace(/:.*none.*;/gi, ': block;');
				ads[currentKey][j].html = ads[currentKey][j].html.replace(/<div id=\"?altFlashImage\"?.*<\/a><\/div>/gi, '');
			}
		}
	}
	
}

