

/*	EventCache Version 1.0
	Copyright 2005 Mark Wubben

	Provides a way for automagically removing events from nodes and thus preventing memory leakage.
	See <http://novemberborn.net/javascript/event-cache> for more information.
	
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

/*	Implement array.push for browsers which don't support it natively.
	Please remove this if it's already in other code */
if(Array.prototype.push == null){
	Array.prototype.push = function(){
		for(var i = 0; i < arguments.length; i++){
			this[this.length] = arguments[i];
		};
		return this.length;
	};
};

/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
var EventCache = function(){
	var listEvents = [];
	
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();

//End Event Cache





sM = {
 init: function() {
   var uls = document.getElementsByTagName('ul');
   for (var u = 0; u < uls.length; u++) {
     if (uls[u].className.search(/\bslidingmenu\b/) == -1) continue;
     var lis = uls[u].getElementsByTagName('li');
     for (var i = 0; i < lis.length; i++) {
       var node = lis[i];
       if (node.nodeName.toLowerCase() == 'li' &&
           node.getElementsByTagName('ul').length > 0) {
         sM.addEvent(node, 'mouseover', sM.getMoverFor(node), false);
         sM.addEvent(node, 'mouseout', sM.getMoutFor(node), false);
         node.getElementsByTagName('a')[0].className += ' subheader';
         node.isIn = false;
       }
     }
   }
 },

 getMoverFor: function(node) {
   return function(e) { sM.mover(e, node); };
 },

 getMoutFor: function(node) {
   return function(e) { sM.mout(e, node); };
 },

 mover: function(e, targetElement) {
   var el = window.event ? targetElement : e ? e.currentTarget : null;
   if (!el) return;
   clearTimeout(el.outTimeout);
   if (!el.isIn) {
     for (var i = 0; i < el.childNodes.length; i++) {
       var node = el.childNodes[i];
       if (node.nodeName.toLowerCase() == 'ul') {
         // Stop current animation
         clearInterval(node.intervalID);
         // Assign initial visible area
         node.clippingRectangle = [0, 0, 4, 0];
         // Save full width and height
         node.style.display = 'block';
         node.savedOW = node.offsetWidth;
         node.savedOH = node.offsetHeight;
//         node.style.display = 'none';
         // Start animation
		// alert("sadfsd");
		 //looks like we can add in background change here...
		
		//targetElement.style.backgroundColor="#FFFFFF";
		//alert(targetElement.innerHTML+targetElement.style.backgroundColor);
		//targetElement.style.color="#00FF00";
		 //targetElement.style.backgroundImage="none";
		 //targetElement.style.borderColor="#003B51";
		 //targetElement.style.borderWidth="1px 1px 0px 1px";
		 //targetElement.style.borderStyle="solid";

//node.intervalID = setInterval(function() { sM.showMenu(node); }, 10);

         break;
       }
     }
   }
   el.isIn = true;
   el.className += '';   // Force IE to recompute styles
 },

 mout: function(e, targetElement) {
   var el = window.event ? targetElement : e ? e.currentTarget : null;
   if (!el) return;
   el.outTimeout = setTimeout(function() { sM.mout2(el); }, 40);
 },

 mout2: function(el) {
	 //ok assume this is mout of the top ul so.... how do we get the top button and reset it?
	 //!ooh looks good!
	 el.style.backgroundColor="";
	//	 el.style.backgroundImage="url('../images/vertical_toolbar_button.gif')";
	//	 el.style.borderColor="#B0C8A2";
	//	 el.style.borderWidth="0px 1px 0px 1px";
//	 alert(el.innerHTML);
   for (var i = 0; i < el.childNodes.length; i++) {
     var node = el.childNodes[i];
     if (node.nodeName.toLowerCase() == 'ul') {
       // Stop current animation
       clearInterval(node.intervalID);
       // Start animation

node.intervalID = setInterval(function() { sM.hideMenu(node); }, 1);

       break;
     }
   }
   el.isIn = false;
 },

 showMenu: function(el) {
   el.clippingRectangle[1] += 200;
   if (el.clippingRectangle[1] >= el.savedOW) {
     el.clippingRectangle[1] = el.savedOW;
     el.clippingRectangle[2] += 10;
     if (el.clippingRectangle[2] >= el.savedOH) {
       el.clippingRectangle[2] = el.savedOH;
       clearInterval(el.intervalID);
       // reset the clip: browser-specific
       if (document.all && !window.opera) {
         el.style.clip = 'rect(auto)';
       } else {
         el.style.clip = '';
       }
       return;
     }
   }
   el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
   el.style.display = 'block';
 },

 hideMenu: function(el) {
   el.clippingRectangle[2] -= 10;
   if (el.clippingRectangle[2] <= 4) {
     el.clippingRectangle[2] = 4;
     el.clippingRectangle[1] -= 200;
     if (el.clippingRectangle[1] <= 0) {
       clearInterval(el.intervalID);
       // reset the clip: browser-specific
       if (document.all && !window.opera) {
 try{ el.style.clip = 'rect(auto)';}
 catch(er){}
       } else {
         el.style.clip = '';
       }
       el.style.display = 'none';
       return;
     }
   }
   el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
 },

 addEvent: function(elm, evType, fn, useCapture) {
   // cross-browser event handling for IE5+, NS6 and Mozilla
   // By Scott Andrew
   if (elm.addEventListener) {
     elm.addEventListener(evType, fn, useCapture);
     return true;
   } else if (elm.attachEvent) {
     var r = elm.attachEvent('on' + evType, fn);

		EventCache.add(elm, evType, fn);
     return r;
   } else {
     elm['on' + evType] = fn;
   }
 }
};

sM.addEvent(window, 'load', sM.init, false);
sM.addEvent(window, 'unload', EventCache.flush, false);

