function AnimatedList(aID, aTagName, aClassName, aParam1, aParam2, aMode) { if (!arguments.callee.apply) { return false; } this.handlersList = []; this.dependentLists = []; this.listID = aID; this.openerTag = aTagName; this.className = aClassName; this.openingMode = aMode; this.openersPreparator(aParam1, aParam2); if (!this.itemsProto.isSet) { this.itemsProto.call(this.itemsConstructor.prototype, this.contexAdaptor); } this.progectLoader(this.contexAdaptor .call(this.listInitializer, this)); } AnimatedList.prototype = { openersPreparator: function (aParam1, aParam2) { if (this.openerTag === 'img') { (new Image).src = aParam1; (new Image).src = aParam2; } this.openedVal = aParam1; this.closedVal = aParam2; }, openersCreator: function (aTag) { var shell = document.createElement('a'), elem = document.createElement(aTag); if (aTag === 'img') { elem.setAttribute('src', this.closedVal); } else { elem.appendChild( document.createTextNode(this.closedVal)); } shell.appendChild(elem); shell.setAttribute('href', '#'); return shell; }, listInitializer: function (aList, aBool) { aList = aList || document.getElementById(this.listID); if (!aList) { return false; } if (!this.listTag) { this.listTag = aList.tagName.toLowerCase(); } var items = aList.getElementsByTagName('li'), len = items.length, listTag = this.listTag, openerTag = this.openerTag, className = this.className, dependentLists = this.dependentLists, currItem, childList, openerNode, newObj; while (len--) { currItem = items[len]; if (currItem.parentNode === aList) { childList = currItem.getElementsByTagName(listTag)[0]; if (childList) { this.listInitializer(childList, true); openerNode = this.openersCreator(openerTag); childList.className = className; currItem.insertBefore(openerNode, childList); newObj = new this.itemsConstructor(this, childList, openerNode, dependentLists.length); dependentLists[dependentLists.length] = newObj; this.addHandler(openerNode, 'click', newObj.onClickHandler .adapt(newObj, true)); } } } if (!aBool && this.handlersList.length) { this.addHandler(window, 'unload', this.contexAdaptor .call(this.handlersRemover, this)); } }, progectLoader: function (aCallBack) { if (!document.getElementById && !document.createElement) { return false; } var script, strIE = '<script defer src="//:" id="scriptForIE' + this.listID + '"><\/script>', opera9 = window.opera && window.opera.version && parseInt(window.opera.version()) >= 9; if (document.addEventListener && !window.opera || opera9) { document.addEventListener('DOMContentLoaded', aCallBack, false); } else if (/WebKit|Khtml/i.test(navigator.userAgent) || window.opera && !opera9 && document.readyState) { return (function () { (/loaded|complete/.test(document.readyState)) ? aCallBack() : setTimeout(arguments.callee, 100); })(); } if (/MSIE/i.test(navigator.userAgent)) { document.write(strIE); script = document.getElementById('scriptForIE' + this.listID); if (!script) { return false; } script.onreadystatechange = function () { if (this.readyState === 'complete') { aCallBack(); script = null; } } } }, addHandler: function (aObj, aEvent, aFunc) { if (aObj.addEventListener) { aObj.addEventListener(aEvent, aFunc, false); } else if (aObj.attachEvent) { try { var b = aObj.attachEvent('on' + aEvent, aFunc); if (b) { this.handlersList.push(arguments); } } catch (aEx) {} } }, removeHandler: function (aObj, aEvent, aFunc) { aObj.detachEvent('on' + aEvent, aFunc); }, handlersRemover: function () { var list = this.handlersList, len = list.length; while (len--) { this.removeHandler.apply(this, list[len]); } }, itemsConstructor: function (aEngine, aList, aOpener, aID) { this.listItems = []; this.openerNode = aOpener; this.openedVal = aEngine.openedVal; this.closedVal = aEngine.closedVal; this.openingMode = aEngine.openingMode; this.listIndex = 0; this.indexIncrement = 1; this.itemsInitializer(aList, aEngine, aID); }, itemsProto: function (aFunc) { arguments.callee.isSet = true; this.intervalDelay = 100; this.itemsInitializer = function (aList, aEngine, aID) { var currItem, listItems = this.listItems, listTag = aEngine.listTag, items = aList.getElementsByTagName('li'), len = items.length; while (len--) { currItem = items[len]; if (currItem.parentNode === aList) { listItems.push(currItem); } if (currItem.getElementsByTagName(listTag)[0]) { this.childList = aEngine.dependentLists[aID - 1]; } } if (this.openingMode) { listItems.reverse(); } }; this.onClickHandler = function (aEvent, aBool) { var child = this.childList; if (this.timerID) { this.sequenceBreaker(this.indexIncrement); this.listIndex += this.indexIncrement; } else if (child && this.indexIncrement < 0) { child.onClickHandler(null, true); } else if (aBool) { this.indexIncrement = -1; } this.openerChanger(this.indexIncrement); return (aEvent) ? (aEvent.preventDefault) ? aEvent.preventDefault() : aEvent.returnValue = false : false; }; this.sequenceMaker = function (aInc, aFunc) { var items = this.listItems, index = this.listIndex; this.visibilityChanger(items[index], aInc); if (items[index + aInc]) { this.listIndex += aInc; this.timerID = window.setTimeout( aFunc.adapt(this, false, aInc, aFunc), this.intervalDelay); } else { this.sequenceBreaker(aInc); } }; this.sequenceBreaker = function (aInc) { window.clearTimeout(this.timerID); this.indexIncrement = -aInc; this.timerID = false; }; this.openerChanger = function (aInc) { var attr, el = this.openerNode.firstChild; if (el.tagName.toLowerCase() === 'img') { attr = 'src'; } else { attr = 'nodeValue'; el = el.firstChild; } el[attr] = (aInc > 0) ? this.openedVal : this.closedVal; this.sequenceMaker(aInc, this.sequenceMaker); }; this.visibilityChanger = function (aObj, aInc) { aObj.style.display = (aInc > 0) ? 'block' : 'none'; }; this.sequenceMaker.adapt = this.onClickHandler.adapt = aFunc; }, contexAdaptor: function (aContext, aBool) { var caller = this, arr = Array.prototype, args = arr.slice.call(arguments, 2); return function () { caller.apply(aContext, aBool ? arr.slice.call(arguments) .concat(args) : args); } } }
