clock.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // ---------------- variables --------------------------------------------------
  2. var clock_host = 'http://clock.time.in.ua/';
  3. var clock_html = 'clock.html';
  4. var offsetCookie = 'to';
  5. var req, reqstart, reqend, response, offset;
  6. var time_width,time_height,time_border,time_bg,w,h,undef;
  7. // ---------------- SNTP via HTTP syncronization -------------------------------
  8. function loadXMLDoc(url) {
  9. // branch for native XMLHttpRequest object
  10. if (window.XMLHttpRequest) {
  11. req = new XMLHttpRequest();
  12. req.onreadystatechange = processReqChange;
  13. req.open("GET", url, true);
  14. req.setRequestHeader('Pragma', 'no-cache');
  15. req.setRequestHeader('Cache-control', 'no-cache');
  16. req.send(null);
  17. }
  18. else if (window.ActiveXObject) {
  19. // branch for IE/Windows ActiveX version
  20. req = new ActiveXObject("Microsoft.XMLHTTP");
  21. if (req) {
  22. req.onreadystatechange = processReqChange;
  23. req.open("GET", url, true);
  24. req.setRequestHeader('Pragma', 'no-cache');
  25. req.setRequestHeader('Cache-control', 'no-cache');
  26. req.send();
  27. }
  28. }
  29. }
  30. function processReqChange()
  31. {
  32. // only if req shows "complete"
  33. if (req.readyState == 4) {
  34. // only if "OK"
  35. if (req.status == 200 || req.responseText != undef) {
  36. // ...processing statements go here...
  37. response = req.responseText;
  38. reqend = (new Date()).getTime();
  39. offset = (reqend-reqstart)/2;
  40. // real time (in microseconds) is response * 1000 + offset
  41. // user's time (in microseconds) is reqend
  42. // difference is reqend - (response * 1000 + offset)
  43. offset = reqend - response*1000 - offset;
  44. var expire = new Date();
  45. expire.setTime(expire.getTime() + 300 * 1000); // 1 hour
  46. if (Math.abs(offset) < 1e4) setCookie(offsetCookie, offset, expire.toGMTString());
  47. display();
  48. }
  49. else {
  50. alert("There was a problem retrieving the XML data:\n" + req.statusText);
  51. }
  52. }
  53. }
  54. function display() {
  55. var timer = (new Date()).getTime();
  56. var real = timer + offset;
  57. real = Math.round(real);
  58. real = real.toString();
  59. real = 1000-real.substr(real.length-3);
  60. setTimeout("displayTime()", real);
  61. var im = document.getElementById("img");
  62. if (im) {
  63. if (Math.abs(offset) > 1000) im.src = "http://clock.time.in.ua/images/clock-no.png";
  64. else im.src = "http://clock.time.in.ua/images/clock-ok.png";
  65. im.title = "Your system clock is "+(offset >= 0 ? 'faster' : 'slower')+" in "+Math.abs(offset)+" ms";
  66. }
  67. }
  68. function startSync() {
  69. offset = parseInt(getCookie(offsetCookie));
  70. if (offset) {
  71. display(offset);
  72. }
  73. else {
  74. url = clock_host + "?r=" + Math.random();
  75. loadXMLDoc(url);
  76. }
  77. }
  78. function displayTime() {
  79. var div = document.getElementById("time");
  80. var time = new Date();
  81. time.setTime(time.getTime() - offset);
  82. div.innerHTML = d2(time.getHours())+":"+d2(time.getMinutes())+":"+d2(time.getSeconds());
  83. setTimeout("displayTime()", 1000);
  84. }
  85. function d2(val) {
  86. if (val < 10) val = "0"+val;
  87. return val;
  88. }
  89. function setCookie (name, value, expires, path, domain, secure) {
  90. document.cookie = name + "=" + escape(value) +
  91. ((expires) ? "; expires=" + expires : "") +
  92. ((path) ? "; path=" + path : "") +
  93. ((domain) ? "; domain=" + domain : "") +
  94. ((secure) ? "; secure" : "");
  95. }
  96. function getCookie(name) {
  97. var cookie = " " + document.cookie;
  98. var search = " " + name + "=";
  99. var setStr = null;
  100. var offset = 0;
  101. var end = 0;
  102. if (cookie.length > 0) {
  103. offset = cookie.indexOf(search);
  104. if (offset != -1) {
  105. offset += search.length;
  106. end = cookie.indexOf(";", offset);
  107. if (end == -1) end = cookie.length;
  108. setStr = unescape(cookie.substring(offset, end));
  109. }
  110. }
  111. return(setStr);
  112. }
  113. // ---------------- CSS definition --------------------------------------------
  114. function css() {
  115. try {
  116. if (time_width == undef || time_height == undef || time_width == 0 || time_height == 0) {
  117. time_width = undef;
  118. time_height = undef;
  119. }
  120. if (time_width == undef) time_width = 88;
  121. if (time_height == undef) time_height = 31;
  122. var w=time_width,h=time_height;
  123. if (time_border == undef) time_border = 'black 1px solid';
  124. if (time_border == '') time_border = '0px none';
  125. if (match = time_border.match(/\b([0-9]+)px\b/)) {
  126. w -= match[1]*2;
  127. h -= match[1]*2;
  128. }
  129. if (time_bg == undef) time_bg = 'http://clock.time.in.ua/images/clock-bg-'+time_width+'-'+time_height+'-1.gif';
  130. var obj = document.getElementById("timeinua");
  131. if (obj) {
  132. if (obj.style) {
  133. obj.style.margin = '0px';
  134. obj.style.padding = '0px';
  135. obj.style.fontSize = '1px';
  136. obj.style.width = w+'px';
  137. obj.style.height = h+'px';
  138. obj.style.border = time_border;
  139. obj.style.overflow = 'hidden';
  140. obj.style.whiteSpace = 'nowrap';
  141. if (time_bg) {
  142. if (time_bg.indexOf('#') == 0) obj.style.backgroundColor = time_bg;
  143. else if (time_bg.indexOf('http://') == 0) obj.style.background = "url('"+time_bg+"')";
  144. }
  145. }
  146. var obj1 = obj.getElementsByTagName('div');
  147. for(var i=0; i<obj1.length; i++) {
  148. if (obj1[i].style) {
  149. obj1[i].style.font = '8pt Verdana,Tahoma';
  150. obj1[i].style.textAlign = 'center';
  151. if (i == 0) {
  152. obj1[i].style.lineHeight = '0.9';
  153. obj1[i].style.height = '10px';
  154. obj1[i].style.overflow = 'hidden';
  155. }
  156. }
  157. }
  158. obj1 = obj.getElementsByTagName('a');
  159. if (obj1.length && obj1[0] && obj1[0].href.indexOf('http://time.in.ua/') == 0) {
  160. obj1[0].style.color = '#0000a0';
  161. obj1[0].style.textDecoration = 'none';
  162. }
  163. else {
  164. clock_html += '?error';
  165. }
  166. }
  167. else {
  168. clock_html += '?error';
  169. }
  170. }
  171. catch(e) { }
  172. }
  173. // ---------------- main programm start --------------------------------------
  174. if (window.location.href.indexOf(clock_host) == 0) {
  175. if (window.location.href.indexOf('error') == -1) {
  176. reqstart = (new Date()).getTime();
  177. startSync();
  178. }
  179. else {
  180. var obj = document.getElementById("img");
  181. if (obj) {
  182. obj.style.visibility = 'hidden';
  183. obj.style.width = '0px';
  184. }
  185. obj = document.getElementById("time");
  186. obj.innerHTML = 'code error';
  187. }
  188. }
  189. else {
  190. css();
  191. document.write('<iframe src="'+clock_host+clock_html+'" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" width="'+w+'" height="18" scrooling="no" allowtransparency="true"></iframe>');
  192. }