// helper scripts for http://www.lagom.nl/lcd-test/ and the offline version.
// Copyright 2007, Han-Kwang Nienhuys

var hwin = '';
function helpwin(url) {
   if (!hwin.closed && hwin.location) {
      hwin.close();
   }
   hwin = window.open(url, "lcdhelp",
      'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=500,height=400');
   hwin.focus();
}


// Get window width, height in pixels; returns 1024x768 width if unidentifiable.
// borrowed from http://www.xs4all.nl/~sbpoley/webmatters/resize.html
function getWindowWidth() 
{
     var ww = 1024;
     d = document;
     if ( typeof window.innerWidth != 'undefined' )
	  ww = window.innerWidth;  // NN and Opera version
     else if ( d.documentElement
	       && typeof d.documentElement.clientWidth!='undefined'
	       && d.documentElement.clientWidth != 0 )
	  ww = d.documentElement.clientWidth;
     else if ( d.body
	       && typeof d.body.clientWidth != 'undefined' )
	  ww = d.body.clientWidth;
   return ww;
}
function getWindowHeight()
{
     var wh = 768;
     d = document;
     if ( typeof window.innerHeight != 'undefined' )
	  wh = window.innerHeight;  // NN and Opera version
     else if ( d.documentElement
	       && typeof d.documentElement.clientHeight!='undefined'
	       && d.documentElement.clientHeight != 0 )
	  wh = d.documentElement.clientHeight;
     else if ( d.body
	       && typeof d.body.clientHeight != 'undefined' )
	  wh = d.body.clientHeight;
   return wh;
}


// resize imgs and divs to window size
function imgsize() {
    var imagelist = ["plain-purple.png", "plain-red.png", "plain-green.png", "plain-blue.png", "invpattern", "clockcalib", "angleGrey"];
    var is_css = [0,0,0,0,1,1,1];
    var substract_size = [0,0,0,0,0,2,0]; // reduce wxh with this amount
    var multiply_y=[1.05, 1.05, 1.05, 1.05, 1.05, 1.05, 1.05];
    var xres = getWindowWidth();
    var yres = getWindowHeight();
    for (i = 0; i < imagelist.length; ++i) {
        var elm = document.getElementById(imagelist[i]);
	if (elm) {
	    hgt = Math.round(yres * multiply_y[i] - substract_size[i]);
	    wid = Math.round(xres - substract_size[i]);
	    if (!is_css[i]) {
		elm.width = wid;
		elm.height = hgt;
	    } else {
		elm.style.width = wid + "px";
		elm.style.height = hgt + "px";
	    }
	}
    }
    // and set the resolution
    update_resolution();
}

// resize images on window resize
window.onresize = imgsize;

// Browsers have problems with jumping to in-document anchors because
// of image resizing after jumping to the anchor
function checkanchor()
{
    u = document.URL;
    a = u.match('#.*$');
    if (a) {
	document.location = a;
    }
}

// gamma calibration checkers/stripes swap
var gamma_no = 0;
function swap_dvi_vga() {
//     img1 = document.getElementById("gammatest.png");
     img2 = document.getElementById("sharpness-d.png");
     if (gamma_no == 0) {
//	  img1.src = "http://www.lagom.nl/lcd-test/gammatest-striped.png";
	  img2.src = "http://www.lagom.nl/lcd-test/sharpness-a.png";
	  gamma_no = 1;
     } else {
//	  img1.src = "http://www.lagom.nl/lcd-test/gammatest.png";
	  img2.src = "http://www.lagom.nl/lcd-test/sharpness-d.png";
	  gamma_no = 0;
     }
     return false;
}

// onclick on div
function goanchor(a)
{
    document.location.href = "#" + a;
    return false;
}

// functions for inversion patterns

var ipcurr=8;
var ipfilenrs=["1", "2a", "2b", "3", "4a", "4b", "5", "6a", "6b"];
var ipnum=9;
function nextpattern() {
    ipcurr = (ipcurr+1) % ipnum;
    updatepattern();
    return false;
}
function prevpattern() {
    ipcurr = (ipcurr-1+ipnum
	      ) % ipnum;
    updatepattern();
    return false;
}
function setpattern(n) {
    ipcurr = n;
}
function updatepattern() {
    elm = document.getElementById("invpattern");
    tmp = "url(\"bgimg/inversion-" + ipfilenrs[ipcurr]  +".png\")";
    elm.style.background = tmp;
    elm = document.getElementById("currentdesc");
    elm.innerHTML = ipfilenrs[ipcurr];
    return false;
}


