
function id() {
    var _id = arguments[0];
    if (typeof _id == 'string') _id = document.getElementById(_id);
    return _id;
}

/*

function homepage(index) {
    var main = id('top-rotator-main'), stor = id('top-rotator-store'), boxs = '';
    for (var i=0; i<highlights.length; i++) {
        if (i == index) continue; var obj = highlights[i];
        boxs += '<div id="top-product-' + i + '" class="top-product" style="background: url(' + obj.img2 + ') no-repeat center center;"><a onclick="homepage(' + i + '); return false;" href="#-product-a-' + i + '"><span>' + obj.title + '</span></a></div>';
    }
    stor.innerHTML = boxs; var obj = highlights[index];
    main.innerHTML = '<div id="top-product-' + index + '" class="top-product" style="background: url(' + obj.img1 + ') no-repeat center center"><a href="' + obj.url + '"><span id="bg">&nbsp;</span><span id="fg"><h1>' + obj.title + '</h1></span></a></div>';
}

*/

var Ticketlogic = {
    Version: '0.1.0.0',
    Registered: new Array(),
    Browser: { // thanks prototype.js
      IE:     !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1),
      Opera:  navigator.userAgent.indexOf('Opera') > -1,
      WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
      Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') === -1,
      MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
    },
    check: function() {
        var name = arguments[0];
        for (var i=0; i<this.Registered.length; i++) { var entry = this.Registered[i]; if (entry == name) return true; }
        return false;
    },
    register: function() {
        var ename = arguments[0], ext = arguments[1];
        function Extension(pname, powner) { this.name = pname; this.owner = powner; };
        if (!this.check(ename)) {
            ext.prototype = new Extension(ename, this); this.Registered.push(ename); this[ename] = ext;
            if (this[ename].initialise) this[ename].initialise();
            window.status = 'Loaded extension ' + ename;
        }
    }
}


Ticketlogic.register('Strings', {
    initialise: function() {
        if (!String.concat) { String.prototype.concat = function() { var s = this; for (var i = 0; i < arguments.length; i++) { var a = arguments[i]; s += a; } return s; } }
        if (!String.join) { String.prototype.join = function() { var s = this, j = arguments[0]; for (var i = 1; i < arguments.length; i++) { var a = arguments[i]; s += a; } return s; } }
        if (!String.trim) { String.prototype.trim = function() { var s = this; return s.replace(/^[\s\n\t\r ]+/, '').replace(/[\s\n\t\r ]+$/, ''); } }
        if (!String.trimLeft) { String.prototype.trimLeft = function() { var s = this; return s.replace(/^\s+/, ''); } }
        if (!String.trimRight) { String.prototype.trimRight = function() { var s = this; return s.replace(/\s+$/, ''); } }
    }
});

Ticketlogic.register('Session', {
    cookies: false, sid: null, user: null, timeout: 900, initialised: 0,
    initialise: function() { },
    create: function(id, timeout, cookies, user) {
        this.sid = id; this.timeout = timeout; this.cookies = cookies; this.user = user; this.initialised = new Date();
    },
    encodeURL: function() {
        var url = arguments[0]; if (this.cookies) return url;
        if (url.indexOf("?") > -1) { var path = url.split("?"); return path[0] + ";jsessionid=" + this.sid + "?" + path[1]; }
        return url + ";jsessionid=" + this.sid;
    }
});

Ticketlogic.register('Search', {
    initialise: function() { },
    qfocus: function() {
        var field = id(arguments[0]), dummy = arguments[1];
        if (field.value == dummy) {
            field.value = '';
            field.style.color = '#000';
        }
    },
    qblur: function() {
        var field = id(arguments[0]), dummy = arguments[1];
        if (field.value == '') { 
            field.value = dummy;
            field.style.color = '';
        }
    }
});

Ticketlogic.register('Timer', {
    eventMap: new Array(),
    initialise: function() { },
    create: function() {
        var name = arguments[0], func = arguments[1], interval = arguments[0];
        if (this.eventMap[name] != null) {
            return;
        }
        var key = window.setInterval(func, interval);
        this.eventMap[name] = key;
    },
    remove: function() {
        var name = arguments[0];
        if (this.eventMap[name] != null) {
            var key = this.eventMap[name];
            window.clearInterval(key);
        }
    }
});

Ticketlogic.register('Coordinates', {
    initialise: function() {},
    locate: function() {
        var o = id(arguments[0]), x = 0, y = 0;
        var Coordinate = function() { this.x = arguments[0]; this.y = arguments[1]; };
        while (o.tagName != 'BODY') { if (o) { x += (o.offsetLeft), y += (o.offsetTop); } o = o.offsetParent; }
        return new Coordinate(x, y);
    }
});

