﻿var m_y = 0;
var m_x = 1;
var m_name = 2;
var m_type = 3;
var m_typename = 4;
var m_desk = 5;
var fir = true;

function InitMap() {
    InitFilter();
    maps = new Maps();
    if (location.hash.length > 0) {
        var hash = unescape(location.hash.substring(1, location.hash.length));
        if (maps.Contains(hash)) { ChangeMap(maps.Get(hash)); }
        else { ChangeMap(maps.Get("Breeland")); }
    }
    else ChangeMap(maps.Get("Breeland"));
    AttachEvent(map.Element, "mousemove", MapMouseMove, false);
    AttachEvent(document.getElementById("fastfind"), "change", FastFind, false);
}

function MapMouseMove(event) {
    event = Event(event);
    cursorposlbl.innerHTML = ConvertIgCoords(ConvertToIg(new Coord(event.Event.clientX - offsetX(), event.Event.clientY - offsetY())));
}

function ConvertIgCoords(inp) {
    var posStr = Math.abs(inp.Y);
    if (inp.Y % 1 == 0) posStr += ".0";
    if (inp.Y > 0) posStr += "N ";
    else posStr += "S ";

    posStr += Math.abs(inp.X);
    if (inp.X % 1 == 0) posStr += ".0";
    if (inp.X > 0) posStr += "E ";
    else posStr += "W";
    return posStr;
}

function offsetX() { return (div_cont.offsetLeft + map.Element.offsetLeft) - getScrollX(); }
function offsetY() { return (div_cont.offsetTop + map.Element.offsetTop) - getScrollY(); }

function getScrollX() {
    var x = window.scrollX;
    if (isIE || isPS3) x = document.documentElement.scrollLeft;
    return x;
}

function getScrollY() {
    var y = window.scrollY;
    if (isIE || isPS3) y = document.documentElement.scrollTop;
    return y;
}

function ConvertToIg(obje) {
    return new Coord(Math.round((map.StartX + ((obje.X * map.ScaleToIg) / 10)) * 10) / 10, Math.round((map.StartY - ((obje.Y * map.ScaleToIg) / 10)) * 10) / 10);
}

function ConvertToMap(obje) {
    return new Coord(Math.round(((map.StartX - obje.X) * map.ScaleToMap * 10) * -1), Math.round(((map.StartY - obje.Y) * map.ScaleToMap * 10)));
}

function Coord(x, y) {
    this.X = x;
    this.Y = y;
}

function FastFind(e) {
    e = Event(e);
    if (e.Element[e.Element.selectedIndex].value != "none") {
        ShowMarkerDetail(null, e.Element[e.Element.selectedIndex].value);
        var mrk = document.getElementById("mrk" + e.Element[e.Element.selectedIndex].value);
        var img = document.getElementById("blink");
        img.style.zIndex = 3;
        img.style.left = mrk.offsetLeft - Math.round(map.MarkersSize / 2) - 4 + "px";
        img.style.top = mrk.offsetTop - Math.round(map.MarkersSize / 2) - 4 + "px";
        img.style.visibility = "visible";
        setTimeout(function() { img.style.visibility = "hidden"; }, 800);
        setTimeout(function() { img.style.visibility = "visible"; }, 1600);
        setTimeout(function() { img.style.visibility = "hidden"; }, 2400);
        setTimeout(function() { img.style.visibility = "visible"; }, 3200);
        setTimeout(function() { img.style.visibility = "hidden"; }, 3800);
    }
}

function MapSelected(select) {
    ChangeMap(maps.Get(select[select.selectedIndex].innerHTML));
}

function ChangeMap(_map) {
    if (!fir) {
        location.hash = "#" + _map.Name;
        location.reload();
        return;
    }
    fir = false;
    var mapsel = document.getElementById("mapsel");
    map = _map;
    map.LoadImage();
    map.LoadMarkers();
    tooltip.Hide();
    detailed.Hide();
    HideAbout();
    if (f_visible) ToggleFilterPane();
    document.title = "Lord of the Rings Online - Dynamic Map - " + map.Name;
    if (mapsel.selectedIndex != map.sId) mapsel.selectedIndex = map.sId;
}

