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); }
};

var pretimeout = null;
var sometime = null;
var preLang = new Array();
preLang['whileloading'] = 'Game is being loaded';
preLang['clicktoclose'] = 'Close the advertisement and go on to the game';

function showGameNow(movieName, dWidth, dHeight, cbfoo) {
    $j("#pre_loader").hide();
    $j("#pre_c2c_container").hide();
    $j("#" + movieName).css('width', dWidth + 'px');
    $j("#" + movieName).css('height', dHeight + 'px');
    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) { $j("#pre_whilead").hide(); $j("#pre_closead").show(); }
        if (pretimeout != -1) {
            pretimeout = setTimeout("Preload('" + moviename + "','" + dWidth + "','" + dHeight + "','" + preTimer + "','" + bwidth + "','" + cbfoo + "')", 200);
        }
    }
    else {
        clearTimeout(pretimeout); pretimeout = -1;
        showGameNow(showmoviename, dWidth, dHeight, cbfoo);
    }
};

var flashLoadTries = 0; var flashLoadTriesMax = 3;
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 { 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 { console.log('couldn\'t load preloader after ' + flashLoadTries + ' tries.'); }
            catch (e) { }
        }
    }
};
function showPreloader(flashDivId, ad, width, height, barwidth, cbfooBefore, cbfooAfter) {
    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_loader"><div id="pre_bar">&#160;</div></div>');
    $j("#pre_loader").after('<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_percent").css('float', 'left');    
    $j("#pre_c2c_container").after('<div id="pre_clear">&#160;</div>');
    $j("#pre_loader").css('width', barWidth + 'px');

    if (preTimer <= 10000) {
        $j("#pre_click2close").append('<a id="pre_closead" href="#" onClick="showGameNow(\'' + flashDivId + '\',\'' + width + '\',\'' + height + '\',\'' + cbfooAfter + '\'); return false;" style="display: none;">' + preLang['clicktoclose'] + '</a>');
    } else {
        $j("#pre_click2close").append('<font id="pre_closead" style="display: none;">' + preLang['whileloading'] + '</font>');
    }

    setTimeout("startPreloaderIfMovieExist('" + flashDivId + "_mc', '" + width + "','" + height + "','" + preTimer + "','" + barWidth + "','" + cbfooAfter + "')", 1000);
};

