// Woobius-specific functions
var woobius = {

  _sendToFlex: function(object) {
    if (parent.FABridge!==undefined){
      var flexApp = parent.FABridge.flash.root();
      flexApp.receivedMessage(object);
    }
  },

  _sendJsonToFlex: function(object) {
    woobius._sendToFlex($.toJSON(object));
  },

  sendNotification: function(notificationName, value) {
    if (value === null || value === undefined) {
        woobius._sendJsonToFlex({
          notificationName: notificationName
        });
    } else {
        woobius._sendJsonToFlex({
          notificationName: notificationName,
          value: value
        });
    }
  },

  setCurrentDiscussionId: function(messageId) {
    woobius.sendNotification("setCurrentDiscussionId", messageId);
  },

  createTag: function(projectId, tagName, callBack) {
    $.ajax({
      "type" : "POST",
      "url" : "/projects/" + projectId + "/tags",
      "data" : {
        "name" : tagName
      },
      "success" : function(data) {
        callBack(data);
      },
      "dataType" : "json"
    });
  },

  deleteTag: function(tagId, callBack) {
    $.ajax({
      "type"    :   "POST",
      "url"      :   "/tags/" + tagId,
      "data"    : {
        "_method" : "DELETE"
      },
      "success"  :   function(data) {
        callBack(data);
      },
      "dataType"  : "json"
    });
  },
  
  renameTag: function(tagId, name, callBack){
    $.ajax({
      "type"      :   "POST",
      "url"        :   "/tags/" + tagId,
      "data"       : {
        "_method" :   "PUT",
        "name"    :   name
      },
      "success"   : function(data) {
        callBack(data);
      },
      "dataType"   : "json"
    });
  }, 
  
  attachTag: function(tagId, discussionIds, callBack) {
    $.ajax({
      "type"      :   "POST",
      "url"        :   "/tag/" + tagId+ "/attach",
      "data"       : {
        "discussions" :   discussionIds  
      },
      "success"   : function(data) {
        callBack(data);
      },
      "dataType"   : "json"
    });
  },

  detachTag: function(tagId, discussionIds, callBack) {
    $.ajax({
      "type"      :   "POST",
      "url"        :   "/tag/" + tagId+ "/detach",
      "data"       : {
        "discussions" :   discussionIds  
      },
      "success"   : function(data) {
        callBack(data);
      },
      "dataType"   : "json"
    });
  },
  
  sendFeedback: function(content, callBack) {
    $.ajax({
      "type"      :   "POST",
      "url"        :   "/json_api/current_user/send_feedback",
      "data"       : {
        "feedback_body" : content  
      },
      "success"   : function(data) {
        callBack(data);
      },
      "dataType"   : "json"
    });
  }  
  
}

Array.prototype.index = function(val) {
  for(var i = 0, l = this.length; i < l; i++) {
    if(this[i] == val) return i;
  }
  return null;
}

Array.prototype.include = function(val) {
  return this.index(val) !== null;
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
