﻿// Extend the String object
String.prototype.left = function(i) {
    if (this == null) { return null; }
    else { return this.length < i ? this : this.substring(0, i); }
}
String.prototype.right = function(i) {
    if (this == null) { return null; }
    else { return this.length < i ? this : this.substring(this.length - i); }
}
String.prototype.startsWith = function(s) {
    if (this == null) { return false; }
    else { return this.length < s.length ? false : this.substring(0, s.length) == s; }
}
String.prototype.endsWith = function(s) {
    if (this == null) { return false; }
    else { return this.length < s.length ? false : this.substring(this.length - s.length) == s; }
}
String.prototype.format = function(s) {
    var d = Date.parse(this);
    if (d) { return d.toString(s); }
    else { return this; }
}
String.prototype.indexOfAny = function(a, iS) {
    var iF, iFL = -1;
    if (a) {
        if (i == null) { i = 0; }
        for (var i = 0; i < a.length; i++) {
            iF = this.indexOf(a[i], iS);
            if (iF > -1 && (iFL == -1 || iF < iFL)) { iFL = iF }
        }
    }
    return iFL;
}
// Extend the Date object
Date.months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
Date.monthsAbbr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
Date.days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday");
Date.daysAbbr = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

Date.prototype.toString = function(s) {
    if (!s) { s = "d mmm yyyy hh:nn:ss"; }
    var d = this;
    return s.replace(/(yyyy|mmmm|mmm|mm|m|dddd|ddd|dd|d|hh|h|nn|n|ss|s)/ig,
        function($1) {
            switch ($1.toLowerCase()) {
                case "yyyy": return d.getFullYear();
                case "mmmm": return Date.months[d.getMonth()];
                case "mmm": return Date.monthsAbbr[d.getMonth()];
                case "mm": return ("0" + (d.getMonth() + 1)).right(2);
                case "m": return d.getMonth() + 1;
                case "dddd": return Date.days[d.getDay()];
                case "ddd": return Date.daysAbbr[d.getDay()];
                case "dd": return ("0" + d.getDate()).right(2);
                case "d": return d.getDate();
                case "hh": return ("0" + d.getHours()).right(2);
                case "h": return d.getHours();
                case "nn": return ("0" + d.getMinutes()).right(2);
                case "n": return d.getMinutes();
                case "ss": return ("0" + d.getSeconds()).right(2);
                case "s": return d.getSeconds();
            }
        }
    );
}
Date.prototype.addSeconds = function(i) { this.setTime(Date.addSeconds(this, i).getTime()); }
Date.prototype.addMinutes = function(i) { this.setTime(Date.addMinutes(this, i).getTime()); }
Date.prototype.addHours = function(i) { this.setTime(Date.addHours(this, i).getTime()); }
Date.prototype.addDays = function(i) { this.setTime(Date.addDays(this, i).getTime()); }
Date.prototype.addMonths = function(i) { this.setTime(Date.addMonths(this, i).getTime()); }
Date.prototype.addYears = function(i) { this.setTime(Date.addYears(this, i).getTime()); }
Date.prototype.parse = function(value, defaultValue) {
    d = Date.parse(value, defaultValue);
    if (!isNaN(d)) { this.setTime(d.getTime()); }
}

Date.addSeconds = function(d, i) { return new Date(Date.parse(d).getTime() + i * 1000); }
Date.addMinutes = function(d, i) { return new Date(Date.parse(d).getTime() + i * 60000); }
Date.addHours = function(d, i) { return new Date(Date.parse(d).getTime() + i * 3600000); }
Date.addDays = function(d, i) { return new Date(Date.parse(d).getTime() + i * 86400000); }
Date.addMonths = function(d, i) { d = Date.parse(d); d.setMonth(d.getMonth() + i); return d; }
Date.addYears = function(d, i) { d = Date.parse(d); d.setYear(d.getFullYear() + i); return d; }
Date.parse = function(value, defaultValue) {
    var d;
    if (value) {
        switch (Antix.getTypeName(value).toLowerCase()) {
            case "date":
                d = new Date(value); break;
            case "string":
                var o = new RegExp(
		            "([0-9]{4}|[0-2]?[0-9]|3[0-1])([\\-\\/](1[0-2]|0?[0-9])|\\s("
		            + Date.monthsAbbr.join("|") + "|"
		            + Date.months.join("|")
		            + "))([\\s\\-\\/]([0-9]{2,4}))?", "i");
                var m = o.exec(value);
                if (m) {
                    d = new Date(0, 0, 0, 0, 0, 0);
                    if (m[1].length == 4) {
                        d.setYear(m[1]);
                        d.setDate(m[6]);
                    } else {
                        d.setYear(Date.year(m[6] ? m[6] : new Date().getFullYear()));
                        d.setDate(m[1]);
                    }
                    m[3] ? d.setMonth(m[3] - 1) : d.setMonth(Date.month(m[4]));
                }
                o = new RegExp("([0-1]{0,1}[0-9]|2[0-4]):([0-5]?[0-9]|6[0-9])(:([0-5]?[0-9]|6[0-9]))?");
                var m = o.exec(value);
                if (m) {
                    if (!d) { d = new Date(0, 0, 0, 0, 0, 0); }
                    d.setHours(m[1], m[2]);
                    if (m[4]) { d.setSeconds(m[4]); }
                } break;
        }
    }
    if (isNaN(d)) { if (defaultValue) { return Date.parse(defaultValue); } }
    else { return d; }
}
Date.prototype.getFirstDayOfMonth = function() { return new Date(this.getFullYear(), this.getMonth(), 1); }
Date.prototype.getDatePart = function() { return new Date(this.getFullYear(), this.getMonth(), this.getDate()); }
Date.month = function(s) {
    i = Date.monthsAbbr.indexOf(s); if (i > -1) { return i; }
    return Date.months.indexOf(s);
}
Date.year = function(s) {
    var iY = parseInt(s);
    return iY < 1000 ? (iY > 50 ? iY + 1900 : iY + 2000) : iY;
}

// Extend Arrays
Array.prototype.indexOf = function(item, index) {
    if (Antix.getTypeName(item) == "string") {
        for (index = index ? index : 0; index < this.length; index++) {
            if (this[index].toLowerCase() == item.toLowerCase()) { return index; }
        }
    } else {
        for (index = index ? index : 0; index < this.length; index++) {
            if (this[index] == item) { return index; }
        }
    }
    return -1;
}
Array.prototype.contains = function(item) {
    return this.indexOf(item) != -1;
}
Array.prototype.remove = function(item, replacement) {
    var index = this.indexOf(item);
    if (index != -1) { this.splice(index, 1, replacement); }
}

// Set up links between items in the gallery data
for (var index = 0; index < gallery.items.length; index++) {
    var item = gallery.items[index];
    item.previous = index == 0
                ? gallery.items[gallery.items.length - 1]
                : gallery.items[index - 1];
    item.next = index == gallery.items.length - 1
                ? gallery.items[0]
                : gallery.items[index + 1];
    var re = new RegExp("/" + item.path + "[^/]?");
    if (re.exec(window.location.pathname)) {
        gallery.currentItem = item;
    }
    item.sibling = function(relIndex) {
        if (relIndex == 0) { return this; }
        else if (relIndex > 0) { return this.next.sibling(relIndex - 1); }
        else { return this.previous.sibling(relIndex + 1); }
    }
}