
///************************************///
/********* simple image swapper *********/
/*********         2009         *********/
/*********    Markus Emrich     *********/
/*****************************************

 This Script adds hover functionality to
 all images on a website, which have the
 same classname, e.g. 'hoverable' o smth.
 similar. It also preloads the hover
 images.
 
 Currently every image needs to have its
 hover image in the relative directory
 'hover/' with the same name like the
 original image.
 
 USAGE
 
 1) include this js-file in your head
    section
 2) call registerHoverImages in the
    onload attribute of your body tag
	
 YOU NEED TO USE ALT DESCRIPTIONS FOR
    YOUR IMAGES TO RUN THIS SCRIPT

    DON'T USE THIS SCRIPT WITHOUT
      PERMISSION OF THE AUTHOR
	  
	            2009

///************************************///

var hoversave = new Array();
var hoverpath = "hover/";

function hoverImage(img)
{
	hoversave[img.alt] = img.src;	
	if( img && document.images )
	{
		img.src = getHoverUrl(img);
	}
}

function hoverRollBack(img)
{
	img.src = hoversave[img.alt];
}

///************************************///

function getHoverUrl(img)
{
	var lastindex = img.src.lastIndexOf("/");
	var filename = img.src.substring(lastindex+1);
	var path = img.src.substring(0, lastindex+1);
	
	return path + hoverpath + filename;
}

function registerImage(img)
{
	img.onmouseover = function(){ hoverImage(img) };
	img.onmouseout = function(){ hoverRollBack(img) };
	
	//preload
	var bild = new Image();
  	bild.src = getHoverUrl(img);
}

///*********** registering ************///

function registerHoverImages( classname )
{
	for( var i=0; i<document.images.length; i++ )
	{
		var img = document.images[i];
		if( img.className == classname )
		{
			registerImage(img);
		}
	}
}

///************************************///
///************************************///
