var imgpreload = {
   progress: 0,
   lengthOfPreloadBar: 150,   heightOfPreloadBar: 15,
   images: [ { id: 'ldimg1', url: 'imgs/final/loading2.gif' },
    { id: 'ldimg2', url: 'imgs/final/loading3.gif' },
    { id: 'ldimg3', url: 'imgs/final/loading4.gif' },
      { id: 'preimg51', url: 'imgs/1/3bargold.png' },
      { id: 'preimg50', url: 'imgs/1/backtotop.gif' },
      { id: 'preimg49', url: 'imgs/1/homeleft.png' },
      { id: 'preimg48', url: 'imgs/1/homeright.png' },
      { id: 'preimg47', url: 'imgs/1/hometext.png' },
      { id: 'preimg46', url: 'imgs/1/name.png' },
      { id: 'preimg45', url: 'imgs/1/tn30days.png' },
      { id: 'preimg44', url: 'imgs/1/tn66discount.png' },
      { id: 'preimg43', url: 'imgs/1/tnfindperfect.png' },
      { id: 'preimg42', url: 'imgs/1/tnfreeshipping.png' },
      { id: 'preimg41', url: 'imgs/1/tnhome.png' },
      { id: 'preimg40', url: 'imgs/1/tnlogin.png' },
      { id: 'preimg39', url: 'imgs/1/tnsignup.png' },
      { id: 'preimg38', url: 'imgs/1/topnav1.png' },
      { id: 'preimg37', url: 'imgs/1/trshoppingbag.png' },
      { id: 'preimg36', url: 'imgs/1/trwishlist.png' },
      { id: 'preimg35', url: 'imgs/1/bg.png' },
      { id: 'preimg34', url: 'imgs/1/gifts.png' },
      { id: 'preimg33', url: 'imgs/1/rings.png' },
      { id: 'preimg32', url: 'imgs/1/baglamp.png' },
      { id: 'preimg31', url: 'imgs/1/necklaces.png' },
      { id: 'preimg30', url: 'imgs/1/bracelets.png' },
      { id: 'preimg29', url: 'imgs/1/auctions.png' },      
      { id: 'preimg28', url: 'imgs/1/diamonds.png' },
      { id: 'preimg27', url: 'imgs/1/earrings.png' },
      { id: 'preimg26', url: 'imgs/1/guidance.png' },
      { id: 'preimg25', url: 'imgs/final/bgcontrol.png' },
      { id: 'preimg24', url: 'imgs/final/test3.png' },
      { id: 'preimg23', url: 'imgs/showcase.jpg' },
      { id: 'preimg22', url: 'imgs/btop.gif' },
      { id: 'preimg21', url: 'imgs/final/handlet_slide.gif' },
      { id: 'preimg20', url: 'imgs/final/handleb_slide.gif' },
      //{ id: 'preimg19', url: 'imgs/final/sepgold.png' },
      { id: 'preimg18', url: 'imgs/final/additm.gif' },
      { id: 'preimg17', url: 'imgs/final/wishlistmid.jpg' },
      { id: 'preimg16', url: 'imgs/final/clarity_slide.png' },
      { id: 'preimg15', url: 'imgs/final/price_slide.png' },
      { id: 'preimg14', url: 'imgs/final/carat_slide.png' },
      { id: 'preimg13', url: 'imgs/final/color_slide.png' },
      { id: 'preimg12', url: 'imgs/final/sbagmid.jpg' }, 
      { id: 'preimg11', url: 'imgs/final/printit.png' },
      { id: 'preimg10', url: 'imgs/final/emailit.png' },
      { id: 'preimg9', url: 'imgs/final/addtowishlist.png' },
      { id: 'preimg8', url: 'imgs/final/addtobag.png' },
      { id: 'preimg7', url: 'imgs/final/zoomin.png' },
      { id: 'preimg6', url: 'imgs/final/itmincludes.png' },
      { id: 'preimg5', url: 'imgs/final/itmsheads.png' },
      { id: 'preimg4', url: 'imgs/final/itmshead.png' }, 
      { id: 'preimg3', url: 'imgs/final/loginbg.gif' },
      { id: 'preimg2', url: 'imgs/final/close.png' },                 
      { id: 'preimg1', url: 'imgs/final/slidethelp.gif' }      
   ],
   startLoading: function() {
	$('center').hide();
	
    var coverage = Math.floor(this.lengthOfPreloadBar/this.images.length);
    var leftOverWidth = this.lengthOfPreloadBar%coverage;
    var forlen = (this.images.length-1);	
    var preloadBar = '';
    for(var i = 0; i < forlen; i++) {        preloadBar += '<img src="' + this.images[1].url + '" width="' + coverage + '" height="' + this.heightOfPreloadBar + '" name="stpbarimg' + i + '" align="absmiddle">'    }    preloadBar += '<img src="' + this.images[1].url + '" width="' + (leftOverWidth+coverage) + '" height="' + this.heightOfPreloadBar + '" name="img' + forlen + '" align="absmiddle">'
    
	$('premessage').innerHTML = '<div><img src="./imgs/final/loading2.gif"></div><div>'+preloadBar+' &nbsp; <font id="ldbpercent">1%</font></div>';
      this.loadNextImage();
   },
   loadNextImage: function() {
      if (this.progress >= (this.images.length-1)) {
         this.progress = 0;
         this.success();
         return;
      }
      imageObject = new Image();
      imageObject.onload = function() { imgpreload.imageLoaded(); }
      imageObject.onerror = function() { imgpreload.imageError(); }
      imageObject.src = this.images[ this.progress ].url;
      this.images[ this.progress ].image = imageObject;
   },
   imageLoaded: function() {
   	  $('ldbpercent').innerHTML = Math.floor((100*this.progress)/this.images.length) + '%';
   	  eval("document.stpbarimg" + this.progress + ".src=this.images[2].url");
      this.progress++;
      this.loadNextImage();
   },
   imageError: function() {
   		alert(this.images[this.progress].url);
      this.progress++;
      this.loadNextImage();
   },
   success: function() {
	$('premessage').hide();
	$('center').show();
   }
}
