﻿/*
 *  You seems likes to dig in source codes =)
 *  Made by Garret
 */
var maps;
var map;
var tooltip;
var detailed;
var isIE = false;
var isPS3 = false;
/* Elements */
var div_cont;
var dynscript;
var cursorposlbl;
/*----------*/

function init() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { isIE = true; }
    else if (/PLAYSTATION 3/.test(navigator.userAgent)) { isPS3 = true; }

    div_cont = document.getElementById("cont");
    dynscript = document.getElementById("dynscript");
    cursorposlbl = document.getElementById("cursorpos");
    tooltip = new ToolTip();
    detailed = new Detailed();
    //CalcPositions();
    InitMap();
    AttachEvent(document.getElementById("about"), "click", ShowAbout, false);
    AttachEvent(document.getElementById("aboutinf"), "click", HideAbout, false);
}

function AttachEvent(who, what, where, orly) {
    if (isIE || isPS3) { who.attachEvent("on" + what, where); }
    else { who.addEventListener(what, where, orly); }
}

function Event(event) {
    var val = new Object();
    if (event) val.Event = event;
    else val.Event = window.event;
    if (event.target) val.Element = event.target;
    else val.Element = event.srcElement;
    return val;
}

function WindowResized() {
    var w_width;
    if (isIE || isPS3) { w_width = document.body.offsetWidth; }
    else { w_width = window.innerWidth; }
    if ((w_width - 1024) > 0) {
        div_cont.style.left = (w_width - 1024) / 2 + "px";
    }
    else { div_cont.style.left = "0px"; }
}

function CalcPositions() {
    AttachEvent(window, "resize", WindowResized, false);
    WindowResized();
}

function ToolTip() {
    this.Element = document.getElementById("tooltip");
}

ToolTip.prototype.Show = function(x, y) {
    if (map != null) {
        if (isPS3) {
            this.Element.style.width = "0%";
            this.Element.style.height = "0%";
        }
        if (x > map.Element.clientWidth - this.Element.clientWidth) x -= 9 + this.Element.clientWidth;
        else x += 9;
        this.Element.style.left = x + "px";
        this.Element.style.top = y - this.Element.clientHeight / 2 + "px";
        this.Element.style.visibility = "visible";
    }
}

ToolTip.prototype.Hide = function() {
    this.Element.style.visibility = "hidden";
}

function Detailed() {
    this.Element = document.getElementById("detailed");
    this.NameNode = document.getElementById("d_name");
    this.TypeNode = document.getElementById("d_type");
    this.LocationNode = document.getElementById("d_loc");
    this.DescriptionNode = document.getElementById("d_desc");
}

Detailed.prototype.Name = function(text) {
    this.NameNode.innerHTML = text;
}

Detailed.prototype.Type = function(text) {
    this.TypeNode.innerHTML = text;
}

Detailed.prototype.Location = function(text) {
    this.LocationNode.innerHTML = text;
}

Detailed.prototype.Description = function(text) {
    if (text == null) text = "";
    this.DescriptionNode.innerHTML = text;
}

Detailed.prototype.Show = function(x, y) {
    if (map != null) {
        if (isIE) {
            var width = this.NameNode.innerHTML.length * 9 + "px";
            if (width < 170) width = 170 + "px";
            this.NameNode.style.width = width;
        }
        if (x > map.Element.clientWidth - this.Element.clientWidth) x -= 9 + this.Element.clientWidth;
        else x += 9;
        this.Element.style.left = x + "px";
        this.Element.style.top = y - this.Element.clientHeight / 2 + "px";
        this.Element.style.visibility = "visible";
    }
}

Detailed.prototype.Hide = function() {
    this.Element.style.visibility = "hidden";
}

function ShowAbout() {
    document.getElementById("aboutinf").style.visibility = "visible";
}

function HideAbout() {
    document.getElementById("aboutinf").style.visibility = "hidden";
}

Array.prototype.inArray = function(value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == value) {
            return true;
        }
    }
    return false;
};

Array.prototype.Equals = function(value) {
    if (value.constructor != Array) return false;
    if (this.length != value.length) return false;
    var ar1 = new Array().concat(this);
    var ar2 = new Array().concat(value);
    var result = ar1.sort().toString() == ar2.sort().toString();
    delete ar1;
    delete ar2;
    return result;
};
