var cx=0,cy=0;
var basecolor = new Object();
basecolor.red = 232;
basecolor.green = 209;
basecolor.blue = 255;
var changecolor = 5;
var cmin = 220;
var cmax = 255;
//window.onresize = bgstarsquares2;
function background() {
   bgclear();
   var left,top;
   var windowdims = windowsize();
   var myWidth  = windowdims[0];
   var myHeight = windowdims[1];   
   var boxsize = 60;
   var border = 20;
   cx = 0;
   cy = 0;
   for (y=5;y<myHeight-boxsize;y+=boxsize) {
      cy++;
      cx=0;
      for (x=5;x<myWidth-boxsize;x+=boxsize) {
         cx++;
         var div = document.createElement('DIV');
         div.style.position = 'absolute';
         div.style.top = y + 'px';
         div.style.left = x + 'px';
         div.style.width = boxsize + 'px';
         div.style.height = boxsize + 'px';
         div.style.backgroundColor = '#fff';
         div.style.zIndex = -1;
         //div.style.border = '1px dotted #ccc';
         //div.id = 'x:'+cx+',y:'+cy;
         div.className = 'backsquare';
         var div2 = document.createElement('DIV');
         div2.id = 'x:'+cx+',y:'+cy;
         left = Math.floor(Math.random()*border);
         top = Math.floor(Math.random()*border);
         div2.style.margin = top + 'px ' + left + 'px ' + (border-top) + 'px ' + (border-left) + 'px';
         div2.style.width = (boxsize - border) + 'px';
         div2.style.height = (boxsize - border) + 'px';
         div2.style.backgroundColor = '#fff';
         div.appendChild(div2);
         document.body.insertBefore(div,document.body.firstChild);
      }
   }
   node = document.body.firstChild;
   while(node = node.nextSibling) if (node.style && node.className != 'backsquare') node.style.zIndex = 3;
   shimmer();
}
var wait2,dx,dy,dc,step,upordown,showhide,node;
function shimmer() {
   var i;
   step = 1;
   for (var n=0;n<20;n++) {
      dx = Math.ceil(Math.random()*cx);
      dy = Math.ceil(Math.random()*cy);
      dc = Math.ceil(Math.random()*3);
      upordown = (Math.floor(Math.random()*2)>=1)?1:-1;
      showhide = (Math.floor(Math.random()*2)>=1)?1:0;
      node = document.getElementById('x:'+dx+',y:'+dy);
      if (node) {
         // color is like rgb(xxx, xxx, xxx)
         var color = node.style.backgroundColor;
         if (!showhide) {
            node.style.display = 'none';
            node.style.backgroundColor = '#fff';
         } else {
            node.style.display = 'block';
            if (color.substring(0,1)=='#') {
               var ncolor = '#';
               var nval;
               var channel;
               var channels = new Array();
               var clen = Math.floor(((color.length)-1)/3);
               for (i=0;i<=2;i++) {
                  nval = color.substr(1+(i*clen),clen);   
                  if (clen == 1) nval = new String(nval + nval);
                  channels[i] = h2d(nval);
               }
               target = Math.floor((parseInt(channels[0]) + parseInt(channels[1]) + parseInt(channels[2]))/3) + (upordown * step);
               target = (target<cmin)?cmin+(2*step):target;
               target = (target>cmax)?cmax-(2*step):target;
               color = unifyColor(basecolor,target);
               ncolor = getHexColor(color);
               //ncolor = 'rgb('+color.red+','+color.green+','+color.blue+')';
               node.style.backgroundColor = ncolor;
            } else {
               color = color.substring(4,color.length-1);
               var channels = color.split(/,\s+/);
               target = Math.floor((parseInt(channels[0]) + parseInt(channels[1]) + parseInt(channels[2]))/3) + (upordown * step);
               target = (target<cmin)?cmin+(2*step):target;
               target = (target>cmax)?cmax-(2*step):target;
               color = unifyColor(basecolor,target);
               //ncolor = getHexColor(color);
            //if (n==0)alert(target + ' r' + color.red + ' g'+color.green + ' b'+color.blue + ' h' + ncolor);
               ncolor = 'rgb('+color.red+','+color.green+','+color.blue+')';
               node.style.backgroundColor = ncolor;
            }
         }
      }
   }
   wait2 = setTimeout("shimmer();",1000);
}
function bodyheight() {
   var D = document;
   return Math.max(
      Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
      Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
      Math.max(D.body.clientHeight, D.documentElement.clientHeight)
   );
}
function bodywidth() {
   var D = document;
   return Math.max(
      Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
      Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
      Math.max(D.body.clientWidth, D.documentElement.clientWidth)
   );
}
function getDocDimensions() {
   return new Array(bodywidth(),bodyheight());
} 
function bgstarsquares() {
   //bgclear();
   var left,top;
   var windowdims = windowsize();
   var myWidth  = windowdims[0];
   var myHeight = windowdims[1];   
   var border = 20;
   var boxsize = 25;
   cx = 0;
   cy = 0;
   for (y=5;y<myHeight-boxsize;y+=boxsize) {
      cy++;
      cx=0;
      for (x=5;x<myWidth-boxsize;x+=boxsize) {
         cx++;
         div = createDiv(cx,cy,boxsize);
         document.body.insertBefore(div,document.body.firstChild);
      }
   }
   node = document.body.firstChild;
   while(node = node.nextSibling) if (node.style && node.className != 'backsquare') node.style.zIndex = 3;
   starfield();
}
function windowsize() {
   var myWidth = 0, myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
   //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
      // on mozilla the space used by the vertical scroll bar is included so 
      // you have to test for it and remove it.
      if (window.scrollMaxY > 0) 
         myWidth -= 17;
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
   //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
   //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }
   myHeight = (bodyheight() > myHeight)?bodyheight():myHeight;
//alert(myWidth + '  ' + myHeight);
   return new Array(myWidth,myHeight);
}
function bgstarsquares2() {
   bgclear();
   var left,top;
   var windowdims = windowsize();
   var myWidth  = windowdims[0];
   var myHeight = windowdims[1];   
   var border = 20;
   var boxsize = 60;
   cx = 0;
   cy = 0;
   for (y=0;y<myHeight;y+=boxsize) {
      cy++;
      cx=0;
      for (x=0;x<myWidth;x+=boxsize) {
         cx++;
         div = createDiv2(cx,cy,boxsize);
         if ((x+boxsize) > myWidth) div.style.width = (myWidth - x -2) + 'px';
         if ((y+boxsize) > myHeight) div.style.height = (myHeight - y -2) + 'px';
         document.body.insertBefore(div,document.body.firstChild);
      }
   }
   node = document.body.firstChild;
   var z = 10;
   while(node = node.nextSibling) if (node.style && node.className != 'backsquare') node.style.zIndex = z--;
   starfield();
}                                                 

