function MGNswitch(obj) {
 MGon[obj]=((MGon[obj])?(0):(1));
 return MGon[obj];
}
function MGNif(obj,highres) {
 if(MGon[obj]) {MGN(obj,highres);}
}
function MGN(obj,highres) {
 MGNratio=MGNheight/MGNwidth;
 if(MGNoffsetx > 1) {
 MGNoffset='dumb';
 MGNoffsetx=MGNoffsetx/MGNwidth;
 MGNoffsety=MGNoffsety/MGNheight;
 }
 if(!obj.style.width) {
 if(obj.width > 0) {
 obj.style.width=obj.width+'px';
 obj.style.height=obj.height+'px';
 }
 }
 if(typeof(highres) != typeof('')) {highres=obj.src}
 var MGstage=document.createElement("div");
 MGstage.style.width=obj.style.width;
 MGstage.style.height=obj.style.height;
 MGstage.style.overflow='hidden';
 MGstage.style.position='absolute';
 if(typeof(MGstage.style.filter) != typeof(nosuchthing)) {
 //IE
 if(navigator.appVersion.indexOf('Mac') == -1) { 
 //Mac IE
 MGstage.style.filter='alpha(opacity=0)';
 MGstage.style.backgroundColor='#ffffff';
 }
 } else {
 MGstage.style.backgroundImage='none';
 }
 MGstage.setAttribute('onmousemove','MGhandlemouse(event,this);');
 MGstage.setAttribute('onmousedown','MGhandlemouse(event,this);');
 MGstage.setAttribute('onmouseup','MGhandlemouse(event,this);');
 MGstage.setAttribute('onmouseout','MGhandlemouse(event,this);');
 if(navigator.userAgent.indexOf('MSIE')>-1) {
 MGstage.onmousemove = function() {MGhandlemouse(event,this);}
 MGstage.onmousedown = function() {MGhandlemouse(event,this);}
 MGstage.onmouseup = function() {MGhandlemouse(event,this);}
 MGstage.onmouseout = function() {MGhandlemouse(event,this);}
 }
 obj.parentNode.insertBefore(MGstage,obj);
 
 MGwin=document.createElement("div");
 MGwin.style.width='0px';
 MGwin.style.height='0px';
 MGwin.style.overflow='hidden';
 MGwin.style.position='absolute';
 MGwin.style.display='none';
 tw1='<div style="position:absolute;overflow:hidden;margin:';
 MGwin.innerHTML= 
 tw1 +'0; background-color:'+MGbordercolor+'; width:'+(MGNwidth)+'px;height:'+(MGNheight)+'px"></div>' +
 tw1+(MGborderthick)+'px 0 0 '+(MGborderthick)+'px; width:'+(MGNwidth -MGborderthick*2)+'px;height:'+(MGNheight-MGborderthick*2)+'px;"><img src="'+obj.src+'" style="position:absolute;margin:0;padding:0;border:0; width:'+(MGNamount*parseInt(obj.style.width))+'px;height:'+(MGNamount*parseInt(obj.style.height))+'px;" />'+((obj.src!=highres)?('<img src="'+highres+'" style="position:absolute;margin:0;padding:0;border:0; width:'+(MGNamount*parseInt(obj.style.width))+'px;height:'+(MGNamount*parseInt(obj.style.height))+'px;" onload="if(this.parentNode.parentNode) {this.parentNode.parentNode.getElementsByTagName(\'div\')[2].style.display=\'none\';}" />'):(''))+'</div>';
 if(highres != obj.src) {
 MGwin.innerHTML+='<div style="position:absolute; margin:'+(MGborderthick)+'px 0 0 '+(MGborderthick)+'px;">'+MGloading+'</div>';
 }
 

 
 obj.parentNode.insertBefore(MGwin,MGstage);

 MGresize(obj);
}

function MGresize(obj) {
 sbr=0; sbl=0;
 if(MGNwidth-2*MGborderthick  < 22) {sbr=1}
 if(MGNheight-2*MGborderthick  < 22) {sbr=1}
 if(MGNwidth > parseFloat(obj.style.width)) {sbl=1;}
 if(MGNheight > parseFloat(obj.style.height)) {sbl=1}
 
 if(sbr==1 && sbl == 1) {
 MGNwidth=parseFloat(obj.style.width)/2;
 MGNheight=parseFloat(obj.style.height)/2;
 MGNratio=MGNheight/MGNwidth;
 }

 if(sbr==1) {
 if(MGNwidth<MGNheight) {
 MGNheight=MGNheight/MGNwidth*(22+2*MGborderthick ); MGNwidth=22+2*MGborderthick ;
 } else {
 MGNwidth=MGNwidth/MGNheight*(22+2*MGborderthick ); MGNheight=22+2*MGborderthick ;
 }
 }
 

 if(sbl==1) {
 if(parseFloat(obj.style.width)/parseFloat(obj.style.height) > MGNwidth/MGNheight) {
 MGNheight=parseFloat(obj.style.height);
 MGNwidth=MGNheight/MGNratio;
 } else {
 MGNwidth=parseFloat(obj.style.width);
 MGNheight=MGNratio*MGNwidth;
 }
 }

 MGNwidth=Math.floor(MGNwidth/2)*2;
 MGNheight=Math.floor(MGNheight/2)*2;

 ww=obj.parentNode.getElementsByTagName('div')[0];
 ww.style.width=MGNwidth+'px';
 ww.style.height=MGNheight+'px';
 w=ww.getElementsByTagName('div')[0];
 w.style.width=MGNwidth +'px';
 w.style.height=MGNheight +'px';
 w=ww.getElementsByTagName('div')[1];
 w.style.width=MGNwidth -MGborderthick*2+'px';
 w.style.height=MGNheight -MGborderthick*2+'px';
}

