/* thanks to  Mike Foster's X library rocks out at http://www.cross-browser.com */
var posterwidth = 512; 
var posterheight = 384;
zoom = new Image;
zoom.src = "http://www.makelinux.net/system/FOSS_map_2048.png";
var zoomwidth = 2048; 
var zoomheight = 1536;

var ratio = zoomwidth/posterwidth; // scale of large vs. smaller image
function eg(e){return document.getElementById(e)}

var lens = eg('lens');
var src = eg('src');
var poster = eg('poster');
var docPos    = getPosition(poster);

function srcOnDrag(ele, mdx, mdy)
{
  	xMoveTo(src, xLeft(src) + mdx, xTop(src) + mdy);
	view_update();
}

function lensOnDrag(ele, mdx, mdy)
{
	var lp    = getPosition(lens);
	var sp    = getPosition(src);
	//lens.style.left = lp.x + mdx;
	//lens.style.top = lp.y + mdy;
  	xMoveTo(lens, xLeft(lens) + mdx, xTop(lens) + mdy);
  	xMoveTo(src, xLeft(src) + mdx, xTop(src) + mdy);
	//leftpos =  ( - lp.x - mdx + docPos.x - 290/2 )  * ratio  + 290/2;
	//toppos =  ( - lp.y - mdy + docPos.y - 230/2 )  * ratio + 230/2 ;
	//lens.style.backgroundPosition = leftpos+"px "+toppos+"px";  
	view_update();
}

function view_update()
{
	var sp    = getPosition(src);
	var lp    = getPosition(lens);
	//lens.style.position='absolute';
	//lens.style.left = lp.x;
	//lens.style.top = lp.y;
  	xMoveTo(lens, xLeft(lens) , xTop(lens) );
	//src.style.position='absolute';
	//src.style.left = sp.x
	//src.style.top = sp.y;
	leftpos =  (  docPos.x - sp.x)  * ratio ;
	toppos =  (  docPos.y - sp.y )  * ratio;
	lens.style.backgroundPosition = leftpos+"px "+toppos+"px";  
	src.style.backgroundPosition = (docPos.x - sp.x-2)+"px "+(docPos.y-sp.y-2)+"px";  
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function LensSetup()
{
	xEnableDrag(lens, null, lensOnDrag, null);
	xEnableDrag(src, null, srcOnDrag, null);
		var sp    = getPosition(src);
	//xEnableDrag(getPosition('src2'), null, srcOnDrag, null);
	//lens.style.visibility = 'visible';
	lens.style.background = "#eee url("+ zoom.src +") no-repeat";
	lens.style.display = "block";
	var pp    = getPosition(poster);
	src.style.position='absolute';
	src.style.left = pp.x;
	src.style.top = pp.y;
	src.style.display = "block";
	src.style.background = "#eee url("+ poster.src +") no-repeat";
	var sp    = getPosition(src);
	var lp    = getPosition(lens);
	lens.style.position='absolute';
	lens.style.left = lp.x;
	lens.style.top = lp.y;
  	xMoveTo(lens, xLeft(lens) , xTop(lens) );
	src.style.position='absolute';
	src.style.left = sp.x
	src.style.top = sp.y;
	view_update()
}

//addLoadEvent(function() { 
LensSetup();	
//});

window.onload = function() { eg("loadinglens").style.display="none"; }

