//
// Structure Prototypes
//

function PIC (src, bordercolor, bgcolor)
{
   this.img         = new Image ();
   this.img.src     = XX (src);
   this.bordercolor = bordercolor;
   this.bgcolor     = bgcolor;
}

//
// Globals
//

var xx = new Array
(
   'ABCDEFGHIJ',
   'KLMNOPQRST',
   'UVWXYZabcd',
   'efghijklmn',
   'opqrstuvwx',
   'yz01234567',
   '89-_./****'
);

var pics = new Array
(
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('9132645263734306301432627393043620057526164354132',                           128, 255),
   new PIC ('91234444534630230434326620053615464354132',                                   128, 185),
   new PIC ('9014330302639631740273439444039620057525364354132',                            85,   8),
   new PIC ('9003932303726631034433637263929620154586164354132',                           171,  94),
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('90930393934313043631730302830620058585464354132',                              85,  17),
   new PIC ('9052648393426630334304543342833620154615464354132',                            85, 164),
   new PIC ('9003932303726631034433637263929620052605964354132',                           128,  75),
   new PIC ('9113444266300463637263929620054565764354132',                                 128, 191),
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('911302633632250393930620053526064354132',                                     171, 220),
   new PIC ('900373734444039630140403637304444620053596064354132',                         171, 108),
   new PIC ('9063439266300373440454534620157555664354132',                                 128, 179),
   new PIC ('9133428403730631226433430630526453432264534620059605864354132',               128,  71),
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('90946373430631226434437263929620053565664354132',                             128, 212),
   new PIC ('9003734631228103934323345620054556064354132',                                 192, 132),
   new PIC ('9092638343063183334433044620053585864354132',                                 128,  61),
   new PIC ('909303950393063223437444039620154525364354132',                                85, 173),
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('910343863193437293039620054535864354132',                                      85,  19),
   new PIC ('9092638343063183334433044620058535564354132',                                 128, 185),
   new PIC ('918333447266301263233304334620052566064354132',                               128,  58),
   new PIC ('90037373444403963044533343043620055595964354132',                              85, 177),
   new PIC ('805344563063443374463632140374638306353636302404730436363112643323064413932',  85, 170),
   new PIC ('919343926630940631443272639620152595564354132',                               129, 255),
   new PIC ('9113439294430506302404130620057615564354132',                                 128,  71),
   new PIC ('909303939343130436311503939630240482639620052605364354132',                   171, 121),
   new PIC ('90940295063122650620052535464354132',                                         128,   0)
);

var state   = 0;
var bgcolor = 0;

//
// Functions
//

function TimedOpacity (id, start, milliseconds, o_from, o_to, bg_from, bg_to)
{
   var element = document.getElementById (id);
   var d = new Date ();
   var t = d.getTime ();

   if (t < start + milliseconds)
   {
      SetOpacity (element, o_from + (o_to - o_from) * ((t - start) / milliseconds));
         SetElementBackground ('bkg', Math.round (bg_from + (bg_to - bg_from) * ((t - start) / milliseconds)));

      window.setTimeout ("TimedOpacity ('" + id + "', " + start + ", " + milliseconds + ", " + o_from + ", " + o_to + ", " + bg_from + ", " + bg_to + ")", 10);
   }
   else
   {
      SetOpacity (element, o_to);
         SetElementBackground ('bkg', bg_to);

      window.setTimeout ("Control ('" + id + "')", 0);
   }
}

function FadeImage (id, milliseconds, o_from, o_to, bg_from, bg_to)
{
   var d = new Date ();
   var start = d.getTime ();

   element = document.getElementById (id);

   SetOpacity (element, o_from);
      SetElementBackground ('bkg', bg_from);

   TimedOpacity (id, start, milliseconds, o_from, o_to, bg_from, bg_to);
}

function InitImage (id, src, bordercolor)
{
   element = document.getElementById (id);

   element.src               = src;
   element.style.borderColor = RGB_Gray (bordercolor);
}

function Control (id)
{
   var next;
   
   if (state % 4 == 0)
   {
      InitImage (id, pics[state/4].img.src, pics[state/4].bordercolor);
      bgcolor = pics[state/4].bgcolor;
   }

   next = (state + 2) / 4;
   if (next == pics.length)
      next = 0;

   if (state % 4 == 0)
      window.setTimeout ("FadeImage ('" + id + "', 3000, 0, 100, " + bgcolor + ", " + bgcolor + ")", 0);
   if (state % 4 == 1)
      window.setTimeout ("Control ('" + id + "')", 2000);
   if (state % 4 == 2)
      window.setTimeout ("FadeImage ('" + id + "', 1500, 100, 0, " + bgcolor + ", " + pics[next].bgcolor + ")", 0);
   if (state % 4 == 3)
      window.setTimeout ("Control ('" + id + "')", 250);

   if (++state == pics.length * 4)
      state = 0;
}

function OnLoad (x)
{
   if (x != null)
   {
      var i, j;

      for (i=0; i<pics.length; i++)
         if (pics[i].img.src.indexOf ('__Cover__') >= 0)
         {
            for (j=i+1; j<pics.length; j++)
               pics[j-1] = pics[j];
            pics.length--;
            i--;
         }
   }

   Control ('xyz');
}

function InitWindow ()
{
   element = document.getElementById ('xyz');

   SetOpacity (element, 0);

   window.onload = OnLoad;
}

//
// Obfuscation
//

function XX (s)
{
   var r = '/images/';
   var s, i, a;

   if (s.charAt (0) == '7')
      r += 'portfolio/';
   if (s.charAt (0) == '8')
      r += 'nav/';
   if (s.charAt (0) == '9')
      r += 'books/fit_girls__volume_1/';
   for (i=1; i<s.length; i+=2)
   {
      a = s.charCodeAt (i) - 48;
      b = s.charCodeAt (i + 1) - 48;
      r += xx[a].charAt (b);
   }
   return r;
}

function XY (s)
{
   var r = '';
   var s, i, j, k;

   if (s.indexOf ('/images/portfolio/') >= 0)
   {  r += '7'; i = 18; }
   if (s.indexOf ('/images/nav/') >= 0)
   {  r += '8'; i = 12; }
   if (s.indexOf ('/images/books/fit_girls__volume_1/') >= 0)
   {  r += '9'; i = 34; }
   for (; i<s.length; i++)
   {
      for (j=0; j<7; j++)
         for (k=0; k<10; k++)
            if (s.charAt (i) == xx[j].charAt (k))
            {
               r += j;
               r += k;
            }
   }
   return r;
}

