﻿// *** Service Calling Proxy Class
function serviceProxy(serviceUrl)
{
   var _I = this;
   this.serviceUrl = serviceUrl;


   // *** Call a wrapped object
   this.invoke = function(method, data, callback, error, bare)
   {
      // *** Convert input data into JSON - REQUIRES Json2.js
      var json = JSON2.stringify(data);


      // *** The service endpoint URL        
      var url = _I.serviceUrl + method;

      $.ajax({
         url: url,
         data: json,
         type: "POST",
         processData: false,
         contentType: "application/json",
         timeout: 10000,
         dataType: "text",  // not "json" we'll parse
         success:
                    function(res)
                    {
                       if (!callback) return;


                       // *** Use json library so we can fix up MS AJAX dates
                       var result = JSON2.parse(res);

                       // *** Bare message IS result
                       if (bare)
                       { callback(result); return; }


                       // *** Wrapped message contains top level object node

                       // *** strip it off
                       for (var property in result)
                       {
                          callback(result[property]);
                          break;
                       }
                    },
         error: function(xhr)
         {
            if (!error) return;
            if (xhr.responseText)
            {
               var err = JSON2.parse(xhr.responseText);
               if (err)
                  error(err);
               else
                  error({ Message: "Unknown server error." })
            }

            return;
         }
      });
   }
}

$(document).ready(function()
{
   $("#tagsDialog").dialog({
      autoOpen: false,
      title: "Categories",
      open: function() { $("#tagsDialog").dialog("option", "position", [$("#tagsDialogLink").offset().left - 70,100]); }
   });

   $("#tagsDialogLink").click(function()
   {
      $("#tagsDialog").dialog("open");
      return false;
   });

   $("#archiveMonthsDialog").dialog({
      autoOpen: false,
      title: "Archived Months",
      open: function() { $("#archiveMonthsDialog").dialog("option", "position", [$("#archiveMonthsLink").offset().left - 70,100]); }
   });

   $("#archiveMonthsLink").click(function()
   {
      $("#archiveMonthsDialog").dialog("open");
      return false;
   });

   $("#siteNameLink").hover(
               function() { $(this).addClass("ui-state-hover"); },
               function() { $(this).removeClass("ui-state-hover"); });

   $("input[type='button']").addClass("ui-state-default").addClass("ui-corner-all");

   $("input[type='button']").hover(
                function() { $(this).addClass("ui-state-hover"); },
                function() { $(this).removeClass("ui-state-hover"); });

   $("input[type='submit']").addClass("ui-state-default").addClass("ui-corner-all");
   $("input[type='submit']").hover(
                function() { $(this).addClass("ui-state-hover"); },
                function() { $(this).removeClass("ui-state-hover"); });

   $(".commentViewBoxStyle [type='submit']").removeClass("commentViewControlStyle");

   $(".blogEntryArrowButton").hover(
      function() { $(this).addClass("blogEntryCloseOpenLinkHover"); },
      function() { $(this).removeClass("blogEntryCloseOpenLinkHover"); });

   $(".blogEntryCloseOpenLink").click(
      function()
      {
         var box = $(this).siblings(".entryContentBox");
         
         if (box.is(":visible"))
         {
            box.hide("explode", null, "slow");
            $(this).children(".blogEntryArrowButton")[0].src = "/images/DownArrowHead.png";
         }
         else
         {
            box.show("explode", null, "slow");
            $(this).children(".blogEntryArrowButton")[0].src = "/images/UpArrowHead.png";
         }

         return false;
      });

   var img = new Image();

   img.src = "/images/DownArrowHead.png";
});
