try{preTimer}catch(e){preTimer=10000;}
try{fakePreLoadedPercentage}catch(e){fakePreLoadedPercentage=30;}
var actPerc = 0;
var preFull = preTimer;
var pretimeout = null;
var sometime = null;
var preLang = new Array();
var flashLoadTries = 0;
var flashLoadTriesMax = 3;
var pre_load_procent = 0;
var pre_load_done = false;
preLang['whileloading'] = 'Game is being loaded';
preLang['clicktoclose'] = 'Close the advertisement and go on to the game';

function fake_pre_load_timer_temp(pTime){
	pre_load_procent = pre_load_procent + (100 / (pTime / 100));

	$j("#pre_percent").html('<span>' + Math.round(pre_load_procent) + '%</span>');
    $j("#pre_bar").css('width', Math.round(pre_load_procent * 3) + 'px');
};

function show_loaded_text(skip){
	if(preTimer < (preFull-preTimerWait) || skip==true) {
		$j("#pre_whilead").hide();
		$j("#pre_closead").show();
	}
}

function fake_pre_load_timer(pTime){
	pre_load_procent = pre_load_procent + (100 / (pTime / 100));

	if(pre_load_procent >= 100) pre_load_procent = 100;

	if(pre_load_done){
		pre_load_procent = 100;
	}

	if(pre_load_procent >= fakePreLoadedPercentage){
		show_loaded_text(true);
	}

	$j("#pre_percent").html('<span>' + Math.round(pre_load_procent) + '%</span>');
    $j("#pre_bar").css('width', Math.round(pre_load_procent * 3) + 'px');

	if((pre_load_procent < 100) && (pre_load_done == false)){setTimeout('fake_pre_load_timer(\''+pTime+'\')',100);}
	else{PreloaderCallbackAfter();}
};

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName]) {
            return document.embeds[movieName];
        }
    } else {
        return document.getElementById(movieName);
    }
};

function showGameNow(movieName, dWidth, dHeight, cbfoo) {
    clearTimeout(pretimeout);
    pretimeout = -1;
    try {
        var callback = eval(cbfoo);
        callback();
    } catch(e) {
        try {
            console.log('CANNOT CALL CALLBACK FUNCTION (after)');
        } catch(e) {}
    }
    showGameNow = function() {};
};

function check98percent(oldvalue, moviename, timer, dWidth, dHeight, cbfoo) {
    var showmoviename = moviename.substr(0, moviename.length - 3);
    var fromTimer = parseInt((preFull - timer) * 100 / preFull);
    var flashMovie = getFlashMovieObject("" + moviename);
    var loaded = flashMovie.PercentLoaded();
    if (oldvalue == loaded) {
        showGameNow(showmoviename, dWidth, dHeight, cbfoo);
        if (sometime != null) {
            clearTimeout(sometime);
            sometime = null;
        }
    }
};

function Preload(moviename, dWidth, dHeight, timer, bwidth, cbfoo) {
    var showmoviename = moviename.substr(0, moviename.length - 3);
    var fromTimer = parseInt((preFull - timer) * 100 / preFull);
    var flashMovie = getFlashMovieObject("" + moviename);
    var loaded = flashMovie.PercentLoaded();
    if (loaded < 0) loaded = 100;
    actPerc = fromTimer < loaded ? fromTimer: loaded;
    var barContainerWidth = bwidth;
    var barWidth = parseInt((actPerc * barContainerWidth) / 100);
    $j("#pre_bar").css('width', barWidth + 'px');
    $j("#pre_percent").html('<span>' + actPerc + '%</span>');
    preTimer -= 200;
    if (fromTimer >= 100) {
        sometime = setTimeout("check98percent('" + loaded + "','" + moviename + "','" + timer + "','" + dWidth + "', '" + dHeight + "', '" + cbfoo + "')", 2500);
    };
    if (actPerc < 100) {
        if (loaded >= 100) {
			show_loaded_text(false);
        }
        if (pretimeout != -1) {
            pretimeout = setTimeout("Preload('" + moviename + "','" + dWidth + "','" + dHeight + "','" + preTimer + "','" + bwidth + "','" + cbfoo + "')", 200);
        }
    } else {
        clearTimeout(pretimeout);
        pretimeout = -1;
        showGameNow(showmoviename, dWidth, dHeight, cbfoo);
    }
};