function Map(name, shortname, sid) {
    this.Name = name;
    this.ShortName = shortname;
    this.sId = sid;
    this.RawMarkers = new Array();
    this.Markers = new Array();
    this.MarkersSize = 11;
    this.StartX = 0;
    this.StartY = 0;
    this.ScaleToMap = 0;
    this.ScaleToIg = 0;
    this.Modified = null;
    this.Element = document.getElementById("map");
}

Map.prototype.LoadImage = function() {
    this.Element.style.backgroundImage = "url(images/maps/" + this.ShortName + ".jpg)";
}

Map.prototype.LoadMarkers = function() {
    if (this != null) {
        var el = map.Element.firstChild;
        while (el) {
            var next = el.nextSibling;
            if (/mrk\d+/.test(el.id)) {
                map.Element.removeChild(el);
            }
            el = next;
        }

        var fastfind = document.getElementById("fastfind");
        el = fastfind.firstChild.nextSibling;
        while (el) {
            var next = el.nextSibling;
            if (el.value != "none" && el.nodeType != 3) {       // Ignore option "..." and empty new lines (3 equals String in ie or Text in constructor at other browsers)
                fastfind.remove(el.index);
            }
            el = next;
        }

        var head = document.getElementsByTagName("head").item(0);
        head.removeChild(dynscript);
        dynscript = document.createElement("script");
        dynscript.id = "dynscript";
        dynscript.src = "data/" + this.ShortName + ".js";
        dynscript.type = "text/javascript";
        head.appendChild(dynscript);
    }
    else alert("Map is null, can't load markers.");
}

Map.prototype.InitMarkers = function() {
    if (map.Modified != null) {
        document.title += " (Last Update: " + map.Modified + ")";
    }

    for (var num = 0; num < this.RawMarkers.length; num++) {
        var mrk = this.RawMarkers[num];
        if (mrk != null)  //ie dunno why counts length of array single more
            this.AddMarker(new Marker(num, mrk[m_x], mrk[m_y], mrk[m_name], mrk[m_type], mrk[m_typename], mrk[m_desk]));
    }
    delete this.RawMarkers;      //we trying to free some reources. will it work?

    var fastfind = document.getElementById("fastfind");
    var mrks = this.Markers.slice(0);    //asking what the hell? just copying an array =)
    mrks.sort(function(a, b) { return (a.Name.toLowerCase() < b.Name.toLowerCase()) ? -1 : 1 });
    for (var num = 0; num < mrks.length; ++num) {
        var opt = new Option(mrks[num].Name, mrks[num].Num);
        if (isIE) fastfind.add(opt);
        else fastfind.add(opt, null);
    }
}

Map.prototype.AddMarker = function(marker) {
    var mrk = document.createElement("div");
    mrk.id = "mrk" + marker.Num;
    mrk.className = "marker abs";
    mrk.style.width = this.MarkersSize + "px";
    mrk.style.height = this.MarkersSize + "px";
    mrk.style.backgroundImage = "url(" + marker.ImageUrl() + ")";
    mrk.style.left = marker.ToMap.X - Math.round(this.MarkersSize / 2) + "px";
    mrk.style.top = marker.ToMap.Y - Math.round(this.MarkersSize / 2) + "px";
    AttachEvent(mrk, "mouseover", ShowMarkerToolTip);
    AttachEvent(mrk, "mouseout", HideMarkerToolTip);
    AttachEvent(mrk, "click", ShowMarkerDetail);
    this.Element.appendChild(mrk);
    marker.Element = mrk;
    this.Markers.push(marker);
}

