﻿var objColors = new function() {
    this.normalColor = "#006600";
    this.normalOpacity = 0.5;
    this.wrongColor = "#FF0000";
    this.wrongOpacity = 0.8;
    this.pointedColor = "#00CCCC";
    this.pointedOpacity = 1;
    this.selectedColor = "#3333FF";
    this.selectedOpacity = 1;

    this.hiddenOpacity = 0.1;
}

function clsLine(prmId, prmName, prmStateId, prmPolyline, prmHead, prmTail, prmColor, prmOpacity) {
    this.id = prmId; // id polaczenia - int
    this.name = prmName; // nazwa polaczenia - head,tail (id przystankow)    
    this.stateId = prmStateId;
    this.polyline = prmPolyline; // GPolyline
    this.head = prmHead;
    this.tail = prmTail;
    this.bounds = new GLatLngBounds(new GLatLng(prmPolyline.getBounds().getSouthWest().lat() - 0.002,prmPolyline.getBounds().getSouthWest().lng() - 0.002),
                                    new GLatLng(prmPolyline.getBounds().getNorthEast().lat() + 0.002,prmPolyline.getBounds().getNorthEast().lng() +  0.002));
    this.isPointed = false;
    this.isSelected = false;
    this.isEdited = false;
    this.isHidden = false;
    this.isChecked = false;
    prmColor != null ? this.defaultColor = prmColor : this.defaultColor = prmPolyline.color;
    prmOpacity != null ? this.defaultOpacity = prmOpacity : this.defaultOpacity = prmPolyline.opacity;
    this.currentColor = prmPolyline.color; // kolor po wskazaniu, wybraniu
    this.currentOpacity = prmPolyline.opacity;
    
    this.setPolyline = function(prmPolyline) {
        this.polyline = prmPolyline;
        this.bounds = new GLatLngBounds(new GLatLng(prmPolyline.getBounds().getSouthWest().lat() - 0.002,prmPolyline.getBounds().getSouthWest().lng() - 0.002),
                                        new GLatLng(prmPolyline.getBounds().getNorthEast().lat() + 0.002,prmPolyline.getBounds().getNorthEast().lng() +  0.002));
    }

    this.setStateId = function(prmStateId) {
        this.stateId = prmStateId;
        switch (prmStateId) {
            case "1":
                document.getElementById("line_state_" + this.id).innerHTML = lstStatesNames[0];
                break;
            case "2":
                document.getElementById("line_state_" + this.id).innerHTML = lstStatesNames[1];
                break;
            case "3":
                document.getElementById("line_state_" + this.id).innerHTML = lstStatesNames[2];
                break;
        }
    }

    var lstStatesNames = new Array("Domyślny", "Edytowany", "Poprawny");
    
    this.insertRow = function(prmId) {
        if (prmId == null)
            prmId = this.id;
        //document.getElementById("echo").innerHTML += prmName;
        var row = document.getElementById("tbl_lines").insertRow(-1);
        row.id = "line_" + this.id;
        row.onmouseover = function() { objLC.array[prmId].mouseover(); };
        row.onmouseout = function() { objLC.array[prmId].mouseout(); };
        var cell_0 = row.insertCell(0);
        //cell_0.innerHTML = '<input id="chkline_' + prmId + '" type="checkbox" onclick="objLC.array[' + prmId + '].isSelected = this.checked ? true : false"/>';
        cell_0.innerHTML = '<input id="chkline_' + this.id + '" type="checkbox" onclick="objLC.array[' + this.id + '].chklineChange(this.checked)"/>';
        if (!objSC.schemaIsEdited)
            cell_0.style.display = "none";
        var cell_1 = row.insertCell(1);
        cell_1.onclick = function() { objLC.array[prmId].centerAt() };
        cell_1.setAttribute('class', 'line_name');
        cell_1.setAttribute('title', 'Nazwa połączenia');
        cell_1.innerHTML = this.name;
        var cell_2 = row.insertCell(2);
        cell_2.id = "line_state_" + this.id;
        cell_2.onclick = function() { objLC.array[prmId].click() };
        cell_2.setAttribute('class', 'line_state');
        cell_2.setAttribute('title', 'Kształt połączenia');
        switch (this.stateId) {
            case "1":
                cell_2.innerHTML = lstStatesNames[0];
                break;
            case "2":
                cell_2.innerHTML = lstStatesNames[1];
                break;
             case "3":
                cell_2.innerHTML = lstStatesNames[2];
                break;
        }
    }
    
    this.mouseover = function() {
        // jezeli isPointed nie trzeba drugi raz zmieniac koloru, return
        // jezeli !isSelected zmiana koloru (jasny niebieski), isPointed = true
        if (this.isPointed || this.isSelected || this.isHidden)
            return;
        this.isPointed = true;
        document.getElementById('line_' + this.id).style.background = '#E8F3FF';
        this.polyline.setStrokeStyle({ color: objColors.pointedColor, opacity: objColors.pointedOpacity });
        this.currentColor = objColors.pointedColor;
        this.currentOpacity = objColors.pointedOpacity;
        this.head.putHead();
        this.tail.putTail();
    }

    this.mouseout = function() {
        // jezeli isPointed zmiana koloru na domyslny
        // jezeli isSelected nie ma zmiany koloru, utrzymuje sie ciemny niebieski
        if (!this.isPointed || this.isSelected || this.isHidden)
            return;
        this.isPointed = false;
        document.getElementById('line_' + this.id).style.background = 'Transparent';
        this.polyline.setStrokeStyle({ color: this.defaultColor, opacity: this.defaultOpacity });
        this.currentColor = this.defaultColor;
        this.currentOpacity = this.defaultOpacity;
        if (!this.head.isSelected)
            this.head.putDefault();
        else
            this.head.putPrev();
        if (!this.tail.isSelected)
            this.tail.putDefault();
        else
            this.tail.putPrev();
    }
    
    this.select = function() {
        // jezeli tryb edycji
        // wyswietlenie markerow edycji
        // isSelected = true;
        // jezeli isSelected nie trzeba drugi raz        
        // zmiana koloru na ciemny niebieski
        if (this.isSelected || this.isHidden)
            return;
        this.isSelected = true;        
        this.polyline.setStrokeStyle({color: objColors.selectedColor, opacity: objColors.selectedOpacity });
        document.getElementById('line_' + this.id).style.background = '#88CDFF';
        this.currentColor = objColors.selectedColor;
        this.currentOpacity = objColors.selectedOpacity;
        this.head.isSelected = true;
        this.tail.isSelected = true;
        this.head.putHead();
        this.tail.putTail();
    }

    this.deselect = function() {
        // jezeli !isSelected nie potrzeba
        // ustalenie domyslnego koloru
        if (!this.isSelected || this.isHidden)
            return;
        this.isSelected = false;
        this.isPointed = false;
        this.polyline.setStrokeStyle({ color: this.defaultColor, opacity: this.defaultOpacity });
        document.getElementById('line_' + this.id).style.background = 'Transparent';
        this.currentColor = this.defaultColor;
        this.currentOpacity = this.defaultOpacity;
        this.head.isSelected = false;
        this.tail.isSelected = false;
        this.head.putDefault();
        this.tail.putDefault();
    }

    this.centerAt = function() {
        // centrowanie mapy na polyline.. opcja wywolywana tylko z tabeli
        objMap.setPointedLine(objLC.array[this.id]);
        //document.getElementById('echo').innerHTML = objMap.getPointedLine().name;
        objMap.setCenter(this.bounds.getCenter());
        if (objMap.getSelectedLine() != null && objMap.getSelectedLine() != objMap.getPointedLine()) {
            objMap.getSelectedLine().deselect();
            objMap.setSelectedLine(null);
        }
        if (objMap.getPointedLine() != null) {
            //document.getElementById('line_' + pointedLine.id).scrollIntoView(true);
            objMap.getPointedLine().select();
            objMap.setSelectedLine(objMap.getPointedLine());
        }
    }

    this.show = function() {
        if (this.isSelected)
            return;
        this.isHidden = false;
        this.polyline.setStrokeStyle({ color: this.defaultColor, opacity: this.defaultOpacity });
        document.getElementById("line_" + this.id).style.display = "table-row";
    }

    this.hide = function() {
        if (this.isSelected)
            return;
        this.isHidden = true;
        this.polyline.setStrokeStyle({ color: this.defaultColor, opacity: objColors.hiddenOpacity });
        document.getElementById("line_" + this.id).style.display = "none";
    }

    this.enableEditing = function() {
        // "nadpisanie" aby zapobiec tworzeniu kilku punktow w tym samym miejscu
        for (var i = 0; i < this.polyline.getVertexCount()-1; i++) {
            if (this.polyline.getVertex(i).distanceFrom(this.polyline.getVertex(i+1)) < 2) {
                // usuniecie niepotrzebnych wierzcholkow
                this.polyline.deleteVertex(i+1);
                i--;
            }
        }
        this.polyline.enableEditing();
    }

    this.chklineChange = function(prmChecked) {
        // zaznaczono-odznaczono checkbox
        this.isChecked = prmChecked
        //if (prmChecked) {
        //    this.polylineSelect();
        //} else {
        //    this.polylineDeselect();
        //}
    };
}