// update display resolution in HTML (id="yourResolution"), 
// and load web bug for resolution statistics (id="statimg")
function update_resolution() {
   var w = screen.width;
   var h = screen.height;
   var cd = 1*screen.colorDepth;

   var yearrem = "(as of 2007)";
   var minw = 1024;
   var minh = 768; 
   var refer = document.referrer;
   if (refer.indexOf("small") != -1) {
      minw = 320;
      minh = 240;
      refer = "small";
   } else 
      refer = "big";
   
   
   var resdescs = [
       "320x240", "QVGA 4:3", "handheld devices.",
       "240x320", "QVGA 3:4", "handheld devices, portrait mode.",
       "320x396", "iPod 4:5", "The iPod Touch is 320x480, but only part of the screen is available for the web browser.",
       "480x320", "HVGA 3:2", "handheld devices (iPod Touch).",
       "640x480", "VGA 4:3", "handheld devices; not very common.",
       "800x600", "SVGA 4:3", "not common for LCD screens, may not be the native resolution of your monitor.",
       "800x480", "WVGA 5:3", "Portable devices; Asus Eee PC.",
       "1024x768", "XGA 4:3", "older LCD screens, low-end new 15 inch screens.",
       "1152x864", "XGA+ 4:3", "not common for LCD screens, may not be the native resolution of your monitor.",
       "1280x1024", "SXGA 5:4", "older screens and low-end new screens.",
       "1280x720", "720p 16:9", "HDTV format, not common for computer monitors.",
       "1280x768", "WXGA 5:3", "widescreen format, mainly TV sets, may not be the native resolution of your monitor",
       "1280x800", "WXGA 16:10", "small widescreen format, mainly laptops.",
       "1280x854", "3:2", "only used on Mac Powerbook G4.",
       "1280x960", "4:3", "not common for LCD screens, may be not be the native resolution of your monitor.",
       "1360x768", "WXGA 16:9", "common widescreen LCD TV format, may not be the native resolution of your monitor.",
       "1366x768", "WXGA 16:9", "common widescreen LCD TV format, may not be the native resolution of your monitor.",
       "1440x900", "WXGA+ 16:10", "common widescreen format.",
       "1400x1050", "SXGA+ 4:3", "some newer monitors and high-end laptops.",
       "1600x1200", "UXGA 4:3", "common format for older high-end monitors.",
       "1680x1050", "WSXGA+ 16:10", "common large widescreen format.",
       "1920x1200", "WUXGA 16:10", "high-end large widescreen format.",
       "2560x1600", "WQXGA 16:10", "high-end large widescreen format. This screen had better pass these tests! :-)"
   ];
    var toolow_rem = "<br>Note: your screen is too small to display the images without rescaling." +
	  " (at least " + minw + "x" + minh + " recommended)";
    var uncommon_rem = "<br>Note: monitors for this resolution are uncommon; the resolution may not match the native"+
	  " resolution of your monitor.";
    var uncommon_ratio = "<br>Note: your resolution does not have "+
	  "a common 5:4 (1.25), 4:3 (1.33), or 16:10 (1.6) aspect ratio, " +
	  "which means that images can be distorted. Your aspect ratio is: ";

   var elm = document.getElementById("yourResolution");
   if (elm) {
	if (w && h) {
	     var res = w + "x" + h;
	     var longres = res;
	     var comment = "Uncommon resolution.";
	     var common = 0;
	     for (i = 0; i < resdescs.length; i+=3) {
		  if (resdescs[i] == res) {
		       comment = resdescs[i+2] + " " + yearrem;
		       longres = "<b>" + res + "</b> &nbsp; (" + resdescs[i+1] + ")";
		       common = 1;
		       break;
		  }
	     }
	     if (w < minw || h < minh)
		  comment +=  toolow_rem;
	     if (!common)
		  comment +=  uncommon_rem;
	     aspect = 1.0*w/h;
	     if (Math.abs(aspect-1.333) > 0.01 && Math.abs(aspect-1.25) > 0.01 && Math.abs(aspect-1.6) > 0.01) {
		  aspect = Math.round(aspect*100)*0.01;
		  comment += uncommon_ratio + aspect;
	     }
	     var cddesc = "";
	     if (cd == 16) {
		  cddesc = " (Highcolor, Thousands of colors)";
	     } else if (cd == 24) {
		  cddesc = " (Truecolor, Millions of colors)";
	     } else if (cd == 32) {
		  cddesc = " (Truecolor, Millions of colors)";
	     } else if (cd == 8) {
		  cddesc = " (256 indexed colors)";
	     }
	     elm.innerHTML = "Resolution: " + longres + "<br>" +
		  "Description: " + comment + "<br>" +
		  "Color depth: " + cd + " bits" + cddesc;
	     // collect statistics
	     img = document.getElementById("statimg");
	     if (img) {
		  var cd1 = cd;
		  if (cd1 == 32)
		       cd1 = 24; // no separate stats for 32/24 bpp
		  img.src = "/lcd-test/stats?lcd=" + res + "," + cd1 + "bpp," + refer;
	     }
	} else {
	     elm.innerHTML = "unknown";
	}
   }
}


