﻿
////////////////////////////////////////////////////////////////////////////////////////////////////
// defined
////////////////////////////////////////////////////////////////////////////////////////////////////
function defined(iObject)
{ return typeof iObject != "undefined"; }


////////////////////////////////////////////////////////////////////////////////////////////////////
// Implements
////////////////////////////////////////////////////////////////////////////////////////////////////
function Implements(iInstance, iSuperClass)
{
    if (!defined(iInstance.Implements))
        iInstance.Implements = {};

    if (defined(iInstance.Implements[iSuperClass]))
        return;

    iInstance.Implements[iSuperClass] = iSuperClass;

    if (arguments.length > 2)
    {
        var args = Array.prototype.slice.apply(arguments, [2]);
        iSuperClass.apply(iInstance, args);
    }
    else
        iSuperClass.call(iInstance);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// String
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to string.js
String.prototype.encodeHTML = function()
{
    var encoded = this;
    encoded = encoded.replace(/&/g, "&amp;");
    encoded = encoded.replace(/</g, "&lt;");
    encoded = encoded.replace(/>/g, "&gt;");
    return encoded;
}

String.prototype.endsWith = function(iString)
{ return (this.substr(-iString.length, iString.length) == iString); }

String.prototype.format = function()
{
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}

String.prototype.padLeft = function(iChar, iLength)
{
    var ret = this;
    for (var i = 0; ret.length < iLength; i++)
        ret = iChar + ret;
    return ret;
}

String.prototype.padRight = function(iChar, iLength)
{
    var ret = this;
    for (var i = 0; ret.length < iLength; i++)
        ret = iChar + ret;
    return ret;
}

String.prototype.startsWith = function(iString)
{ return (this.substr(0, iString.length) == iString); }

String.prototype.trim = function()
{ return this.replace(/^\s+|\s+$/, ''); }

String.format = function()
{
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return args[0].replace(pattern, function(capture) { return args[parseInt(capture.match(/\d+/)) + 1]; });
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Array
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to array.js
Array.prototype.remove = function(iIndex, iCount)
{
    if (!defined(iCount))
        iCount = 1;
    this.splice(iIndex, iCount);
}

Array.prototype.insert = function(iIndex, iValue)
{ this.splice(iIndex, 0, iValue); }

////////////////////////////////////////////////////////////////////////////////////////////////////
