Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

How to make javascript integration works

16 replies [Last post]

Hi' this is great player, but why seems to difficult to make javascript integration works.
this is my code:

<script type="text/javascript">

var so = new SWFObject('player.swf','jstest','400','220','8');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen','true');
  so.addVariable('width','400');
  so.addVariable('height','220');
  so.addVariable('file','video.flv');
  so.addVariable('javascriptid','jstest');
  so.addVariable('enablejs','true');
  so.write('container');
 
 
function sendEvent(swf,typ,prm) {
  thisMovie(swf).sendEvent(typ,prm);
};

function getUpdate(typ,pr1,pr2,swf) {};

function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};

function getUpdate(typ,pr1,pr2,swf) {
  if(typ == 'state') {
  alert('the current state is: '+pr1+');');
  }
};

function loadFile(swf,obj) {
  thisMovie(swf).loadFile(obj);
};
</script>

Call it with this:
<a href="javascript:loadFile('jstest',{file:'/ironman.flv'})">load a single file</a>

Why it's not working? what part did i miss?

Thanks a lot..

Sorry, forgot to tell. This is error that i always got until now:
thisMovie(swf).loadFile is not a function

i also already try this code, but the problem it's same, why?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

  <head>

    <title>JW Player</title>

    <script type="text/javascript" src="swfobject.js"></script>

    <script type="text/javascript">
      function sendEvent(swf, typ, prm)
      {
        thisMovie(swf).sendEvent(typ, prm);
      };

      function loadFile(swf,obj)
      {
        thisMovie(swf).loadFile(obj);
      };

      function getUpdate(typ, pr1, pr2, swf)
      {
        if(typ == 'state' && swf == 'jstest')
        {
          gid('stateshow').innerHTML = pr1;
        }
      };

      function itemData(swf, idx)
      {
        var obj = thisMovie(swf).itemData(idx);
        var txt = '';
        for(var j in obj)
        {
          txt += j + ': ' + obj[j] + '\n';
        }
        alert(txt);
      };

      function gid(name)
      {
        return document.getElementById(name);
      };

      function thisMovie(movieName)
      {
        return document.getElementsByName(movieName)[0];
      };

      function createPlayer()
      {
        var so = new SWFObject('mediaplayer.swf', 'jstest', '400', '220', '8');
            so.addParam('allowscriptaccess',    'always');
            so.addParam('allowfullscreen',      'true');
so.addParam('id', 'jstest');
            so.addVariable('width',             '400');
            so.addVariable('height',            '220');
            so.addVariable('file',              'video.flv');           
            so.addVariable('javascriptid',      'jstest');
            so.addVariable('enablejs',          'true');
            so.write('container');
      };
    </script>

  </head>

  <body onload="createPlayer();">

    <div id="container">
      This text will be replaced
    </div>
    <br />   
    <a href="#" onclick="loadFile('jstest', {file:'ironman.flv'}); sendEvent('jstest', 'playpause'); return false;">Load Playlist</a>
    <br />
    <a href="#" onclick="sendEvent('jstest', 'playpause'); return false;">Play / Pause</a>

  </body>

</html>

Error: thisMovie(swf).loadFile is not a function

Can anyone help me what wrong with this?
Thanks...

@Hudoq,

Which version of the player are you using?

Some of your code has player.swf which usually implies a v4.x player and some of your code has mediaplayer.swf which implies a v3.x player.

The JavaScript API (and the flashvars) is very different for the different versions of the player. See the documentation for the v4.x player API here: http://code.longtailvideo.com/trac/

@Hudoq,

Here's tested and working code for a v4.1.54 player to get you started:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

  <head>

    <title>Hudoq</title>

    <script type="text/javascript" src="swfobject.js"></script>

    <script type="text/javascript">
      function createPlayer()
      {
        var so = new SWFObject('player.swf', 'jstest', '400', '220', '9.0.124');
            so.addParam('allowscriptaccess', 'always');
            so.addParam('allowfullscreen',   'true');
            so.addVariable('file',           'video.flv');
            so.write('player');
      };

      var player = null;

      function playerReady(obj)
      {
        player = document.getElementsByName(obj.id)[0];
      };

      function loadNplay(file)
      {
        player.sendEvent('LOAD', {file:file, type:'video'});
        player.sendEvent('PLAY', 'true');
      };
    </script>

  </head>

  <body onload="createPlayer();">

    <div id="playercontainer" class="playercontainer">
      <a id="player" class="player" href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Get the Adobe Flash Player to see this video.</a>
    </div>

    <a href="#" onclick="loadNplay('ironman.flv')">LOAD & PLAY a single file</a>

  </body>

