Posted By


dividespace on 10/21/06

Tagged


Statistics


Viewed 160 times
Favorited by 0 user(s)

ruthsarian_utilities.js


/ Published in: JavaScript
Save to your folder(s)

/*******************************************************************************
* ruthsarian_utilities.js : 2006.02.27
* -----------------------------------------------------------------------------
* A group of useful JavaScript utilities that can aid in the development
* of webpages. Credit and source of code is given before each set of
* functions.
*******************************************************************************/


Copy this code and paste it in your HTML
  1. /*******************************************************************************
  2. * ruthsarian_utilities.js : 2006.02.27
  3. * -----------------------------------------------------------------------------
  4. * A group of useful JavaScript utilities that can aid in the development
  5. * of webpages. Credit and source of code is given before each set of
  6. * functions.
  7. *******************************************************************************/
  8.  
  9. /* event_attach() takes care of attaching event handlers (functions) to events.
  10.  * this simplifies the process of attaching multiple handlers to a single event
  11.  *
  12.  * NOTE: the onload stack is executed in a LIFO manner to mimic
  13.  * IE's window.attachEvent function. However, Opera also has its own
  14.  * window.attachEvent function which executes the onload stack in a
  15.  * FIFO manner. FIFO is better, but IE has a larger user base, so
  16.  * LIFO is the way we go.
  17.  */
  18. function event_attach( event , func )
  19. {
  20. if ( window.attachEvent )
  21. {
  22. window.attachEvent( event , func );
  23. }
  24. else
  25. {
  26. if ( ( typeof( func ) ).toLowerCase() != 'function' )
  27. {
  28. return;
  29. }
  30. if ( ( typeof( document.event_handlers ) ).toLowerCase() == 'undefined' )
  31. {
  32. document.event_handlers = new Array();
  33. }
  34. if ( ( typeof( document.event_handlers[ event ] ) ).toLowerCase() == 'undefined' )
  35. {
  36. document.event_handlers[ event ] = new Array();
  37. }
  38. if ( ( typeof( eval( 'window.' + event ) ) ).toLowerCase() != 'function' )
  39. {
  40. eval( 'window.' + event + ' = function () { if ( ( typeof( document.event_handlers[ \'' + event + '\' ] ) ).toLowerCase() != \'undefined\' ) { for ( i = document.event_handlers[ \'' + event + '\' ].length - 1 ; i >= 0 ; i-- ) { document.event_handlers[ \'' + event + '\' ][ i ](); } } } ' );
  41. }
  42. document.event_handlers[ event ][ document.event_handlers[ event ].length ] = func;
  43. }
  44. }
  45.  
  46. /* Browser Detect v2.1.6
  47.  * documentation: http://www.dithered.com/javascript/browser_detect/index.html
  48.  * license: http://creativecommons.org/licenses/by/1.0/
  49.  * code by Chris Nott (chris[at]dithered[dot]com)
  50.  *
  51.  * modified to include Dreamcast
  52.  */
  53. function browser_detect()
  54. {
  55. var ua = navigator.userAgent.toLowerCase();
  56. this.isGecko = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
  57. this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);
  58. this.isKonqueror = (ua.indexOf('konqueror') != -1);
  59. this.isSafari = (ua.indexOf('safari') != - 1);
  60. this.isOmniweb = (ua.indexOf('omniweb') != - 1);
  61. this.isDreamcast = (ua.indexOf("dreamcast") != -1);
  62. this.isOpera = (ua.indexOf('opera') != -1);
  63. this.isIcab = (ua.indexOf('icab') != -1);
  64. this.isAol = (ua.indexOf('aol') != -1);
  65. this.isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1));
  66. this.isMozilla = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
  67. this.isFirebird = (ua.indexOf('firebird/') != -1);
  68. this.isNS = ((this.isGecko) ? (ua.indexOf('netscape') != -1) : ((ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1)));
  69. this.isIECompatible = ((ua.indexOf('msie') != -1) && !this.isIE);
  70. this.isNSCompatible = ((ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
  71. this.geckoVersion = ((this.isGecko) ? ua.substring((ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14)) : -1);
  72. this.equivalentMozilla = ((this.isGecko) ? parseFloat(ua.substring(ua.indexOf('rv:') + 3)) : -1);
  73. this.appleWebKitVersion = ((this.isAppleWebKit) ? parseFloat(ua.substring(ua.indexOf('applewebkit/') + 12)) : -1);
  74. this.versionMinor = parseFloat(navigator.appVersion);
  75. if (this.isGecko && !this.isMozilla) {
  76. this.versionMinor = parseFloat(ua.substring(ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1));
  77. }
  78. else if (this.isMozilla) {
  79. this.versionMinor = parseFloat(ua.substring(ua.indexOf('rv:') + 3));
  80. }
  81. else if (this.isIE && this.versionMinor >= 4) {
  82. this.versionMinor = parseFloat(ua.substring(ua.indexOf('msie ') + 5));
  83. }
  84. else if (this.isKonqueror) {
  85. this.versionMinor = parseFloat(ua.substring(ua.indexOf('konqueror/') + 10));
  86. }
  87. else if (this.isSafari) {
  88. this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('safari/') + 7));
  89. }
  90. else if (this.isOmniweb) {
  91. this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('omniweb/') + 8));
  92. }
  93. else if (this.isOpera) {
  94. this.versionMinor = parseFloat(ua.substring(ua.indexOf('opera') + 6));
  95. }
  96. else if (this.isIcab) {
  97. this.versionMinor = parseFloat(ua.substring(ua.indexOf('icab') + 5));
  98. }
  99. this.versionMajor = parseInt(this.versionMinor);
  100. this.isDOM1 = (document.getElementById);
  101. this.isDOM2Event = (document.addEventListener && document.removeEventListener);
  102. this.mode = document.compatMode ? document.compatMode : 'BackCompat';
  103. this.isWin = (ua.indexOf('win') != -1);
  104. this.isWin32 = (this.isWin && (ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1));
  105. this.isMac = (ua.indexOf('mac') != -1);
  106. this.isUnix = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
  107. this.isLinux = (ua.indexOf('linux') != -1);
  108. this.isNS4x = (this.isNS && this.versionMajor == 4);
  109. this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
  110. this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
  111. this.isNS4up = (this.isNS && this.versionMinor >= 4);
  112. this.isNS6x = (this.isNS && this.versionMajor == 6);
  113. this.isNS6up = (this.isNS && this.versionMajor >= 6);
  114. this.isNS7x = (this.isNS && this.versionMajor == 7);
  115. this.isNS7up = (this.isNS && this.versionMajor >= 7);
  116. this.isIE4x = (this.isIE && this.versionMajor == 4);
  117. this.isIE4up = (this.isIE && this.versionMajor >= 4);
  118. this.isIE5x = (this.isIE && this.versionMajor == 5);
  119. this.isIE55 = (this.isIE && this.versionMinor == 5.5);
  120. this.isIE5up = (this.isIE && this.versionMajor >= 5);
  121. this.isIE6x = (this.isIE && this.versionMajor == 6);
  122. this.isIE6up = (this.isIE && this.versionMajor >= 6);
  123. this.isIE7x = (this.isIE && this.versionMajor == 7);
  124. this.isIE7up = (this.isIE && this.versionMajor >= 7);
  125. this.isIE4xMac = (this.isIE4x && this.isMac);
  126. }
  127.  
  128. /* Opacity Displayer, Version 1.0 - http://old.alistapart.com/stories/pngopacity/
  129.  * Copyright Michael Lovitt, 6/2002.
  130.  */
  131. function opacity( strId , strPath , intWidth , intHeight , strClass , strAlt )
  132. {
  133. if ( document.pngAlpha )
  134. {
  135. document.write( '<div style="height:'+intHeight+'px;width:'+intWidth+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'.png\', sizingMethod=\'scale\')" id="'+strId+'" class="'+strClass+'"></div>' );
  136. }
  137. else if ( document.pngNormal )
  138. {
  139. document.write( '<img src="'+strPath+'.png" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
  140. }
  141. else if ( document.layers )
  142. {
  143. return( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
  144. }
  145. else
  146. {
  147. document.write( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
  148. }
  149. return( '' );
  150. }
  151. function opacity_init()
  152. {
  153. var browser = new browser_detect();
  154. document.pngAlpha = false;
  155. document.pngNormal = false;
  156. document.strExt = ".gif";
  157.  
  158. if ( ( browser.isIE55 || browser.isIE6up ) && browser.isWin32 )
  159. {
  160. document.pngAlpha = true;
  161. document.strExt = ".png";
  162. }
  163. else if (
  164. ( browser.isGecko ) ||
  165. ( browser.isIE5up && browser.isMac ) ||
  166. ( browser.isOpera && browser.isWin && browser.versionMajor >= 6 ) ||
  167. ( browser.isOpera && browser.isUnix && browser.versionMajor >= 6 ) ||
  168. ( browser.isOpera && browser.isMac && browser.versionMajor >= 5 ) ||
  169. ( browser.isOmniweb && browser.versionMinor >= 3.1 ) ||
  170. ( browser.isIcab && browser.versionMinor >= 1.9 ) ||
  171. ( browser.isWebtv ) ||
  172. ( browser.isDreamcast )
  173. )
  174. {
  175. document.pngNormal = true;
  176. document.strExt = ".png";
  177. }
  178. }
  179.  
  180. /* handler for Netscape Navigator clients that screw up the display
  181.  * of CSS pages when reloaded
  182.  */
  183. function NN_reloadPage( init )
  184. {
  185. if ( init == true ) with ( navigator )
  186. {
  187. if ( ( appName == "Netscape" ) && ( parseInt ( appVersion ) == 4 ) )
  188. {
  189. document.NN_pgW = innerWidth;
  190. document.NN_pgH = innerHeight;
  191. event_attach ( 'onresize' , NN_reloadPage );
  192. }
  193. }
  194. else if ( innerWidth != document.NN_pgW || innerHeight != document.NN_pgH )
  195. {
  196. location.reload();
  197. }
  198. }
  199.  
  200. /* Min Width v1.1.3 by PVII-www.projectseven.com
  201.  * http://www.projectseven.com/tutorials/css/minwidth/index.htm
  202.  *
  203.  * modified for readability and ability to limit application to
  204.  * IE only so CSS min-width property may be used by compliant
  205.  * browsers.
  206.  *
  207.  * NOTE: horizontal spacing (margins, padding, borders) set in
  208.  * % values may cause IE to crash when using this script.
  209.  *
  210.  * ALSO: padding, margins, and borders on parents of the element
  211.  * you specify may result in IE getting suck in an infinite
  212.  * loop. Please be sure to check your layout before you
  213.  * publish it!
  214.  */
  215. function set_min_width( obj_name , min_width , ieOnly )
  216. {
  217. if ( ( typeof( ieOnly ) ).toLowerCase() == 'undefined' )
  218. {
  219. ieOnly = true;
  220. }
  221. if ( ieOnly == false || ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera ) )
  222. {
  223. document.min_width_obj_name = obj_name;
  224. document.min_width_size = min_width;
  225. document.resizing = false;
  226. event_attach( 'onload' , control_min_width );
  227. event_attach( 'onresize' , control_min_width );
  228. }
  229. }
  230. function control_min_width()
  231. {
  232. var cw , w , pl , pr , ml , mr , br , bl , ad , theDiv = document.min_width_obj_name;
  233. var g = document.getElementById( theDiv );
  234. w = parseInt(document.min_width_size);
  235. if ( g && document.body && document.body.clientWidth )
  236. {
  237. gs = g.currentStyle;
  238. cw = parseInt( document.body.clientWidth );
  239. pl = parseInt( gs.paddingLeft );
  240. pr = parseInt( gs.paddingRight );
  241. ml = parseInt( gs.marginLeft );
  242. mr = parseInt( gs.marginRight );
  243. bl = parseInt( gs.borderLeftWidth );
  244. br = parseInt( gs.borderRightWidth );
  245. ml = ml ? ml : 0;
  246. mr = mr ? mr : 0;
  247. pl = pl ? pl : 0;
  248. pr = pr ? pr : 0;
  249. bl = bl ? bl : 0;
  250. br = br ? br : 0;
  251. ad = pl + pr + ml + mr + bl + br;
  252. if ( cw <= w )
  253. {
  254. w -= ad;
  255. g.style.width = w + "px";
  256. }
  257. else
  258. {
  259. g.style.width = "auto";
  260. }
  261. }
  262. }
  263.  
  264. /* Cookie API v1.0.1
  265.  * documentation: http://www.dithered.com/javascript/cookies/index.html
  266.  * license: http://creativecommons.org/licenses/by/1.0/
  267.  * code (mostly) by Chris Nott (chris[at]dithered[dot]com)
  268.  */
  269. function setCookie( name, value, expires, path, domain, secure )
  270. {
  271. var curCookie = name + "=" + escape(value) +
  272. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  273. ((path) ? "; path=" + path : "") +
  274. ((domain) ? "; domain=" + domain : "") +
  275. ((secure) ? "; secure" : "");
  276. document.cookie = curCookie;
  277. }
  278. function getCookie( name )
  279. {
  280. var dc = document.cookie;
  281. var prefix = name + "=";
  282. var begin = dc.indexOf( "; " + prefix );
  283. if ( begin == -1 )
  284. {
  285. begin = dc.indexOf(prefix);
  286. if (begin != 0) return null;
  287. }
  288. else
  289. {
  290. begin += 2;
  291. }
  292. var end = document.cookie.indexOf( ";", begin );
  293. if ( end == -1 )
  294. {
  295. end = dc.length;
  296. }
  297. return unescape(dc.substring(begin + prefix.length, end));
  298. }
  299. function deleteCookie( name, path, domain )
  300. {
  301. var value = getCookie( name );
  302. if ( value != null )
  303. {
  304. document.cookie = name + "=" +
  305. ((path) ? "; path=" + path : "") +
  306. ((domain) ? "; domain=" + domain : "") +
  307. "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  308. }
  309. return value;
  310. }
  311.  
  312. /* font size functions operate on the body element's
  313.  * style and defines sizes in percentages. because
  314.  * the default font size is set to 0 in the array,
  315.  * the first value in the font_sizes array should
  316.  * _ALWAYS_ be 100.
  317.  *
  318.  * var font_sizes = new Array( 100, 110, 120 );
  319.  * var current_font_size = 0;
  320.  * event_attach( 'onload' , loadFontSize );
  321.  */
  322. function loadFontSize()
  323. {
  324. current_font_size = parseInt( '0' + getCookie ( "font_size" ) );
  325. setFontSize ( current_font_size );
  326. }
  327. function setFontSize( size )
  328. {
  329. if( size >= 0 && size < font_sizes.length )
  330. {
  331. current_font_size = size;
  332. }
  333. else if( ++current_font_size >= font_sizes.length )
  334. {
  335. current_font_size = 0;
  336. }
  337. if ( document.body )
  338. {
  339. document.body.style.fontSize = font_sizes[ current_font_size ] + '%';
  340. setCookie( "font_size" , current_font_size );
  341. }
  342. }
  343.  
  344. /* standard trim function to remove leading and trailing
  345.  * whitespace from a given string
  346.  */
  347. function trim( str )
  348. {
  349. return str.replace(/^\s*|\s*$/g,"");
  350. }
  351.  
  352. /* stylesheets should be defined in the HTML via a LINK tag
  353.  * and rel attribute set to "alternate stylesheet". the title
  354.  * attribute is then set in the format of "title : group"
  355.  * this function will disable all but the stylesheet specified
  356.  * by title in the group specified by group.
  357.  *
  358.  * Based on code by Paul Sowden
  359.  * http://www.alistapart.com/articles/alternate/
  360.  *
  361.  */
  362. function setActiveStyleSheet( title , group )
  363. {
  364. var i, a, b, g, t;
  365. if ( !title || !group )
  366. {
  367. return;
  368. }
  369. for ( i = 0; ( a = document.getElementsByTagName( "link" )[ i ] ); i++ )
  370. {
  371. if ( a.getAttribute( "rel" ).indexOf( "style" ) != -1 && a.getAttribute( "title" ) )
  372. {
  373. b = ( a.getAttribute( "title" ) ).split( ":" );
  374. g = trim( b[ b.length - 1 ] );
  375. if ( g.toLowerCase() == group.toLowerCase() )
  376. {
  377. a.disabled = true;
  378. t = trim( ( a.getAttribute( "title" ) ).substring( 0, a.getAttribute( "title" ).length - b[ b.length - 1 ].length - 1 ) );
  379. if( t.toLowerCase() == title.toLowerCase() )
  380. {
  381. a.disabled = false;
  382. }
  383. }
  384. setCookie( "style_" + g.toLowerCase() , title );
  385. }
  386. }
  387. }
  388. function getPreferredStylesheet ( group )
  389. {
  390. return ( getCookie ( "style_" + group ) );
  391. }
  392.  
  393. /* Son of Suckerfish Dropdowns
  394.  * This attaches an event to each LI element so when the mouseover event triggers,
  395.  * the element's class is altered to include (and remove on mouseout) an extra class.
  396.  * We can then use that class, in conjunction with stylesheets, to trigger drop-down
  397.  * menus that are (mostly) CSS-based.
  398.  *
  399.  * Original: http://www.htmldog.com/articles/suckerfish/dropdowns/
  400.  * Fixes to work with IE/Mac: http://carroll.org.uk/sandbox/suckerfish/bones2.html
  401.  */
  402. function sfHover ( objID )
  403. {
  404. var browser = new browser_detect();
  405. if ( browser.isIE && !browser.isIE7up )
  406. {
  407. var sfEls = document.getElementById( objID ).getElementsByTagName( "LI" );
  408. for (var i=0; i<sfEls.length; i++)
  409. {
  410. sfEls[i].onmouseover = function()
  411. {
  412. this.className+=(this.className.length>0? " ": "") + "sfhover";
  413. }
  414. sfEls[i].onmouseout = function()
  415. {
  416. this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
  417. }
  418. }
  419. }
  420. }
  421.  
  422. /*
  423. // ///////////////////////////
  424. // isdefined v1.0
  425. //
  426. // Check if a javascript variable has been defined.
  427. //
  428. // Author : Jehiah Czebotar
  429. // Website: http://www.jehiah.com
  430. // Usage : alert(isdefined('myvar'));
  431. // ///////////////////////////
  432. */
  433. function isDefined ( variable )
  434. {
  435. return ( typeof( window[ variable ] ) == "undefined" ) ? false : true;
  436. }
  437.  
  438. /* DEV CODE : IGNORE ME */
  439. function rmHoverAndDelay( objID )
  440. {
  441. if ( document.getElementById )
  442. {
  443. if ( !isDefined( "rmHaD_guid" ) )
  444. {
  445. rmHaD_guid = 0;
  446. rmHaD_over = function ( e ) { e.className += ( e.className.length > 0 ? " " : "" ) + "sfhover"; }
  447. rmHaD_out = function ( e ) { e.className = e.className.replace( new RegExp( "( ?|^)sfhover\\b" ), "" ); }
  448. rmHaD_Els = document.getElementById( objID ).getElementsByTagName( "LI" );
  449. rmHaD_Last = -1;
  450. rmHaD_Delay = 500;
  451. rmHaD_OverGlobalTimeout = null;
  452. }
  453.  
  454. var i = rmHaD_guid;
  455. rmHaD_guid += rmHaD_Els.length;
  456. var newMaxGUID = rmHaD_guid;
  457.  
  458. for ( ; i < newMaxGUID ; i++ )
  459. {
  460. if ( rmHaD_Els[i].className.indexOf( "rMenu-expand" ) >= 0 )
  461. {
  462. rmHaD_Els[i].rmTimeout = null;
  463. rmHaD_Els[i].rmId = i;
  464. rmHaD_Els[i].onmouseover = function()
  465. {
  466. clearTimeout ( rmHaD_OverGlobalTimeout );
  467. // clearTimeout ( this.rmTimeout );
  468. rmHaD_OverGlobalTimeout = setTimeout( 'rmHaD_over( rmHaD_Els[' + this.rmId + '] )', rmHaD_Delay );
  469. }
  470. rmHaD_Els[i].onmouseout = function()
  471. {
  472. this.rmTimeout = setTimeout( 'rmHaD_out( rmHaD_Els[' + this.rmId + '] )', rmHaD_Delay );
  473. }
  474. }
  475. }
  476. }
  477. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.