// preloader init values
var halt                = true;
var gameSO              = null;
var beaconDiv			= 'beacon';

// static preloader vars
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;

var preloadNumbers = 3;	
var staticPreloaderNumbers = 6;
var staticAdDiv = 'ad_static_preloader';

function launchGameAfterAd() {
	
	var staticAdDiv = 'ad_static_preloader';

	if(advDiv = document.getElementById(gamePreloaderDiv)){
		advDiv.style.display = 'none';			
	}
	if(advMsg = document.getElementById(gamePreloaderMsgDiv)) {
		advMsg.style.display = 'none';
	}
	if(sAdDiv = document.getElementById(staticAdDiv)){
		sAdDiv.style.display = 'none';
	}
	document.body.style.backgroundImage = 'none'; // remove the background
	document.body.style.backgroundColor = 'transparent';
		
	//display game
	if(gameDiv = document.getElementById(gameContainerDiv)){
		gameDiv.style.display = "block";
	}
	
}
function initializeTimer()
{
    secs = 5;
    startTheTimer();
}
function startTheTimer() {
    if (secs == 0) {
    	launchGameAfterAd();
    } else {
    	defaultStatus = "Your game will load in: " + secs + " seconds";
    	secs = secs - 1;
    	timerRunning = true;
    	timerID = self.setTimeout("startTheTimer()", delay);
	}
}

function showStaticAd(){
	if (saDiv = document.getElementById(staticAdDiv)) {		
		saDiv.style.display="block";
	}
}

function show_preloader() {
	// make a new cookie
	var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var oneCookie = allCookies[i].split('=');
			this[oneCookie[0]] = oneCookie[1];
		}
	},
	create: function (name,value,hours) {
		if (hours) {
			var date = new Date();
			date.setTime(date.getTime()+(hours*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
	};

	Cookies.init();

	// get the video cookie
	if(Cookies.gamePreloader){
		ccount = new Number(Cookies.gamePreloader);		
		if(ccount <= preloadNumbers) {
		ccount+=1;		
		Cookies.erase("gamePreloader");
		Cookies.create("gamePreloader",ccount,24);
		}
	} else {
		Cookies.create("gamePreloader",1,24);
	}

	// get the static ad cookie
	if (Cookies.gamePreloaderStatic) {
		sccount = new Number(Cookies.gamePreloaderStatic);
		if (sccount <= staticPreloaderNumbers) {
			sccount += 1;
			Cookies.erase("gamePreloaderStatic");
			Cookies.create("gamePreloaderStatic", sccount, 24);
		}
	} else {
		Cookies.create("gamePreloaderStatic", 1, 24);
	}
	
	if(halt && Cookies.gamePreloader && new Number(Cookies.gamePreloader)<(preloadNumbers+1)) {
		if(gameCon = document.getElementById(gameContainerDiv)) {
			gameCon.style.display = 'none';
		}
		
		var advertismentSO = new SWFObject(gamePreloaderSwf, "AdLoader", "640", "480", "9", "#ffffff");
	
		advertismentSO.addParam("allowScriptAccess", "always");
		advertismentSO.addParam("play", "true");
		advertismentSO.addParam("quality", "high");
		advertismentSO.addParam("loop", "false");
		advertismentSO.addParam("wmode", "opaque");
		
		advertismentSO.addParam("flashVars", "dartSite="+dartSite+"&gameName="+gameName+"&rugrat="+rugrat+"&adComplete=launchGameAfterAd&mode=mode.live&yumeURL=http://images.neopets.com/ads/preloader/plugins/yumesdk3/");
		
		advertismentSO.write(gamePreloaderDiv);
		
		// change the background of the body
		document.body.style.backgroundImage = 'url(http://images.neopets.com/games/video_greenbg.jpg)';
		
		// drop a beacon to track
		if (imgBeacon = document.getElementById(beaconDiv)) {
			var cache_buster = Math.random();
			imgBeacon.innerHTML = '<img src="http://www.neopets.com/process_click.phtml?item_id=12923&beacon=1&status=10&type_id=12&nc_multiple=97&ord=' + cache_buster + '" widht="0" height="0" />';
		}
	} else if (Cookies.gamePreloaderStatic) {
		// hide everything
		if(gameCon = document.getElementById(gameContainerDiv)) {
			gameCon.style.display = 'none';
		}
		if(preDiv = document.getElementById(gamePreloaderDiv)) {
			preDiv.style.display = 'none';
		}
		if(advMsg = document.getElementById(gamePreloaderMsgDiv)) {
			advMsg.style.display = 'none';
		}
		// .. except the static div
		document.body.style.background = 'url(http://images.neopets.com/ads/game_preloader_bg.jpg) no-repeat 0 90px #B9ACCC';
		showStaticAd();
		initializeTimer();
		
	} else { 
		if(gameCon = document.getElementById(gameContainerDiv)) {
			gameCon.style.display = 'block';
		}
		if(preDiv = document.getElementById(gamePreloaderDiv)) {
			preDiv.style.display = 'none';
		}
		if(advMsg = document.getElementById(gamePreloaderMsgDiv)) {
			advMsg.style.display = 'none';
		}
	}
	
} 