function MGfindposy(obj) {
 var curtop = 0;
 if(!obj) {return 0;}
 if (obj.offsetParent) {
 while (obj.offsetParent) {
 curtop += obj.offsetTop
 obj = obj.offsetParent;
 }
 } else if (obj.y) {
 curtop += obj.y;
 }
 return curtop;
}

function MGfindposx(obj) {
 var curleft = 0;
 if(!obj) {return 0;}
 if (obj && obj.offsetParent) {
 while (obj.offsetParent) {
 curleft += obj.offsetLeft
 obj = obj.offsetParent;
 }
 } else if (obj.x) {
 curleft += obj.x;
 }
 return curleft;
}


function MGhandlemouse(evt,obj) {
 var evt = evt?evt:window.event?window.event:null; if(!evt) { return false; }
 if(evt.pageX) {
 nowx=evt.pageX-MGfindposx(obj)-MGadjustx;
 nowy=evt.pageY-MGfindposy(obj)-MGadjusty;
 } else {
 if(document.documentElement && document.documentElement.scrollTop) {
 nowx=evt.clientX+document.documentElement.scrollLeft-MGfindposx(obj)-MGadjustx;
 nowy=evt.clientY+document.documentElement.scrollTop-MGfindposy(obj)-MGadjusty;
 } else {
 nowx=evt.x+document.body.scrollLeft-MGfindposx(obj)-MGadjustx;
 nowy=evt.y+document.body.scrollTop-MGfindposy(obj)-MGadjusty;
 }
 }
 if(evt.type == 'mousemove') {
 MGsetwin(obj,nowx,nowy);
 } else if(evt.type == 'mousedown') {
 MGmouse=1; //left: 1, middle: 2, right: 3
 MGmousey=nowy;
 MGmousex=nowx;
 } else if(evt.type =='mouseup') {
 MGmouse=0;
 } else if(evt.type =='mouseout') {
 MGmouse=0;
 if(navigator.appVersion.indexOf('Mac') == -1 || navigator.appVersion.indexOf('MSIE') == -1) { //hi Mac IE
 x=obj.parentNode;
 x.removeChild(x.getElementsByTagName('div')[0]);
 x.removeChild(x.getElementsByTagName('div')[0]);
 }
 }
}




