﻿var map = null;
var marker = [];
var geocoder = null;
var bounds = null;
var zoomLvl = 10;//default
var temppoint = "";
var isMaxZoomSet = false;
var markerImage = [
"../images/gmap/green.png", //http://maps.google.com/mapfiles/dd-start.png
"http://maps.google.com/mapfiles/dd-end.png",
"../images/gmap/yellow.png", //3 yellow
"../images/gmap/temp_marker.png"//4 turquoise http://www.google.com/uds/samples/places/temp_marker.png
];

/**
* Center map and set zoom level so that all markers are visible
* global markers array required
*/

//Array to store all our markers
var markers = [];
function orderOfCreation(marker, b) {
    return 1;
}

centerMap = function() {
    /* if an infoWindow is open, do not center map (return / do nothing) */
    var iW = map.getInfoWindow();
    if (markers.length <= 0)
        return;
    for (var i = 0; i < markers.length; i++) {
        var this_lat = markers[i].getLatLng().lat() || null;
        var this_lng = markers[i].getLatLng().lng() || null;
        if (
(this_lat && !isNaN(this_lat)) &&
(this_lng && !isNaN(this_lng))
) {
            var minLat = minLat || this_lat;
            var maxLat = maxLat || this_lat;
            var minLng = minLng || this_lng;
            var maxLng = maxLng || this_lng;
            //get min and max markers and save in variables
            minLat = Math.min(minLat, this_lat);
            maxLat = Math.max(maxLat, this_lat);
            minLng = Math.min(minLng, this_lng);
            maxLng = Math.max(maxLng, this_lng);
        }
    }
    //avg of the coordinates
    var centerLat = minLat + ((maxLat - minLat) / 2);
    var centerLng = minLng + ((maxLng - minLng) / 2);
    //what's the distance of our coordinates? (in kilometers)
    var dist = (6371 *
Math.acos(
Math.sin(minLat / 57.2958) *
Math.sin(maxLat / 57.2958) + (
Math.cos(minLat / 57.2958) *
Math.cos(maxLat / 57.2958) *
Math.cos(maxLng / 57.2958 - minLng / 57.2958)
)
)
);

    //default zoom level
    zoomLvl = 10;

    if (markers.length == 1)
    {
        zoomLvl = 15;
    }
    else if (dist > 12288)
        zoomLvl = 1;
    else if (dist > 6144)
        zoomLvl = 2;
    else if (dist > 3072)
        zoomLvl = 3;
    else if (dist > 1536)
        zoomLvl = 4;
    else if (dist > 768)
        zoomLvl = 5;
    else if (dist > 384)
        zoomLvl = 6;
    else if (dist > 192)
        zoomLvl = 7;
    else if (dist > 96)
        zoomLvl = 8;
    else if (dist > 48)
        zoomLvl = 9;
    else if (dist > 24)
        zoomLvl = 10;
    else if (dist > 5)
        zoomLvl = 12;
    else if (dist > 1)
        zoomLvl = 13;
    else {
        zoomLvl = 17;
    }

    zoomLvl -= 2;

    //center map and set zoomLvl
    var point = new GLatLng(centerLat, centerLng);
	temppoint=point;
    map.setCenter(point, zoomLvl);
};


