/*	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(){}

	// Use the longdesc url to display a tooltip
	jQuery.classBehaviours.handlers.longdescToolTip = {
		// properties
		name: 'longdescToolTip',
		// methods
		start: function(node){
			// set the event handlers of the image
			node.onmouseover = this.overImage;
			node.onmouseout = this.offImage;
//				node.onmousemove = this.moveImage;
		},
		waitForDesc: function(importProgress, referedNode){
			targetNode = jQuery.classBehaviours.utilities.previousNode(referedNode);
			targetNode.innerHTML = 'loading: ' + Math.round(importProgress*100) + '%';
		},
		insertDesc: function(importedObj, referedNode, importedText){
			targetNode = jQuery.classBehaviours.utilities.previousNode(referedNode);
			targetNode.innerHTML = importedText.split('<body>')[1].split('</body>')[0];;
		},
		// events
		overImage: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ltt = jQuery.classBehaviours.handlers.longdescToolTip;
			// get the taget node
			targetNode = jQuery.classBehaviours.utilities.previousNode(objNode);
			// if this node is an existing tooltip
			if(targetNode.nodeName=='DIV' && targetNode.className.indexOf(ltt.name)>-1){
				// show it
				targetNode.style.visibility = 'visible';
			}else{
				// make a new one
				newToolTip = document.createElement('DIV');
				newToolTip.className = ltt.name;
				objNode.parentNode.insertBefore(newToolTip, objNode);
				// order it to be filled
				jQuery.classBehaviours.ajax.addRequest(objNode.getAttribute('longdesc'), ltt.insertDesc, ltt.waitForDesc, null, objNode);
			}
		},
		offImage: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ltt = jQuery.classBehaviours.handlers.longdescToolTip;
			// get the next node
			targetNode = jQuery.classBehaviours.utilities.previousNode(objNode);
			// if this node is an existing tooltip
			if(targetNode.nodeName=='DIV' && targetNode.className.indexOf(ltt.name)>-1){
				// hide it
				targetNode.style.visibility = 'hidden';
			}
		},
		moveImage: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ltt = jQuery.classBehaviours.handlers.longdescToolTip;
			// get the next node
			targetNode = jQuery.classBehaviours.utilities.previousNode(objNode);
			// if this node is an existing tooltip
			if(targetNode.nodeName=='DIV' && targetNode.className.indexOf(ltt.name)>-1){
				targetNode.style.position = 'absolute';
				targetNode.style.left = (typeof(that)!='undefined') ? (that.layerX) + 'px' : (event.x) + 'px' ;
				targetNode.style.top = (typeof(that)!='undefined') ?  (that.layerY) + 'px' : (event.y) + 'px' ;
			}
		}
	}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.longdescToolTip = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.longdescToolTip.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".longdescToolTip").longdescToolTip();
			}
		);
	}