</html>

@kLink,

Thanks for your answer, i already try your code but still had that problems, i already download for v4.0.46.
This is debug result with firebug:
player has no properties
player.sendEvent('LOAD', {file:file, type:'video'});

i'm using firefox 2.0.0.16 and IE 6, what's wrong with this? what url if i want to get v4.1.54.

Thanks again...

@Hudoq,

Just tested in FF2, it's OK.

Try from here: http://willswonders.myip.org:8085/php/Hudoq.html then you will have player-4.1.54.swf in your browser's cache.

Keep up to date by installing a Subversion client. Details here: http://code.longtailvideo.com/trac/

@kLink,

Just tested in FF2, it's OK..
How? i do exactly copy-paste your code but still had that problems, player still have no properties. am i miss something in here? i already download the latest version now, in http://code.longtailvideo.com/svn/trunk/as3, which is v4.1.55 but still had that problems. Can u help me more?

Thanks a lot...

@Hudoq,

Can you post a link to your test page?

Did you try my test page?

@kLink,

Can you post a link to your test page?
Right now, Sorry i can't, i don't have any access for that.

Did you try my test page? (http://willswonders.myip.org:8085/php/Hudoq.html)
Yes i try to open it, but seems your page can't be access.

I try to modify your code:

............................
function playerReady(obj){
   player = document.getElementsByName(obj)[0];
};
.............................
<a href="#" onclick="playerReady('jstest');">Make sure player set</a><br/>
<a href="#" onclick="loadNplay('ironman.flv')">LOAD & PLAY a single file</a>
..............................

I had different problems, says :
player.sendEvent is not a function
player.sendEvent('LOAD', {file:file, type:'video'});

Now it says "is not a function", before says "has no properties"

Thanks...

@Hudoq,

Are you testing this online (from a server)?

Because of Adobe Flash Player security restrictions, the JavaScript API only works online.

I just checked my link to the test page at: http://willswonders.myip.org:8085/php/Hudoq.html — and no problems.

@kLink,

It works right now..
Really Thanks for great support it's totally my mistake, the problems came from security restrictions, when i try it online it works perfectly...

Thanks a lot for great support...

@Hudoq,

You're welcome. I glad to hear that you got it working.

finnkle.proboards55.com

i have a windows vista from toshiba, and i cant open any programs it keeps showing the plug in icon and keeps telling me that i have javascript turned off or the old version of adoby reader in which i have already up dated !!!!! please fix my problem .

// ==UserScript==
// @author usr8472@gmail.com
// @name Travian: Resource++ v3 الموارد بالعربي - ترفيان
// @namespace http://userscripts.org/
// @description Travian resource summary, overflow of resources and timer, and resource per build item. Rewrite of original with HTML DOM methods instead of innerHTML. Thanks to everyone who sent translations. Works in Opera.
// @include http://*.travian.*php*
// @include http://*.travian3.*php*
// @exclude http://forum.travian.*
// @exclude http://*.travian.*/index.php*
// @exclude http://*.travian.*/anleitung.php*
// @exclude http://*.travian.*/login.php*
// @exclude http://*.travian.*/logout.php*
// @exclude http://*.travian.*/chat.php*
// @exclude http://*.travian.*/impressum.php*
// @exclude http://*.travian.*/karte2.php*
// @exclude http://www.travian.*
// @exclude http://forum.travian3.*
// @exclude http://*.travian3.*/index.php*
// @exclude http://*.travian3.*/anleitung.php*
// @exclude http://*.travian3.*/login.php*
// @exclude http://*.travian3.*/logout.php*
// @exclude http://*.travian3.*/chat.php*
// @exclude http://*.travian3.*/impressum.php*
// @exclude http://*.travian3.*/karte2.php*
// @exclude http://www.travian.*
// ==/UserScript==

var eventSource= (navigator.appName.indexOf== 'Opera') ? document : window; // I use this because I might want to make it cross browser later
eventSource.addEventListener( 'load', function( e ) { onLoad(); } ,false); //to be run on load of the page

var loc=window.location.href; // the current page href
var lang;

var order=0;// what is the order of the resources lumber to crop or crop to lumber
var fields=[]; //setup the space for the resource names
var langfile=[]//multi lang support, the space for the different translations
var resource; //setup the space for the resource data
var overflow;//setup the space for the overflow data
var autotime; // the timeout control, to update the countdown of overflow
var pagetime; //the time on the page server
var military=true; // is the pagetime 24 or 12 based clock

function onLoad(){ // runs the differnt functions and procedures,

lang=loc.match(/travian(\.[a-zA-Z]{2,3})+/ );
if(!lang) {
lang = loc.match(/travian3(\.[a-zA-Z]{2,3})+/ ).pop(); }
else {
lang=loc.match(/travian(\.[a-zA-Z]{2,3})+/ ).pop();
}

if(!lang) return;

fields[lang]=gatherFields(); // gets the names of the resources
if(!fields[lang]) return; //if you can't find the names of the resources you don't need to run this script, return to stop the script

switch(lang){//load only the lang you need into memory
case '.se':
langfile[lang]=['Behöver för uppgradering:','överfullt om','förbruka om','Resurs Meters','Resurs Bars',
' ','Tillräckligt med råvaror idag klockan ',"Tillräckligt med råvaror imorgon klockan ", "Tillräckligt med råvaror om ",
" dagar, kl " , " Efter max väntan: ", "Överskott: ","Vänta: ","Saknar","Klar"," Vänta: " ,"Blanserad",
"Förbruka överskott om: ","Uppgradera ", " resurs produktion.","Neutral balans.", "Bygg ut magasin", "Bygg ut silo"];
break;
case '.uk':
langfile[lang]=['Needed for upgrading:','Overflow', 'Depleted','Resource Meters','Resource Bars',
' Clock' ,'Enough resources today at ',"Enough resources tomorrow at ", "Enough resources in ",
" days at " , " After max wait: " , "Storage: ","Wait time: ","Deficit" ,"Surplus"," Max wait: ","Balanced",
"Surplus depleted in: ","Upgrade "," resource production.","Neutral balance.", "Extend Warehouse" , "Extend Granary"];
break;

case '.ae':
langfile[lang]=['المـطلوب للإرتـقاء','سـيـفيـض بعد', 'يـستـنـفـذ','مقيــاس المــوارد','مـقيـاس المـوارد بالنـِسـب',
' ســاعة' ,'تكتــمال المـوارد اليــوم في ',"سـتـكتــمال المـوارد غـداً في ", "اكتـمال المـوارد في ",
" أيـام في " , " بـعد اقصى مـدة : " , "المخـزون : ","مـدة الإنـتـظار ","لديـك عـجـز بـمقـدار " ,"الـبـاقـي بعد الإرتـقـاء"," أقصـى مـدة ","لن يـبـقى في المخـزن شـيـئ",
"تـقـلـص الـفائـض : ","إرتـقاء "," إنـتـاج المــوارد" ," محايد ", "تـوسـيـع مـخازن السلـع" , "تـوسـيـع مـخازن الحبـوب"];
break;

case '.com':
langfile[lang]=['Needed for upgrading:','Overflow', 'Depleted','Resource Meters','Resource Bars',
" o'clock",'Enough resources today at ',"Enough resources tomorrow at ", "Enough resources in ",
" days at ", " After max wait: " , "Storage: ","Wait time: ","Deficit","Surplus", " Max wait: ","Balanced",
"Surplus depleted in: ","Upgrade "," resource production.","Neutral balance." , "Extend Warehouse" , "Extend Granary" ];
break;
case '.hu':
langfile[lang]=[ 'Fejlesztéshez kell:', 'betelik', 'betelt', 'Nyersanyagok' , 'Nyersanyagok',
' órakor' ,'Elég nyersanyag ma ',"Elég nyersanyag holnap ", "Elég nyersanyag ",
" nap múlva " , " Max várakozás után: " , "Készleten: ","Várj: ","Hiány" ,"Többlet"," Max várakozás: ","Kiegyensúlyozott",
"A többlet elfogy: ","Fejlesztés "," nyersanyag termelés.","Semleges egyensúly.", "Fejlesztd hozzá a raktárat!" , "Fejlesztd hozzá a magtárat!" ];
break;
case '.de':
langfile[lang]=['Für Ausbau benoetigt:','Überschuß', 'Erschöpft','Resourcenmeter','Resourcenbalken',
' Uhr','Genug Resourcen heute um ',"Genug Resourcen morgen um ", "Genug Resourcen in ",
" Tagen um ", " Nach maximaler Wartezeit: " , "Vorrat: ","Wartezeit: ","Defizit","Überschuß", " Maximale Wartezeit: ","Ausgeglichen",
"Überschuß aufgebraucht in: ","Ausbau "," Resourcenproduktion.","Neutrale Bilanz." , "Rohstofflager erweitern" , "Kornspeicher erweitern" ];
break
case '.sk':
langfile[lang]=['Stavba vyžaduje:','pretečie', 'vyčerpané','Merač surovín','Ukazatele zásob',
' hod','Dostatok surovín dnes o ',"Dostatok surovín zajtra o ", "Dostatok surovín za ",
" dní o ", " Po max. čakaní: " , "Sklad: ","Čakacia doba: ","Chýba nám","Prebytok", " Max. doba čakania: ","Vyvážené",
"Prebytok vyčerpaný za: ","Vylepši "," produkciu surovín.","Vyváženosť." , "Rozšír sklad" , "Rozšír sýpku"];
break;
case '.cz':
langfile[lang]=['Stavba vyžaduje:','přeteče', 'vyčerpáno','Merič surovin','Ukazatele zásob',
' hod','Dostatek surovin dnes v ',"Dostatek surovin zítra v ", "Dostatek surovin za ",
" dny v ", " Po max. vyčkávaní: " , "Sklad: ","Čekací doba: ","Chybí nám","Přebytek", " Max. vyčkávaní: ","Vyvážené",
"Přebytek vyčerpaný za: ","Vylepši "," produkci surovín.","Vyváženost."
, "Vylepši sklad" , "Vylepši sýpku"];
break;
case '.bg':
langfile[lang]=['Нужни за upgrade:','запълване', 'изчерпване','Resource Meters','Графики на ресурсите',
' часа','Достатъчно ресурси днес в ',"Достатъчно ресурси утре в", "Достатъчно ресурси в ", " дни в ", " След максимално изакване: " ,
"Склад: ","Време заизчакване: ","Недостиг","Излишък", " Максимално изчакване: ","Балансирано", "Изчерпване на излишъка след: ","Upgrade ",
" Добив наресурси.","Neutral balance." , "Разширете склада" , "Разширете хамбара"];
break;
default:
langfile[lang]=['Needed for upgrading:','Overflow', 'Depleted','Resource Meters','Resource Bars',
' Clock' ,'Enough resources today at ',"Enough resources tomorrow at ", "Enough resources in ",
" days at " , " After max wait: " , "Storage: ","Wait time: ","Deficit" ,"Surplus"," Max wait: ","Balanced",
"Surplus depleted in: ","Upgrade "," resource production.","Neutral balance.", "Extend Warehouse" , "Extend Granary"];
break;
}

//if somehow the lang file is not in the script, return because it can't display the information correctly
if(!langfile[lang]) return;

resource=getResourceInfo(); // gathers the current store, capacity and production of each resource
if(!resource) return; // if no resources exist on the page, return to stop the script
overflow=calOverflow(); // computes the overflow information using resource information
if(!overflow) return; //if overflow can't be computed, return to stop the script

if(loc.indexOf('build')!=-1){
getTime();
resourcePerBuild();
}

var tme=menu();//make the menu
if(!tme) return; // if we can't make the menu return
addElement( tme ); // add the menu to the page
if(!returnObjById('blocktimer')) return; //if we can't find the added menu return
autotime = window.setTimeout(countdown, 1000); // start the timeout

}

function rowOpera(i,leftval){
var tol= (leftval<50) ? 150 : 255;
var sec=155-leftval;

var testmain = document.createElement( 'div' );
var value = document.createElement( 'div' );
var bar = document.createElement( 'table' );
var row= document.createElement( 'tr' );
var left= document.createElement( 'td' );
var right= document.createElement( 'td' );
var simg = document.createElement( 'img' );

testmain.style.width='500px';
testmain.style.height='16px';

simg.src="img/un/r/"+(i+1)+".gif";
simg.className='res';
simg.style.cssFloat="left";
simg.title=fields[lang][i];
simg.alt=fields[lang][i];

bar.style.cssFloat="left";
bar.style.width='442px';
bar.style.height='16px';
bar.style.backgroundColor="white"
bar.setAttribute("cellpadding","1");
bar.setAttribute("cellspacing","0");

left.id=fields[lang][i]+"left";
right.id=fields[lang][i]+"right";
value.id=fields[lang][i]+"value";

left.style.width=leftval+'%';
left.style.backgroundColor=rgb2HEX(sec, sec, parseInt(tol*(leftval/100)) );
right.style.width=(100-leftval)+'%';

if(leftval>=90){
value.setAttribute("style","text-decoration:blink;");
value.style.color='red';
} else {
value.setAttribute("style","text-decoration:none;");
}

value.style.width='40px';
value.appendChild( document.createTextNode( leftval+"%" ) );
value.style.cssFloat="left"
value.title=leftval;

row.appendChild(left);
row.appendChild(right);
bar.appendChild(row);

testmain.appendChild( simg );
testmain.appendChild( value );
testmain.appendChild( bar );

testmain.id='resbar'+i;

return testmain
}

function returnObjById( id ){ //compatibility, gets the object by id, based on different returns of document
if (document.getElementById)
return document.getElementById(id);
else if (document.all)
return document.all[id];
else if (document.layers)
return document.layers[id];
}

function getElementsByClassName(oElm, strTagName, strClassName){ // searches the oElm for strTagName objects with strClassName class
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);

var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}

