var current_color;

function preload(src){
 var img=new Image();
 img.src=src;
}

function init(){
  document.getElementById('content_center').style.height=Math.max(document.getElementById('content_content').offsetHeight-370,0)+'px';
  current_color=document.getElementById('wrap').className;
  window.setInterval(nextColor,8000);
  preload('grfx/button_h.png');
  preload('grfx/icon_a.png');
  preload('grfx/icon_z.png');
  preload('grfx/icon_h.png');
  preload('grfx/icon_f.png');
  preload('grfx/icon_o.png');
  preload('grfx/icon_d.png');
  preload('grfx/icon_k.png');
}

function nextColor(){
   hex=current_color;
   var r=parseInt(hex.substr(0,2),16)/255;
   var g=parseInt(hex.substr(2,2),16)/255;
   var b=parseInt(hex.substr(4,6),16)/255;
   var max=Math.max(r,g,b);
   var min=Math.min(r,g,b);
   var h,s,l=(max+min)/2;
   if(max==min){
      h=s=0;
   }
   else{
      var d=max-min;
      s=(l>0.5)?d/(2-max-min):d/(max + min);
      switch(max){
         case r:h=(g-b)/d+(g<b?6:0); break;
         case g:h=(b-r)/d+2; break;
         case b:h=(r-g)/d+4; break;
      }
      h/=6;
   }

   h=(h+0.01)%1;

   if(s == 0){
      r = g = b = l;
   }
   else{
      function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
      }
      var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
      var p = 2 * l - q;
      r = hue2rgb(p, q, h + 1/3);
      g = hue2rgb(p, q, h);
      b = hue2rgb(p, q, h - 1/3);
   }
   r=(Math.round(r*255)).toString(16);
   //if(r.length<2)r='0'+r;
   g=(Math.round(g*255)).toString(16);
   //if(g.length<2)r='0'+g;
   b=(Math.round(b*255)).toString(16);
   //if(b.length<2)r='0'+b;
   current_color=r.concat(g,b);
   document.getElementById('wrap').style.backgroundColor='#'+current_color;
}


var menu_slide_flag;
var menu_slide_out_wanted=false;

function menu_icon(icon){
   menu_slide_out_wanted=false;
   menu_slide_flag=icon;
   document.getElementById('menu').style.background='url(grfx/icon_'+icon+'.png) 334px 50% no-repeat';
   menu_slide_in(334,icon);
}

function menu_slide_in(rest,icon){
   if(menu_slide_flag!=icon)return;
   document.getElementById('menu').style.backgroundPosition=rest+'px 50%';
   var nrest=Math.floor(rest-(rest-234)/5);
   if(nrest>234){
         window.setTimeout(function(){menu_slide_in(nrest,icon);},40);
   }
   else{
      document.getElementById('menu').style.backgroundPosition='234px 50%';
      menu_slide_flag='';
      if(menu_slide_out_wanted) menu_slide_out(224,icon);
   }
}

function menu_slide_out(rest,icon){
   if(menu_slide_flag!='')return;
   document.getElementById('menu').style.backgroundPosition=rest+'px 50%';
   var nrest=Math.ceil(rest+(334-rest)/6);
   if(nrest<334){
      window.setTimeout(function(){menu_slide_out(nrest,icon);},40);
   }
   else{
      document.getElementById('menu').style.backgroundPosition='334px 50%';
   }
}
