/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 20081124	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(){}

	// replace in src sub-string
	jQuery.classBehaviours.handlers.navigationColumnScroller = {
		// properties
		name: 'navigationColumnScroller',
		timeOut: null,
		index: 0,
		// methods
		start: function(node){
			// get the buttons from the scroll canvas
			allButtons = node.getElementsByTagName('button');
			if(allButtons.length>1){
				// set the up button's events
				allButtons[allButtons.length-2].id = (allButtons[allButtons.length-2].id) ? allButtons[allButtons.length-2].id : this.name + this.index++ ;
				allButtons[allButtons.length-2].onmousedown = this.scrollUp;
				document.onmouseup = this.stopScrolling;
				// set the down button's events
				allButtons[allButtons.length-1].id = (allButtons[allButtons.length-1].id) ? allButtons[allButtons.length-1].id : this.name + this.index++ ;
				allButtons[allButtons.length-1].onmousedown = this.scrollDown;
				document.onmouseup = this.stopScrolling;
			}
		},
		// events
		scrollUp: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ncs = jQuery.classBehaviours.handlers.navigationColumnScroller;
			// remove the focus from the button
			objNode.blur();
			// cancel any previous loops still running
			ncs.stopScrolling();
			// get the content container
			contentContainer = objNode.parentNode.getElementsByTagName('UL')[0];
			// get its position
			contentPosition = (contentContainer.style.marginTop!='') ? parseInt(contentContainer.style.marginTop) : 0 ;
			// check the maximum position
			if(contentPosition < 0){
				// scroll it up
				contentContainer.style.marginTop = contentPosition + 4 + 'px';
				// repeat
				ncs.timeOut = setTimeout('jQuery.classBehaviours.handlers.navigationColumnScroller.scrollUp(document.getElementById("'+objNode.id+'"))', 10);
			}
		},
		scrollDown: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ncs = jQuery.classBehaviours.handlers.navigationColumnScroller;
			// remove the focus from the button
			objNode.blur();
			// cancel any previous loops still running
			ncs.stopScrolling();
			// get the content container
			contentContainer = objNode.parentNode.getElementsByTagName('UL')[0];
			// get its position
			contentPosition = (contentContainer.style.marginTop!='') ? parseInt(contentContainer.style.marginTop) : 0 ;
			// check the maximum position
			if(contentPosition > (-1 * (contentContainer.offsetHeight - contentContainer.parentNode.offsetHeight + 80))){
				// scroll it up
				contentContainer.style.marginTop = contentPosition - 4 + 'px';
				// repeat
				ncs.timeOut = setTimeout('jQuery.classBehaviours.handlers.navigationColumnScroller.scrollDown(document.getElementById("'+objNode.id+'"))', 10);
			}
		},
		stopScrolling: function(){
			var ncs = jQuery.classBehaviours.handlers.navigationColumnScroller;
			// cancel the scrolling
			clearTimeout(ncs.timeOut);
		}
	}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.navigationColumnScroller = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.navigationColumnScroller.start(this);
				}
			);
		};
		// if the main parser isn't present
		if(typeof(jQuery.classBehaviours.parser)=='undefined'){
			// set the event handler for this jQuery method
			$(document).ready(
				function(){
					$(".navigationColumnScroller").navigationColumnScroller();
				}
			);
		}
	}