function Marker(num, x, y, name, type, typename, desc) {
    this.Num = num;
    this.X = x;
    this.Y = y;
    this.Name = name;
    this.Type = type;
    this.TypeName = typename;
    this.Description = desc;
    this.ToMap = ConvertToMap(new Coord(this.X, this.Y));
    this.Element = null;
}

Marker.prototype.Visible = function(value) {
    if (this.Element != null) {
        if (value != null) {
            this.Element.style.visibility = value ? "visible" : "hidden";
        }
    }
    else alert("Marker " + this.Name + " Element property is null");
}

Marker.prototype.ImageUrl = function() {
    var type;
    switch (this.Type) {
        case 1: type = "r_green"; break;    //NPC
        case 2: type = "r_white"; break;    //POI
        case 3: type = "s_purple"; break;   //Item
        case 4: type = "r_red"; break;      //Mob
        case 10: type = "r_gray"; break;    //Milestone
        case 11: //Stablemaster
        case 12: //Bard
        case 13: //Auctioneer
        case 14: //Item-Master
        case 15: //Clerk of Kinships
        case 16: //Monster Player Trainer
        case 17: //Master of Apprentices
        case 19: type = "p_green"; break; //Misc
        case 20: //Burglar Trainer
        case 21: //Captain Trainer
        case 22: //Champion Trainer
        case 23: //Guardian Trainer
        case 24: //Hunter Trainer
        case 25: //Lore-Master Trainer
        case 26: //Minstrel Trainer
        case 27: //Rune-keeper Trainer
        case 28: //Warden Trainer
        case 29: type = "p_purple"; break;  //Trainers
        case 40: //Tavern Keeper
        case 41: //Provisioner
        case 42: //Supplier
        case 43: //Grocer
        case 44: //Healer
        case 45: //Notary
        case 46: //Light Armor Trader
        case 47: //Medium Armor Trader
        case 48: //Heavy Armor Trader
        case 49: //Weapontrader
        case 50: //Bowyer
        case 51: type = "p_orange"; break;  //Merchants
        case 60: //Novice Cook
        case 61: //Novice Jeweller
        case 62: //Novice Metalsmith
        case 63: //Novice Scholar
        case 64: //Novice Tailor
        case 65: //Novice Weaponcrafter
        case 66: //Novice Woodworker
        case 67: //Expert Cook
        case 68: //Expert Jeweller
        case 69: //Expert Metalsmith
        case 70: //Expert Scholar
        case 71: //Expert Tailor
        case 72: //Expert Weaponcrafter
        case 73: //Expert Woodworker
        case 74: //Novice Farmhand
        case 75: //Expert Farmhand
        case 79: type = "p_blue"; break;  //Craft
        default: type = "s_red"; break;
    }
    return "images/m_" + type + "_" + map.MarkersSize + ".gif";
}

function ShowMarkerToolTip(event) {
    event = Event(event);
    if (/mrk(\d+)/.test(event.Element.id)) {
        var id = new Number(RegExp.$1);
        tooltip.Element.style.backgroundImage = event.Element.style.backgroundImage;
        tooltip.Element.innerHTML = map.Markers[id].Name;
        tooltip.Show(event.Event.clientX - offsetX(), event.Event.clientY - offsetY());
    }
}

function HideMarkerToolTip() {
    tooltip.Hide();
}

function ShowMarkerDetail(event, id) {
    if (event != null) {
        event = Event(event);
        var coord;
        if (/mrk(\d+)/.test(event.Element.id)) {
            id = new Number(RegExp.$1);
            coord = new Coord(event.Event.clientX - offsetX(), event.Event.clientY - offsetY());
        }
    }
    else {
        var mrk = document.getElementById("mrk" + id);
        coord = new Coord(mrk.offsetLeft + Math.round(map.MarkersSize / 2), mrk.offsetTop + Math.round(map.MarkersSize / 2));
    }
    detailed.Name(map.Markers[id].Name);
    detailed.Type(map.Markers[id].TypeName);
    var loc = new Coord(map.Markers[id].X, map.Markers[id].Y);
    detailed.Location(ConvertIgCoords(loc));
    detailed.Description(map.Markers[id].Description);
    detailed.Show(coord.X, coord.Y);
}

