var CONFIRM_TEXT = "Weet je zeker dat je {type} wilt verwijderen?";

//-- plugins

/**
 * Check to see if the array contains the element.
 */
Array.prototype.contains = function (element) { 
  for (var i = 0; i < this.length; i++) { 
    if (this[i] == element) { 
      return true; 
    } 
  }   
  return false; 
};

/**
 * Get all the unique items from an array.
 */
Array.prototype.unique = function() {
  var a = [];
  for(var i=0; i < this.length; i++) {
    if (a.contains(this[i]) == false) {
      a.push(this[i]);
    }
  }
  return a;
};

jQuery.fn.outerHTML = function(s) {
  return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}

//-- global funcitons

function cancel_edit () {
  jQuery('a.cancel').click();
}

function delete_part (type, element) {
  var confirm_text = CONFIRM_TEXT.replace("{type}", type);
  if (confirm(confirm_text)) {
    var id = element.get(0).id.match(/[\d\.]+/g).join("");
    
    jQuery.ajax({
      type: "DELETE",
      url: element.get(0).href,
      success: function (deleted) {          
        if (deleted) {
          jQuery("#part_" + id).remove();
        }
      }
    });
  }
}

function load_css_file (css_href) {
  var head = document.getElementsByTagName('head')[0]; 
  jQuery(document.createElement('link')).attr({type: 'text/css', href: css_href, rel: 'stylesheet', media: 'screen'}).appendTo(head);
}

function unload_css_file (css_href) {
  var css_link_elements = css_href.split("/");
  jQuery.each(jQuery('link'), function (i, link) {
    var link_elements = link.href.split("/");
    if (link_elements[link_elements.length-1] == css_link_elements[css_link_elements.length-1]) {
      jQuery(link).remove();
    }
  });
}