web/wp-content/themes/inove/js/menu.js
changeset 1 0d28b7c10758
equal deleted inserted replaced
0:0d9a58d2c515 1:0d28b7c10758
       
     1 /*
       
     2 Author: mg12
       
     3 Update: 2009/08/07
       
     4 Author URI: http://www.neoease.com/
       
     5 */
       
     6 (function() {
       
     7 
       
     8 var Class = {
       
     9 	create: function() {
       
    10 		return function() {
       
    11 			this.initialize.apply(this, arguments);
       
    12 		}
       
    13 	}
       
    14 }
       
    15 
       
    16 var GhostlyMenu = Class.create();
       
    17 GhostlyMenu.prototype = {
       
    18 
       
    19 	initialize: function(target, align, sub) {
       
    20 		this.obj = cleanWhitespace(target);
       
    21 		this.align = align || 'left';
       
    22 		this.sub = sub || -1;
       
    23 
       
    24 		this.menu = this.obj.childNodes;
       
    25 		if (this.menu.length < 2) { return; }
       
    26 
       
    27 		this.title = this.menu[0];
       
    28 		this.body = this.menu[1];
       
    29 
       
    30 		cleanWhitespace(this.body).lastChild.getElementsByTagName('a')[0].className += ' last';
       
    31 
       
    32 		setStyle(this.body, 'visibility', 'hidden');
       
    33 		setStyle(this.body, 'display', 'block');
       
    34 
       
    35 		addListener(this.obj, 'mouseover', bind(this, this.activate), false);
       
    36 		addListener(this.obj, 'mouseout', bind(this, this.deactivate), false);
       
    37 	},
       
    38 
       
    39 	activate: function() {
       
    40 		if(this.sub == 1) {
       
    41 			var pos = currentOffset(this.title);
       
    42 			var top = pos[1] - 1;
       
    43 			var left = getWidth(this.body) - 2;
       
    44 			if (this.align == 'right') {
       
    45 			var left = getWidth(this.body) * (-1);
       
    46 			}
       
    47 		} else {
       
    48 			var pos = cumulativeOffset(this.title);
       
    49 			var top = pos[1] + getHeight(this.title);
       
    50 			var left = pos[0];
       
    51 			if (this.align == 'right') {
       
    52 				left += getWidth(this.title) - getWidth(this.body);
       
    53 			}
       
    54 		}
       
    55 
       
    56 		if(!/current/.test(this.title.className)) {
       
    57 			this.title.className += ' current';
       
    58 		}
       
    59 
       
    60 		setStyle(this.body, 'left', left + 'px');
       
    61 		setStyle(this.body, 'top', top + 'px');
       
    62 		setStyle(this.body, 'visibility', 'visible');
       
    63 	},
       
    64 
       
    65 	deactivate: function(){
       
    66 		this.title.className = this.title.className.replace('current', '');
       
    67 		var thismenu = this;
       
    68 		var tid = setInterval( function() {
       
    69 			clearInterval(tid);
       
    70 			if (!/current/.test(thismenu.title.className)) {
       
    71 				setStyle(thismenu.body, 'visibility', 'hidden');
       
    72 			}
       
    73 			return false;
       
    74 		}, 400);
       
    75 	}
       
    76 }
       
    77 
       
    78 $A = function(iterable) {
       
    79 	if(!iterable) {
       
    80 		return [];
       
    81 	}
       
    82 	if(iterable.toArray) {
       
    83 		return iterable.toArray();
       
    84 	} else {
       
    85 		var results = [];
       
    86 		for(var i = 0; i < iterable.length; i++) {
       
    87 			results.push(iterable[i]);
       
    88 		}
       
    89 		return results;
       
    90 	}
       
    91 }
       
    92 
       
    93 bind = function() {
       
    94 	var array = this.$A(arguments);
       
    95 	var func = array[array.length - 1];
       
    96 	var method = func, args = array, object = args.shift();
       
    97 	return function() {
       
    98 		return method.apply(object, args.concat(array));
       
    99 	}
       
   100 }
       
   101 
       
   102 getHeight = function(element) {
       
   103 	return element.offsetHeight;
       
   104 }
       
   105 
       
   106 getWidth = function(element) {
       
   107 	return element.offsetWidth;
       
   108 }
       
   109 
       
   110 setStyle = function(element, key, value) {
       
   111 	element.style[key] = value;
       
   112 }
       
   113 
       
   114 cleanWhitespace = function(list) {
       
   115 	var node = list.firstChild;
       
   116 	while (node) {
       
   117 		var nextNode = node.nextSibling;
       
   118 		if(node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
       
   119 			list.removeChild(node);
       
   120 		}
       
   121 		node = nextNode;
       
   122 	}
       
   123 	return list;
       
   124 }
       
   125 
       
   126 currentOffset = function(element) {
       
   127 	var valueT = element.offsetTop  || 0;
       
   128 	var valueL = element.offsetLeft || 0;
       
   129 	return [valueL, valueT];
       
   130 }
       
   131 
       
   132 cumulativeOffset = function(element) {
       
   133 	var valueT = 0, valueL = 0;
       
   134 	do {
       
   135 		valueT += element.offsetTop  || 0;
       
   136 		valueL += element.offsetLeft || 0;
       
   137 		element = element.offsetParent;
       
   138 	} while (element);
       
   139 	return [valueL, valueT];
       
   140 }
       
   141 
       
   142 addListener = function(element, name, observer, useCapture) {
       
   143 	if(element.addEventListener) {
       
   144 		element.addEventListener(name, observer, useCapture);
       
   145 	} else if(element.attachEvent) {
       
   146 		element.attachEvent('on' + name, observer);
       
   147 	}
       
   148 }
       
   149 
       
   150 function loadMenus() {
       
   151 	var align = 'left';
       
   152 	for(var i = 0; (a = document.getElementsByTagName('link')[i]); i++) {
       
   153 		if((a.getAttribute('rel') == 'stylesheet') && (a.getAttribute('href').indexOf('rtl.css') != -1)) {
       
   154 			align = 'right';
       
   155 		}
       
   156 	}
       
   157 
       
   158 	var subscribe = document.getElementById('subscribe');
       
   159 	if (subscribe) {
       
   160 		new GhostlyMenu(subscribe, align);
       
   161 	}
       
   162 
       
   163 	var menubar = document.getElementById('menus');
       
   164 	if (menubar) {
       
   165 		var list = menubar.getElementsByTagName('ul');
       
   166 		for (var i = 0; i < list.length; i++) {
       
   167 			var menu = list[i].parentNode;
       
   168 			if(menu.parentNode === menubar) {
       
   169 				new GhostlyMenu(menu, align);
       
   170 			} else {
       
   171 				new GhostlyMenu(menu, align, 1);
       
   172 				menu.firstChild.className += ' subtitle';
       
   173 			}
       
   174 		}
       
   175 	}
       
   176 }
       
   177 
       
   178 if (document.addEventListener) {
       
   179 	document.addEventListener("DOMContentLoaded", loadMenus, false);
       
   180 
       
   181 } else if (/MSIE/i.test(navigator.userAgent)) {
       
   182 	document.write('<script id="__ie_onload_for_inove" defer src="javascript:void(0)"></script>');
       
   183 	var script = document.getElementById('__ie_onload_for_inove');
       
   184 	script.onreadystatechange = function() {
       
   185 		if (this.readyState == 'complete') {
       
   186 			loadMenus();
       
   187 		}
       
   188 	}
       
   189 
       
   190 } else if (/WebKit/i.test(navigator.userAgent)) {
       
   191 	var _timer = setInterval( function() {
       
   192 		if (/loaded|complete/.test(document.readyState)) {
       
   193 			clearInterval(_timer);
       
   194 			loadMenus();
       
   195 		}
       
   196 	}, 10);
       
   197 
       
   198 } else {
       
   199 	window.onload = function(e) {
       
   200 		loadMenus();
       
   201 	}
       
   202 }
       
   203 
       
   204 })();