function addElement(theElement) { // adds an element to the page either by the center object lmidlc or appends to html
var htmldoc=returnObjById("lmid2");

if(!htmldoc) {
htmldoc=document.getElementsByTagName('body')[0];
if(!htmldoc) return;
theElement.style.marginLeft="130px";
theElement.style.marginBottom="10px";
theElement.style.width="550px";

if(loc.indexOf('dorf2')!=-1){
theElement.style.top="150px";}
else {
theElement.style.top="50px";}
} else {
theElement.style.width = "500px";
if(loc.indexOf('karte.php?d=')!=-1){
theElement.style.top="428px";
}
}

htmldoc.appendChild(theElement);
}

function formatTime(maxtime, hours, minutes, seconds, off){ // given maxtime in secs and and offset values, returns a array of [hrs,min,sec]
return [Math.floor(maxtime/3600)+hours+off,(Math.floor(maxtime/60)%60)+minutes,(maxtime % 60)+seconds];
}

function gatherFields(){//gathers the name of each resource
var orgbar=returnObjById('lres0');
if(!orgbar) {orgbar=getElementsByClassName(document, 'div', 'div4')[0];}
if(!orgbar) {orgbar=returnObjById('lres');}
if(!orgbar) return;
var resbar=orgbar.getElementsByTagName('img');
var ret=fields[lang]=[resbar[0].title,resbar[1].title,resbar[2].title,resbar[3].title]
var idbar=orgbar.getElementsByTagName('td');
if(idbar[1].id.indexOf('1')==-1) order=1;
return ret;
}

