﻿$(document).ready(function() {
  // Extends
  jQuery.extend({
    postJSON: function(url, data, callback) {
      return jQuery.post(url, data, callback, "json");
    }
  });

  jQuery.fn.scrollToEnd = function() {
    return jQuery(this).scrollTop(9000);
  };

  jQuery.fn.appendXml = function(xml) {
    return jQuery(this).each(function() {
      var xd;

      if (window.ActiveXObject) {
        xd = new ActiveXObject("Microsoft.XMLDOM");
        xd.async = false;
        xd.loadXML(xml);
      } else if (typeof DOMParser != "undefined") {
        xd = new DOMParser().parseFromString(xml, "text/xml");
      } else {
        xd = null;
      };

      if (xd != null) {
        var node = xd.firstChild.cloneNode(true);
        this.appendChild(node);
      };
    });
  };
});

// Formattings
function formatJSONToString(json) {
  var result = new String("{");

  $.each(json, function(parameter, value) {
    result += "\"" + parameter + "\": \"" + value + "\", ";
  });

  if (result.length > 1) {
    result = result.substring(0, result.length - 2);
  };

  result += "}";

  return result;
};

function formatISODate(isoDate) {
  return isoDate.substring(8, 10) + "/" + isoDate.substring(5, 7) + "/" + isoDate.substring(0, 4);
};

function formatBasic(text) {
  if (text != "") {
    text = replaceChars(text, "<", "");
    text = replaceChars(text, "/", "");
  };

  return text;
};

function formatExpression(text, capitalized, separator, add) {
  var words;
  var word;
  var i;
  var result = "";

  text = $.trim(text);
  words = text.split(separator);

  for (i = 0; i < words.length; i++) {
    word = words[i];
    word = $.trim(word);

    if (word != "") {
      if (capitalized) {
        result += capitalize(word);
      } else {
        result += word;
      };

      result += add + " ";
    };
  };

  return result;
};

function formatText(text) {
  var result = formatExpression(text, true, " ", "");

  if (result != "") {
    result = replaceChars(result, " Da ", " da ");
    result = replaceChars(result, " De ", " de ");
    result = replaceChars(result, " Do ", " do ");
    result = replaceChars(result, " Das ", " das ");
    result = replaceChars(result, " Dos ", " dos ");
    result = replaceChars(result, " Na ", " na ");
    result = replaceChars(result, " No ", " no ");
    result = replaceChars(result, " Nas ", " nas ");
    result = replaceChars(result, " Nos ", " nos ");
    result = replaceChars(result, " Ao ", " ao ");
    result = replaceChars(result, " E ", " e ");
    result = replaceChars(result, " É ", " é ");
    result = replaceChars(result, " O ", " o ");
    result = replaceChars(result, " Para ", " para ");
    result = replaceChars(result, " Em ", " em ");
    result = replaceChars(result, " Com ", " com ");
  };

  return result;
};

function formatInline(value, type) {
  if (type == "domain" || type == "email") {
    value = value.toLowerCase();
  };

  if (type == "title" || type == "name") {
    value = formatBasic(value);
    value = formatText(value);
  };

  if (type == "spaces") {
    value = formatExpression(value, false, " ", "");
  };

  value = $.trim(value);

  return value;
};

// Visibilities
function resizeHeight(id, margin) {
  var windowScrollTop = $(window).scrollTop();
  var windowHeight = $(window).height();
  var offset = $(id).offset();
  var offsetHeight = offset.top;

  $(id).height((windowHeight + windowScrollTop) - offsetHeight - margin);

  return false;
};

function resizeWidth(id, margin) {
  var windowWidth = $(window).width();
  var offset = $(id).offset();
  var offsetWidth = offset.left;

  $(id).width(windowWidth - offsetWidth - margin);

  return false;
};

function centralizePosition(id) {
  var windowScrollTop = $(window).scrollTop();
  var windowHeight = $(window).height();
  var windowWidth = $(window).width();
  var elementHeight = $(id).height();
  var elementWidth = $(id).width();
  var elementLeft = (windowWidth - elementWidth) / 2;
  var elementTop = (windowHeight - elementHeight) / 2;

  $(id).css("left", elementLeft).css("top", elementTop + windowScrollTop);
};

function scrollContainer(id) {
  $(id).scrollTop($(id).height());
};

// Templates
function templateOption(value, text) {
  return "<option value=\"" + value + "\">" + text + "</option>";
};

// Utilities
function truncate(text, limit) {
  if (text.length > limit) {
    text = text.substring(0, limit) + "...";
  };

  return text;
};

function capitalize(text) {
  return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
};

function breakLine(text) {
  return text.replace(/\n/g, "<br />");
};

function replaceChars(entry, out, add) {
  var temp = entry;

  while (temp.indexOf(out) > -1) {
    pos = temp.indexOf(out);
    temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
  };

  return temp;
};

function timeStamp() {
  var time = new Date();
  var stamp = new String();

  stamp = time.getFullYear() + "" + time.getMonth() + "" + time.getDay() + "" + time.getHours() + "" + time.getMinutes() + "" + time.getSeconds() + time.getMilliseconds();
  return stamp;
};