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 preTimer = 20000;
var actPerc = 0;
var preFull = 20000;
var spillpretimeout = null;
var sometime = null; // 98 timer

var spillpreLang = new Array();
spillpreLang['whileloading'] = 'Game is being loaded';
spillpreLang['clicktoclose'] = 'Close the advertisement and go on to the game';


function showSpillGameNow(movieName, dWidth, dHeight, cbfoo) {
    $("#spillpre_ad").hide();
    $("#"+movieName).css('width', dWidth+'px');
    $("#"+movieName).css('height', dHeight+'px');
    clearTimeout(spillpretimeout);
    spillpretimeout = -1;
    try {
        var callback = eval(cbfoo);
        callback();
    } catch(e) {
        try {
            console.log('CANNOT CALL CALLBACK FUNCTION (after)');
        } catch(e){}
    }
    showSpillGameNow = function(){}; // workaround (to be sure callback function is called only once)
};

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){
			showSpillGameNow(showmoviename, dWidth, dHeight, cbfoo);
			if (sometime != null) {
				clearTimeout(sometime);
				sometime = null;
			}
		}
};

function SpillPreload(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);
		$("#spillpre_bar").css('width', barWidth + 'px');
		$("#spillpre_percent").html('<span>' + actPerc + '%</span>');
		
		preTimer -= 200;
		
		// 98%
		if (fromTimer >= 100){
			sometime = setTimeout("check98percent('"+loaded+"','" + moviename + "','"+timer+"','"+dWidth+"', '"+dHeight+"', '"+cbfoo+"')",2500);
		};
		//
		if (actPerc < 100) {
			if (loaded >= 100) {
				//$("#spillpre_click2close").html('<a style="display: block;" href="#" onClick="showSpillGameNow(\''+showmoviename+'\',\''+dWidth+'\',\''+dHeight+'\',\''+cbfoo+'\');">'+spillpreLang['clicktoclose']+'</a>');
				$("#spillpre_whilead").hide();
				$("#spillpre_closead").show();
			}
			if (spillpretimeout != -1) {
				spillpretimeout = setTimeout("SpillPreload('" + moviename + "','" + dWidth + "','" + dHeight + "','" + preTimer + "','" + bwidth + "','" + cbfoo + "')", 200);
			}
		}
		else {
			clearTimeout(spillpretimeout);
			spillpretimeout = -1;
			showSpillGameNow(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();
        spillpretimeout = setTimeout("SpillPreload('"+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);
            showSpillGameNow(mn, w, h, cba);
            try {
                console.log('couldn\'t load preloader after '+flashLoadTries+' tries.');
            } catch(e){}
        }
    }
};


function showSpillPreloader(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_spillpre_whileloading != '' && js_spillpre_whileloading != null && js_spillpre_whileloading != "undefined" ) {
            spillpreLang['whileloading'] = js_spillpre_whileloading;
        }
        if( js_spillpre_click2close != '' && js_spillpre_click2close != null && js_spillpre_click2close != "undefined" ) {
            spillpreLang['clicktoclose'] = js_spillpre_click2close;
        }
    } catch (e) {}
        
    var barWidth = barwidth;
    $("#"+flashDivId).css('width', '0px');
    $("#"+flashDivId).css('height', '0px');
    $("#"+flashDivId).after('<div id="spillpre_ad"><div id="spillpre_loader"><div id="spillpre_bar">&#160;</div></div></div>');
    $("#spillpre_ad").css('width', width+'px');
    $("#spillpre_ad").css('height', height+'px');
    $("#spillpre_ad").prepend('<div id="spillpre_adcont">'+ad+'</div>');
    $("#spillpre_ad").append('<div id="spillpre_c2c_container"></div>');
    $("#spillpre_c2c_container").css('width', barWidth+'px');
    //$("#spillpre_c2c_container").append('<div id="spillpre_click2close">'+spillpreLang['whileloading']+'</div>');
    $("#spillpre_c2c_container").append('<div id="spillpre_click2close"><div id="spillpre_whilead">'+spillpreLang['whileloading']+'</div></div>');
    $("#spillpre_c2c_container").append('<div id="spillpre_percent">&#160;</div>');
    $("#spillpre_click2close").css('width', (barWidth-35)+'px');
    $("#spillpre_percent").css('width', '35px');
    $("#spillpre_c2c_container").after('<div id="spillpre_clear">&#160;</div>');
    $("#spillpre_loader").css('width', barWidth+'px'); 
    
    $("#spillpre_click2close").append('<a id="spillpre_closead" href="#" onClick="showSpillGameNow(\''+flashDivId+'\',\''+width+'\',\''+height+'\',\''+cbfooAfter+'\'); return false;" style="display: none;">'+spillpreLang['clicktoclose']+'</a>');
   
    //spillpretimeout = setTimeout("SpillPreload('"+flashDivId+"_mc', '"+width+"','"+height+"','"+preTimer+"','"+barWidth+"','"+cbfooAfter+"')", 500);
    setTimeout("startPreloaderIfMovieExist('"+flashDivId+"_mc', '"+width+"','"+height+"','"+preTimer+"','"+barWidth+"','"+cbfooAfter+"')", 1000);
};
