function clsPattern(prmId, prmName) {
    this.id = prmId;
    this.name = prmName;    

    this.showDetails = function() {
        // wyswietla szczegoly wzorca (schematu bazowego dla miasta)
        objLC = new clsLinesCollection();
        objMC = new clsMarkersCollection();
        objMap.drawPattern(this.id);
    };
}

function clsPatternsCollection() {
    this.selectedPattern = -1;
    this.sourceType = "";
    this.prevUrl = "";
    this.array = new Object();

    this.showPatternsList = function() {
        // wyswietla liste wzorcow
        this.array = new Object();
        var xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "src/patterns.php";
        url += "?f=1";
        url += url + "&sid=" + Math.random();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                //document.getElementById("lst_lines").innerHTML = xmlhttp.responseText;

                var opts = "";
                var xmlDoc = xmlhttp.responseXML;
                var lines = xmlDoc.getElementsByTagName("city");

                for (var i = 0; i < lines.length; i++) {
                    var id = lines[i].getAttribute("id");
                    var name = lines[i].getAttribute("name");
                    //opts += '<div id="city_' + id + '" onmouseover="mouseover(this)" onmouseout="mouseout(this)">' + name + '</div>';
                    opts = '<option id="city_' + id + '" value="' + id + '">' + name + '</option>';
                    document.getElementById("optNewSchemaSource").innerHTML += opts;
                    var objPattern = new clsPattern(id, name);
                    objPC.array[id] = objPattern;
                }

                //document.getElementById("optNewSchemaSource").innerHTML = opts;
                //document.getElementById("optNewSchemaSource").value = "";
                document.getElementById("optNewSchemaSource").selectedIndex = -1;
                //document.getElementById("echo").innerHTML = document.getElementById("optNewSchemaSource").selectedIndex;
            }
        };
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    };
    this.selectionChanged = function() {
        // zdarzenie: wybrano wzorzec
        if (document.getElementById("optNewSchemaSource").selectedIndex == -1)
            return;
        this.selectedPattern = this.array[document.getElementById("optNewSchemaSource").options[document.getElementById("optNewSchemaSource").selectedIndex].value];
        document.getElementById("sourceTypeDB").checked = "checked";
        this.selectedPattern.showDetails();
    };

    this.checkURL = function(url, sourceTypeChanged) {
        if (this.prevUrl == url && (sourceTypeChanged == null || !sourceTypeChanged))
            return
        if (url.substr(url.length-4) == ".xml") {
            this.prevUrl = url;
            //document.getElementById("echo").innerHTML = url;
            document.getElementById("sourceTypeURL").checked = "checked";
            objLC = new clsLinesCollection();
            objMC = new clsMarkersCollection();
            objMap.drawPattern(url);
        }
    }

    this.sourceTypeChanged = function(sourceType) {
        if (sourceType == this.sourceType)
            return;
        this.sourceType = sourceType;
        if (sourceType == "db") {
            this.selectionChanged();
        }
        else {
            this.checkURL(document.getElementById("txtNewSchemaSource").value, true)
        }
    }
}
