/*
	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(){}

	// Manage all event handlers of an imagemap
	jQuery.classBehaviours.handlers.imageMap = {
		// properties
		name: 'imageMap',
		// methods
		start: function(node){
			// start the storing of the map object
			node.onmouseover = this.map.over;
			this.map.over(node);
			// get all areas in this map
			areas = document.getElementById(this.map.name).getElementsByTagName('area');
			// for all areas in this map
			for(var a=0; a<areas.length; a++){
				// cache it's image equivalent
				this.cache(node.src.replace(this.map.passive, areas[a].id));
				// give the area event handlers
				areas[a].onmouseover = this.areas.over;
				areas[a].onmouseout = this.areas.out;
			}
		},
		// events
		cache: function(url) {
				var imp = jQuery.classBehaviours.handlers.imageMap;
				// preload the image into an array
				imp.map.images[imp.map.images.length] = new Image();
				imp.map.images[imp.map.images.length-1].src = url;
		}
	}
		jQuery.classBehaviours.handlers.imageMap.map = {
			// properties
			object: null,
			name: null,
			passive: 'default',
			images:	new Array(),
			// methods
			over: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				var imp = jQuery.classBehaviours.handlers.imageMap;
				// store the currently active map
				imp.map.object = objNode;
				imp.map.name = objNode.getAttributeNode('usemap').value.replace('#','');
			}
		}
		jQuery.classBehaviours.handlers.imageMap.areas = {
			// methods
			over: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				var imp = jQuery.classBehaviours.handlers.imageMap;
				// apply the area id to the map's source
				imp.map.object.src = imp.map.object.src.replace(imp.map.passive, objNode.id);
			},
			out: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				var imp = jQuery.classBehaviours.handlers.imageMap;
				// apply the area id to the map's source
				imp.map.object.src = imp.map.object.src.replace(objNode.id, imp.map.passive);
			}
		}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.imageMap = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.imageMap.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".imageMap").imageMap();
			}
		);
	}