Ticketlogic.register('Date', {
    Version: '0.1.0.0',

    initialise: function() {
        Date.prototype.isToday = function() {
            var d1 = this, d2 = new Date();
            return ((d1.getYear() == d2.getYear()) && (d1.getMonth() == d2.getMonth()) && (d1.getDate() == d2.getDate()));
        }
        Date.prototype.before = function() {
            var d1 = this, d2 = arguments[0];
            return (d1.getTime() < d2.getTime());
        }
        Date.prototype.beforeByMonth = function() {
            var d1 = this, d2 = arguments[0];
            if (d1.before(d2)) {
                return true;
            }
            if ((d1.getMonth() == d2.getMonth()) && (d1.getYear() == d2.getYear())) {
                return true;
            }
            if ((d1.getMonth() == 11) && (d2.getMonth() == 0) && (d1.getYear() < d2.getYear())) {
                return true;
            }
            return false;
        }
        Date.prototype.after = function() {
            var d1 = this, d2 = arguments[0];
            return (d1.getTime() > d2.getTime());
        }
        Date.prototype.addYears = function() {
            var d = this, i = arguments[0];
            return new Date(d.getTime() + (i * 365 * 24 * 60 * 60 * 1000));
        };
        Date.prototype.addMonths = function() {
            var d = this, i = arguments[0];

            if (i < 0) {
                while (i < 0) {
                    var originalMonth = d.getMonth();
                    d = d.addDays( - 27);
                    while (d.getMonth() == originalMonth) {
                        d = d.addDays( - 1);
                    }
                    i++;
                }
            }
            else if (i > 0) {
                while (i > 0) {
                    var originalMonth = d.getMonth();
                    d = d.addDays(27);
                    while (d.getMonth() == originalMonth) {
                        d = d.addDays(1);
                    }
                    i--;
                }
            }
            return d;
        };
        Date.prototype.addDays = function() {
            var d = this, i = arguments[0];
            return new Date(d.getTime() + (i * 24 * 60 * 60 * 1000));
        };
        Date.prototype.addHours = function() {
            var d = this, i = arguments[0];
            return new Date(d.getTime() + (i * 60 * 60 * 1000));
        };
        Date.prototype.addMinutes = function() {
            var d = this, i = arguments[0];
            return new Date(d.getTime() + (i * 60 * 1000));
        };
        Date.prototype.getMonthName = function() {
            var d = this;
            var array = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
            return array[d.getMonth()];
        }
    },

    parse: function() {
        var d = arguments[0],
        dt = new Date();
        dt.setYear(d.replace(/(\d{4})\-\d{2}\-\d{2} \d{2}:\d{2}/, '$1'));
        dt.setMonth((d.replace(/\d{4}\-(\d{2})\-\d{2} \d{2}:\d{2}/, '$1') - 1));
        dt.setDate(d.replace(/\d{4}\-\d{2}\-(\d{2}) \d{2}:\d{2}/, '$1'));
        dt.setHours(d.replace(/\d{4}\-\d{2}\-\d{2} (\d{2}):\d{2}/, '$1'));
        dt.setMinutes(d.replace(/\d{4}\-\d{2}\-\d{2} \d{2}:(\d{2})/, '$1'));
        dt.setSeconds(0);

        return dt;
    },

    format: function() {
        var t = arguments[0];
        if (typeof t == 'string') t = parseInt(t);
        var d = new Date(t);
        var month = ((d.getMonth() < 9) ? '0': '') + (d.getMonth() + 1);
        var date = ((d.getDate() < 10) ? '0': '') + d.getDate();
        var hours = ((d.getHours() < 10) ? '0': '') + d.getHours();
        var mins = ((d.getMinutes() < 10) ? '0': '') + d.getMinutes();
        return (1900 + d.getYear()) + "-" + month + "-" + date + ' ' + hours + ':' + mins;
    }

});

Ticketlogic.register('Clock', {
    initialise: function() { },
    start: function() {
        this.show();
        new PeriodicalExecuter(function(pe) {
            Ticketlogic.Clock.show();
        }, 60);
    },
    show: function() {
        var span_utc = id('user-prefs-utc-clock');
        var span_loc = id('user-prefs-loc-clock');
        
        if (!span_utc || !span_loc) return; 
        
        var d = new Date();
        span_utc.innerHTML = 'UTC ' + Ticketlogic.Date.format(d.getTime() + (d.getTimezoneOffset() * 60 * 1000));
        span_utc.style.color = '#fff';
        var arr = d.toUTCString().split(' ');
        span_loc.innerHTML = arr[arr.length-1] + ' ' + Ticketlogic.Date.format(d.getTime());
        span_loc.style.color = '#cfc';
        setTimeout("id('user-prefs-utc-clock').style.color = ''", 5000);
        setTimeout("id('user-prefs-loc-clock').style.color = ''", 5000);        
    }
});

Ticketlogic.register('Indicators', {
    initialise: function() { },
    start: function() {
        var url = arguments[0];
        new Ajax.PeriodicalUpdater('indicators', url, {
          method: 'get', frequency: 30, decay: 5,
          onFailure: function(pu) {
              var href = window.location.href;
              // alert(href);
              if (href.indexOf('&timed_out=') > -1) {
                  href = href.replace(/\&timed_out\=\d+/, '&timed_out=' + (new Date()).getTime());
              }
              else if (href.indexOf('?') > -1) {
                  href += "&timed_out=" + (new Date()).getTime();
              }
              else {
                  href += "?timed_out=" + (new Date()).getTime();
              }
              window.location = href;
              pu.stop();
          }
        });
    }
});