function startPreloaderIfMovieExist(mc, w, h, t, bw, cba) {
    flashLoadTries++;
    try {
        var flashMovie_temp = getFlashMovieObject(mc);
        var loaded_temp = flashMovie_temp.PercentLoaded();
        pretimeout = setTimeout("Preload('" + mc + "', '" + w + "','" + h + "','" + t + "','" + bw + "','" + cba + "')", 500);
    } catch(e) {
        if (flashLoadTries <= flashLoadTriesMax) {
            try {
            	PreloaderCallbackAfterMaxTemp();
                console.log('couldn\'t load preloader [' + flashLoadtries + ']');
            } catch(e) {}
            setTimeout("startPreloaderIfMovieExist('" + mc + "', '" + w + "','" + h + "','" + t + "','" + bw + "','" + cba + "')", 1000);
        } else {
            var mn = mc.substr(0, mc.length - 3);
            //showGameNow(mn, w, h, cba);
            try {
            	PreloaderCallbackAfterMax();
                console.log('couldn\'t load preloader after ' + flashLoadTries + ' tries.');
            } catch(e) {}
        }
    }
};

function showPreloader(flashDivId, ad, width, height, barwidth, cbfooBefore, cbfooAfter, connectionType) {
    if (cbfooBefore != null) {
        try {
            var callbackBefore = eval(cbfooBefore);
            callbackBefore();
        } catch(e) {
            try {
                console.log('CANNOT CALL CALLBACK FUNCTION (before)');
            } catch(e) {}
        }
    }
    try {
        if (js_pre_whileloading != '' && js_pre_whileloading != null && js_pre_whileloading != "undefined") {
            preLang['whileloading'] = js_pre_whileloading;
        }
        if (js_pre_click2close != '' && js_pre_click2close != null && js_pre_click2close != "undefined") {
            preLang['clicktoclose'] = js_pre_click2close;
        }
    } catch(e) {}

    var barWidth = barwidth;
    $j("#" + flashDivId).css('width', '0px');
    $j("#" + flashDivId).css('height', '0px');
    $j("#" + flashDivId).after('<div id="pre_ad"><div id="pre_loader"><div id="pre_bar">&#160;</div></div></div>');
    //$j("#pre_ad").css('width', width + 'px');
    $j("#pre_ad").css('width', '500px');
    $j("#pre_ad").css('height', height + 'px');

	//check for ajax or iframe
	//iframe or ajax (if ajax then var ad must be the ajax url)
	if(connectionType == "ajax"){
		$j("#pre_ad").prepend('<div id="pre_adcont"></div>');
		//laad de advertentie
		var ajaxManager = $j.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
		ajaxManager.add({
			success: function(htmlCode) {
				document.getElementById('pre_adcont').innerHTML = htmlCode;
			},
			data: '',
			type: 'post',
			url: ad
		});
		ajaxManager = null;
	}
    else{$j("#pre_ad").prepend('<div id="pre_adcont">' + ad + '</div>');}

    $j("#pre_ad").append('<div id="pre_c2c_container"></div>');
    $j("#pre_c2c_container").css('width', barWidth + 'px');
    $j("#pre_c2c_container").append('<div id="pre_click2close"><div id="pre_whilead">' + preLang['whileloading'] + '</div></div>');
    $j("#pre_c2c_container").append('<div id="pre_percent">&#160;</div>');
    $j("#pre_click2close").css('width', (barWidth - 35) + 'px');
    $j("#pre_percent").css('width', '35px');
    $j("#pre_c2c_container").after('<div id="pre_clear">&#160;</div>');
    $j("#pre_loader").css('width', barWidth + 'px');
    $j("#pre_click2close").append('<a id="pre_closead" href="#" onClick="showGameNow(\'' + flashDivId + '\',\'' + width + '\',\'' + height + '\',\'' + cbfooAfter + '\'); return false;" style="display: none;">' + preLang['clicktoclose'] + '</a>');

	setTimeout("startPreloaderIfMovieExist('" + flashDivId + "_mc', '" + width + "','" + height + "','" + preTimer + "','" + barWidth + "','" + cbfooAfter + "')", 1000);
};

function set_resize_game(percentage) {
	resize_game(Math.round(((percentage+50)/100)*GameWidth), Math.round(((percentage+50)/100)*GameHeight));
}

function resize_game(newWidth, newHeight) {
	$j("#game_container").css('width', parseInt(newWidth));
	$j("#game_container").css('height', parseInt(newHeight));
	$j("#game_container").css('marginLeft', parseInt("-"+Math.round(newWidth/2)));
	$j("#gamerDiv").css('height', parseInt(newHeight));

	if(newWidth>770) {
		document.getElementById('game_container').style.border = "5px solid #93add2";
	} else {
		document.getElementById('game_container').style.border = "";
	}


	//if($j.browser.mozilla)
	//{
	//	$j("#game_container").css('width', newWidth);
	//	$j("#game_container").css('height', newHeight);
	//}
	//else
	//{
	//	$j("#game_container").animate({
	//		width: newWidth,
	//		height: newHeight
	//	}
	//	);
	//}
};


function gamezoom(zoommode){
	if(zoommode == 'in') {
		zoom = $j('#game_slider').slider('value')+5;
	}

	if(zoommode == 'out') {
		zoom = $j('#game_slider').slider('value')-5;
	}

	set_resize_game(zoom);
	$j('#game_slider').slider('value', zoom);
}