//So yeah this is really non portable Javascript
//if you can find something useful here nice and please leave
//a thanks to the bloke at www.somedodgywebsite.com in your comments
//--------------------------
//Helpers To put in INC
function PreLoadImages(imageList) {
   for (a=0;a<imageList.length;a++) {
      b=new Image();
      b.src=imageList[a];
   }
}

//-------------------------------------------------------
//Mouse Wheel
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.*/
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                MouseWheel(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
   event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

//--------------------------
//Main

function LoadImages(data,index,ToArray) {
   var Pos=0;
   if (index>0) {
      var atozH = document.getElementById('atoz').offsetHeight;
      ViewingTop ? Pos = index/(data.length-5) * document.getElementById('atoz').offsetHeight : Pos = index/(data.length-4) * document.getElementById('atoz').offsetHeight;
      if (Pos > atozH - 15) Pos = atozH - 15;
   }
   //alert(Pos);
   document.getElementById("atoz").style.backgroundPosition = '3px '+Pos+'px';
   var DisplayIndex=0;
   for (a=index;a<index+10&&a<data.length;a++) {
      var chunks = data[a].split(",");
      if (a<index+5) {
         if (ToArray[a]==null) {
            if (ViewingTop) {
               ToArray[a]=new ImageLoader(chunks[1],document.getElementById('h_img'+DisplayIndex),chunks[0],chunks[2],null);
            } else {
               ToArray[a]=new ImageLoader(chunks[1],document.getElementById('h_img'+DisplayIndex),chunks[0],chunks[2],chunks[3]);
            }
         } else {
            if (ToArray[a].Image.complete) {
               ToArray[a].DisplayTo=document.getElementById('h_img'+DisplayIndex);
               DisplayImage(ToArray[a]);
            } else {
               var ThisImage = document.getElementById('h_img'+DisplayIndex);
               ThisImage.src = LoadingImage;
               ToArray[a].DisplayTo=ThisImage;
            }
         }
         DisplayIndex++;
      } else if (ToArray[a]==null) {
         if (ViewingTop) {
            ToArray[a]=new ImageLoader(chunks[1],null,chunks[0],chunks[2],null);
         } else {
            ToArray[a]=new ImageLoader(chunks[1],null,chunks[0],chunks[2],chunks[3]);
         }
      }
   }
   if (data.length<5) {
      for (a=0;a<5;a++) {
         if (a >= data.length) {
            var ThisDisplay = document.getElementById('h_img'+a);
            ThisDisplay.src = BlankImage;
            ThisDisplay.alt = "Nothing";                        ;
            ThisDisplay.title = "";
            ThisDisplay.onclick = function() {return false;}
            ThisDisplay.parentNode.removeAttribute('href');
         }
      }
   }
   if (!ViewingTop) {
      if ( index+5 > data.length ) {
         //Place our go back;
         var BackImageEl=document.getElementById('h_img'+(data.length-index));
         BackImageEl.src=BackImage;
         BackImageEl.title="Back To Model List";
         BackImageEl.alt="Back";
         BackImageEl.parentNode.href="#";
         BackImageEl.onclick = function() {
            ViewingTop=true;
            //Old bug hack
            //TopGalleries==null ? LoadRandomIndex() : LoadImages(TopGalleries,TopViewIndex,TopImageLoader);
            LoadImages(TopGalleries,TopViewIndex,TopImageLoader);
            return false;
         }
      }                
   }
}

function ImageLoader(Url,DisplayTo,Model,Photographer,hID) {
   this.DisplayTo = DisplayTo;
   this.Image = new Image();
   this.Model = Model;
   this.Photographer = Photographer;
   this.URL = "http://hosted.met-art.com/Full_met-art_"+Url+"/cover.jpg";
   this.hID = hID;
   if (this.DisplayTo != null) {
      this.DisplayTo.src = LoadingImage;
      this.DisplayTo.alt = "loading...";
   }
   var me = this;
   this.Image.onload=function () {
      DisplayImage(me);
   }
   this.Image.src=this.URL;
}

function DisplayImage(owner) {
   //alert(owner.DisplayTo);
   if (owner.DisplayTo != null) {
      //alert('Display ' + ViewingTop);
      //stops the loading of a sub gallery onto top gallery if user has switched (or vis versa)
      if ((owner.hID == null &! ViewingTop) || (owner.hID != null && ViewingTop)) {return;}
      owner.DisplayTo.src = owner.URL;
      owner.DisplayTo.title = owner.Model + " By " + owner.Photographer;
      owner.DisplayTo.alt = owner.Model;
      if (ViewingTop) {
         owner.DisplayTo.onclick = function() {GetSubGalleries(owner.Model);}
         owner.DisplayTo.parentNode.href="mac2.php?model="+owner.Model;
         owner.DisplayTo.parentNode.onclick = function () {return false;};
      } else {
         owner.DisplayTo.onclick = function() {return true;}
         owner.DisplayTo.parentNode.href="showpage.php?hID="+owner.hID;
         owner.DisplayTo.parentNode.onclick = function () {return true;};         
      }
   }
}

function MouseWheel(delta) {
   delta < 0 ? Nav(1) : Nav(-1);
}

function Nav(inc) {
   if (ViewingTop) {
      if ((inc == 1) && ((TopViewIndex + 5) < TopGalleries.length)) {
         TopViewIndex++;
         LoadImages(TopGalleries,TopViewIndex,TopImageLoader);
         return;
      }
      if (inc == -1 && TopViewIndex > 0) {
         TopViewIndex--;
         LoadImages(TopGalleries,TopViewIndex,TopImageLoader);
         return;
      }
   } else {
      if ((inc == 1) && ((SubViewIndex + 4) < SubGalleries.length)) {
         SubViewIndex++;
         LoadImages(SubGalleries,SubViewIndex,SubImageLoader);
         return;
      }
      if (inc == -1 && SubViewIndex > 0) {
         SubViewIndex--;
         LoadImages(SubGalleries,SubViewIndex,SubImageLoader);
         return;
      }
   }

   
}

//--------------------------
//Vars

var TopGalleries;
var SubGalleries;

var TopImageLoader;
var SubImageLoader;

var TopViewIndex;
var SubViewIndex;

var ViewingTop;
//--------------------------
//Properties
function GetTopGalleries(letter) {
   var atozimg = document.getElementById('atoz').getElementsByTagName('img');
   for (a=0;a<atozimg.length;a++)
   {
      if (atozimg[a].alt==letter) {
         atozimg[a].parentNode.parentNode.style.backgroundImage="url('images/atoz.gif')";
         atozimg[a].parentNode.parentNode.style.backgroundPosition='top center'; //"-1px -1px";
         atozimg[a].parentNode.parentNode.style.backgroundRepeat='no-repeat';
      } else {
         atozimg[a].parentNode.parentNode.style.background="";
      }
   }
   XmlGetRequest('i='+letter,SetTopGalleries);
}

function SetTopGalleries(dataarray) {
   ViewingTop=true;
   TopGalleries=dataarray;
   TopImageLoader=new Array();
   TopViewIndex=0;
   LoadImages(TopGalleries,0,TopImageLoader);
   //alert(TopGalleries[0]);
}

function GetSubGalleries(name) {
   XmlGetRequest('m='+name,SetSubGalleries);
}

function SetSubGalleries(dataarray) {
   ViewingTop=false;
   SubGalleries=dataarray;
   SubImageLoader=new Array();
   SubViewIndex=0;
   LoadImages(SubGalleries,0,SubImageLoader);
   //alert(SubGalleries[0]);
}
//--------------------------
//Events and instants

var BlankImage = "images/blank.gif";
var LoadingImage = "images/loading.gif";
var BackImage = "images/back.gif";

PreLoadImages([BlankImage,LoadingImage,BackImage]);

window.onresize=SetGalleryWidth;
window.onload=function () {
   SetGalleryWidth();
   XmlTest();
   GetRandomTopGalleries();
   if (typeof(GetSubGalleriesOnLoad)!="undefined") {
      //alert(GetSubGalleriesOnLoad);
      GetSubGalleries(GetSubGalleriesOnLoad);   
   }
   //GetTopGalleries("Q");
   //GetSubGalleries('Ariel A');
}

//--------------------------
//Helpers Unique
function SetGalleryWidth() {
   //alert(document.getElementById('h_img0').width);
   document.getElementById('galleries').style.width = document.getElementById('h_img0').width+'px';
   document.getElementById('galleries').style.maxWidth = document.getElementById('h_img0').width+'px';
   try {
      var thissize = document.getElementById('h_img0').width*1.8
      if (thissize < 110) thissize=110;
      if (thissize > 170) thissize=170;      
      parent.document.getElementById('left_frame').cols=thissize+', *';
   } catch (err) {
   }
   //alert('OK');
}

function GetRandomTopGalleries() {
   do {
      var RandomCap = String.fromCharCode(Math.floor(Math.random()*26)+65);      
   } while (RandomCap=='X' || RandomCap=='W' || RandomCap=='Q')
   GetTopGalleries(RandomCap);
}

function JumpToModel(name) {
   if (TopGalleries==null || TopGalleries=="") GetRandomTopGalleries();
   GetSubGalleries(name);
}