/* Script by: www.jtricks.com * Version: 20071017 * Latest version: * www.jtricks.com/javascript/navigation/floating.html */ var shfloatingMenuId = 'shfloatdiv'; var shfloatingMenu = { targetX: 300, targetY: 200, hasInner: typeof(window.innerWidth) == 'number', hasElement: typeof(document.documentElement) == 'object' && typeof(document.documentElement.clientWidth) == 'number', menu: document.getElementById ? document.getElementById(shfloatingMenuId) : document.all ? document.all[shfloatingMenuId] : document.layers[shfloatingMenuId] }; shfloatingMenu.move = function () { shfloatingMenu.menu.style.left = shfloatingMenu.nextX + 'px'; shfloatingMenu.menu.style.top = shfloatingMenu.nextY + 'px'; } shfloatingMenu.computeShifts = function () { var de = document.documentElement; shfloatingMenu.shiftX = shfloatingMenu.hasInner ? pageXOffset : shfloatingMenu.hasElement ? de.scrollLeft : document.body.scrollLeft; if (shfloatingMenu.targetX < 0) { shfloatingMenu.shiftX += shfloatingMenu.hasElement ? de.clientWidth : document.body.clientWidth; } shfloatingMenu.shiftY = shfloatingMenu.hasInner ? pageYOffset : shfloatingMenu.hasElement ? de.scrollTop : document.body.scrollTop; if (shfloatingMenu.targetY < 0) { if (shfloatingMenu.hasElement && shfloatingMenu.hasInner) { // Handle Opera 8 problems shfloatingMenu.shiftY += de.clientHeight > window.innerHeight ? window.innerHeight : de.clientHeight } else { shfloatingMenu.shiftY += shfloatingMenu.hasElement ? de.clientHeight : document.body.clientHeight; } } } shfloatingMenu.calculateCornerX = function() { if (shfloatingMenu.targetX != 'center') return shfloatingMenu.shiftX + shfloatingMenu.targetX; var width = parseInt(shfloatingMenu.menu.offsetWidth); var cornerX = shfloatingMenu.hasElement ? (shfloatingMenu.hasInner ? pageXOffset : document.documentElement.scrollLeft) + (document.documentElement.clientWidth - width)/2 : document.body.scrollLeft + (document.body.clientWidth - width)/2; return cornerX; }; shfloatingMenu.calculateCornerY = function() { if (shfloatingMenu.targetY != 'center') return shfloatingMenu.shiftY + shfloatingMenu.targetY; var height = parseInt(shfloatingMenu.menu.offsetHeight); // Handle Opera 8 problems var clientHeight = shfloatingMenu.hasElement && shfloatingMenu.hasInner && document.documentElement.clientHeight > window.innerHeight ? window.innerHeight : document.documentElement.clientHeight var cornerY = shfloatingMenu.hasElement ? (shfloatingMenu.hasInner ? pageYOffset : document.documentElement.scrollTop) + (clientHeight - height)/2 : document.body.scrollTop + (document.body.clientHeight - height)/2; return cornerY; }; shfloatingMenu.doFloat = function() { // Check if reference to menu was lost due // to ajax manipuations if (!shfloatingMenu.menu) { menu = document.getElementById ? document.getElementById(shfloatingMenuId) : document.all ? document.all[shfloatingMenuId] : document.layers[shfloatingMenuId]; initSecondary(); } var stepX, stepY; shfloatingMenu.computeShifts(); var cornerX = shfloatingMenu.calculateCornerX(); var stepX = (cornerX - shfloatingMenu.nextX) * .07; if (Math.abs(stepX) < .5) { stepX = cornerX - shfloatingMenu.nextX; } var cornerY = shfloatingMenu.calculateCornerY(); var stepY = (cornerY - shfloatingMenu.nextY) * .07; if (Math.abs(stepY) < .5) { stepY = cornerY - shfloatingMenu.nextY; } if (Math.abs(stepX) > 0 || Math.abs(stepY) > 0) { shfloatingMenu.nextX += stepX; shfloatingMenu.nextY += stepY; shfloatingMenu.move(); } setTimeout('shfloatingMenu.doFloat()', 20); }; // addEvent designed by Aaron Moore shfloatingMenu.addEvent = function(element, listener, handler) { if(typeof element[listener] != 'function' || typeof element[listener + '_num'] == 'undefined') { element[listener + '_num'] = 0; if (typeof element[listener] == 'function') { element[listener + 0] = element[listener]; element[listener + '_num']++; } element[listener] = function(e) { var r = true; e = (e) ? e : window.event; for(var i = element[listener + '_num'] -1; i >= 0; i--) { if(element[listener + i](e) == false) r = false; } return r; } } //if handler is not already stored, assign it for(var i = 0; i < element[listener + '_num']; i++) if(element[listener + i] == handler) return; element[listener + element[listener + '_num']] = handler; element[listener + '_num']++; }; shfloatingMenu.init = function() { shfloatingMenu.initSecondary(); shfloatingMenu.doFloat(); }; // Some browsers init scrollbars only after // full document load. shfloatingMenu.initSecondary = function() { shfloatingMenu.computeShifts(); shfloatingMenu.nextX = shfloatingMenu.calculateCornerX(); shfloatingMenu.nextY = shfloatingMenu.calculateCornerY(); shfloatingMenu.move(); } if (document.layers) shfloatingMenu.addEvent(window, 'onload', shfloatingMenu.init); else { shfloatingMenu.init(); shfloatingMenu.addEvent(window, 'onload', shfloatingMenu.initSecondary); }