/* Imapper script - replaces an image map with an absolutely positioned image and links,
   often allowing it to 'play well' with scripts.  Optionally allows for a resizable map.
   ©2007 John Davenport Scheuer.  This Credit Must Remain for Legal Use */

var imapper={
//set units, use 'px' for a static sized image map (standard), 'em' for one that can change size with font changes.
u:'px',
//if using 'em', set what the image's font size would have been in pixels (use 16 if unsure).
f:'16',
///////////////// Stop Editing /////////////////
clone:function(im){
if(!document.images||!document.removeChild)
return;
var him=document.getElementById(im), c=new Image();
c.onload=function(){imapper.resize(him, c.width, c.height);};
c.src=him.src;
},
resize:function(im, w, h){
if(!document.images||!document.removeChild)
return;
var m=imapper, d=document.createElement('div'), mapref,
p='href,alt,title,target,onmouseover,onmouseout,onclick,onmousedown,onmouseup,rel,rev,id,className'.split(',');
m.m=m.u=='em'?m.f:1;
im.style.position='';
d.className='map_link';
d.style.width=im.style.width=w/m.m+m.u;
d.style.height=im.style.height=h/m.m+m.u;
im.parentNode.insertBefore(d,im);
for (var i = 0, maps=document.getElementsByTagName('map'); i< maps.length; i++)
if(maps[i].name==im.getAttribute('usemap',0).replace(/#/,'')){
mapref=maps[i];
break;
}
if(!mapref){
alert('No Map Linked by that Name');
return;
}
im.removeAttribute('usemap',0);
d.appendChild(im.parentNode.removeChild(im));
for (var i = 0, a=mapref.getElementsByTagName('area'); i < a.length; i++)
if (a[i].getAttribute('coords',0)){
var l=document.createElement('a');
var c=a[i].getAttribute('coords',0).replace(/ /g,'').split(',');
if (c.length==3){
var b=[];
b[0]=c[0]-c[2];
b[1]=c[1]-c[2];
b[2]=(c[0]-0)+(c[2]-0);
b[3]=(c[1]-0)+(c[2]-0);
c=b;
}
else if (a[i].getAttribute('shape',0).toLowerCase()=='poly'){
var x=[], y=[];
for (var z = 0; z < c.length; z++){
x[x.length]=c[z++];
y[y.length]=c[z];
}
x.sort(function(a,b){return a-b;});
y.sort(function(a,b){return a-b;});
c.length=4;
c[0]=x[0];
c[1]=y[0];
c[2]=x[x.length-1];
c[3]=y[y.length-1];
}
for (var j in p)
if(a[i][p[j]])
l[p[j]]=a[i][p[j]];
with(l.style){
left=m.u=='px'? c[0]+m.u : c[0]/(w/100)+'%';
top=m.u=='px'? c[1]+m.u : c[1]/(h/100)+'%';
width=(c[2]-c[0])/m.m+m.u;
height=(c[3]-c[1])/m.m+m.u;
}
d.appendChild(l);
}
mapref.parentNode.removeChild(mapref);
}
};
