/* Portlet functionality */

(function($) {
  $.fn.portlet = function(options) {
    // build main options before element iteration
    $.fn.portlet.opts = $.extend({}, $.fn.portlet.defaults, options);

    //$("#portlet").height($("#portlet").height());  // Set the height so that columns within have correct 100% height.

    //
    // Make portlets sortable
    //
    return this.each(function() {
      $(this).sortable({
        items: 'div.userPortlet',
        connectWith: ['.portletColumnInner'],
        handle: 'div.portletHeader',
        placeholder: 'drop',
        forcePlaceholderSize: true,
        cursor: 'move',
        tolerance: 'pointer',
        start: StartDrag,
        stop: StopDrag
      });
    });
  };

  //
  // Start Drag
  //
  function StartDrag(e, ui) {

      $("div.portletContent2 div.filter").css("display", "none");
      $("div.portletContent2 div.filter").css("z-index", "0");
      $("div.portletContent2").css("z-index", "0");
    //ui.item.find("iframe").css("display", "none");
    //$("#portlet").height('auto');    // Set the height to auto so that columns expand as needed.
  }

  //
  // Stop Drag
  //
  function StopDrag(e, ui) {
    //ui.item.find("iframe").css("display", "block");

    SaveChanges();

    //$("#portlet").height($("#portlet").height());    // Set the height so that columns within have correct 100% height.
  }

  //
  // Get portlet data for a given column
  //
  function GetColumnPortlets(colNo) {
    var s = "";

    $("#pCol" + colNo + " div.userPortlet").each(function() {
      if (s != "") {
        s += ",";
      }
      var pId = $(this).attr("id").replace("portlet_", "");
      pId = pId.substring(0, pId.indexOf("_"));
      s += pId;
    });

    return s;
  }

  //
  // Save portlet changes
  //
  function SaveChanges() {

    var nColumns = $.fn.portlet.opts.nColumns;
    if ((nColumns == undefined) || (nColumns == "")) {
      nColumns = 3;
    }

    var i = 0;
    var portletOrder = "";
    while (i < nColumns) {
      if (i > 0) { portletOrder += '|'; }
      portletOrder += GetColumnPortlets(i);
      i++;
    }

    // Work out the base url for AJAX requests
    var urlPrefix = $.fn.portlet.opts.urlPrefix;

    var url = (urlPrefix + "?appaction=saveportlets");

    var viewId = $.fn.portlet.opts.viewId;
    if ((viewId != undefined) && (viewId != "") && (viewId > 0)) {
      url += ("&tab=view&uvId=" + viewId);
    }

    url += ("&data=" + portletOrder);

    // Add an every changing argument (date/time in millisecond)
    // so very poorly written browsers do not cache the AJAX response
    url += ('&timestamp=' + new Date().getTime());

    $.get(url);
  }

  //
  // Remove a portlet
  //
  $.fn.portlet.RemovePortlet = function(uniquePortletId, portletId) {
    $('#' + uniquePortletId).remove();

    SaveChanges();

    // Enable the portlet to be added again from add content pop-up
    $('li#portletItem_' + portletId).removeClass("disable");

    return false;
  }

  //
  // plugin defaults
  //
  $.fn.portlet.defaults = {
    urlPrefix: '/SecureArea/Dashboard',
    nColumns: 3,
    viewId: -1
  };

})(jQuery);
