function PagePeel() {
	window.PPDataSet = function() {
		this.smallImagePath;
		this.bigImagePath;
		this.linkURL;
		this.linkTarget;
		this.probability;
	}

	var PPData = function() {
		this.id;
		this.flashVars;
		this.containerId;
		this.size;
	}

	var PPSettings = function() {
		this.smallSwfPath;
		this.bigSwfPath;
		this.zIndex = 9999;
	}	

	var dataSet;
	var nDataSets = 0;
	var reAlign = false;
	var useEmbedTag = true;
	var clientSupported = true;
	var bigLoaded = false;
	var smallLoaded = false;
	var loadingComplete = false;

	this.smallData = new PPData();
	this.bigData = new PPData();

	if (!window.pagePeels) window.pagePeels = new Array();
	var ppId = window.pagePeels.length;
	window.pagePeels[ppId] = this;

	var checkClient = function() {
		if (!document.getElementById) clientSupported = false;
		else if (navigator && navigator.userAgent) {
			if (navigator.userAgent.indexOf("Netscape6") != -1
			|| navigator.userAgent.indexOf("Netscape/7") != -1
			|| (navigator.userAgent.indexOf("Mozilla") != -1 && (
			navigator.userAgent.indexOf("rv:1.0") != -1
			|| navigator.userAgent.indexOf("rv:1.1") != -1
			|| navigator.userAgent.indexOf("rv:1.2") != -1
			|| navigator.userAgent.indexOf("rv:1.3") != -1
			|| navigator.userAgent.indexOf("rv:1.4") != -1
			|| navigator.userAgent.indexOf("rv:1.5") != -1
			|| navigator.userAgent.indexOf("rv:1.6") != -1
			|| navigator.userAgent.indexOf("rv:1.7)") != -1))) clientSupported = false;
			else if (navigator.appVersion && navigator.userAgent.indexOf("Opera") != -1) {
				var lPos = navigator.appVersion.indexOf(".");
				if (lPos != -1) {
					var opVersion = parseInt(navigator.appVersion.substr(0, lPos));
					if (opVersion != NaN) {
						switch(opVersion) {
							case 5:
							case 6:
							case 7:
								clientSupported = false;
						}
					}
				}
			} else if (navigator.userAgent.indexOf("Gecko/") != -1) {
				if (window.innerHeight) {
					var lPos = navigator.userAgent.indexOf("Gecko/") + 6;
					var gVer = parseInt(navigator.userAgent.substr(lPos, 6));
					if ((gVer != NaN && gVer > 200000 && gVer < 200610)
						|| navigator.userAgent.indexOf("Firefox/1.5") != -1
						|| navigator.userAgent.indexOf("Netscape/7.") != -1
						|| navigator.userAgent.indexOf("Netscape/8.") != -1) reAlign = true;
				}
			}
		}
		if (window.ActiveXObject) {
			useEmbedTag = false;
		}
	}

	checkClient();

	var chooseDataSet = function(dataSets) {
		dataSet = dataSets[0];
		var probabTotal = 0;
		for (var i = 0; i < nDataSets; i++) probabTotal += dataSets[i].probability;
		var rndValue = Math.random() * probabTotal;
		var cumulValue = 0;
		for (var i = 0; i < nDataSets; i++) {
			cumulValue += dataSets[i].probability;
			if (rndValue < cumulValue) {
				dataSet = dataSets[i];
				break;
			}
		}
	}

	var formatURL = function(urlString) {
		if (urlString.indexOf("www.") == 0) urlString = "http://" + urlString;
		return urlString;
	}

	var getClientLang = function() {
		if (navigator) {
			if (navigator.language) return navigator.language;
			if (navigator.userLanguage) return navigator.userLanguage;
		} 
		return "";
	}

	this.setImmutableVars = function() {
		this.smallData.size = 130;
		this.bigData.size = 625;

		this.smallData.containerId = "ppSmallCont" + ppId;
		this.bigData.containerId = "ppBigCont" + ppId;

		this.smallData.id = "ppSmallSwf" + ppId;
		this.bigData.id = "ppBigSwf" + ppId;

		this.smallData.flashVars = "ppId=" + ppId;
		this.bigData.flashVars = "ppId=" + ppId;
	}

	this.setImmutableVars();

	this.setVars = function(ppSettings) {
		this.smallData.swfPath = formatURL(ppSettings.smallSwfPath);
		this.bigData.swfPath = formatURL(ppSettings.bigSwfPath);

		var connId =  Math.floor(Math.random() * 100000) + Math.floor(Math.random() * 100000);
		this.smallData.flashVars += "&" + "bigConn=" + "ppBigConn" + connId;
		this.smallData.flashVars += "&" + "smallConn=" + "ppSmallConn" + connId;
		this.smallData.flashVars += "&" + "isIE=" + (window.ActiveXObject ? "true" : "false");

		this.bigData.flashVars += "&" + "bigConn=" + "ppBigConn" + connId;
		this.bigData.flashVars += "&" + "smallConn=" + "ppSmallConn" + connId;
		this.bigData.flashVars += "&" + "clientLang=" + getClientLang();
		this.bigData.flashVars += "&" + "smallImageURL=" + formatURL(dataSet.smallImagePath);
		this.bigData.flashVars += "&" + "bigImageURL=" + formatURL(dataSet.bigImagePath);
		this.bigData.flashVars += "&" + "linkURL=" + formatURL(dataSet.linkURL);
		this.bigData.flashVars += "&" + "linkTarget=" + dataSet.linkTarget;
		this.bigData.flashVars += "&" + "isIE=" + (window.ActiveXObject ? "true" : "false");
	}

	var packSWF = function(ppData, ppSettings) {
		document.write('<div id="' + ppData.containerId + '" style="position:absolute; width:' + ppData.size + 'px; height:' + ppData.size + 'px; z-index:' + ppSettings.zIndex + '; right:0px; top:0px;">');

		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" id="' + ppData.id + '" width="' + ppData.size + '" height="' + ppData.size + '">');

		document.write(' <param name="allowScriptAccess" value="always"/>');
		document.write(' <param name="movie" value="' + ppData.swfPath + '"/>');
		document.write(' <param name="wmode" value="transparent" />');
		document.write(' <param name="quality" value="high" /> ');
		document.write(' <param name="FlashVars" value="' + ppData.flashVars + '"/>');

		if (useEmbedTag) {
			document.write(' <embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + ppData.swfPath + '" name="' + ppData.id + '" quality="high" width="' + ppData.size + '" height="' + ppData.size + '" wmode="transparent" flashVars="' + ppData.flashVars + '" allowScriptAccess="always"></embed>');
		}

		document.write('</object>');

		document.write('</div>');
	}

	var loadSWF = function(ppData) {
		document.getElementById(ppData.containerId).style.top = "-" + (ppData.size - 1) + "px";
	}

	this.settings = new PPSettings();
	this.dataSets = new Array();

	this.loadedSmall = function() {
		if (loadingComplete) return;
		smallLoaded = true;
		if (bigLoaded) {
			loadingComplete = true;
			this.showSmall();
		}
	}

	this.loadedBig = function() {
		if (loadingComplete) return;
		bigLoaded = true;
		if (smallLoaded) {
			loadingComplete = true;
			this.showSmall();
		} else if (!window.ActiveXObject) {
			this.hide();
			loadSWF(this.smallData)
		}
	}

	this.hide = function() {
		document.getElementById(this.bigData.containerId).style.top = '-1000px';
		document.getElementById(this.smallData.containerId).style.top = '-1000px';
	}

	this.showSmall = function() {
		document.getElementById(this.smallData.containerId).style.top = '0px';
		document.getElementById(this.bigData.containerId).style.top = '-1000px';
	}

	this.showBig = function() {
		document.getElementById(this.bigData.containerId).style.top = (reAlign && window.pageYOffset ? window.pageYOffset : 0) + (reAlign && window.innerHeight < this.bigData.size ? 1 : 0) + 'px';
		document.getElementById(this.smallData.containerId).style.top = '-1000px';
	}

	this.createDataSets = function(number) {
		nDataSets = number;
		for (var i = 0; i < number; i++) {
			this.dataSets[i] = new PPDataSet();
		}
	}

	this.pack = function() {
		if (!clientSupported) return;
		chooseDataSet(this.dataSets);
		this.setVars(this.settings);
		packSWF(this.bigData, this.settings);
		packSWF(this.smallData, this.settings);
		if (!window.ActiveXObject) this.hide();
		loadSWF(this.bigData);
	}
}


var peel = new PagePeel();

if (window.ActiveXObject) eval(
'function ' + peel.bigData.id + '_DoFSCommand(command, args) {'
+ '	if (command == "hide") peel.hide();'
+ '	else if (command == "showSmall") peel.showSmall();'
+ '	else if (command == "loadedBig") peel.loadedBig();'
+ '}'
+ 'function ' + peel.smallData.id + '_DoFSCommand(command, args) {'
+ '	if (command == "hide") peel.hide();'
+ '	else if (command == "showBig") peel.showBig();'
+ '	else if (command == "loadedSmall") peel.loadedSmall();'
+ '}'
);


if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language="VBScript">\n');
	document.write('On Error Resume Next\n');
	document.write('Sub ' + peel.bigData.id + '_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call ' + peel.bigData.id + '_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('Sub ' + peel.smallData.id + '_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call ' + peel.smallData.id + '_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script>\n');
}