function MGsetwin(obj,nowx,nowy) {
 obj.parentNode.getElementsByTagName('div')[0].style.display='block';
 if(MGNoffset=='smart') {
 MGNoffsetx=.1+.8*nowx/parseFloat(obj.style.width);
 MGNoffsety=.1+.8*nowy/parseFloat(obj.style.height);
 }

 stage=obj.parentNode.getElementsByTagName('div')[0];
 if(MGmouse == 1) {
 if(Math.abs(nowy-MGmousey) >= 1) {
 MGNamount*=((nowy>MGmousey)?(0.909):(1.1));
 MGmousey=nowy;
 if(MGNamount < MGNamountmin) {MGNamount=MGNamountmin;}
 if(MGNamount > MGNamountmax) {MGNamount=MGNamountmax;}
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.width= parseInt(obj.style.width)*MGNamount+'px';
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.height= parseInt(obj.style.height)*MGNamount+'px';
 if(stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1]) {
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1].style.width= stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.width;
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1].style.height= stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.height;
 }
 }
 if(Math.abs(nowx-MGmousex) >= 12 && MGNwindowlock==0) {
 MGNwidth*=((nowx>MGmousex)?(1.1):(0.909));
 MGNheight=MGNwidth*MGNratio;
 MGresize(obj);
 MGmousex=nowx;
 }
 }
 stage.style.marginLeft=nowx-(MGNwidth -2*MGborderthick )*MGNoffsetx-MGborderthick +'px';
 stage.style.marginTop= nowy-(MGNheight-2*MGborderthick )*MGNoffsety-MGborderthick +'px';
 clip1=0; clip2=MGNwidth; clip3=MGNheight; clip4=0;
 nwidth=MGNwidth; nheight=MGNheight;
 tmp=(1-2*MGNoffsetx)*(MGborderthick );
 
 if(nowx-MGNwidth*MGNoffsetx < tmp) {
 clip4=MGNwidth*MGNoffsetx-nowx + tmp;
 } else if(parseFloat(nowx-MGNwidth*MGNoffsetx+MGNwidth) > parseFloat(obj.style.width)+tmp) {
 clip2= MGNwidth*MGNoffsetx - nowx + parseFloat(obj.style.width)+tmp;
 nwidth=MGNwidth*MGNoffsetx-nowx+parseInt(obj.style.width)+MGborderthick ;
 }
 
 tmp=(1-2*MGNoffsety)*(MGborderthick );
 
 if(nowy-MGNheight*MGNoffsety < tmp) {
 clip1=MGNheight*MGNoffsety-nowy+tmp;
 } else if(parseFloat(nowy-MGNheight*MGNoffsety+MGNheight) > parseFloat(obj.style.height)+tmp) {
 clip3= MGNheight*MGNoffsety - nowy + parseFloat(obj.style.height)+tmp;
 nheight=MGNheight*MGNoffsety - nowy + parseFloat(obj.style.height)+MGborderthick ;
 }
 stage.style.width=nwidth+'px';
 stage.style.height=nheight+'px';

 stage.style.clip='rect('+clip1+'px,'+clip2+'px,'+clip3+'px,'+clip4+'px)';

 if(nowy-MGNoffsety*(MGNheight-2*MGborderthick ) < 0) { t=-(nowy-MGNoffsety*(MGNheight-2*MGborderthick ))} 
 else if(nowy-MGNoffsety*(MGNheight-2*MGborderthick ) > parseFloat(obj.style.height)-MGNheight+MGborderthick*2 ) { t=-MGNamount*parseFloat(obj.style.height)+MGNheight-MGborderthick*2 -((nowy-MGNoffsety*(MGNheight-2*MGborderthick ))-(parseFloat(obj.style.height)-MGNheight+MGborderthick*2 )); }
 else { t=(-MGNamount*parseFloat(obj.style.height)+MGNheight-MGborderthick*2 )/(parseFloat(obj.style.height)-MGNheight+MGborderthick*2 )*(nowy-MGNoffsety*(MGNheight-2*MGborderthick )) }
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.marginTop=t+'px';

 if(stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1]) {
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1].style.marginTop=t+'px';
 }

 if(nowx-MGNoffsetx*(MGNwidth-2*MGborderthick ) < 0) { t=-(nowx-MGNoffsetx*(MGNwidth-2*MGborderthick ))} 
 else if(nowx-MGNoffsetx*(MGNwidth-2*MGborderthick ) > parseFloat(obj.style.width)-MGNwidth+MGborderthick*2 ) { t=-MGNamount*parseFloat(obj.style.width)+MGNwidth-MGborderthick*2 -((nowx-MGNoffsetx*(MGNwidth-2*MGborderthick ))-(parseFloat(obj.style.width)-MGNwidth+MGborderthick*2 )); }
 else { t=(-MGNamount*parseFloat(obj.style.width)+MGNwidth-MGborderthick*2 )/(parseFloat(obj.style.width)-MGNwidth+MGborderthick*2 )*(nowx-MGNoffsetx*(MGNwidth-2*MGborderthick )) }
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[0].style.marginLeft=t+'px';

 if(stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1]) {
 stage.getElementsByTagName('div')[1].getElementsByTagName('img')[1].style.marginLeft=t+'px';
 }
}

function MGinit() {
 MGadjustx=0; MGadjusty=0;
 if(navigator.userAgent.indexOf('MSIE')>-1) {MGadjustx=2;MGadjusty=2;}
 if(navigator.userAgent.indexOf('Opera')>-1) {MGadjustx=0; MGadjusty=0;}
 if(navigator.userAgent.indexOf('Safari')>-1) {MGadjustx=1; MGadjusty=2;}
 if(navigator.userAgent.indexOf('Chrome')>-1) {MGadjustx=1; MGadjusty=2;MGloading=''}
}

var MGon=new Array();
var MGadjustx,MGadjusty;
var MGmouse=0; var MGmousey; var MGmousex;
var sLoading='. . .';
var MGNwidth=120; 
var MGNheight=120; 
var MGNwindowlock=1; // 0 / 1
var MGNoffsetx=.5;
var MGNoffsety=.5;
var MGNamount=2;
var MGNamountmax=2;
var MGNamountmin=2;
var MGborderthick=1; 
var MGbordercolor='#cccccc'; 
var MGNoffset='dumb';

var MGloading='<div style="background-color: #ffeb77; color: #333333; padding:2px; font-family: verdana,arial,helvetica; font-size: 10px;">'+sLoading+'</div>';
MGinit();
