﻿var hashTimer;
var isHashTimerOn = false;

function SetHashValues() {
    hashString = "";
    if (lat != undefined && lng != undefined && zoom != undefined) {
        hashString = "#lat:" + lat + "|lng:" + lng + "|z:" + zoom;
    }
    
    window.location.hash = hashString;
}

function ReadHashValuesFromString(hashstring) {
    hashstring = hashstring.replace("#", "");

    latRegEx = /(?:lat:-\d{1,2}.\d+|lat:\d{1,2}.\d+|lat:-\d{1,2}|lat:\d{1,2})/;
    latMatch = latRegEx.exec(hashstring);
    lngRegEx = /(?:lng:-\d{1,3}.\d+|lng:\d{1,3}.\d+|lng:-\d{1,3}|lng:\d{1,3})/;
    lngMatch = lngRegEx.exec(hashstring);
    zoomRegEx = /(?:z:\d{1,2})/;
    zoomMatch = zoomRegEx.exec(hashstring);

    HScrollRegEx = /(?:HS:\d+)/;
    HScrollMatch = HScrollRegEx.exec(hashstring);
    VScrollRegEx = /(?:VS:\d+)/;
    VScrollMatch = VScrollRegEx.exec(hashstring);

    CustomerRegEx = /(?:CID:\d+)/;
    CustomerMatch = CustomerRegEx.exec(hashstring);

    changed = false;

    if (CustomerMatch !== null) {
        SearchedCompanyID = CustomerMatch[0].split(":")[1];
        if (lat != undefined || lng != undefined || zoom != undefined) {
            this.location.reload();
        }
        else {
            SetHashValues();
        }
    }

    if (lat === null || lng === null || zoom === null) {
        if (latMatch !== null && lngMatch !== null && zoomMatch !== null) {
            lat = latMatch[0].split(":")[1];
            lng = lngMatch[0].split(":")[1];
            zoom = parseInt(zoomMatch[0].split(":")[1], 10);
        }
        else {
            lat = null;
            lng = null;
            zoom = null;
        }
    }
    else {
        if (latMatch !== null && lngMatch !== null && zoomMatch !== null) {
            tmpLat = latMatch[0].split(":")[1];
            tmpLng = lngMatch[0].split(":")[1];
            tmpZoom = zoomMatch[0].split(":")[1];

            if (tmpLat !== String(lat) || tmpLng !== String(lng) || tmpZoom !== String(zoom)) {
                lat = latMatch[0].split(":")[1];
                lng = lngMatch[0].split(":")[1];
                zoom = parseInt(zoomMatch[0].split(":")[1], 10);
                changed = true;
            }
        }
    }

    if (HScrollMatch !== null && VScrollMatch !== null) {
        hs = HScrollMatch[0].split(":")[1];
        vs = VScrollMatch[0].split(":")[1];
        window.scroll(hs, vs);
        SetHashValues();
    }


    return changed;
}

//Läser av hashdelen av URL:en och sätter eventuella cordinater och zoom.
function ReadHashValues() {
    var hashstring = window.location.hash;

    return ReadHashValuesFromString(hashstring);
}

//Rekursiv timerfunktion som hanterar om hashdelen av URL:en ändras av besökaren.
function hashTimerThread() {
    if (ReadHashValues()) {
        UpdateMapLocation();
    }
    hashTimer = setTimeout("hashTimerThread()", 500);
}

//Startfunktion för hashtimer.
function RunHashTimer() {
    if (!isHashTimerOn) {
        isHashTimerOn = true;
        hashTimerThread();
    }
}

function StopHashTimer() {
    clearTimeout(hashTimer);
    isHashTimerOn = false;
}

function IsBaseURL() {
    return window.location.host.toLowerCase() + window.location.pathname.toLowerCase() === setting.rootHost;
}

function GetScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) === 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function Redirect(url, stripHash) {
    StopHashTimer();
    if (stripHash !== null && stripHash) {
        window.location.hash = '';
    }

    var scrollLoc = GetScrollXY();
    if (scrollLoc[0] !== 0 || scrollLoc[1] !== 0) {
        window.location.hash = window.location.hash + '|HS:' + scrollLoc[0] + '|VS:' + scrollLoc[1];
    }
    if (SearchedCompanyID !== '') {
        window.location.hash = window.location.hash + '|CID:' + SearchedCompanyID;
    }
    if (setting.lngParam !== '') {
        url = url + '?lng=' + setting.lngParam;
    }
    window.location = "http://" + url + window.location.hash;
}

function URLChkCallBack(UrlName) {
    tmpURL = setting.rootHost + UrlName + "/";
    if (window.location.host.toLowerCase() + window.location.pathname.toLowerCase() !== tmpURL &&
        window.location.host.toLowerCase() + window.location.pathname.toLowerCase() + '/' !== tmpURL) {
        Redirect(tmpURL);
    }
}

function CheckMapURL() {
    if (zoom > setting.OverviewZoomThreshold) {
        var cityURL = window.location.pathname.toLowerCase().replace(/\//g, "");
        CityService.GetNearestCityURL(lat, lng, cityURL, URLChkCallBack, null, null);
    }
    else if (!(IsBaseURL()) && zoom <= setting.OverviewZoomThreshold) {
        Redirect(setting.rootHost);
    }
}

function gup(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if (results == null) return ""; else return results[1]; }

function ChangeLanguage(lngParam) {
    setting.lngParam = lngParam;
    Redirect(window.location.host + window.location.pathname);
}
