/**
 * Extension for wireframe implementation for default (web) interface
 */

/**
 * Return indicator URL
 * 
 * type value can be one of the:
 * 
 * - big
 * - small
 * - ok
 * - error
 * - pending
 * - warning
 * 
 * Leave type empty for normal, 16x16 animated indicator
 * 
 * @param String type
 * @param String i (client_interface)
 */
App.Wireframe.Utils['indicatorUrl'] = function(type, i) {
  switch(type) {
    case 'big':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-loading-big.gif', 'environment', i);
    case 'small':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-loading-small.gif', 'environment', i);
    case 'ok':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-ok.png', 'environment', i);
    case 'error':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-error.png', 'environment', i);
    case 'pending':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-pending.png', 'environment', i);
    case 'warning':
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-warning.png', 'environment', i);
    default:
      return App.Wireframe.Utils.imageUrl('layout/bits/indicator-loading-normal.gif', 'environment', i);
  }
};

/**
 * Print ago based on provided datetime
 * 
 * @param mixed datetime
 * @return string
 */
App.Wireframe.Utils['ago'] = function(datetime) {
  return datetime['formatted'];
};

/**
 * Print due data based on provided datetime
 * 
 * @param mixed datetime
 * @return string
 */
App.Wireframe.Utils['due'] = function(datetime) {
  return datetime['formatted_gmt'];
};

/**
 * Print due data based on provided datetime
 * 
 * @param string filename
 * @param integer len
 * @return string
 */
App.Wireframe.Utils['shortFilename'] = function(filename, len) {
  if(typeof(len) != 'number' || len < 1) {
    len = 15;
  } // if
  
  if(filename.length > len) {
    return filename.substr(0, len);
  } else {
    return filename;
  } // if
};

/**
 * Return user link based on user object
 *
 * @param Object user
 * @param Boolean use_short_name
 * @return jQuery
 */
App.Wireframe.Utils['userLink'] = function(user, use_short_name) {
  var display_name = use_short_name ? user['short_display_name'] : user['display_name'];
  var link = $('<a class="user_link"></a>').attr('href', user['permalink']).text(display_name);
  
  if(user['id'] == 0) {
    link.addClass('anonymous_user_link');
  } // if
  
  return link;
};

/**
 * Return company link based on company object
 * 
 * @param Object company
 * @return jQuery
 */
App.Wireframe.Utils['companyLink'] = function(company) {
  return $('<a href="' + company.permalink + '">' + company.name.clean() + '</a>');
};

/**
 * Returns the role name
 * 
 * @param Object role
 * @returns jQuery
 */
App.Wireframe.Utils['roleName'] = function (role) {
  if (typeof(role) == 'object' && role['name'] && role['id']) {
    return $('<span>'+role['name'].clean()+'</span>');
  } else {
    return $('<span style="color: red; font-weight: bold">' + App.lang('Error: Unknown Role') + '</span>');
  } // if
};

/**
 * Renders visibility indicator
 *
 * @param visibility
 */
App.Wireframe.Utils['renderVisibilityIndicator'] = function (visibility) {
  if (visibility == 1) {
    return '';
  } // if

  if (visibility == 0) {
    return '<img src="' + App.Wireframe.Utils.imageUrl('icons/12x12/private-object.png', 'environment') + '" title="' + App.lang('Private') + '" class="visibility" />';
  } // if

  if (visibility == 2) {
    return '<img src="' + App.Wireframe.Utils.imageUrl('icons/16x16/sharing-on.png', 'system') + '" title="' + App.lang('Public') + '" class="visibility" />'
  } // if

  return '';
};

/**
 * Modal dialog module
 */
App.Wireframe.ModalDialog = function() {
  
  /**
   * Array of opened dialogs
   *
   * @var Object
   */
  var dialog_objects = {};
  
  // Let's return public interface object
  return {
    
    /**
     * Show modal dialog
     *
     * @param String name
     * @param String title
     * @param mixed body
     * @param mixed settings
     */
    'show' : function(name, title, body, settings) {
      var settings = jQuery.extend({
        'close' : null, 
        'width' : null, 
        'height' : null, 
        'buttons' : null
      }, settings);
      
      // dialog options
      var options = {
        'modal' : true,
        'draggable' : false,
        'resizable' : true,
        'title' : title,
        'id' : name,
        'position' : 'top',
        'bgiframe' : true,
        'close' : function (type, data) {
          if(settings.close) {
            settings.close();
          } // if
          dialog_objects[name].dialog('destroy').remove();
        },
        'resizeStart' : function (type,data) {
          
        }
      };

      // width and height settings
      options.width = settings.width ? settings.width : 410;
      options.height = settings.height ? settings.height : 'auto';
      
      // additional buttons
      options.buttons = {};
      if (settings.buttons) {
        for (var x = 0; x < settings.buttons.length; x++) {
          if (settings.buttons[x].callback) {
            var callback_function = settings.buttons[x].callback;
            options.buttons[settings.buttons[x].label] = function () {
              callback_function();
              dialog_objects[name].dialog('close');
            }; // function
          } else {
            options.buttons[settings.buttons[x].label] = function () {
              dialog_objects[name].dialog('close');
            }; // function
          } // if
        } // if
      } // if

      options.maxWidth = options.width;
      options.minWidth = options.width;
      
      dialog_objects[name] = $(body).dialog(options);
     
      var counter = 0;
      dialog_objects[name].parent().parent().find('.ui-dialog-buttonpane button').each(function () {
        var button = $(this);
        button.removeClass('ui-state-default').removeClass('ui-corner-all');

        var label = button.html();
        button.html('<span><span>' + label + '</span></span>');
        if (counter != 0) {
          button.addClass('alternative');
        } // if
        counter++;
      });
    },
    
    /**
     * Close the dialog
     *
     * @param String name
     */
    'close' : function(name) {
      if(name == '') {
        for(var remove in dialog_objects) {
          // Just walt through the object, so we have last key in remove
        } // for
      } else {
        var remove = name;
      } // if
      
      if(remove) {
        dialog_objects[remove].dialog('destroy').remove();
        
        // Lets remove dialog from the list...
        var tmp_object = {};
        
        for(var dialog_object in dialog_objects) {
          if(dialog_object != remove) {
            tmp_object[dialog_object] = dialog_objects[dialog_object];
          } // if
        } // for
        
        dialog_objects = tmp_object;
      } // if
    },
    
    /**
     * sets width of dialog
     */
    'setWidth' : function (width_px) {     
      var dom_dialog = $('.ui-dialog');
      var position = dom_dialog.position();
      var new_left_offset = position.left - ((width_px - dom_dialog.width())/2);
      dom_dialog.css('width' , width_px+'px').css('left', new_left_offset+'px');
    },
    
    /**
     * Sets dialog title
     */
    'setTitle' : function (title) {
      var dom_dialog = $('.ui-dialog .ui-dialog-titlebar span.ui-dialog-title').html(title);
    },
    
    /**
     * Checks if dialog is open
     */
    'isOpen' : function () {
      return $('.ui-dialog').length > 0;
    }
  };
  
}();

// Handle window resize event
$(window).resize(function (event) {
  if (event.target == window) {
    App.Wireframe.Events.trigger('window_resize', arguments);  
  } // if
});