function clsSchema(prmId, prmName) {
    this.id = prmId;
    this.name = prmName;
    this.isSelected = false;
    this.isLocked = false;

    var row = document.getElementById("tbl_schemas").insertRow(-1);
    row.id = "schema_" + prmId;
    row.onmouseover = function() { objSC.array[prmId].mouseover() };
    row.onmouseout = function() { objSC.array[prmId].mouseout() };
    //row.setAttribute('onclick', 'alert("cos")');
    row.onclick = function() { objSC.array[prmId].select(); objSC.array[prmId].showDetails(); };
    row.setAttribute("class", "schema");
    var cell_0 = row.insertCell(0);
    cell_0.setAttribute("class", "schema_name");
    cell_0.innerHTML = prmName;
    var cell_1 = row.insertCell(1);
    cell_1.id = "schema_state_" + prmId;
    cell_1.innerHTML = "&nbsp;";
    
    //document.getElementById("tbl_schemas").innerHTML +=    
    //    '<tr id="schema_' + prmId + '" onmouseover="objSC.array[' + prmId + '].mouseover()" onmouseout="objSC.array[' + prmId + '].mouseout()" onclick="objSC.array[' + prmId + '].select(); objSC.array[' + prmId + '].showDetails();" class=\'schema\'>' +
    //             '<td class="schema_name">' + prmName + '</td>' +
    //             '<td id="schema_state_' + prmId + '">&nbsp;</td></tr>';
    
    //'\n<div id="schema_' + id + '" onmouseover="objSC.array[' + id + '].mouseover()" onmouseout="objSC.array[' + id + '].mouseout()" onclick="objSC.array[' + id + '].select(); objSC.array[' + id + '].showDetails();" class=\'schema\'>' + name + '</div>';
    objSC.array[prmId] = this;

    this.showDetails = function() {
        // wyswietla szczegoly sieci
	if (objSC.schemaIsEdited)
            return;
        //document.getElementById("tbl_lines").innerHTML = "";
        document.getElementById("lst_lines").innerHTML = '<table id="tbl_lines" cellpadding="0" cellspacing="0"></table>';
        objLC = new clsLinesCollection();
        objMC = new clsMarkersCollection();
        objLC.isLoaded = true;
        objMap.drawSchema(this.id);        
        document.getElementById("optSortType").selectedIndex = 0;
        document.getElementById("optCardDir").selectedIndex = 0;
    };
    this.editionStarted = function() {
        // rozpoczeto edycje sieci        
        //selectedSchema.style.background = "#FFC36B"; //"#FFCC14";
        //document.getElementById("i" + selectedSchema.id).innerHTML = ' <img src="img/pencil.png" style="margin-top:0px">';
        //document.getElementById(selectedSchema.id).style = 'background-color: "#88CDFF"; background-image:url(img/pencil.png); background-position:right center; background-repeat:no-repeat;';

        //document.getElementById('schema_state_' + this.id).innerHTML = '<img src="img/pencil.png" />';
        document.getElementById('schema_state_' + this.id).innerHTML = '<img src="img/point.gif" style="background: transparent url(img/icons.png) no-repeat scroll 0px 0px; width: 13px; height: 13px;" />';
    };    
    this.mouseover = function() {
    
        // zdarzenie: kursor nad elementem
        if (objSC.schemaIsEdited)
            return;        
        if (!this.isSelected) {            
            document.getElementById('schema_'+this.id).style.background = '#E8F3FF';
            //this.div.style.background = '#E8F3FF';
        }
        
    };
    this.mouseout = function() {
        // zdarzenie: kursor poza elementem
        if (!this.isSelected) {
            document.getElementById('schema_'+this.id).style.background = 'Transparent';
        }
    };
    this.select = function() {
        // wybrano siec        
        if (objSC.schemaIsEdited)
            return;
        for (var key in objSC.array) {
            document.getElementById('schema_' + objSC.array[key].id).style.background = 'Transparent';
            objSC.array[key].isSelected = false;
        }
        objSC.selectedSchema = this;
        this.isSelected = true;
        document.getElementById('schema_' + this.id).style.background = '#88CDFF';
        //document.getElementById('schema_state_' + this.id).innerHTML = '&nbsp;';
        document.getElementById('input_schema_id').value = this.id;
        //document.getElementById('echo').innerHTML = document.getElementById('input_schema_id').value;
    };    
}

