/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.3.17	author			: Maurice van Creij	dependencies	: jquery.classbehaviours.js	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.
    
    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};
	
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};
	
	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// make all sub elements fake the :hover attribute with .hover
	jQuery.classBehaviours.handlers.pseudoHover = {
		// properties
		name: 'pseudoHover',
		index: 0,
		timeout: null,
		timeout2: null,
		// methods
		start: function(node){
			// fix the width for internet explorer 7
			/*
			if(navigator.userAgent.indexOf('MSIE ')>-1){
				for(var a=0; a<node.childNodes.length; a++){
					if(node.childNodes[a].nodeName=='LI'){
						node.childNodes[a].style.width = (node.childNodes[a].getElementsByTagName('A')[0].offsetWidth + 1) + 'px';
					}
				}
			}
			*/
			//for the childnodes of the navigation
			allItems = node.getElementsByTagName('LI');
			for(var a=0; a<allItems.length; a++){
				if(allItems[a].nodeName == 'LI'){
					// add starting properties
					allItems[a].className += (allItems[a].className.indexOf('link')<0) ? ' link' : '' ;
					// add the mouse events
					allItems[a].onmouseover = this.hoverOver;
					allItems[a].onmouseout = this.hoverOff;
				}
			}
		},
		resetSub: function(objNode, subMenu){
			var psh = jQuery.classBehaviours.handlers.pseudoHover;
			// cancel the end animation
			subMenuTimeout = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'time', null);
			clearTimeout(subMenuTimeout);
			// reset the animation
			jQuery.classBehaviours.utilities.setClassParameter(subMenu, 'step', '0');
		},
		animateSub: function(objNode, subMenu){
				var psh = jQuery.classBehaviours.handlers.pseudoHover;
				// give the submenu an id if it doesn't have one yet
				subMenu.id = (subMenu.id) ? subMenu.id : psh.name + psh.index++ ;
				// set the animation properties
				jQuery.classBehaviours.utilities.setClassParameter(objNode, 'sub', subMenu.id);
				jQuery.classBehaviours.utilities.setClassParameter(subMenu, 'step', '4');
				// start the animation
				psh.animateStep(subMenu);
		},
		animateStep: function(subMenu){
			var objNode = subMenu.parentNode;
			var psh = jQuery.classBehaviours.handlers.pseudoHover;
			// get the current animation step
			currentStep = parseInt(jQuery.classBehaviours.utilities.getClassParameter(subMenu, 'step', '0'));
			// if the step isn't 0 yet
			if(currentStep>0){
				// set the new animation step
				jQuery.classBehaviours.utilities.setClassParameter(subMenu, 'step', currentStep-1);
				// order a new step
				subMenuTimeout = setTimeout('jQuery.classBehaviours.handlers.pseudoHover.animateStep(document.getElementById("' + subMenu.id + '"))', 100);
				jQuery.classBehaviours.utilities.setClassParameter(objNode, 'time', subMenuTimeout);
			}
		},
		// events
		hoverOver: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var psh = jQuery.classBehaviours.handlers.pseudoHover;
			// cancel the reset time-out
			clearTimeout(psh.timeout);
			// change the classname to show the item in MSIE 6
			objNode.className = objNode.className.replace('link', 'hover');
			// tweak the z-index for Internet Explorer
			if(navigator.userAgent.indexOf('MSIE ')>-1) objNode.style.zIndex = 2000;
			// stop the animated sub-menu if there is any
			subMenus = objNode.getElementsByTagName('UL');
			if(subMenus.length>0) psh.resetSub(objNode, subMenus[0]);
		},
		hoverOff: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var psh = jQuery.classBehaviours.handlers.pseudoHover;
			// cancel the reset time-out
			clearTimeout(psh.timeout);
			// change the classname to show the item in MSIE 6
			objNode.className = objNode.className.replace('hover', 'link');
			// tweak the z-index for Internet Explorer
			if(navigator.userAgent.indexOf('MSIE ')>-1) objNode.style.zIndex = 1000;
			// animate the sub-menu if there is any
			subMenus = objNode.getElementsByTagName('UL');
			if(subMenus.length>0) psh.animateSub(objNode, subMenus[0]);
		}
	}
	
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.pseudoHover = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.pseudoHover.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".pseudoHover").pseudoHover();
			}
		);
	}