function clsLinesCollection() {
    //this.selectedPattern = -1;
    this.array = new Object();
    this.lstNE = new Array();
    this.lstNW = new Array();
    this.lstSW = new Array();
    this.lstSE = new Array();
    var selectedCardDir = "all";
    //this.sortType = "name";
    //this.sortAsc = true;
    this.selectedKeys = new Array();
    this.isLoaded = false;

    this.selectAll = function() {
        // zaznacza wszystkie polaczenia
        for (var key in this.array) {
            document.getElementById("chkline_" + key).checked = true;
            this.array[key].isChecked = true;
            //this.array[key].polylineSelect();
        }
    };
    this.selectNone = function() {
        // odznacza wszystkie polaczenia
        for (var key in this.array) {
            document.getElementById("chkline_" + key).checked = false;
            this.array[key].isChecked = false;
            //this.array[key].polylineDeselect();
        }
    };
    this.selectEdited = function() {
        for (var key in this.array) {
            if (this.array[key].stateId == "2") {
                document.getElementById("chkline_" + key).checked = true;
                this.array[key].isChecked = true;
                //this.array[key].polylineSelect();
            }
            else {
                document.getElementById("chkline_" + key).checked = false;
                this.array[key].isChecked = false;
                //this.array[key].polylineDeselect();
            }
        }
    };

    this.sortByName = function() {        
        var table = document.getElementById("tbl_lines");
        while (table.rows.length > 0) {            
            table.deleteRow(0);
        }
        for (var key in objLC.array) {
            objLC.array[key].insertRow(key);
        }
    };
    this.sortByState = function() {        
        var linesEdited = new Array();
        var linesAccepted = new Array();
        var table = document.getElementById("tbl_lines");
        for (var i = 0; i < table.rows.length; i++) {
            if (objLC.array[table.rows[i].id.substr(5)].stateId == "2") {
                linesEdited.push(objLC.array[table.rows[i].id.substr(5)]);
                table.deleteRow(i);
                i--;
            }
            else if (objLC.array[table.rows[i].id.substr(5)].stateId == "3") {
                linesAccepted.push(objLC.array[table.rows[i].id.substr(5)]);
                table.deleteRow(i);
                i--;
            }
        }

        for (var j = 0; j < linesEdited.length; j++) {
            linesEdited[j].insertRow(linesEdited[j].id);
        }
        for (var j = 0; j < linesAccepted.length; j++) {
            linesAccepted[j].insertRow(linesAccepted[j].id);
        }
    };

    this.showCardDir = function(prmCardDir) {        
        if (selectedCardDir == prmCardDir)
            return;
        if (selectedCardDir != "all") {
            var arrayCardDir = new Array();
            if ((prmCardDir == "all" && selectedCardDir != "NE") || prmCardDir == "NE")
                arrayCardDir.push(this.lstNE);
            if ((prmCardDir == "all" && selectedCardDir != "NW") || prmCardDir == "NW")
                arrayCardDir.push(this.lstNW);
            if ((prmCardDir == "all" && selectedCardDir != "SW") || prmCardDir == "SW")
                arrayCardDir.push(this.lstSW);
            if ((prmCardDir == "all" && selectedCardDir != "SE") || prmCardDir == "SE")
                arrayCardDir.push(this.lstSE);

            //var showedLinesCounter = 0;

            // pokazywanie
            for (var i = 0; i < arrayCardDir.length; i++) {
                var lstCardDir = arrayCardDir[i];
                for (var j = 0; j < lstCardDir.length; j++) {
                    lstCardDir[j].show();
                    //showedLinesCounter++;
                }
            }
        }

        //if (prmCardDir != "all")
        //    document.getElementById("progress").innerHTML = "Wyświetlono " + showedLinesCounter + "/" + lstPolylinesLength + " połączeń";
        //else
        //    document.getElementById("progress").innerHTML = "Wyświetlono " + lstPolylinesLength + "/" + lstPolylinesLength + " połączeń";

        //ukrywanie
        if (prmCardDir != "all") {
            arrayCardDir = new Array();
            if (selectedCardDir == "NE" || (selectedCardDir == "all" && prmCardDir != "NE"))
                arrayCardDir.push(this.lstNE);
            if (selectedCardDir == "NW" || (selectedCardDir == "all" && prmCardDir != "NW"))
                arrayCardDir.push(this.lstNW);
            if (selectedCardDir == "SW" || (selectedCardDir == "all" && prmCardDir != "SW"))
                arrayCardDir.push(this.lstSW);
            if (selectedCardDir == "SE" || (selectedCardDir == "all" && prmCardDir != "SE"))
                arrayCardDir.push(this.lstSE);
            for (var i = 0; i < arrayCardDir.length; i++) {
                var lstCardDir = arrayCardDir[i];
                for (var j = 0; j < lstCardDir.length; j++) {
                    lstCardDir[j].hide();
                }
            }
        }
        selectedCardDir = prmCardDir;
    }
}