function CreateMarker(fullAddress, label, imInd, i, visited, divid, markerText, divdesc, btnexpid, rowIndex, gridId, org_Name, start_Date, divClientId, btnClientId, chkClientId, job_id) {
   
markers=[];
geocoder = new GClientGeocoder();

if (geocoder) {
    geocoder.getLatLng(fullAddress,
function(point) {

    if (point) {

        bounds.extend(point);


        //letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

        // Set up our GMarkerOptions object
        var greenIcon = new GIcon(G_DEFAULT_ICON);
        greenIcon.image = markerImage[2];

        markerOptions = { icon: greenIcon,zIndexProcess:orderOfCreation};

        marker[i] = new GMarker(point, markerOptions);

        marker[i].visited = false;
        var markerTexts = [];
        var a = "<b><div>" + org_Name + " </b><br/>" + markerText + "<br/><br/>";
        a += "<a href=# onclick=\"showhide('" + divdesc + "','" + btnexpid + "','" + rowIndex + "','" + gridId + "','" + job_id + "');return false;\">Läs mer</a><br/></div>";
        // map.addOverlay();
        map.addOverlay(marker[i]);

        map.enableScrollWheelZoom();

        if (fullAddress != "") {
            markers.push(marker[i]);
            centerMap();
        }

        bjids = get_cookie('bookMarkedJobs');
        if (bjids == null) bjids = '';

        if (bjids.indexOf('j' + job_id) != -1) {
            marker[i].setImage(markerImage[3]);
            marker[i].visited = true;
        }
        else {
            marker[i].setImage(markerImage[0]);
        }


        document.getElementById(divid).onmouseover = function(e) {

            map.removeOverlay(marker[i]);
            map.addOverlay(marker[i]);
            // marker[i].setImage(markerImage[2]);
            //marker[i].old_ZIndex = marker[i].getZIndex();
            // alert(GOverlay.getZIndex(marker[i].getPoint().lat()));
            // marker[i].setZIndex(99999);
            //marker[i].Ir[0].style.zIndex=999999
            //alert(GOverlay.getZIndex(marker[i].getPoint().lat()));

            //document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bgH.jpg) repeat-x";
        }
        document.getElementById(divid).onclick = function(e) {
            if (point) {
                map.panTo(point);
                if (document.getElementById(divdesc).style.display == 'none') {
                    map.setCenter(point, 15);
                    isMaxZoomSet = true;

                }
                else {
                    map.setCenter(point, zoomLvl);
                    isMaxZoomSet = false;
                }
            }
            showhide(divdesc, btnexpid, rowIndex, gridId, job_id);
            //document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bgH.jpg) repeat-x";
        }

        document.getElementById(divid).onmouseout = function(e) {

            if (marker[i].visited == true) {
                marker[i].setImage(markerImage[3]);
            } else {
                marker[i].setImage(markerImage[0]);
            }
            //            if (document.getElementById(divdesc).style.display == 'none')
            //                document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bg.gif) repeat-x";

        }

        GEvent.addListener(marker[i], "click", function() {
            if (point) {
                map.panTo(point);
                if (document.getElementById(divdesc).style.display == 'none') {
                    map.setCenter(point, 15);
                    isMaxZoomSet = true;
                }
                else {
                    map.setCenter(point, zoomLvl);
                    isMaxZoomSet = false;
                   
                   
                }
            }
            //marker[i].openInfoWindowHtml(a);
            //GEvent.trigger(marker[i], "mouseover");
            showhide(divdesc, btnexpid, rowIndex, gridId, job_id);
        });

        GEvent.addListener(marker[i], 'mouseover', function() {
            marker[i].setImage(markerImage[2]);
            //$("#" + divid + " a").css("color", "#618208");
            var objDivHead = document.getElementById(divid).getElementsByTagName('div')[0];
            objDivHead.getElementsByTagName('a')[0].className = "minusLink";
            // document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bgH.jpg) repeat-x";
        });

        GEvent.addListener(marker[i], 'mouseout', function() {
            if (marker[i].visited == true) {
                marker[i].setImage(markerImage[3]);
            } else {
                marker[i].setImage(markerImage[0]);
            }
            //added code here
            var objDivHead = document.getElementById(divid).getElementsByTagName('div')[0];
            if (document.getElementById(divdesc).style.display == 'none')
                objDivHead.getElementsByTagName('a')[0].className = "plusLink";
           
            //objDivHead.getElementsByTagName('a')[0].style.color = "black";
            //document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bg.gif) repeat-x";
        });

    }
    else {
        document.getElementById(divid).onclick = function(e) {
            showhide(divdesc, btnexpid, rowIndex, gridId, job_id);
        }

    }
}
);                
}

}

var countdown = 5;
doCount = function() {
if (countdown > 0) {
countdown--;		 
} else {
centerMap();
return; 
}

var s = countdown;
setTimeout(function() { 
doCount(); 
}, 1000);
};