// Rewriting index frame hash to go directly to specific post(s)

var redirecter;

$(document).ready(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\.([^\/]+)\/blog\/([^\?\#]+)/);
//   var aMatches = this.srcOriginal.match(/([^\/]+)\/blog\/([^\?\#]+)/);
   if(aMatches)
   {
      this.domain = aMatches[1];
      this.blogLocation = 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(this.hashCurrent != location.hash)
      {
         var txtBlogSrc = 'http://iloapp.' + this.domain + '/blog/' + this.blogLocation + '?';
//         var txtBlogSrc = 'http://' + this.domain + '/blog/' + this.blogLocation + '?';

         if(aMatches = location.hash.match(/^#home(\.(\d+))?$/))
         {
         // Requesting home page
            if(aMatches[2] != '')
            {
            // Include page number
               this.domFrame.src = txtBlogSrc + 'Home&page=' + parseInt(aMatches[1]);
            }
            else
            {
               this.domFrame.src = txtBlogSrc + 'Home';
            }
         }
         else
         {
            var aMatches = location.hash.match(/^#(post|comments|category|user)?(\d+)(?:\.(\d+))?/);
            if(aMatches)
            {
               var field = aMatches[1], id = aMatches[2], page = aMatches[3];
               var src;

            // Set frame src
               if(field == 'comments')
               {
                  src = txtBlogSrc + 'NewComment&post=' + id;
               }
               else if(field == 'post' || field == 'category' || field == 'user')
               {
                  src = txtBlogSrc + 'Home&' + field + '=' + id;
               }

            // Add page if included
               if(page != null)
               {
                  src += '&page=' + page;
               }

            // Set url with fragment/identifier
               this.domFrame.src = src + '#niceURL';
            }
         }

         this.hashCurrent = location.hash;
      }
      else
      {
      // Check if cookie contains a message
         var aMatches = document.cookie.match(/\bbmsg(\d+)=([^;]+)/);
         if(aMatches)
         {
            var blogId = aMatches[1];
            var message = aMatches[2];

         // Delete the cookie
            var expires = new Date(0);
            document.cookie = 'bmsg' + blogId + '=_;path=/;domain=.' + this.domain + ';expires=' + expires.toGMTString();
            document.cookie = 'bmsg' + blogId + '=_;path=/;expires=' + expires.toGMTString();

         // Set hash according to message
            if(!navigator.userAgent.match(/safari/i))
            {
               location.replace('#' + message);
               this.hashCurrent = '#' + message;
            }
         }
      }
   }
}
