/*
    Mapa Inwestycji
    Copyright (C) 2006-2009 Jakub Labenski

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function log(txt)
{
//    document.getElementById("log").value = txt + "\nxxx" + document.getElementById("log").value;
}


STATE_EMPTY = "0";
STATE_RB    = "1";
STATE_BR    = "2";
STATE_END   = "3";

TAG_ID    = "ID";
TAG_B     = "B";
TAG_E_B   = "/B";
TAG_I     = "I";
TAG_E_I   = "/I";
TAG_U     = "U";
TAG_E_U   = "/U";
TAG_URL   = "URL";
TAG_E_URL = "/URL";
TAG_BR    = "BR";
TAG_END   = "END";
TAG_FOOTER = "FOOTER";
TAG_THUMB = "THUMB";

function Parser(astring)
{
    this.txt = astring;
}

Parser.prototype.index = 0;
Parser.prototype.state = STATE_EMPTY;

Parser.prototype.emptyState = function()
{
    start = this.index;
    while (this.index < this.txt.length) {
        if (this.txt.charAt(this.index) == "[") {
            this.state = STATE_RB;
            break;
        } else if (this.txt.charAt(this.index) == "\n") {
            this.state = STATE_BR;
            break;
        }
        ++this.index;
    }
    if (this.index == this.txt.length) {
        this.state = STATE_END;
    }
    ++this.index;
    return this.txt.substring(start, this.index - 1);
};

Parser.prototype.getId = function (param)
{
    if (this.state == STATE_END) {
        return TAG_END;
    } else if (this.state == STATE_EMPTY) {
        param[0] = this.emptyState();
        return TAG_ID;
    } else if (this.state == STATE_BR) {
        this.state = STATE_EMPTY;
        return TAG_BR;
    } else if (this.state == STATE_RB) { 
        this.state = STATE_EMPTY;
        var start = this.index;
        var end = "";
        var next = this.peekNext();
        if (next == '/') {
            end = "/";
            ++this.index;
        }

        var id = this.getInTagId();
        var urlEnd = "";
        if (id != 0) {
            id = id.toUpperCase();
            if (id == "I" || id == "B" || id == "U") {
                next = this.peekNext();
                if (next == ']') {
                    ++this.index;
                    return end + id;
                }
            } else if (id == "URL") {
                next = this.peekNext();
                if (end == "" && next == '=') {
                    ++this.index;
                    urlEnd = this.txt.indexOf("]", this.index);
                    if (urlEnd != -1) {
                        param[0] = this.txt.substring(this.index, urlEnd);
                        this.index = urlEnd + 1;
                        return "URL";
                    }
                } else if (next == ']') {
                    ++this.index;
                    if (end != "") {
                        return end + "URL";
                    }
                    urlEnd = this.txt.indexOf("[", this.index);
                    if (urlEnd != -1) {
                        param[0] = this.txt.substring(this.index, urlEnd);
                        return "URL";
                    }
                }
            } else if (id == "FOOTER") {
                next = this.peekNext();
                if (end == "" && next == '=') {
                    ++this.index;
                    var urlEnd = this.txt.indexOf("]", this.index);
                    if (urlEnd != -1) {
                        param[0] = this.txt.substring(this.index, urlEnd);
                        this.index = urlEnd + 1;
                        tag = this.findEndTag("FOOTER");
                        if (tag != 0) {
                            param[1] = tag;
                            return "FOOTER";
                        }
                    }
                }
            } else if (id == "THUMB") {
                next = this.peekNext();
                if (end == "" && next == ']') {
                    ++this.index;
                    tag = this.findEndTag("THUMB");
                    if (tag != 0) {
                        param[1] = tag;
                        return "THUMB";
                    }
                }
            } 
        }
        this.index = start;
        param[0] = "[";
        return TAG_ID;
    }
};

Parser.prototype.findEndTag = function(tag)
{
    var start = this.index;
    var end = this.txt.indexOf("[", this.index);
    if (end == -1) {
        return 0;
    }
 
    this.index = end + 1;
    var next = this.peekNext();
    if (next != "/") {
        this.index = start;
        return 0;
    }

    ++this.index;
    var id = this.getInTagId();
    if (id == 0) {
        this.index = start;
        return 0;
    }

    id = id.toUpperCase();
    if (id != tag) {
        this.index = start;
        return 0;
    }

    next = this.peekNext();
    if (next != "]") {
        this.index = start;
        return 0;
    }
    ++this.index;

    return this.txt.substring(start, end);
};

Parser.prototype.skipSpaces = function() 
{
    while (this.txt.charAt(this.index) == ' ') {
        ++this.index;
    }
};

Parser.prototype.peekNext = function()
{
    this.skipSpaces();
    if (this.index >= this.txt.length) {
        return 0;
    }
    return this.txt.charAt(this.index);
};

Parser.prototype.getInTagId = function()
{
    this.skipSpaces();
    if (this.index >= this.txt.length) {
        return 0;
    }

    var start = this.index;
    while (this.index < this.txt.length && 
            ((this.txt.charAt(this.index) >= 'a' && this.txt.charAt(this.index) <= 'z') ||
             (this.txt.charAt(this.index) >= 'A' && this.txt.charAt(this.index) <= 'Z')))
    {
        ++this.index;
    }
    
    if (start != this.index) {
        return this.txt.substring(start, this.index);
    }
    return 0;
};

Parser.prototype.parse = function parse(footer, thumb)
{
    var param = new Array();
    var ret = "<div>";
    var u = 0;
    var i = 0;
    var b = 0;

    while (true) {
        tag = this.getId(param);
        if (tag == TAG_END) {
            break;
        } else if (tag == TAG_ID) {
            ret += param[0];
        } else if (tag == TAG_BR) {
            ret += "<br/>";
        } else if (tag == TAG_U) {
            ret += "<U>";
            ++u;
        } else if (tag == TAG_E_U) {
            ret += "</U>";
            if (u > 0) {
                --u;
            }
        } else if (tag == TAG_I) {
            ret += "<I>";
            ++i;
        } else if (tag == TAG_E_I) {
            ret += "</I>";
            if (i > 0) {
                --i;
            }
        } else if (tag == TAG_B) {
            ret += "<B>";
            ++b;
        } else if (tag == TAG_E_B) {
            ret += "</B>";
            if (b > 0) {
                --b;
            }
        } else if (tag == TAG_URL) {
            ret += "<A href=\"" + param[0] + "\">";
        } else if (tag == TAG_FOOTER) {
            pair = new Object();
            pair.url = param[0];
            pair.name = param[1];
            footer.push(pair);
        } else if (tag == TAG_THUMB) {
            thumb[0] = param[1];
        } else if (tag == TAG_E_URL) {
            ret += "</A>";
        }
    }
    while (b) { ret += "</b>"; --b;}
    while (i) { ret += "</i>"; --i;}
    while (u) { ret += "</u>"; --u;}

    ret += "</div>";
    return ret;
};

function convertText(txt, footer, thumb) 
{
    var pr = new Parser(txt);
    return pr.parse(footer, thumb);
}

function quote(text)
{
    var content = text.replace(/>/g, "&gt;");
    return content.replace(/</g, "&lt;");
}

function skipSpaces(txt, index)
{
    while (txt.length > index && txt.charAt(index) == ' ') {
        ++index;
    }
    return index;
}

function nextNumber(txt, index, list)
{
    index = skipSpaces(txt, index);

    var start = index;
    while (txt.length > index) {
        if ((txt.charAt(index) == ",") || (txt.charAt(index) == ")")) {
            if (start != index) {
                list.push(txt.substring(start, index));
            }
            break;
        }
        ++index;
    }
    return index;
}

function nextList(txt, index, list, readList1, readList2, readList3)
{
    index = skipSpaces(txt, index);

    if (txt.charAt(index) == "(") {
        var newList = [];
        list.push(newList);
        ++index;
        while (txt.length > index) {
            index = readList1(txt, index, newList, readList2, readList3);
            if (txt.charAt(index) == ",") {
                ++index;
            }
            index = skipSpaces(txt, index);
      
            if (txt.charAt(index) == ")") {
                ++index;
                break;
            }
        }
    } else {
        index = txt.length;
    }
    return index;
}


var grayIcon;

    
var communicationYellowIcon;

var activeIcon = G_DEFAULT_ICON;
var visibleIcon;
var inactiveIcon;

var map;

var OTHER_ICON         = 0;
var COMMUNICATION_ICON = 1;
var PUBLIC_ICON        = 2;
var RESIDENTIAL_ICON   = 3;
var COMMERCIAL_ICON    = 4;
var INDUSTRIAL_ICON    = 5;

var PLANNED_ICON = 0;
var FINISHED_ICON   = 1;
var STARTED_ICON  = 2;
var IDEA_ICON = 3;
var SUSPENDED_ICON = 4;
var ABANDONED_ICON = 5;

var icons;


if (navigator.appName != "Opera") {
    _mSvgEnabled = true;
    _mSvgForced  = true; 
}



function getIcon(type, status)
{
    if (type == "Communication") {
        oType = COMMUNICATION_ICON;
    } else if (type == "Public Utility") {
        oType = PUBLIC_ICON;
    } else if (type == "Residential") {
        oType = RESIDENTIAL_ICON;
    } else if (type == "Industrial") {
        oType = INDUSTRIAL_ICON;
    } else if (type == "Commercial") {
        oType = COMMERCIAL_ICON;
    } else {
        oType = OTHER_ICON;
    }

    if (status == "Started") {
        oStatus = STARTED_ICON;
    } else if (status == "Finished") {
        oStatus = FINISHED_ICON;
    } else if (status == "Idea") {
        oStatus = IDEA_ICON;
    } else if (status == "Suspended") {
        oStatus = SUSPENDED_ICON;
    } else if (status == "Abandoned") {
        oStatus = ABANDONED_ICON;
    } else {
        oStatus = PLANNED_ICON;
    }

    return icons[oType][oStatus];
}

function getShapeColor(status)
{
    if (status == "Started") {
        return startedColor;
    } else if (status == "Finished") {
        return finishedColor;
    } else if (status == "Idea") {
        return ideaColor;
    } else if (status == "Suspended") {
        return suspendedColor;
    } else if (status == "Abandoned") {
        return abandonedColor;
    } else {
        return plannedColor;
    }
}

function MiControl() {
}

MiControl.prototype = new GControl();

MiControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 200));
};

MiControl.prototype.createButton_ = function(container, text, enabled) {
    var borderDiv = document.createElement("div");
    container.appendChild(borderDiv);
    borderDiv.style.border = "1px solid black";
    borderDiv.style.marginBottom = "3px";
 
    var button = document.createElement("div");
    button.style.backgroundColor = "white";
    button.style.font = "small Arial";
    button.style.fontSize = "12px";
    button.style.border = "1px solid white";
    button.style.textAlign = "center";
    button.style.width = "6em";
    button.style.cursor = "pointer";
    if (enabled) {
        setButtonState(button, 0);
    } else {
        dissableButton(button);
    }

    borderDiv.appendChild(button);
    button.appendChild(document.createTextNode(text));

    return button;
};

function dissableButton(button) {
    button.style.borderTop = "1px solid white";
    button.style.borderLeft = "1px solid white";
    button.style.borderBottom = "1px solid white";
    button.style.borderRight = "1px solid white";
    button.style.fontWeight = "";
}

function setButtonState(button,state) {
    if (state == 0) {
        button.style.borderBottom = "1px solid #b0b0b0";
        button.style.borderRight = "1px solid #b0b0b0";
        button.style.borderTop = "1px solid white";
        button.style.borderLeft = "1px solid white";
        button.style.fontWeight = "";
    } else {
        button.style.borderTop = "1px solid #b0b0b0";
        button.style.borderLeft = "1px solid #b0b0b0";
        button.style.borderBottom = "1px solid white";
        button.style.borderRight = "1px solid white";
        button.style.fontWeight = "bold";
    }
}

function showTopInfoWindowHtml(marker, city, all, planned, started, finished, idea, suspended, abandoned)
{
    var content = "<div style=\"text-align: left; width: 350px\"><div style=\"text-align: center;\"><span style=\"font-weight: bold; font-size: 12pt;\">" + city + 
        "</span></div><br/>" + 
        " <ul>" +
        "     <li><img src=\"" + getBrownIcon() + "\" alt=\"Brown\"/>" + lcIdea + ": " + idea +
        "     <li><img src=\"" + getOrangeIcon() + "\" alt=\"Orange\"/>" + lcPlanned + ": " + planned +
        "     <li><img src=\"" + getGreenIcon() + "\" alt=\"Green\"/>" + lcStarted + ": " + started +
        "     <li><img src=\"" + getBlueIcon() + "\" alt=\"Blue\"/>" + lcFinished + ": " + finished +
        "     <li><img src=\"" + getRedIcon() + "\" alt=\"Red\"/>" + lcSuspended + ": " + suspended +
        "     <li><img src=\"" + getGrayIcon() + "\" alt=\"Gray\"/>" + lcAbandoned + ": " + abandoned +
        " </ul>" + lcTotal + ": " + all;
        
    content += "<hr/><div style=\"float: right\"><a href=\"show.php?city=" + city + "\">" + lcShow + "</a></div></div>";

    marker.openInfoWindowHtml(content);
}

function localeStatus(status)
{
    if (status == "Planned") {
        return lcPlanned;
    } else if (status == "Started") {
        return lcStarted;
    } else if (status == "Finished") {
        return lcFinished;
    }
    return status;
}
 
function showInfoWindowHtml(marker, name, status, description, id)
{
    var footer = []; 
    var thumb = []; 
    thumb.push("");
        
    var convertedText = convertText(description, footer, thumb);
    var thumbUrl = "";
 
    if (thumb[0] != "") {
        thumbUrl = "<div style=\"float: left; margin-right: 10px; height:250px\"><a href=\"" + thumb[0] + "\"><img src=\"" + thumb[0] + "\" style=\"width:250px\"></img></a></div>";
        width = "400";
    } else {
        width = "350";
    }


    var content = "<div style=\"text-align: left; width: " + width + "px\"><div style=\"float: left; font-size: 7pt;\">" + id + 
        ")</div><div style=\"text-align: center;\"><span style=\"font-weight: bold; font-size: 12pt;\">" + name + "</span><small> (" + localeStatus(status) +
        ")</small></div><br/><div style=\"overflow: hidden\">" + thumbUrl + convertedText + "</div><hr/>";
        
    if (id != "") {
        content += "<div style=\"float: right\"><a href=\"add.php?id=" + id + extraUrlArgs + "\">" + lcEdit + "</a></div>";
    }

    for (r in footer)
    {
        content += " <a href=\"" + footer[r].url + "\">" + footer[r].name + "</a>";
    }

    content += "</div>";

    marker.openInfoWindowHtml(content);
}
    
function initIcon(base, image)
{
    icon = new GIcon(base);
    icon.image = image;
    return icon;
}

function addIcon(name, path, baseIcon)
{
    ext = ".png";
    icons[name] = [];
    icons[name][PLANNED_ICON]  = initIcon(baseIcon, path + plannedColorName + ext);
    icons[name][STARTED_ICON]  = initIcon(baseIcon, path + startedColorName + ext);
    icons[name][FINISHED_ICON] = initIcon(baseIcon, path + finishedColorName + ext);
    icons[name][IDEA_ICON]     = initIcon(baseIcon, path + ideaColorName + ext);
    icons[name][SUSPENDED_ICON] = initIcon(baseIcon, path + suspendedColorName + ext);
    icons[name][ABANDONED_ICON] = initIcon(baseIcon, path + abandonedColorName + ext);
}
 
function initIcons()
{
    var baseIcon = new GIcon();
    baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    baseIcon.iconSize = new GSize(12, 20);
    baseIcon.shadowSize = new GSize(22, 20);
    baseIcon.iconAnchor = new GPoint(6, 20);
    baseIcon.infoWindowAnchor = new GPoint(5, 1);
    grayIcon = new GIcon(baseIcon);
    grayIcon.image = "http://labs.google.com/ridefinder/images/mm_20_gray.png";

    iconUrl = getIconUrl();
    
    baseIcon.iconSize = new GSize(22, 33);
    baseIcon.iconAnchor = new GPoint(12, 33);
    baseIcon.shadow = iconUrl + "shadow.png";
    baseIcon.shadowSize = new GSize(51, 33);
 
    icons = [];

    addIcon(OTHER_ICON, iconUrl + otherIconPrefix + "_", baseIcon);
    addIcon(COMMUNICATION_ICON, iconUrl + communicationIconPrefix + "_", baseIcon);
    addIcon(PUBLIC_ICON, iconUrl + publicIconPrefix + "_", baseIcon);
    addIcon(RESIDENTIAL_ICON, iconUrl + residentialIconPrefix + "_", baseIcon);
    addIcon(COMMERCIAL_ICON, iconUrl + commercialIconPrefix + "_", baseIcon);
    addIcon(INDUSTRIAL_ICON, iconUrl + industrialIconPrefix + "_", baseIcon);

}


