String.prototype.supplant = function(o)
{
  return this.replace(/{([^{}]*)}/g,
    function(a,b)
    {
      return(typeof(o[b]) !== "undefined" && o[b] !== null) ? o[b].toString() : "";
    }
  )
};

var WEBLINC = {};

WEBLINC.ModalContent = 
{
  DefaultWidth: 600,
  LoadingImagePath : "",
  CloseLinkClass: "",
  CloseLinkHtml: "Close",
  ErrorHtml: "<h2>Error!</h2><p>Sorry, there was an error loading this content.</p>",
  Init: function()
    {
      var triggers = jQuery("a.modal");

      jQuery("<div id=\"modal-content\" class=\"jqmWindow\"><img id=\"loading\" src=\"" + WEBLINC.ModalContent.LoadingImagePath + "\" alt=\"Loading\" /><div id=\"modal-flash-content\"></div><div id=\"modal-body\"></div><a id=\"modal-close-link\" class=\"" + WEBLINC.ModalContent.CloseLinkClass + "\" href=\"#\">" + WEBLINC.ModalContent.CloseLinkHtml + "</a></div>")
        .appendTo("body")
        .hide()
        .css("textAlign", "center")
        .jqm(
          {
            trigger: triggers,
            onShow: function(hash)
            {
              jQuery("embed,object").css("visibility", "hidden");
              jQuery("#modal-body", hash.w).empty();
              jQuery("#modal-flash-content", hash.w).empty();
              jQuery("#modal-close-link", hash.w).bind("click", function(event) { event.preventDefault(); jQuery(hash.w).jqm().jqmHide(); });               

              jQuery(hash.w).css("textAlign", "center");
              jQuery("img#loading", hash.w).show();
              
              WEBLINC.ModalContent.LoadContent(jQuery.event.fix({ type: "click", target: hash.t }), hash.w);
              
              hash.o.fadeIn('normal', function() { hash.w.slideDown('fast'); });
            },
            onHide: function(hash)
            {
              hash.w.slideUp('fast', 
                function() { hash.o.fadeOut('fast', 
                  function() 
                  { 
                    jQuery("embed,object").css("visibility", "visible"); 
                    jQuery("#modal-body", hash.w).empty(); 
                    jQuery("#modal-flash-content", hash.w).empty();
                    hash.w.css("width", parseInt(WEBLINC.ModalContent.DefaultWidth) + "px").css("marginLeft", ((WEBLINC.ModalContent.DefaultWidth / 2) - WEBLINC.ModalContent.DefaultWidth) + "px");
                  }); 
               });     									
            }									
          }
        );
    },
  LoadContent: function(event, $modalWindow)
    {
      event.preventDefault();
      
      jQuery("img#loading", $modalWindow).show();
      jQuery("#modal-body", $modalWindow).empty();
      jQuery("#modal-flash-content", $modalWindow).hide().empty();
      $modalWindow.slideUp("fast").css({
        textAlign: "center",
        top: jQuery(window).scrollTop() + 100
      }).slideDown("fast");  
      
      // Hack for IE. IE returns event.target.href of img
      var url = (event.target.href && (event.target.href.indexOf("modalContent") >= 0 || event.target.href.indexOf("sendFriendEmailForm") >= 0)) ? event.target.href : jQuery(event.target).parent("a.modal[href*=modalContent]").get(0).href;
      if(!url)
      {               
        return false;
      }
      if(WEBLINC.Domain.indexOf(document.domain) === -1)
      {
       url += "&jsoncallback=?";
      }

      jQuery.ajax(
        {
          dataType: "jsonp",
          jsonp: "jsoncallback", 
          url: url,
          data: { async: 1 },
          complete: function(request, success)
            {
              jQuery("img#loading", $modalWindow).hide();
              $modalWindow.css("textAlign", "left");
            },
          success: function(json) 
            { 
              if(json.error)
              {
                jQuery("#modal-body", $modalWindow).html(json.error);
              }
              else
              {
                $modalWindow.append(json.straightToPage);
                
                var width = (json.width) ? json.width : WEBLINC.ModalContent.DefaultWidth;
                $modalWindow.animate(
                    {
                      width: parseInt(width, 10) + "px",
                      marginLeft: ((width / 2) - width) + "px"
                    },
                    "fast",
                    function()
                    {      
                      if(json.flashSource && json.flashType)
                      {
                        jQuery("#modal-flash-content", $modalWindow).show();
                        new WEBLINC.FlashContent(jQuery("#modal-flash-content", $modalWindow), json.flashSource, { type: json.flashType, wmode: "window", allowFullScreen: "true", modal: true });
                      }
                      
                      jQuery("#modal-body", $modalWindow).html(json.body).find("a.modal").bind("click", function(event)
                        {
                          WEBLINC.ModalContent.LoadContent(event, $modalWindow);
                        }
                      );
                    }
                  );
              }
            },
          error: function(request, error, exception)
            {
              jQuery("#modal-body", $modalWindow).html(WEBLINC.ModalContent.ErrorHtml);
            }
        }
      );

    }
};

WEBLINC.FlashContent = function(obj, file, options)
{
  var options = (options == null) ? {} : options;
  options.type = (options.type == null) ? "standard" : options.type;
  options.flashvars = (options.flashvars == null) ? {} : options.flashvars;
  options.modal = (options.modal == null) ? false : options.modal;

  jQuery(document).ready(
    function()
    {
      var $containerObj, srcFile;

      if(jQuery(obj).get(0))
      {
        $containerObj = jQuery(obj);
      }
      else if(jQuery("#" + obj.toString()).get(0))
      {
        $containerObj = jQuery("#" + obj.toString());
      }
      
      if(!$containerObj) return;

      if(options.type === "video")
      {
        srcFile = WEBLINC.FlashContent.VideoPlayer;
        options.flashvars.fileName = file;
        options.flashvars.skinName = WEBLINC.FlashContent.SkinName;
		options.flashvars.previewImage = options.previewImage;
        options.wmode = options.wmode || "transparent";
		options.allowFullScreen = options.allowFullScreen || "true";

        if(options.modal)
        {
          options.width = options.height = "100%";
          
          $containerObj.css("height", WEBLINC.FlashContent.VideoPlayerHeight);
          $containerObj.css("width", WEBLINC.FlashContent.VideoPlayerWidth);
        }
      }
      else
      {
        srcFile = file;
      }    

      $containerObj.empty().flash({
          src: srcFile,
          width: options.width || "100%",
          height: options.height || "100%",
          flashvars: options.flashvars,
          wmode: options.wmode,
		  allowFullScreen: options.allowFullScreen,
          params: options.params || {}
      });
    }
  );
};
