﻿//Henrik Lannerhjelm 2010

var PrivpakMap = function () {
    //name of the event
    this.eventName = arguments[0];
    var mEventName = this.eventName;
    this.divName = arguments[0];
    //function to call on event fire
    var eventAction = null;
    this.Width = "100%";
    this.Height = "800px";
    this.ShowHeader = true;
    this.ShowList = true;
    this.ShowSearch = true;
    this.Selectable = true;
    this.SearchString = "";
    this.APIKey = "";
    this.CustomerID = "113";
    this.Country = "SE";
    this.Service = "-1";
    this.StreetView = false;
    this.ShowZoomControl = true;
    this.ShowMapTypesControl = true;
    this.ShowPPCNr = false;


    //subscribe a function to the event
    this.LoadList = function (fn) {
        if (this.Service == -1) {
            if (this.Country == 'SE')
                this.Service = 0;
            else if (this.Country == 'NO')
                this.Service = 2;
            else if (this.Country == 'FI')
                this.Service = 1;
            else if (this.Country == 'ALL')
                this.Service = 3;


        }
        iwin = document.getElementById(this.divName);
        if (this.SearchString != "") this.SearchString = "&search=" + this.SearchString;
        //Nedanstående ska ändras innan driftsättning   
        iwin.innerHTML = '<iframe id="pipeFrame"  style="overflow-x: hidden;"  scrolling=no marginwidth=0 marginheight=0 frameborder=0 src="http://privpakservices.schenker.nu/map/map_1.0/privpakwidget.aspx?APIKey=' + this.APIKey + '&ShowSearch=' + this.ShowSearch + '&Showheader=' + this.ShowHeader + '&ShowZoomControl=' + this.ShowZoomControl + '&ShowMapTypesControl=' + this.ShowMapTypesControl + '&ShowList=' + this.ShowList + '&Selectable=' + this.Selectable + '&Width=' + this.Width + '&Height=' + this.Height + '&CustomerID=' + this.CustomerID + '&Service=' + this.Service + '&StreetView=' + this.StreetView + '&Country=' + this.Country + '&SearchString=' + this.SearchString + '" Width=' + this.Width + ' Height=' + this.Height + 'border="0"></iframe>'
        eventAction = fn;
        setInterval(checkForMessages, 700);
    };

    //subscribe a function to the event
    this.LoadMap = function (fn) {
        if (this.Service == -1) {
            if (this.Country == 'SE')
                this.Service = 0;
            else if (this.Country == 'NO')
                this.Service = 2;
            else if (this.Country == 'FI')
                this.Service = 1;
            else if (this.Country == 'ALL')
                this.Service = 3;


        }
        iwin = document.getElementById(this.divName);
        //iwin.innerHTML = '<iframe id="pipeFrame"  style="overflow-x: hidden;"  scrolling=no marginwidth=0 marginheight=0 frameborder=0 src="http://localhost:4160/Map_1.0/default.aspx?APIKey=' + this.APIKey + '&showSearch=' + this.ShowSearch + '&ShowHeader=' + this.ShowHeader + '&ShowZoomControl=' + this.ShowZoomControl + '&ShowMapTypesControl=' + this.ShowMapTypesControl + '&ShowList=' + this.ShowList + '&Selectable=' + this.Selectable + '&Width=' + this.Width + '&Height=' + this.Height + '&CustomerID=' + this.CustomerID + '&Service=' + this.Service + '&StreetView=' + this.StreetView + '&Country=' + this.Country + '&ShowPPCNr=' + this.ShowPPCNr + '&SearchString=' + this.SearchString + '" Width=' + this.Width + ' Height=' + this.Height + 'border="0"></iframe>'
        iwin.innerHTML = '<iframe id="pipeFrame"  style="overflow-x: hidden;"  scrolling=no marginwidth=0 marginheight=0 frameborder=0 src="http://privpakservices.schenker.nu/map/map_1.0/default.aspx?APIKey=' + this.APIKey + '&showSearch=' + this.ShowSearch + '&ShowHeader=' + this.ShowHeader + '&ShowZoomControl=' + this.ShowZoomControl + '&ShowMapTypesControl=' + this.ShowMapTypesControl + '&ShowList=' + this.ShowList + '&Selectable=' + this.Selectable + '&Width=' + this.Width + '&Height=' + this.Height + '&CustomerID=' + this.CustomerID + '&Service=' + this.Service + '&StreetView=' + this.StreetView + '&Country=' + this.Country + '&ShowPPCNr=' + this.ShowPPCNr + '&SearchString=' + this.SearchString + '" Width=' + this.Width + ' Height=' + this.Height + 'border="0"></iframe>'
        eventAction = fn;
        setInterval(checkForMessages, 700);
    };
    var lastId = "";
    //fire the event
    this.fire = function (sender, eventArgs) {
        this.eventName = eventName2;
        if (eventAction != null) {
            eventAction(sender, eventArgs);
            alert('There was no function subscribed to the ' + mEventName + ' event!');
        }
    };

    function checkForMessages() {
        if (location.hash != lastId) {
            lastId = location.hash;
            lastId = lastId.replace("#", "");
            var ppcArray = lastId.split(';');
            if (ppcArray[1] != null) {
                eventAction(null, {
                    ppc: ppcArray[0].replace("#", ""),
                    name: ppcArray[1],
                    Adress: ppcArray[2],
                    Postalcode: ppcArray[3],
                    Town: ppcArray[4],
                    OpenTime: ppcArray[5]
                });
            }
            location.hash = "";
            lastId = location.hash;


        }
    }



};


   
    function getItem(id) {return;
        var itm = false;
        if (document.getElementById) itm = document.getElementById(id);
        else if (document.all) itm = document.all[id];
        else if (document.layers) itm = document.layers[id];

        return itm;
    }


    // <summary>
    // För att visa standard värde i texboxarna
    // </summary>
    // <param name="field">Fältet</param>
    // <param name="value">Standardvärdet på fältet</param>
    // <returns></returns>
    function deleteDefaulValue(field, value) {

        if (field.value == value) {
            field.value = '';
            field.focus;
        }
        // shortinfo(field, value);
    }
    // <summary>
    // För att visa standard värde i texboxarna
    // </summary>
    // <param name="field">Fältet</param>
    // <param name="value">Standardvärdet på fältet</param>
    // <returns></returns>

    function setDefaultValue(field, value) {
       // if (field.value  == value) return;

        if (field.value == '') field.value = value;
        if (field.value == '' || field.value == value) {
            

            try {
                if (field != null) {
                    if (field.createTextRange) {
                        var range = field.createTextRange();
                        range.move('character', 0);
                        range.select();
                    } else {
                        if (field.selectionStart) {
                            field.focus();
                            field.setSelectionRange(0, 0);
                        } else field.focus();
                    }
                }
                //Run some code here
            }
            catch(err) {
                //Handle errors here
            }
        }
    }

    function SetSelected(id) {
      try{
        var i = 0;
        var point;
        while (i < markers.markers.length) {
            if (markers.markers[i].value == id) {
                point = markers.markers[i];
                i = 9990;
            }
            i++;
        }
        if (!point) return false;
     
        GEvent.trigger(point, "click");
        map.setCenter(new GLatLng(point.getPoint().y, point.getPoint().x));
        document.forms[0].txtSearch.focus();
        }
        catch(err){}
    }

    function clearSelected() {
        var i = 0;
        var itm;
        while (i < 20) {
            itm = getItem('List_' + i);
            if (!itm) return false;
            itm.background = 'images/list.png';
            i++;
        }
        return false;
    }

    function toggleItem(id) {
        itm = getItem(id);

        if (!itm) return false;

        if (itm.style.display == 'none') itm.style.display = '';
        else itm.style.display = 'none';

        return false;
    }




    var lastId = "";
    
    
