var addEvent = function(elem, type, eventHandle) {
    if (elem == null || elem == undefined) return;
    if ( elem.addEventListener ) {
        elem.addEventListener( type, eventHandle, false );
    } else if ( elem.attachEvent ) {
        elem.attachEvent( "on" + type, eventHandle );
    }
};

function myresize(){
var minP  = 0.75;
var maxP  = 1.0;
var clampP = 0.5;
var minWidth = 960;
var minHeight = 500;


//var viewport = document.viewport.getDimensions(); // Gets the viewport as an object literal
//var w = viewport.width; // Usable window width
//var h = viewport.height; // Usable window height

 var h = $(window).height();
 var w = $(window).width();
 //var h = window.height;
 //var w = window.width;


 if( w < minWidth ){
  w = minWidth;
  $(window).width = w;
 }
 
 if( h < minHeight){
  h = minHeight;
  $(window).height = h;
 }


 var canvas  = document.getElementsByTagName('canvas')[0];
 canvas.width = w;
 canvas.height = h;
 var context = canvas.getContext('2d');
 var image1  = document.getElementById('bgimg');
 var sw = image1.width;
 var sh = image1.height;
 
 var sxy = 1.0*w/h;
 var ph = 1.0*h/sh;
 var pw = 1.0*w/sw;
 var mp = Math.min(ph,pw);
 var heightlimited=false;
 if( mp == ph)
   heightlimited = true;
   
 //clip at 50-100%% or whatever is useful
 mp = Math.max(minP,mp);
 mp = Math.min(maxP,mp);
 //half the distance to the goal
 var mpout = (1.0-mp)*clampP + mp
 
 var h2 = sh/2;
 var w2 = sw/2;
 
 /*
 var hout = Math.round( mpout*sh);
 var wout = Math.round( mpout*sw);
 
 var uly=Math.round(h2 - mpout*h2);
 var ulx=Math.round(w2 - mpout*w2);
 */
 var hout = 0;
 var wout = 0;
 var uly  = 0;
 var ulx  = 0;
 if( heightlimited ){
   hout = Math.round( mpout*sw/sxy);
   wout = Math.round( mpout*sw);
 
   uly=Math.round(h2 - mpout*w2/sxy);
   ulx=Math.round(w2 - mpout*w2);
 }
 else{
   hout = Math.round( mpout*sh);
   wout = Math.round( mpout*sh*sxy);
 
   uly=Math.round(h2 - mpout*h2);
   ulx=Math.round(w2 - mpout*h2*sxy);
 }

 

 //document.getElementById("info").innerHTML = "h="+h+" w="+w+" sh="+sh+" sw="+sw+" mp="+Math.round(100*mp)+"% mpout="+Math.round(100*mpout)+"% ulx="+ulx+" uly="+uly+" wout="+wout+" hout="+hout;
 //document.getElementById("info").innerHTML = w + "x" + h;
 context.drawImage(image1,ulx,uly,wout,hout,0,0,w,h);
};


 
function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