function Maps() {
    this.Array = {};
    this.Init();
}

Maps.prototype.Get = function(name) {
    return this.Array[name];
}

Maps.prototype.Contains = function(name) {
    return (this.Array[name] != null);
}

Maps.prototype.Init = function() {
this.Array["Angmar"] = new Map("Angmar", "angmar", 0);
this.Array["Annuminas"] = new Map("Annuminas", "annuminas", 1);
this.Array["Archet"] = new Map("Archet", "archet", 2);
this.Array["Breeland"] = new Map("Breeland", "breeland", 3);
this.Array["Breetown"] = new Map("Breetown", "breetown", 4);
this.Array["Caras Galadhon"] = new Map("Caras Galadhon", "carasgaladhon", 5);
this.Array["Durin's Way"] = new Map("Durin's Way", "durins_way", 6);
this.Array["Enedwaith"] = new Map("Enedwaith", "enedwaith", 7);
this.Array["Ered Luin"] = new Map("Ered Luin", "ered_luin", 8);
this.Array["Eregion"] = new Map("Eregion", "eregion", 9);
this.Array["Ettenmoors"] = new Map("Ettenmoors", "ettenmoors", 10);
this.Array["Evendim"] = new Map("Evendim", "evendim", 11);
this.Array["Flaming Deeps"] = new Map("Flaming Deeps", "flaming_deeps", 12);
this.Array["Forochel"] = new Map("Forochel", "forochel", 13);
this.Array["Foundations Of Stone"] = new Map("Foundations Of Stone", "foundations_of_stone", 14);
this.Array["Lone-Lands"] = new Map("Lone-Lands", "lone_lands", 15);
this.Array["LothLorien"] = new Map("LothLorien", "lothlorien", 16);
this.Array["Mirkwood"] = new Map("Mirkwood", "mirkwood", 17);
this.Array["Misty Mountains"] = new Map("Misty Mountains", "misty_mountains", 18);
this.Array["Moria"] = new Map("Moria", "moria", 19);
this.Array["North Downs"] = new Map("North Downs", "north_downs", 20);
this.Array["Nothern Barrowdowns"] = new Map("Nothern Barrowdowns", "nothern_barrow_downs", 21);
this.Array["Nud-Melek"] = new Map("Nud-Melek", "nud_melek", 22);
this.Array["Old Forest"] = new Map("Old Forest", "old_forest", 23);
this.Array["Redhorn Lodes"] = new Map("Redhorn Lodes", "redhorn_lodes", 24);
this.Array["Rivendell"] = new Map("Rivendell", "rivendell", 25);
this.Array["Southern Barrowdowns"] = new Map("Southern Barrowdowns", "southern_barrow_downs", 26);
this.Array["The Grand Stair"] = new Map("The Grand Stair", "the_grand_stair", 27);
this.Array["The Great Delving"] = new Map("The Great Delving", "the_great_delving", 28);
this.Array["The Shire"] = new Map("The Shire", "the_shire", 29);
this.Array["The Silvertine Lodes"] = new Map("The Silvertine Lodes", "the_silvertine_lodes", 30);
this.Array["The Walls Of Moria"] = new Map("The Walls Of Moria", "the_walls_of_moria", 31);
this.Array["The Water-Works"] = new Map("The Water-Works", "the_water_works", 32);
this.Array["Thorin's Gate"] = new Map("Thorin's Gate", "thorins_gate", 33);
this.Array["Trollshaws"] = new Map("Trollshaws", "trollshaws", 34);
this.Array["Zelem-Melek"] = new Map("Zelem-Melek", "zelem_melek", 35);
this.Array["Zirakzigil"] = new Map("Zirakzigil", "zirakzigil", 36);
}