function clsSchemasCollection() {
    this.array = new Object(); // kolekcja sieci
    this.selectedSchema = -1; // wybrana siec
    this.schemaIsEdited = false; // czy ktoras z sieci jest edytowana
    //var licznik = 0;

    this.showSchemasList = function() {
        // tworzy kolekcje i wyswietla sieci
        this.array = new Object();
        document.getElementById("echo").innerHTML = "";
        var xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "/src/schemas.php";
        url += "?f=1";
        xmlhttp.open("GET", url, true);
        var requestTimer = setTimeout(function() {
            //document.getElementById("echo").innerHTML = "błąd";
            xmlhttp.abort();
            document.getElementById("schema_progress").innerHTML = "i się przywiesiło...";
            document.getElementById("schema_progress").style.display = "block";            
            //xmlhttp.open("GET", url, true);
            //xmlhttp.send(null);
            // Handle timeout situation, e.g. Retry or inform user.
        }, 10000);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState != 4) {
                document.getElementById("schema_progress").innerHTML = '<img src="img/loader.gif" style="margin-right:4px;">Wczytywanie sieci...';
                document.getElementById("schema_progress").style.display = "block";                        
                return;
            }
            clearTimeout(requestTimer);
            //if (xhReq.status != 200)  {
                // Handle error, e.g. Display error message on page
                //return;
            //}
            document.getElementById("schema_progress").style.display = "none";
            var xmlDoc = xmlhttp.responseXML;
            var schemas = xmlDoc.getElementsByTagName("schema");
            for (var i = 0; i < schemas.length; i++) {
                var id = schemas[i].getAttribute("id");
                var name = schemas[i].getAttribute("name");

                var objSchema = new clsSchema(id, name);

            }
            if (document.getElementById('input_schema_id').value != "-1") {

                objSC.array[document.getElementById('input_schema_id').value].select();
                objSC.array[document.getElementById('input_schema_id').value].showDetails();
            }
            objSC.refreshSchemasStates();
        };
        xmlhttp.send(null);
    };
    this.refreshSchemasStates = function() {
        if (objLC != null && objLC.isLoaded) {
            //document.getElementById("echo").innerHTML += ".";
            setTimeout(function() { objSC.refreshSchemasStates() }, 10000);
            return;
        }
        //else
        //    document.getElementById("echo").innerHTML = "";
        // odswieza statusy schematow
        //document.getElementById('echo').innerHTML = licznik++;
        var xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "src/schemas.php";
        url += "?f=6";
        url += "&sid=" + Math.random();
        xmlhttp.open("GET", url, true);
        var requestTimer = setTimeout(function() {
           xmlhttp.abort();
           setTimeout(function() { objSC.refreshSchemasStates() }, 2500);
           //xmlhttp.open("GET", url, true);
           //xmlhttp.send(null);
           // Handle timeout situation, e.g. Retry or inform user.
        }, 1000);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                clearTimeout(requestTimer);
                var xmlDoc = xmlhttp.responseXML;
                var schemas = xmlDoc.getElementsByTagName("schema");
                for (var key in objSC.array) {
                    if (objSC.schemaIsEdited && key == objSC.selectedSchema.id)
                        continue;
                    document.getElementById('schema_state_' + key).innerHTML = '&nbsp;';
                    objSC.array[key].isLocked = false;
                }

                for (var i = 0; i < schemas.length; i++) {
                    var id = schemas[i].getAttribute("id");
                    if (objSC.schemaIsEdited && id == objSC.selectedSchema.id)
                        continue;
                    document.getElementById('schema_state_' + id).innerHTML = '<img src="img/point.gif" style="background: transparent url(img/icons.png) no-repeat scroll -14px 0px; width: 13px; height: 13px;" />';
                    objSC.array[id].isLocked = true;
                }
                setTimeout(function() { objSC.refreshSchemasStates() }, 5000);
            }
        };        
        xmlhttp.send(null);
    };
    this.editSchema = function() {
        // rozpoczyna edycje sieci
        //document.getElementById("echo").innerHTML = selectedSchema.id.substr(7);
        if (this.selectedSchema == -1) {
            alert("Nie wybrano żadnej sieci.");
            return;
        }
        if (this.selectedSchema.isLocked) {
            alert("Sieć jest edytowana przez innego użytkownika.");
            return;
        }
        var xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "src/schemas.php";
        url += "?f=7";
        url += "&id=" + this.selectedSchema.id;
        url += "&sid=" + Math.random();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                objSC.selectedSchema.editionStarted();
                objSC.schemaIsEdited = true;

                document.getElementById("schemas_buttons").innerHTML =
                '<input id="btnGotowe" type="button" value="Gotowe" onclick="objSC.finishEdition()"/>' +
                '<input id="btnZapisz" type="button" value="Zapisano" onclick="objSC.saveEdition()" disabled="disabled"/>';
                //document.getElementById("btnCofnij").disabled = "";
                document.getElementById("optEditStyle").disabled = "";

                document.getElementById("map_tools").style.color = "black";

                var table = document.getElementById("tbl_lines");
                for (var i = 0; i < table.rows.length; i++) {
                    table.rows[i].cells[0].style.display = "inline";
                }
                document.getElementById("lines_selection").style.visibility = "visible";
                document.getElementById("btnZatwierdz").style.visibility = "visible";
                document.getElementById("btnProstuj").style.visibility = "visible";
                document.getElementById("btnResetuj").style.visibility = "visible";
            }
        };
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    };
    this.saveEdition = function() {
        // zapisuje dokonane zmiany
        objMap.saveSchema();
        document.getElementById("btnZapisz").disabled = "disabled";
        document.getElementById("btnZapisz").value = "Zapisano";
    };
    this.finishEdition = function() {
        // zapisuje zmiany i konczy edycje
        var xmlhttp = GetXmlHttpObject();
        if (xmlhttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "src/schemas.php";
        url += "?f=8";
        //url += "&id=" + this.selectedSchema.id;
        url += "&sid=" + Math.random();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                //objSC.saveEdition();
                objSC.schemaIsEdited = false;
                document.getElementById("schemas_buttons").innerHTML =
                    '<input id="btnEdytuj" type="button" value="Edytuj" onclick="objSC.editSchema()"/>';
                document.getElementById("btnCofnij").disabled = "disabled";
                document.getElementById("optEditStyle").disabled = "disabled";

                document.getElementById("map_tools").style.color = "gray";

                var table = document.getElementById("tbl_lines");
                for (var i = 0; i < table.rows.length; i++) {
                    table.rows[i].cells[0].style.display = "none";
                }
                document.getElementById("lines_selection").style.visibility = "hidden";
                document.getElementById("btnZatwierdz").style.visibility = "hidden";
                document.getElementById("btnProstuj").style.visibility = "hidden";
                document.getElementById("btnResetuj").style.visibility = "hidden";                
                document.getElementById('schema_state_' + objSC.selectedSchema.id).innerHTML = '&nbsp;';
                //objSC.selectedSchema.select();
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    };
    this.newSchemaForm = function() {
        // wyswietla formularz dodania nowej sieci
        objPC = new clsPatternsCollection();

        document.getElementById("nav").innerHTML =
            '<div id="new_schema"><h4>Dodaj nową sieć</h4>' +
            '    <table cellpadding="0" cellspacing="0" border="0">' +
            '    <tr style="height:26px">' +
            '        <td style="width:130px">Nazwa:</td>' +
            '        <td><input id="txtNewSchemaName" type="text" style="width:149px" /></td>' +
            '    </tr>' +
            '    <tr style="height:26px">' +
            '        <td>Rodzaj:</td>' +
            '        <td><select id="optType" style="width:149px">' +
            '            <option id="optTram" selected="selected">Tramwajowa</option>' +
            '            <option id="optBus">Autobusowa</option>' +
            '            </select>' +
            '        </td>' +
            '    </tr>' +
            '    <tr style="height:26px">' +
            '        <td><input id="sourceTypeDB" type="radio" name="sourceType" value="db" onclick="objPC.sourceTypeChanged(\'db\')" /> Z bazy:</td>' +
            '        <td><select id="optNewSchemaSource" style="width:149px" onchange="objPC.selectionChanged()">' +
            '            <!-- lista miast z db -->' +
            '            </select>' +
            '        </td>' +
            '    </tr>' +
            '    <tr>' +
            '        <td><input id="sourceTypeURL" type="radio" name="sourceType" value="url"  onclick="objPC.sourceTypeChanged(\'url\')" /> Zasób URL:</td>' +
            '        <td><input id="txtNewSchemaSource" type="text" onkeyup="objPC.checkURL(this.value)" onclick="select()" style="width:149px" />' +
            '        </td>' +
            '    </tr>' +
            '    <tr>' +
            '        <td></td>' +
            '        <td style="height:26px; text-align:right; font-size:smaller;" valign="top"><a href="/src/pattern.xml" target="_blank" style="padding-right: 12px; background-image: url(\'../img/new_window.png\'); background-position: right !important; background-repeat: no-repeat;">Akceptowalny format</a></td>' +
            '    </tr>' +
            '    <tr><td colspan="2"><input id="chkhidden" type="checkbox" value="hidden" />Ukryta (widoczna po zalogowaniu)</td></tr> ' +
            '    </table>' +
            //'    <div><input id="chkhidden" type="checkbox" value="hidden" />Ukryta (Widoczna po zalogowaniu)</div>' +
            '    <div class="buttons">' +
            '        <input id="btnDodajSiec" type="button" value="Dodaj" onclick="objSC.addNewSchema()" />' +
            '        <input id="btnAnuluj" type="button" value="Anuluj" onclick="objSC.showDefaultNav(); objMap.clear();" />' +
            '    </div>' +
            '</div>' +
            '<div id="info"></div>' +
            '<div id="echo"></div>';

        //document.getElementById("optNewSchemaSource").innerHTML = nsCities.getCities();
        objPC.showPatternsList();
        //document.getElementById("optNewSchemaSource").selectedIndex = -1;
        objMap.clear();
    };
    this.addNewSchema = function() {
        // dodaje nowa siec
        //document.getElementById("echo").innerHTML = document.getElementById("optNewSchemaSource").options[document.getElementById("optNewSchemaSource").selectedIndex].value
        var schema_name = document.getElementById("txtNewSchemaName").value;
        var hidden = document.getElementById("chkhidden").checked;
        if (document.getElementById("sourceTypeDB").checked) {
            var city_id = document.getElementById("optNewSchemaSource").selectedIndex;
            if (city_id != -1 && schema_name != "") {
                objMap.savePattern(schema_name, hidden);
            }
            else {
                alert("Nie uzupełniono wszystkich wymaganych pól.");
            }
        } else {
            var url = document.getElementById("txtNewSchemaSource").value;
            if (url.substr(url.length-4) == ".xml" && schema_name != "") {
                objMap.savePattern(schema_name, hidden);
            }
            else {
                alert("Nie uzupełniono wszystkich wymaganych pól.");
            }
        }
        
    };
    this.showDefaultNav = function() {
        // anuluje dodanie nowej sieci
        document.getElementById("nav").innerHTML =
            '<div id="schemas"><h4>Sieci komunikacyjne</h4>' +            
            '    <div id="lst_schemas">' +
            '        <div id="schema_progress" class="progress">Wczytywanie sieci...</div>' +
            '        <table id="tbl_schemas" cellpadding="0" cellspacing="0"></table>' +
            '    </div>' +
            '    <a href="javascript:void(0);" onclick="showInvitationForm();" >Zaproś do edycji</a> | <a href="javascript:void(0)" onclick="objSC.newSchemaForm();">Dodaj nową sieć</a><br />' +
            '    <div id="schemas_buttons" class="buttons">' +
            '        <input id="btnEdytuj" type="button" value="Edytuj" onclick="objSC.editSchema()" title="Edytuj wybraną sieć"/>' +
            '    </div>' +
            '</div>' +
            '<div id="lines">' +
            '    <h4>Połączenia</h4>' +
            '     <div id="lines_options">Sortuj:' +
            '        <select id="optSortType">' +
            '            <option onclick="objLC.sortByName();" value="nazwa">Nazwa</option>' +
            '            <option onclick="objLC.sortByState();" value="ksztalt">Kształt</option>' +
            '        </select>&nbsp;&nbsp;' +
            '        Wyświetl:' +
            '        <select id="optCardDir">' +
            '            <option onclick="objMap.showCardDir(\'all\');" value="wszystkie">Wszystkie</option>' +
            '            <option onclick="objMap.showCardDir(\'NE\');" value="ne">Płn-Wsch</option>' +
            '            <option onclick="objMap.showCardDir(\'NW\');" value="nw">Płn-Zach</option>' +
            '            <option onclick="objMap.showCardDir(\'SW\');" value="sw">Płd-Zach</option>' +
            '            <option onclick="objMap.showCardDir(\'SE\');" value="se">Płd-Wsch</option>' +
            '        </select' +
            '    </div>' +
            '    <div id="lst_lines">' +
            '        <table id="tbl_lines" cellpadding="0" cellspacing="0"></table>' +
            '    </div>' +
            '    <div id="lines_selection">' +
            '       Zaznacz: <a href="javascript:void(0)" onclick="objLC.selectAll()">Wszystko</a>, <a href="javascript:void(0)" onclick="objLC.selectNone()">Żadne</a>, <a href="javascript:void(0)" onclick="objLC.selectEdited()">Edytowane</a>' +
            '    </div>' +
            '    <div id="lines_buttons" class="buttons">' +
            '        <input id="btnZatwierdz" type="button" value="Zatwierdź" onclick="objMap.acceptPolyline()" title="Zatwierdza kształt jako poprawny"/>' +
            '        <input id="btnProstuj" type="button" value="Prostuj" onclick="objMap.makeStraight()" title="Usuwa pośrednie wierzchołki"/>' +
            '        <input id="btnResetuj" type="button" value="Resetuj" onclick="objMap.restoreDefault()" title="Przywraca domyślny kształt"/>' +
            '    </div>' +
            '</div>' +
            '<div id="info"></div>' +
            '<div id="echo"></div>';
        //GmapClear();        
        this.showSchemasList();
        this.selectedSchema = -1;
    };    
}
