source: trunk/www.guidonia.net/wp/wp-content/themes/atahualpa/js/ruthsarian_utilities.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 17.0 KB
Line 
1/*******************************************************************************
2* ruthsarian_utilities.js : 2008.01.22
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 */
18function 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 */
53function 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 */
131function 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}
151function 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.isIE7up && 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 ( browser.isIE7up )
174 )
175 {
176 document.pngNormal = true;
177 document.strExt = ".png";
178 }
179}
180
181/* handler for Netscape Navigator clients that screw up the display
182 * of CSS pages when reloaded
183 */
184function NN_reloadPage( init )
185{
186 if ( init == true ) with ( navigator )
187 {
188 if ( ( appName == "Netscape" ) && ( parseInt ( appVersion ) == 4 ) )
189 {
190 document.NN_pgW = innerWidth;
191 document.NN_pgH = innerHeight;
192 event_attach ( 'onresize' , NN_reloadPage );
193 }
194 }
195 else if ( innerWidth != document.NN_pgW || innerHeight != document.NN_pgH )
196 {
197 location.reload();
198 }
199}
200
201/* Min Width v1.1.3 by PVII-www.projectseven.com
202 * http://www.projectseven.com/tutorials/css/minwidth/index.htm
203 *
204 * modified to support both min and max widths, for readability
205 * and the ability to limit application to IE only so CSS
206 * min-width property may be used by compliant browsers.
207 *
208 * NOTE: horizontal spacing (margins, padding, borders) set in
209 * % values may cause IE to crash when using this script.
210 *
211 * ALSO: padding, margins, and borders on parents of the element
212 * you specify may result in IE getting suck in an infinite
213 * loop. Please be sure to check your layout before you
214 * publish it!
215 */
216function set_min_width( obj_name , min_width , ieOnly )
217{
218 if ( ( typeof( ieOnly ) ).toLowerCase() == 'undefined' ) { ieOnly = true; }
219 set_width_limits( obj_name, min_width, '', ieOnly );
220}
221function set_width_limits( obj_name , min_width , max_width, ieOnly )
222{
223 if ( ( typeof( ieOnly ) ).toLowerCase() == 'undefined' )
224 {
225 ieOnly = true;
226 }
227 if ( ieOnly == false || ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera ) )
228 {
229 document.set_width_obj_name = obj_name;
230 document.min_width_size = min_width;
231 document.max_width_size = max_width;
232 document.resizing = false;
233 event_attach( 'onload' , control_width );
234 event_attach( 'onresize' , control_width );
235 }
236}
237function control_width()
238{
239 var cw , minw , maxw, gs, pl , pr , ml , mr , br , bl , ad , theDiv = document.set_width_obj_name;
240 var g = document.getElementById( theDiv );
241 minw = parseInt( document.min_width_size );
242 maxw = parseInt( document.max_width_size );
243 if ( g && document.body && document.body.clientWidth )
244 {
245 gs = g.currentStyle;
246 cw = parseInt( document.body.clientWidth );
247 pl = parseInt( gs.paddingLeft );
248 pr = parseInt( gs.paddingRight );
249 ml = parseInt( gs.marginLeft );
250 mr = parseInt( gs.marginRight );
251 bl = parseInt( gs.borderLeftWidth );
252 br = parseInt( gs.borderRightWidth );
253 ml = ml ? ml : 0;
254 mr = mr ? mr : 0;
255 pl = pl ? pl : 0;
256 pr = pr ? pr : 0;
257 bl = bl ? bl : 0;
258 br = br ? br : 0;
259 ad = pl + pr + ml + mr + bl + br;
260 if ( cw <= minw )
261 {
262 minw -= ad;
263 g.style.width = minw + "px";
264 }
265 else if ( cw > maxw )
266 {
267 maxw -= ad;
268 g.style.width = maxw + "px";
269 }
270 else
271 {
272 g.style.width = "auto";
273 }
274 }
275}
276
277/* Cookie API v1.0.1
278 * documentation: http://www.dithered.com/javascript/cookies/index.html
279 * license: http://creativecommons.org/licenses/by/1.0/
280 * code (mostly) by Chris Nott (chris[at]dithered[dot]com)
281 */
282function setCookie( name, value, expires, path, domain, secure )
283{
284 var curCookie = name + "=" + escape(value) +
285 ((expires) ? "; expires=" + expires.toGMTString() : "") +
286 ((path) ? "; path=" + path : "") +
287 ((domain) ? "; domain=" + domain : "") +
288 ((secure) ? "; secure" : "");
289 document.cookie = curCookie;
290}
291function getCookie( name )
292{
293 var dc = document.cookie;
294 var prefix = name + "=";
295 var begin = dc.indexOf( "; " + prefix );
296 if ( begin == -1 )
297 {
298 begin = dc.indexOf(prefix);
299 if (begin != 0) return null;
300 }
301 else
302 {
303 begin += 2;
304 }
305 var end = document.cookie.indexOf( ";", begin );
306 if ( end == -1 )
307 {
308 end = dc.length;
309 }
310 return unescape(dc.substring(begin + prefix.length, end));
311}
312function deleteCookie( name, path, domain )
313{
314 var value = getCookie( name );
315 if ( value != null )
316 {
317 document.cookie = name + "=" +
318 ((path) ? "; path=" + path : "") +
319 ((domain) ? "; domain=" + domain : "") +
320 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
321 }
322 return value;
323}
324
325/* font size functions operate on the body element's
326 * style and defines sizes in percentages. because
327 * the default font size is set to 0 in the array,
328 * the first value in the font_sizes array should
329 * _ALWAYS_ be 100.
330 *
331 * var font_sizes = new Array( 100, 110, 120 );
332 * var current_font_size = 0;
333 * event_attach( 'onload' , loadFontSize );
334 */
335function loadFontSize()
336{
337 current_font_size = parseInt( '0' + getCookie ( "font_size" ) );
338 setFontSize ( current_font_size );
339}
340function setFontSize( size )
341{
342 if( size >= 0 && size < font_sizes.length )
343 {
344 current_font_size = size;
345 }
346 else if( ++current_font_size >= font_sizes.length )
347 {
348 current_font_size = 0;
349 }
350 if ( document.body )
351 {
352 document.body.style.fontSize = font_sizes[ current_font_size ] + '%';
353 setCookie( "font_size" , current_font_size );
354 }
355}
356
357/* standard trim function to remove leading and trailing
358 * whitespace from a given string
359 */
360function trim( str )
361{
362 return str.replace(/^\s*|\s*$/g,"");
363}
364
365/* stylesheets should be defined in the HTML via a LINK tag
366 * and rel attribute set to "alternate stylesheet". the title
367 * attribute is then set in the format of "title : group"
368 * this function will disable all but the stylesheet specified
369 * by title in the group specified by group.
370 *
371 * Based on code by Paul Sowden
372 * http://www.alistapart.com/articles/alternate/
373 *
374 */
375function setActiveStyleSheet( title , group )
376{
377 var i, a, b, g, t;
378 if ( !title || !group )
379 {
380 return;
381 }
382 for ( i = 0; ( a = document.getElementsByTagName( "link" )[ i ] ); i++ )
383 {
384 if ( a.getAttribute( "rel" ).indexOf( "style" ) != -1 && a.getAttribute( "title" ) )
385 {
386 b = ( a.getAttribute( "title" ) ).split( ":" );
387 g = trim( b[ b.length - 1 ] );
388 if ( g.toLowerCase() == group.toLowerCase() )
389 {
390 a.disabled = true;
391 t = trim( ( a.getAttribute( "title" ) ).substring( 0, a.getAttribute( "title" ).length - b[ b.length - 1 ].length - 1 ) );
392 if( t.toLowerCase() == title.toLowerCase() )
393 {
394 a.disabled = false;
395 }
396 setCookie( "style_" + g.toLowerCase() , title );
397 }
398 }
399 }
400}
401function getPreferredStylesheet ( group )
402{
403 return ( getCookie ( "style_" + group ) );
404}
405
406/* Son of Suckerfish Dropdowns w/Mac support and IFRAME matting
407 * This attaches an event to each LI element so when the mouseover event triggers,
408 * the element's class is altered to include (and remove on mouseout) an extra class.
409 * We can then use that class, in conjunction with stylesheets, to trigger drop-down
410 * menus that are (mostly) CSS-based.
411 *
412 * The second variable passed to sfHover (noMat), if set to true, will disable
413 * the IFRAME matting used to hide form elements that peek through if the menu
414 * appears over one. Use this option when there's no chance the menu will pop over
415 * a form field as this will remove the lag/performance issues related to using
416 * the IFRAME matting.
417 *
418 * Original:
419 * http://www.htmldog.com/articles/suckerfish/dropdowns/
420 * Fixes to work with IE/Mac:
421 * http://carroll.org.uk/sandbox/suckerfish/bones2.html
422 * IFRAME matting to handle hover over form elements:
423 * http://homepage.mac.com/igstudio/design/ulsmenus/vertical-uls-iframe-2.html
424 */
425function sfHover ( objID, noMat )
426{
427 var browser = new browser_detect();
428 if ( browser.isIE5up && !browser.isIE7up )
429 {
430 var sfEls = document.getElementById( objID ).getElementsByTagName( "LI" );
431 for (var i=0; i<sfEls.length; i++)
432 {
433 if ( !noMat && !browser.isMac && ( browser.isIE55 || browser.isIE6x ))
434 {
435 sfEls[i].onmouseover = function()
436 {
437 this.className += ( this.className.length > 0 ? " " : "" ) + "sfhover";
438 var ieUL = this.getElementsByTagName( "UL" )[0];
439 if ( ieUL )
440 {
441 var ieMat = document.createElement( "IFRAME" );
442 ieMat.style.width = ieUL.offsetWidth + "px";
443 ieMat.style.height = ieUL.offsetHeight + "px";
444 ieMat.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
445 ieUL.insertBefore( ieMat, ieUL.firstChild );
446 ieMat.style.zIndex = "-1";
447 }
448 }
449 sfEls[i].onmouseout = function()
450 {
451 this.className = this.className.replace( new RegExp( "( ?|^)sfhover\\b" ), "" );
452 var ieUL = this.getElementsByTagName('ul')[0];
453 if (ieUL)
454 {
455 ieUL.removeChild( ieUL.firstChild );
456 }
457 }
458 }
459 else
460 {
461 sfEls[i].onmouseover = function()
462 {
463 this.className += ( this.className.length > 0 ? " " : "" ) + "sfhover";
464 }
465 sfEls[i].onmouseout = function()
466 {
467 this.className = this.className.replace( new RegExp( "( ?|^)sfhover\\b" ), "" );
468 }
469 }
470 }
471 }
472}
473
474/*
475// ///////////////////////////
476// isdefined v1.0
477//
478// Check if a javascript variable has been defined.
479//
480// Author : Jehiah Czebotar
481// Website: http://www.jehiah.com
482// Usage : alert(isdefined('myvar'));
483// ///////////////////////////
484*/
485function isDefined ( variable )
486{
487 return ( typeof( window[ variable ] ) == "undefined" ) ? false : true;
488}
489
490/* pause()
491 * modified form of code taken from:
492 * http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
493 */
494function pause ( m )
495{
496 var date = new Date();
497 var curDate = null;
498 do
499 {
500 curDate = new Date();
501 }
502 while (( curDate - date ) < m );
503}
Note: See TracBrowser for help on using the repository browser.