(function (window, undefined) {
    var tt = {
        form : null,
        doc : window.document
    },
    doc = window.document,
    win = window,
    cache = {
        touroperator : [],
        regions : [],
        region : [],
        destination : [],
        termin : {
            min : null,
            max : null
        },
        init : function (arr) {
            this.touroperator = arr[0];
            this.regions = arr[1];
            this.termin.min = arr[2];
            this.termin.max = arr[3];
            return this;
        },
        createRegionAndDirection : function (exp, namePos, flugPos, zielPos) {
            var i = 0, len = 0, tmp = null;
            if (this.regions.length > 0) {
                for (i = 0, len = this.regions.length; i < len; i += 1) {
                    tmp = this.regions[i].split(exp);
                    if (tmp[flugPos] === tmp[zielPos]) {
                        this.region.push(tmp[namePos] + '|' + tmp[flugPos] + '|' + tmp[zielPos]);
                    } else {
                        this.destination.push(tmp[namePos] + '|' + tmp[flugPos] + '|' + tmp[zielPos]);
                    }
                }
            }
            return this;
        },
        getTouroperator : function () {
            return this.touroperator;
        },
        getRegions : function () {
            return this.regions;
        },
        getTerminMin : function () {
            return this.termin.min;
        },
        getTerminMax : function () {
            return this.termin.max;
        },
        getRegion : function (exp, namePos, flugPos, zielPos) {
            if (this.region.length === 0) {
                this.createRegionAndDirection(exp, namePos, flugPos, zielPos);
            }
            return this.region;
        },
        getDestination : function (exp, namePos, flugPos, zielPos) {
            if (this.region.length === 0) {
                this.createRegionAndDirection(exp, namePos, flugPos, zielPos);
            }
            return this.destination;
        }

    };

    tt.init = function (id, arr) {
        this.form = doc.getElementById(id) || null;
        if (arr !== undefined) {
            cache.init(arr);
        }
        return this;
    };

    tt.error = function (msg) {
        throw msg;
    };

    tt.log = function (msg) {
        if (typeof win.console !== 'undefined') {
            win.console.log(msg);
        } else {
            alert(msg);
        }
        return this;
    };

    tt.getTouroperator = function () {
        return cache.getTouroperator();
    };

    tt.getAllRegions = function () {
        return cache.getRegions();
    };

    tt.getTerminMin = function () {
        return cache.getTerminMin();
    };

    tt.getTerminMax = function () {
        return cache.getTerminMax();
    };

    tt.getDestination = function (exp, namePos, flugPos, zielPos) {
        return cache.getDestination(exp, namePos, flugPos, zielPos);
    };

    tt.getRegion = function (exp, namePos, flugPos, zielPos) {
        return cache.getRegion(exp, namePos, flugPos, zielPos);
    };

    tt.addEvent = function (obj, ev, callback) {
        try {
            if (obj.addEventListener) {
                obj.addEventListener(ev, callback, false);
            } else {
                obj.attachEvent('on' + ev, callback);
            }
        } catch(e) {
            this.error('tt.addEvent -> ' + e);
        }
        return this;
    };

    tt.multiEvent = function (from, size, callback) {
        for (var i = from; i < size; i += 1) {
            callback(i);
        }
        return this;
    };

    tt.loadCacheFile = function (kid, port, callback) {
        tt.load('http://ttibe.traveltainment.pl/cache/UDF/' + kid + '/' + port + '/udf.js');
        tt.load('http://ttibe.traveltainment.pl/booking_pl/cache/' + kid + '/' + port + '/cachefiles_' + port + '_js.php?getCacheFiles=1',
            function ()  {
                tt.load('http://ttibe.traveltainment.pl/booking_pl/cache/' + kid + '/' + port + '/' + win['cacheFiles_' + port][0],
                    function () {
                        tt.load('http://ttibe.traveltainment.pl/booking_pl/cache/' + kid + '/' + port + '/' + win['cacheFiles_' + port][1],
                            function () {
                                cache.init([win['arrMarke_' + port], win['TTREGS_' + port], win['termin_min_' + port], win['termin_max_' + port]]);
                                if (callback !== undefined) {
                                    callback();
                                }
                            }
                        );
                    }
                );
            }
        );
        return this;
    };

    tt.load = function (url, callback) {
        var script = doc.createElement('script');
        script.type = 'text/javascript';
        if (script.readyState) {
            script.onreadystatechange = function () {
                if (script.readyState === 'loaded' || script.readyState === 'complete') {
                    script.onreadystatechange = null;
                    if (callback !== undefined) {
                        callback();
                    }
                }
            };
        } else {
            script.onload = function () {
                if (callback !== undefined) {
                    callback();
                }
            };
        }
        script.src = url;
        return doc.getElementsByTagName("head")[0].appendChild(script);
    };
    
    tt.ajaxObj = function () {
        try {
            return new XMLHttpRequest();
        } catch (e) {
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("Twoja przeglądarka nie obsługuje XMLHttpRequest, zaktualizuj swoją przeglądarkę.");
                    //this.error(e);
                }
            }
        }
        return null;
    };
    return (win.tt = tt);
})(window);