function bgclear() {
   var div,d;
   div = document.body.firstChild;
   var removed = 0;
   for (d=(document.body.childNodes.length-1);d>=0;d--) {
      div = document.body.childNodes[d];
      if (div.className == 'backsquare') document.body.removeChild(div);
   }
   return true; 
}
function origin() {
   var dims = getDocDimensions();
   var split = 2;
   var origin = 1;
   var dx = Math.floor(dims[0] * (origin/split));
   var dy = Math.floor(dims[1] * (origin/split));
   return new Array(dx,dy);   
}
function createDiv(cx,cy,boxsize) {
   var x = 5 + ((cx-1) * boxsize);
   var y = 5 + ((cy-1) * boxsize);
   var div = document.createElement('DIV');
   div.style.position = 'absolute';
   div.style.top = y + 'px';
   div.style.left = x + 'px';
   div.style.width = boxsize + 'px';
   div.style.height = boxsize + 'px';
   div.style.backgroundColor = '#fff';
   div.style.zIndex = -1;
   div.id = 'x:'+cx+',y:'+cy;
   //div.style.border = '1px solid #ccc';
   div.className = 'backsquare';
   div.style.display = 'none';
   /*
   var div2 = document.createElement('DIV');
   div2.id = 'x:'+cx+',y:'+cy;
   left = border/2;//Math.floor(Math.random()*border);
   top = border/2;//Math.floor(Math.random()*border);
   div2.style.margin = top + 'px ' + left + 'px ' + (border-top) + 'px ' + (border-left) + 'px';
   div2.style.width = (boxsize - border) + 'px';
   div2.style.height = (boxsize - border) + 'px';
   div2.style.backgroundColor = '#fff';
   div.appendChild(div2);
   */
   return div;      
}
function createDiv2(cx,cy,boxsize) {
   var x = ((cx-1) * boxsize);
   var y = ((cy-1) * boxsize);
   var div = document.createElement('DIV');
   div.style.position = 'absolute';
   div.style.top = y + 'px';
   div.style.left = x + 'px';
   div.style.width = boxsize + 'px';
   div.style.height = boxsize + 'px';
   div.style.backgroundColor = '#edf';
   div.style.zIndex = -1;
   div.id = 'x:'+cx+',y:'+cy;
   div.className = 'backsquare';
   div.style.display = 'none';
   return div;      
}
var step,every,offset,moveby,growby;
function starfield() {
//alert('starfield');
   //document.body.style.backgroundColor = getNewColor(document.body.style.backgroundColor);
   var i;
   var boxsize = 60;
   step = 1;
   every = 20;
   moveby = 6;
   growby = -4;
   var o = origin();
   var p = getDocDimensions();
   for (y=1;y<=cy;y++) {
      for (x=1;x<=cx;x++) {
         dc = Math.ceil(Math.random()*3);
         upordown = (Math.floor(Math.random()*2)>=1)?1:-1;
         node = document.getElementById('x:'+x+',y:'+y);
         offset = Math.floor(Math.random()*every);         
         if (node) {
            // color is like rgb(xxx, xxx, xxx)
            var color = node.style.backgroundColor;
            showhide = (node.style.display == 'block');
            var upordown = (Math.floor(Math.random()*2)>=1)?1:-1;
            /*
            if (showhide) {
                // MOVE
                var dx = parseInt(node.style.left) - o[0];
                dx = (dx<0)?(dx + parseInt(node.style.width)):dx;
                var dy = parseInt(node.style.top) - o[1];
                dy = (dy<=0)?(dy + parseInt(node.style.height)):dy;
                var tht = (dy!=0)?Math.tan(dx/dy):(dx>0)?0:Math.PI;
                var nx = (dx>0)?dx-Math.floor(Math.abs(moveby * Math.sin(tht))):dx+Math.floor(Math.abs(moveby * Math.sin(tht)));
                var ny = (dy>0)?dy-Math.floor(Math.abs(moveby * Math.cos(tht))):dy+Math.floor(Math.abs(moveby * Math.cos(tht)));
                var nw = (parseInt(node.style.width) + growby);
                var nh = (parseInt(node.style.height) + growby); 
                nx = (dx<0)?((nx+o[0])-(nw)):(nx+o[0]);
                ny = (dy<0)?((ny+o[1])-(nh)):(ny+o[1]);
                if (nw <= 0) {
                  nx = 5 + ((x-1)*boxsize);
                  ny = 5 + ((y-1)*boxsize);
                  nw = boxsize;
                  nh = boxsize;
                  node.style.display = 'none';                   
                }                 
                node.style.left = nx + 'px';
                node.style.top =  ny + 'px';
                node.style.width =  nw + 'px';
                node.style.height = nh + 'px';
            }
            */ 
            // COLOUR                                   
            // do 1 in every 20 (every) squares and offset which square by 
            // a random offset between 0 and 19 (offset) 
            if ((x+y+offset) % every == 0) {
               node.style.display = 'block';
               node.style.backgroundColor = getNewColor(color);   
            }
         }
      }
   }
   wait2 = setTimeout("starfield();",5000);   
} 
function getNewColor(color) {
   var dc = Math.ceil(Math.random()*changecolor);
   var changeby = (Math.floor(Math.random()*8));
   var upordown = (Math.floor(Math.random()*2)>=1)?changeby:-changeby;
   var target;      
   if (color.substring(0,1)=='#') {
      var ncolor = '#';
      var nval;
      var channel;
      var channels = new Array();
      var clen = Math.floor(((color.length)-1)/3);
      for (i=0;i<=2;i++) {
         nval = color.substr(1+(i*clen),clen);   
         if (clen == 1) nval = new String(nval + nval);
         channels[i] = h2d(nval);
      }
      target = Math.floor((parseInt(channels[0]) + parseInt(channels[1]) + parseInt(channels[2]))/3) + (upordown * step);
      target = (target<cmin)?cmin+(2*step):target;
      target = (target>cmax)?cmax-(2*step):target;
      color = unifyColor(basecolor,target);
      ncolor = getHexColor(color);
   } else {
      color = color.substring(4,color.length-1);
      var channels = color.split(/,\s+/);
      target = Math.floor((parseInt(channels[0]) + parseInt(channels[1]) + parseInt(channels[2]))/3) + (upordown * step);
      target = (target<cmin)?cmin+(2*step):target;
      target = (target>cmax)?cmax-(2*step):target;
      color = unifyColor(basecolor,target);
      ncolor = 'rgb('+color.red+','+color.green+','+color.blue+')';
   }
   return ncolor;
}
function unifyColor(color,target) {
   var tcolor = new Object();
   var multiplier = (target/((color.red + color.green + color.blue)/3));
   tcolor.red = ((color.red*multiplier)>255)?255:Math.floor(color.red*multiplier);
   tcolor.green = ((color.green*multiplier)>255)?255:Math.floor(color.green*multiplier);
   tcolor.blue = ((color.blue*multiplier)>255)?255:Math.floor(color.blue*multiplier);
   
   return tcolor;
}
function getHexColor(color) {
   var r = d2h(color.red);
   while (r.length < 2) r = new String('0' + r); 
   var g = d2h(color.green);
   while (g.length < 2) g = new String('0' + g); 
   var b = d2h(color.blue);
   while (b.length < 2) b = new String('0' + b); 	
   return '#'+r+g+b;
}
function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}