function rgb2HEX(red, green, blue){ // given red green blue values return their hexcode
var decColor = red + (256 * green) + (65536 * blue);//offset each value and create a new int

decColor=decColor.toString(16);//convert to a base16 string

while( decColor.length < 6){//append 0 till it is a 6 length string
decColor="0"+decColor;
}

return "#"+decColor;
}

function formatTimeString(maxtime){ // maxtime in seconds returns it in hh:mm:ss h format
var helper=formatTime(maxtime, 0, 0, 0, 0);
if(helper[1] < 10){helper[1] = "0"+helper[1];}
if(helper[2] < 10){helper[2] = "0"+helper[2];}
return helper[0]+':'+helper[1]+':'+helper[2]+' h';
}

function getResourceInfo(){ //using the resource values on the page, return the production, store, and capacity values as an array
var resource=new Array();
for(var i=1;i<=4;i++) {
var rtd = returnObjById("l"+i);
if(!rtd) return;
resource.push( [parseInt(rtd.title),parseInt(rtd.textContent.match(/\-?(\d+)\//)),parseInt(rtd.textContent.replace(/(\d+)\//,""))] );
}

if(!order) { //if the order is 0 return the array as is, if not reverse the array
return resource;
} else {
return resource.reverse();
}

}

function calOverflow(){ //using the resource values, return the overflow information as an array
var sec=[];

for(var i=0;i<4;i++) {
if(resource[i][0]>0){
sec[i] = [ Math.ceil(3600*(resource[i][2]-resource[i][1])/resource[i][0]),langfile[lang][1]];
} else if(resource[i][0]<0){
sec[i] = [ Math.ceil(3600*(-resource[i][1])/resource[i][0]), langfile[lang][2] ];
} else {
sec[i]=[ 0, langfile[lang][16] ];
}
}

return sec;
}

function menu(){ //create an resource menu
var mymenu = document.createElement( 'table' );
mymenu.id="blocktimer";
mymenu.setAttribute('style', 'z-index:5;');
mymenu.setAttribute('cellspacing', 1);
mymenu.setAttribute('cellpadding', 1);
mymenu.className = "tbg";
mymenu.style.marginTop="36px";
mymenu.style.clear = "both";
mymenu.style.position="relative";

var row =document.createElement("tr");
var cell = document.createElement("td");
cell.setAttribute("colspan",2);
cell.setAttribute('align', 'center');
cell.appendChild( document.createTextNode( langfile[lang][3] ) );
cell.className = "rbg";
row.appendChild( cell );

mymenu.appendChild(row);

for(var i=0; i<4; i++){
row =document.createElement("tr");

for(var j=0; j<2; j++){
var c=i+j;
cell = document.createElement("td");
var inSpan = document.createElement("span");

var newText = document.createTextNode( fields[lang][c]+" "+overflow[c][1]+": " );
cell.appendChild(newText);
cell.setAttribute('align', 'center');
inSpan.id=fields[lang][c]+"timer";
inSpan.style.color= (overflow[c][0]<300) ? "red" : "green";
newText = document.createTextNode(formatTimeString(overflow[c][0]));
inSpan.appendChild(newText);
cell.appendChild(inSpan);
row.appendChild( cell);
}

i++;

mymenu.appendChild( row);
}

row = document.createElement("tr");
cell = document.createElement("td");
cell.setAttribute("colspan",2);
cell.setAttribute('align', 'center');
cell.appendChild( document.createTextNode( langfile[lang][4] ) );
cell.className = "rbg";
row.appendChild( cell );
mymenu.appendChild(row);

for(var i=0; i<4; i++){

var leftval=parseInt(resource[i][1]/resource[i][2] *100);
var inRow=rowOpera(i,leftval);

row = document.createElement("tr");
cell = document.createElement("td");
cell.setAttribute("colspan",2);
cell.appendChild( inRow );
row.appendChild( cell );

mymenu.appendChild( row );
}

return mymenu;
}

function estTime(time){//using the pagetime and the wait time for the resource, returns a time field of when the resource will be ready
var days=0;
var head="";
var tail=langfile[lang][5];

while(time[2]>=60){
time[2]-=60;
time[1]+=1;
}

while(time[1]>=60){
time[1]-=60;
time[0]+=1;
}

while(time[0]>=24){
time[0]-=24;
days+=1;
}

if(time[0]<10){
time[0]="0"+time[0];
}
if(time[1]<10){
time[1]="0"+time[1];
}
if(time[2]<10){
time[2]="0"+time[2];
}

if(days==0){
head=langfile[lang][6];
} else if(days==1){
head=langfile[lang][7];
} else {
head=langfile[lang][8] +days + langfile[lang][9];
}

return head+time[0]+":"+time[1]+":"+time[2]+tail;
}

function getTime(){ // gets the time from the page or use java time
var servertime=returnObjById("tp1").textContent;

if(!servertime) {
var Digital=new Date();
pagetime=[Digital.getHours(),Digital.getMinutes(),Digital.getSeconds()];
if(pagetime[0]>12)military=true;
return;
}

pagetime=timeField(servertime);
servertime=pagetime;

if(servertime==-1) {
var Digital=new Date();
pagetime=[Digital.getHours(),Digital.getMinutes(),Digital.getSeconds()];
}

if(pagetime[0]>12) military=true;

}

function timeField(time) { // convert a hh:mm:ss time stamp to seconds
var limit = time.split(":");
return (limit.length == 3) ? ([parseInt(limit[0]) , parseInt(limit[1]) , parseInt(limit[2])]) : -1;
}

function countdown(){//updates all countdown values and displays

var go=false;
resource=getResourceInfo();

for(var i=0; i<4; i++){

if(overflow[i][0]>0){
go=true;
overflow[i][0]--;
var leftval=parseInt(resource[i][1]/resource[i][2] *100);
var color=(overflow[i][0]<300) ? "red" : "green";
var newSpan = document.createElement("span");

newSpan.id=fields[lang][i]+"timer";
newSpan.appendChild( document.createTextNode(formatTimeString(overflow[i][0])));

if(newSpan.style.color.indexOf(color)==-1){
newSpan.style.color=color;
}

var old=returnObjById(fields[lang][i]+"timer") ;
old.parentNode.replaceChild(newSpan,old );

old=returnObjById(fields[lang][i]+"value");

if(parseInt(old.title)!=leftval){
overflow=calOverflow();
old=returnObjById("resbar"+i);
old.parentNode.replaceChild(rowOpera(i,leftval),old );
}

} else {
var old=returnObjById(fields[lang][i]+"timer") ;

if(old.textContent.indexOf("0:00:00 h")==-1){
old.textContent="0:00:00 h";
old.style.color='red';
old=returnObjById("resbar"+i);
old.parentNode.replaceChild(rowOpera(i,100),old );
}
}
}

if(go){
autotime = window.setTimeout(countdown, 1000);}
else {
clearTimeout(autotime); //kill the timeout
return;
}
}

function resourceBuildBar(vals){//create a resource needed menu given an array of values
var div1 = document.createElement("div");
var inTable = document.createElement("table");
var row = document.createElement("tr");
var cell = document.createElement("td");

inTable.className = "tbg";
inTable.style.width = "500px";
inTable.setAttribute('cellspacing', 1);
inTable.setAttribute('cellpadding', 1);
cell.setAttribute("colspan",4);

cell.appendChild( document.createTextNode( langfile[lang][0] ) );
cell.className = "rbg";
row.appendChild( cell );
inTable.appendChild( row );

for(var i=0; i<4; i++){
row = document.createElement("tr");
row.style.color=vals[i][3];
cell = document.createElement("td");
var simg = document.createElement("img");
simg.src="img/un/r/"+(i+1)+".gif";
simg.alt=fields[lang][i];
simg.title=fields[lang][i];
cell.appendChild(simg);
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild( document.createTextNode( vals[i][0] ) );
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild( document.createTextNode( vals[i][1] ) );
row.appendChild(cell);
cell = document.createElement("td");
if(vals[i][4]){
cell.appendChild( document.createTextNode( vals[i][2] +" |"+langfile[lang][10]+ vals[i][4] ) );}
else {
cell.appendChild( document.createTextNode( vals[i][2] ) );
}
row.appendChild(cell);
inTable.appendChild(row);
}

return inTable;
}

function resourcePerBuild(){ // search the page for resource needed to build, and calculates the surplus, deficit or other information

var need = document.evaluate('//table[@class="f10"]',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
var end = need.snapshotLength;
if(end==0) return;

var resourceBlock=new Array();

for(var i=0; i<end; i++){
var current=need.snapshotItem(i);
var tdcells=current.getElementsByTagName('td');
var cout=0;

for(var k=0; k<tdcells.length; k++){
if(tdcells[k].getAttribute('class')=='s7'){cout++;}
}

if(cout==0){
var c=current.textContent;
if( c.indexOf(" | ")!=-1 && c.charAt(0)!="\n" ) {
resourceBlock.push( current );}
}

}

end=resourceBlock.length;
if(end==0) return;

for(var i=0; i<end; i++){
var current=resourceBlock[i].textContent.split(" | ");

var stuff=[];
var maxtime=0;
for(var j=0; j<4; j++){
var cneed = parseInt( current[j] );
var cproduction = parseInt( resource[j][0] );
var cstore = parseInt( resource[j][1] );
var cdiff = cstore-cneed;
var ctime;
var font="green"

if(cproduction<0){
if(cdiff>0){
temp=parseInt(-cdiff/cproduction*3600);
if(temp>maxtime) maxtime=temp;
ctime=langfile[lang][17]+formatTimeString(temp);
}
else if(cdiff<0){
ctime=langfile[lang][18]+fields[lang][i]+langfile[lang][19];
font="red"
}
else {
ctime=langfile[lang][18]+fields[lang][i]+langfile[lang][19];
font="red"
}

} else if(cproduction>0){
if(cdiff>0){
ctime=langfile[lang][11]+(cdiff/cneed*100).toFixed(2)+"%";
}
else if(cdiff<0){
temp=parseInt(-cdiff/cproduction*3600);
if(temp>maxtime) maxtime=temp;
ctime=langfile[lang][12]+formatTimeString(temp);
font="red"
}
else {
ctime=langfile[lang][20];
}

} else {

if(cdiff>0){
ctime=langfile[lang][20];
}
else if(cdiff<0){
ctime=langfile[lang][18]+fields[lang][i]+langfile[lang][19];
font="red"
}
else {
ctime=langfile[lang][20];
}
}

if(cdiff<0){
stuff[j]=[langfile[lang][13],(-cdiff),ctime,font];
}
else if(cdiff>0){
stuff[j]=[langfile[lang][14],cdiff,ctime,font];
}
else {
stuff[j]=[langfile[lang][16],0,ctime,font];
}

}

var holdtable;
var w=false;
var g=false;
var blink=false;
var over=false;

var wblink=false;
var gblink=false;

var wover=false;
var gover=false;

if(maxtime>0){

for(var k=0;k<4; k++){

var left=Math.ceil((maxtime/3600)*resource[k][0])+resource[k][1];
var cap=parseInt( resource[k][2] ) ;

if(left>cap){
if(k!=3){
wover=true;
w=true;
} else{
gover=true;
g=true;
}

over=true;
stuff[k].push( left +'*');
} else {
stuff[k].push( left );
}

if(current[k]>cap) {

if(k!=3){
w=true;
wblink=true;
stuff[k].push( langfile[lang][21] );
} else{
g=true;
gblink=true;
stuff[k].push( langfile[lang][22] );
}

blink=true;
}

}

holdtable=resourceBuildBar(stuff);

var row = document.createElement("tr");
var cell = document.createElement("td");
cell.setAttribute("colspan",4);
var timer=estTime( formatTime(maxtime+pagetime[0]*3600+pagetime[1]*60+pagetime[2],0,0,0,0) );

cell.appendChild( document.createTextNode( timer+" |"+langfile[lang][15]+formatTimeString(maxtime) ) );
row.appendChild( cell );
holdtable.appendChild( row );

if(w || g){
var msg="";
row = document.createElement("tr");
cell = document.createElement("td");

if( w && g){
msg=langfile[lang][21];
if(wover) msg+='*';
msg+=" and "+langfile[lang][22];
if(gover) msg+='*';
} else if(w){
msg=langfile[lang][21];
if(wover) msg+='*';
} else {
msg=langfile[lang][22];
if(wover) msg+='*';
}

if(blink){
cell.setAttribute("style","text-decoration:blink;");
cell.style.color="red";
} else {
cell.style.color="green";
}

cell.setAttribute("colspan",4);
cell.appendChild( document.createTextNode( msg ) );
row.appendChild( cell );
holdtable.appendChild( row );
}

} else {
holdtable=resourceBuildBar(stuff);
}

resourceBlock[i].appendChild( holdtable );

:-o
..

Post new comment

  • Allowed HTML tags: <code> <blockquote> <em> <strong> <strike> <ul> <li> <ol>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic).

More information about formatting options