x.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // x.js
  2. // X v3.14.1, Cross-Browser DHTML Library from Cross-Browser.com
  3. // Copyright (c) 2002,2003 Michael Foster (mike@cross-browser.com)
  4. // This library is distributed under the terms of the LGPL (gnu.org)
  5. // Variables:
  6. var xVersion='3.14.1',xOp7=false,xOp5or6=false,xIE4Up=false,xNN4=false,xUA=navigator.userAgent.toLowerCase();
  7. if(window.opera){
  8. xOp7=(xUA.indexOf('opera 7')!=-1 || xUA.indexOf('opera/7')!=-1);
  9. if (!xOp7) xOp5or6=(xUA.indexOf('opera 5')!=-1 || xUA.indexOf('opera/5')!=-1 || xUA.indexOf('opera 6')!=-1 || xUA.indexOf('opera/6')!=-1);
  10. }
  11. else if(document.layers) {xNN4=true;}
  12. else {xIE4Up=document.all && xUA.indexOf('msie')!=-1 && parseInt(navigator.appVersion)>=4;}
  13. // Object:
  14. function xGetElementById(e) {
  15. if(typeof(e)!='string') return e;
  16. if(document.getElementById) e=document.getElementById(e);
  17. else if(document.all) e=document.all[e];
  18. else if(document.layers) e=xLayer(e);
  19. else e=null;
  20. return e;
  21. }
  22. function xParent(e,bNode){
  23. if (!(e=xGetElementById(e))) return null;
  24. var p=null;
  25. if (!bNode && xDef(e.offsetParent)) p=e.offsetParent;
  26. else if (xDef(e.parentNode)) p=e.parentNode;
  27. else if (xDef(e.parentElement)) p=e.parentElement;
  28. else if (xDef(e.parentLayer)){if (e.parentLayer!=window) p=e.parentLayer;}
  29. return p;
  30. }
  31. function xDef() {
  32. for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  33. return true;
  34. }
  35. function xStr(s) {
  36. return typeof(s)=='string';
  37. }
  38. function xNum(n) {
  39. return typeof(n)=='number';
  40. }
  41. // Appearance:
  42. function xShow(e) {
  43. if(!(e=xGetElementById(e))) return;
  44. if(e.style && xDef(e.style.visibility)) e.style.visibility='visible';
  45. else if(xDef(e.visibility)) e.visibility='show';
  46. }
  47. function xHide(e) {
  48. if(!(e=xGetElementById(e))) return;
  49. if(e.style && xDef(e.style.visibility)) e.style.visibility='hidden';
  50. else if(xDef(e.visibility)) e.visibility='hide';
  51. }
  52. function xZIndex(e,uZ) {
  53. if(!(e=xGetElementById(e))) return 0;
  54. if(e.style && xDef(e.style.zIndex)) {
  55. if(xNum(uZ)) e.style.zIndex=uZ;
  56. uZ=parseInt(e.style.zIndex);
  57. }
  58. else if(xDef(e.zIndex)) {
  59. if(xNum(uZ)) e.zIndex=uZ;
  60. uZ=e.zIndex;
  61. }
  62. return uZ;
  63. }
  64. function xColor(e,sColor) {
  65. if(!(e=xGetElementById(e))) return '';
  66. var c='';
  67. if(e.style && xDef(e.style.color)) {
  68. if(xStr(sColor)) e.style.color=sColor;
  69. c=e.style.color;
  70. }
  71. return c;
  72. }
  73. function xBackground(e,sColor,sImage) {
  74. if(!(e=xGetElementById(e))) return '';
  75. var bg='';
  76. if(e.style) {
  77. if(xStr(sColor)) {
  78. if(!xOp5or6) e.style.backgroundColor=sColor;
  79. else e.style.background=sColor;
  80. }
  81. if(xStr(sImage)) e.style.backgroundImage=(sImage!='')? 'url('+sImage+')' : null;
  82. if(!xOp5or6) bg=e.style.backgroundColor;
  83. else bg=e.style.background;
  84. }
  85. else if(xDef(e.bgColor)) {
  86. if(xStr(sColor)) e.bgColor=sColor;
  87. bg=e.bgColor;
  88. if(xStr(sImage)) e.background.src=sImage;
  89. }
  90. return bg;
  91. }
  92. // Position:
  93. function xMoveTo(e,iX,iY) {
  94. xLeft(e,iX);
  95. xTop(e,iY);
  96. }
  97. function xLeft(e,iX) {
  98. if(!(e=xGetElementById(e))) return 0;
  99. var css=xDef(e.style);
  100. if (css && xStr(e.style.left)) {
  101. if(xNum(iX)) e.style.left=iX+'px';
  102. else {
  103. iX=parseInt(e.style.left);
  104. if(isNaN(iX)) iX=0;
  105. }
  106. }
  107. else if(css && xDef(e.style.pixelLeft)) {
  108. if(xNum(iX)) e.style.pixelLeft=iX;
  109. else iX=e.style.pixelLeft;
  110. }
  111. else if(xDef(e.left)) {
  112. if(xNum(iX)) e.left=iX;
  113. else iX=e.left;
  114. }
  115. return iX;
  116. }
  117. function xTop(e,iY) {
  118. if(!(e=xGetElementById(e))) return 0;
  119. var css=xDef(e.style);
  120. if(css && xStr(e.style.top)) {
  121. if(xNum(iY)) e.style.top=iY+'px';
  122. else {
  123. iY=parseInt(e.style.top);
  124. if(isNaN(iY)) iY=0;
  125. }
  126. }
  127. else if(css && xDef(e.style.pixelTop)) {
  128. if(xNum(iY)) e.style.pixelTop=iY;
  129. else iY=e.style.pixelTop;
  130. }
  131. else if(xDef(e.top)) {
  132. if(xNum(iY)) e.top=iY;
  133. else iY=e.top;
  134. }
  135. return iY;
  136. }
  137. function xPageX(e) {
  138. if (!(e=xGetElementById(e))) return 0;
  139. if (xDef(e.pageX)) return e.pageX;
  140. var x = 0;
  141. while (e) {
  142. if (xDef(e.offsetLeft)) x += e.offsetLeft;
  143. e = xDef(e.offsetParent) ? e.offsetParent : null;
  144. }
  145. return x;
  146. }
  147. function xPageY(e) {
  148. if (!(e=xGetElementById(e))) return 0;
  149. if (xDef(e.pageY)) return e.pageY;
  150. var y = 0;
  151. while (e) {
  152. if (xDef(e.offsetTop)) y += e.offsetTop;
  153. e = xDef(e.offsetParent) ? e.offsetParent : null;
  154. }
  155. // if (xOp7) return y - document.body.offsetTop; // v3.14, temporary hack for opera bug 130324
  156. return y;
  157. }
  158. function xOffsetLeft(e) { // v3.14, see x_nn4.js
  159. if (!(e=xGetElementById(e))) return 0;
  160. if (xDef(e.offsetLeft)) return e.offsetLeft;
  161. else return 0;
  162. }
  163. function xOffsetTop(e) {
  164. if (!(e=xGetElementById(e))) return 0;
  165. if (xDef(e.offsetTop)) return e.offsetTop;
  166. else return 0;
  167. }
  168. function xScrollLeft(e) {
  169. var offset=0;
  170. if (!(e=xGetElementById(e))) {
  171. if(xDef(window.pageXOffset)) offset=window.pageXOffset;
  172. else if(document.documentElement && document.documentElement.scrollLeft) offset=document.documentElement.scrollLeft;
  173. else if(document.body && xDef(document.body.scrollLeft)) offset=document.body.scrollLeft;
  174. }
  175. else { if (xNum(e.scrollLeft)) offset = e.scrollLeft; }
  176. return offset;
  177. }
  178. function xScrollTop(e) {
  179. var offset=0;
  180. if (!(e=xGetElementById(e))) {
  181. if(xDef(window.pageYOffset)) offset=window.pageYOffset;
  182. else if(document.documentElement && document.documentElement.scrollTop) offset=document.documentElement.scrollTop;
  183. else if(document.body && xDef(document.body.scrollTop)) offset=document.body.scrollTop;
  184. }
  185. else { if (xNum(e.scrollTop)) offset = e.scrollTop; }
  186. return offset;
  187. }
  188. // Size:
  189. function xResizeTo(e,uW,uH) {
  190. xWidth(e,uW);
  191. xHeight(e,uH);
  192. }
  193. function xWidth(e,uW) {
  194. if(!(e=xGetElementById(e))) return 0;
  195. if (xNum(uW)) { // v3.13.1
  196. if (uW<0) uW = 0;
  197. else uW=Math.round(uW);
  198. }
  199. else uW=0;
  200. var css=xDef(e.style);
  201. if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
  202. if(uW) xSetCW(e, uW);
  203. uW=e.offsetWidth;
  204. }
  205. else if(css && xDef(e.style.pixelWidth)) {
  206. if(uW) e.style.pixelWidth=uW;
  207. uW=e.style.pixelWidth;
  208. }
  209. else if(xDef(e.clip) && xDef(e.clip.right)) {
  210. if(uW) e.clip.right=uW;
  211. uW=e.clip.right;
  212. }
  213. return uW;
  214. }
  215. function xHeight(e,uH) {
  216. if(!(e=xGetElementById(e))) return 0;
  217. if (xNum(uH)) { // v3.13.1
  218. if (uH<0) uH = 0;
  219. else uH=Math.round(uH);
  220. }
  221. else uH=0;
  222. var css=xDef(e.style);
  223. if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
  224. if(uH) xSetCH(e, uH);
  225. uH=e.offsetHeight;
  226. }
  227. else if(css && xDef(e.style.pixelHeight)) {
  228. if(uH) e.style.pixelHeight=uH;
  229. uH=e.style.pixelHeight;
  230. }
  231. else if(xDef(e.clip) && xDef(e.clip.bottom)) {
  232. if(uH) e.clip.bottom=uH;
  233. uH=e.clip.bottom;
  234. }
  235. return uH;
  236. }
  237. function xGetCS(ele,sP){return parseInt(document.defaultView.getComputedStyle(ele,'').getPropertyValue(sP));}
  238. function xSetCW(ele,uW){
  239. var pl=0,pr=0,bl=0,br=0;
  240. if(xDef(document.defaultView) && xDef(document.defaultView.getComputedStyle)){
  241. pl=xGetCS(ele,'padding-left');
  242. pr=xGetCS(ele,'padding-right');
  243. bl=xGetCS(ele,'border-left-width');
  244. br=xGetCS(ele,'border-right-width');
  245. }
  246. else if(xDef(ele.currentStyle,document.compatMode)){
  247. if(document.compatMode=='CSS1Compat'){
  248. pl=parseInt(ele.currentStyle.paddingLeft);
  249. pr=parseInt(ele.currentStyle.paddingRight);
  250. bl=parseInt(ele.currentStyle.borderLeftWidth);
  251. br=parseInt(ele.currentStyle.borderRightWidth);
  252. }
  253. }
  254. else if(xDef(ele.offsetWidth,ele.style.width)){ // ?
  255. ele.style.width=uW+'px';
  256. pl=ele.offsetWidth-uW;
  257. }
  258. if(isNaN(pl)) pl=0; if(isNaN(pr)) pr=0; if(isNaN(bl)) bl=0; if(isNaN(br)) br=0;
  259. var cssW=uW-(pl+pr+bl+br);
  260. if(isNaN(cssW)||cssW<0) return;
  261. else ele.style.width=cssW+'px';
  262. }
  263. function xSetCH(ele,uH){
  264. var pt=0,pb=0,bt=0,bb=0;
  265. if(xDef(document.defaultView) && xDef(document.defaultView.getComputedStyle)){
  266. pt=xGetCS(ele,'padding-top');
  267. pb=xGetCS(ele,'padding-bottom');
  268. bt=xGetCS(ele,'border-top-width');
  269. bb=xGetCS(ele,'border-bottom-width');
  270. }
  271. else if(xDef(ele.currentStyle,document.compatMode)){
  272. if(document.compatMode=='CSS1Compat'){
  273. pt=parseInt(ele.currentStyle.paddingTop);
  274. pb=parseInt(ele.currentStyle.paddingBottom);
  275. bt=parseInt(ele.currentStyle.borderTopWidth);
  276. bb=parseInt(ele.currentStyle.borderBottomWidth);
  277. }
  278. }
  279. else if(xDef(ele.offsetHeight,ele.style.height)){ // ?
  280. ele.style.height=uH+'px';
  281. pt=ele.offsetHeight-uH;
  282. }
  283. if(isNaN(pt)) pt=0; if(isNaN(pb)) pb=0; if(isNaN(bt)) bt=0; if(isNaN(bb)) bb=0;
  284. var cssH=uH-(pt+pb+bt+bb);
  285. if(isNaN(cssH)||cssH<0) return;
  286. else ele.style.height=cssH+'px';
  287. }
  288. function xClip(e,iTop,iRight,iBottom,iLeft) {
  289. if(!(e=xGetElementById(e))) return;
  290. if(e.style) {
  291. if (xNum(iLeft)) e.style.clip='rect('+iTop+'px '+iRight+'px '+iBottom+'px '+iLeft+'px)';
  292. else e.style.clip='rect(0 '+parseInt(e.style.width)+'px '+parseInt(e.style.height)+'px 0)';
  293. }
  294. else if(e.clip) {
  295. if (xNum(iLeft)) { e.clip.top=iTop; e.clip.right=iRight; e.clip.bottom=iBottom; e.clip.left=iLeft; }
  296. else { e.clip.top=0; e.clip.right=xWidth(e); e.clip.bottom=xHeight(e); e.clip.left=0; }
  297. }
  298. }
  299. // Window:
  300. function xClientWidth() {
  301. var w=0;
  302. if(xOp5or6) w=window.innerWidth;
  303. else if(!window.opera && document.documentElement && document.documentElement.clientWidth) // v3.12
  304. w=document.documentElement.clientWidth;
  305. else if(document.body && document.body.clientWidth)
  306. w=document.body.clientWidth;
  307. else if(xDef(window.innerWidth,window.innerHeight,document.height)) {
  308. w=window.innerWidth;
  309. if(document.height>window.innerHeight) w-=16;
  310. }
  311. return w;
  312. }
  313. function xClientHeight() {
  314. var h=0;
  315. if(xOp5or6) h=window.innerHeight;
  316. else if(!window.opera && document.documentElement && document.documentElement.clientHeight) // v3.12
  317. h=document.documentElement.clientHeight;
  318. else if(document.body && document.body.clientHeight)
  319. h=document.body.clientHeight;
  320. else if(xDef(window.innerWidth,window.innerHeight,document.width)) {
  321. h=window.innerHeight;
  322. if(document.width>window.innerWidth) h-=16;
  323. }
  324. return h;
  325. }
  326. // Animation:
  327. function xSlideTo(e,x,y,uTime) {
  328. if (!(e=xGetElementById(e))) return;
  329. if (!e.timeout) e.timeout = 25;
  330. e.xTarget = x; e.yTarget = y; e.slideTime = uTime; e.stop = false;
  331. e.yA = e.yTarget - xTop(e); e.xA = e.xTarget - xLeft(e); // A = distance
  332. e.B = Math.PI / (2 * e.slideTime); // B = period
  333. e.yD = xTop(e); e.xD = xLeft(e); // D = initial position
  334. var d = new Date(); e.C = d.getTime();
  335. if (!e.moving) xSlide(e);
  336. }
  337. function xSlide(e) {
  338. if (!(e=xGetElementById(e))) return;
  339. var now, s, t, newY, newX;
  340. now = new Date();
  341. t = now.getTime() - e.C;
  342. if (e.stop) { e.moving = false; }
  343. else if (t < e.slideTime) {
  344. setTimeout("xSlide('"+e.id+"')", e.timeout);
  345. s = Math.sin(e.B * t);
  346. newX = Math.round(e.xA * s + e.xD);
  347. newY = Math.round(e.yA * s + e.yD);
  348. xMoveTo(e, newX, newY);
  349. e.moving = true;
  350. }
  351. else {
  352. xMoveTo(e, e.xTarget, e.yTarget);
  353. e.moving = false;
  354. }
  355. }
  356. // Event:
  357. function xAddEventListener(e,eventType,eventListener,useCapture) {
  358. if(!(e=xGetElementById(e))) return;
  359. eventType=eventType.toLowerCase();
  360. if((!xIE4Up && !xOp7) && e==window) {
  361. if(eventType=='resize') { window.xPCW=xClientWidth(); window.xPCH=xClientHeight(); window.xREL=eventListener; xResizeEvent(); return; }
  362. if(eventType=='scroll') { window.xPSL=xScrollLeft(); window.xPST=xScrollTop(); window.xSEL=eventListener; xScrollEvent(); return; }
  363. }
  364. var eh='e.on'+eventType+'=eventListener';
  365. if(e.addEventListener) e.addEventListener(eventType,eventListener,useCapture);
  366. else if(e.attachEvent) e.attachEvent('on'+eventType,eventListener);
  367. else if(e.captureEvents) {
  368. if(useCapture||(eventType.indexOf('mousemove')!=-1)) { e.captureEvents(eval('Event.'+eventType.toUpperCase())); }
  369. eval(eh);
  370. }
  371. else eval(eh);
  372. }
  373. function xRemoveEventListener(e,eventType,eventListener,useCapture) {
  374. if(!(e=xGetElementById(e))) return;
  375. eventType=eventType.toLowerCase();
  376. if((!xIE4Up && !xOp7) && e==window) {
  377. if(eventType=='resize') { window.xREL=null; return; }
  378. if(eventType=='scroll') { window.xSEL=null; return; }
  379. }
  380. var eh='e.on'+eventType+'=null';
  381. if(e.removeEventListener) e.removeEventListener(eventType,eventListener,useCapture);
  382. else if(e.detachEvent) e.detachEvent('on'+eventType,eventListener);
  383. else if(e.releaseEvents) {
  384. if(useCapture||(eventType.indexOf('mousemove')!=-1)) { e.releaseEvents(eval('Event.'+eventType.toUpperCase())); }
  385. eval(eh);
  386. }
  387. else eval(eh);
  388. }
  389. function xEvent(evt) { // cross-browser event object prototype
  390. this.type = '';
  391. this.target = null;
  392. this.pageX = 0;
  393. this.pageY = 0;
  394. this.offsetX = 0;
  395. this.offsetY = 0;
  396. this.keyCode = 0;
  397. var e = evt ? evt : window.event;
  398. if(!e) return;
  399. if(e.type) this.type = e.type;
  400. if(e.target) this.target = e.target;
  401. else if(e.srcElement) this.target = e.srcElement;
  402. else if(xNN4) this.target = xLayerFromPoint(e.pageX, e.pageY);
  403. if(xOp5or6) { this.pageX = e.clientX; this.pageY = e.clientY; }
  404. else if(xDef(e.pageX,e.pageY)) { this.pageX = e.pageX; this.pageY = e.pageY; } // v3.14
  405. else if(xDef(e.clientX,e.clientY)) { this.pageX = e.clientX + xScrollLeft(); this.pageY = e.clientY + xScrollTop(); }
  406. if(xDef(e.offsetX,e.offsetY)) { this.offsetX = e.offsetX; this.offsetY = e.offsetY; }
  407. else if(xDef(e.layerX,e.layerY)) { this.offsetX = e.layerX; this.offsetY = e.layerY; }
  408. else { this.offsetX = this.pageX - xPageX(this.target); this.offsetY = this.pageY - xPageY(this.target); }
  409. if (e.keyCode) { this.keyCode = e.keyCode; } // for moz/fb, if keyCode==0 use which
  410. else if (xDef(e.which)) { this.keyCode = e.which; }
  411. }
  412. function xResizeEvent() { // window resize event simulation
  413. if (window.xREL) setTimeout('xResizeEvent()', 250);
  414. var cw = xClientWidth(), ch = xClientHeight();
  415. if (window.xPCW != cw || window.xPCH != ch) { window.xPCW = cw; window.xPCH = ch; if (window.xREL) window.xREL(); }
  416. }
  417. function xScrollEvent() { // window scroll event simulation
  418. if (window.xSEL) setTimeout('xScrollEvent()', 250);
  419. var sl = xScrollLeft(), st = xScrollTop();
  420. if (window.xPSL != sl || window.xPST != st) { window.xPSL = sl; window.xPST = st; if (window.xSEL) window.xSEL(); }
  421. }
  422. // end x.js