// Rewriting index frame hash to go directly to a specific album/image

var redirecter;

onReady(function()
{
   redirecter = new HashRedirecter();
});

function HashRedirecter()
{
   this.domFrame = document.getElementById('mainFrame');

// Extract src from frame src
   this.srcOriginal = this.domFrame.src;
   var aMatches = this.srcOriginal.match(/iloapp.([^\/]+)\/gallery\/([^\?\#]+)/);
//   var aMatches = this.srcOriginal.match(/([^\/]+)\/gallery\/([^\?\#]+)/);
   if(aMatches)
   {
      this.domain = aMatches[1];
      this.galleryLocation = aMatches[2];
   }
   else
   {
      return;
   }

// Update frame for the first time
   this.UpdateFrame();

// Begin interval
   var This = this;
   setInterval(function()
   {
      This.UpdateFrame();
   }, 500);
}

HashRedirecter.prototype =
{
   hashCurrent: '',

   UpdateFrame: function()
   {
      if(location.hash != this.hashCurrent)
      {
         var txtFrameSrc = 'http://iloapp.' + this.domain + '/gallery/' + this.galleryLocation + '?';
//         var txtFrameSrc = 'http://' + this.domain + '/gallery/' + this.galleryLocation + '?';
         var domFrame = document.getElementById('mainFrame');

      // Change domFrame src according to hash
         if(aMatches = location.hash.match(/^#home(\.(\d+))?$/))
         {
            if(aMatches[2] != null)
            {
               txtFrameSrc += 'Home&page=' + parseInt(aMatches[2]);
            }
            else
            {
               txtFrameSrc += 'Home';
            }
         }
         else
         {
         // Get album and image ids from hash
            aMatches = location.hash.match(/^#(edit|slideshow)?(\d+)(?:\.(\d+))?/);
            if(aMatches)
            {
               var albumId = aMatches[2];
               var imageId = aMatches[3];

               if(aMatches[1] == 'edit')
               {
                  txtFrameSrc += 'EditAlbum&album=' + albumId;
               }
               else if(aMatches[1] == 'slideshow')
               {
                  txtFrameSrc += 'SlideShow&album=' + albumId;
               }
               else
               {
                  txtFrameSrc += 'Album&album=' + albumId;
               }

            // Check if option image id was given
               if(imageId != null)
               {
                  txtFrameSrc += '&image=' + imageId;
               }
            }
         }

         this.domFrame.src = txtFrameSrc;

      // Update hashCurrent so it doesn't keep checking
         this.hashCurrent = location.hash;
      }
      else
      {
      // Check if cookie contains a message
         var aMatches = document.cookie.match(/gmsg(\d+)=([^;]+)/);
         if(aMatches)
         {
            var galleryId = aMatches[1];
            var message = aMatches[2];

         // Delete the cookie
            var expires = new Date(0);
            document.cookie = 'gmsg' + galleryId + '=_;path=/;domain=.' + this.domain + ';expires=' + expires.toGMTString();
//            document.cookie = 'gmsg' + galleryId + '=_;path=/;expires=' + expires.toGMTString();

         // Set hash according to message
            if(!navigator.userAgent.match(/safari/i))
            {
               location.replace('#' + message);
               this.hashCurrent = '#' + message;
            }
         }
      }
   }
}