// font-size, font brightness

var curr_fontsize = 2;
var max_fontsize = 5;

var curr_textcontrast = 1;
var max_textcontrast = 2;

function setfontsize(n) {
     var sz;
     switch (n) {
     case 1: sz = "0.6875em"; break;
     case 2: sz = "0.8125em"; break;
     case 3: sz = "0.9375em"; break;
     case 4: sz = "1.0625em"; break;
     case 5: sz = "1.1875em"; break;
     default: return false;
     }
     curr_fontsize = n;
     elm = document.getElementsByTagName("body")[0];
     if (elm) 
	  elm.style.fontSize = sz;
     for (i = 1; i  <= max_fontsize; ++i) {
	  elm = document.getElementById("fsz" + i);
	  if (elm)
	       elm.style.fontWeight = (i == n ? "bold" : "normal");
     }
          
     setstylecookie();
     return false;
}


function setcontrast(n) {
     var fcHead, fcMain;
     switch (n) {
     case 1: fcHead = "#ffff80"; fcMain = "#808080"; break; 
     case 2: fcHead = "#ffffbb"; fcMain = "#d8d8d8"; break;
     default: return false;
     }
     elm = document.getElementsByTagName("body")[0];
     if (elm)
	  elm.style.color = fcMain;
     divlist = document.getElementsByTagName("div");
     for (i = 0; i < divlist.length; ++i) {
	  if (divlist[i].className == "pagehead")
	       divlist[i].style.color = fcHead;
     }
     curr_textcontrast = n;
     for (i = 1; i  <= max_fontsize; ++i) {
	  elm = document.getElementById("fcn" + i);
	  if (elm)
	       elm.style.fontWeight = (i == n ? "bold" : "normal");
     }
     setstylecookie();
     return false;
}

function setstylecookie()
{
     document.cookie = "lcdfont=" + curr_fontsize + "/" + curr_textcontrast + "/;path=/lcd-test/;domain=.lagom.nl;";
}

function getstylecookie()
{
     var cookies = document.cookie.split(";");
     var fset = 0, cset = 0;
     for (i = 0; i < cookies.length; ++i) {
	  var cookieNV = cookies[i].split("=");
	  if (cookieNV[0] == "lcdfont") {
	       fontSC = cookieNV[1].split("/"); // size value (1..2), contrast value (1..5)
	       if (fontSC[0] && fontSC[0] >= 1 && fontSC[1] <= max_fontsize) {
		    setfontsize(1*fontSC[0]);
		    fset = 1;
	       }
	       if (fontSC[1] && fontSC[1] >= 1 && fontSC[1] <= max_textcontrast) {
		    setcontrast(1*fontSC[1]);
		    cset = 1;
	       }
	  }
     }
     if (!cset)
	  setcontrast(curr_textcontrast);
     if (!fset)
	  setfontsize(curr_fontsize);
}

function fontboxinit()
{
     fbox = document.getElementById("fontbox");
     if (fbox) {
	  fbox.innerHTML = 
	       '<b>Customize text display<\/b><br>'+
	       '<table><tr><td>Size:<td>'+
	       '<a href="#" onclick="setfontsize(1)" id="fsz1">1<\/a> '+
	       '<a href="#" onclick="setfontsize(2)" id="fsz2">2<\/a> '+
	       '<a href="#" onclick="setfontsize(3)" id="fsz3">3<\/a> '+
	       '<a href="#" onclick="setfontsize(4)" id="fsz4">4<\/a> '+
	       '<a href="#" onclick="setfontsize(5)" id="fsz5">5<\/a> '+
	       '<tr><td>Contrast:<td>'+
	       '<a href="#" onclick="setcontrast(1)" id="fcn1">standard<\/a> '+
	       '<a href="#" onclick="setcontrast(2)" id="fcn2">high<\/a>'+
	       '<\/table>';
	  fbox.className = "fontbox";
     }
     getstylecookie();
     return false;
}


