﻿function thismap() {
    // Check to see if the counter has been initialized
    if (typeof thismap.map == 'undefined') {
        // It has not... perform the initilization
        var mapOptions = {
            googleBarOptions: {
                style: "new"
            }
        }
        thismap.map = new GMap2(document.getElementById("map"), mapOptions);
        thismap.map.setCenter(new GLatLng(1.28953, 103.84503), 13);
        thismap.map.setUIToDefault();
        thismap.map.enableGoogleBar();
        //alert("init map");
    }

    return thismap.map;
}


function DTrailSet() {
    this.getDTrails = getDTrails;
    this.addDTrail = addDTrail;
    this.getDTrail = getDTrail;

    function getDTrails() {
        isEmptyAndInit();
        return DTrailSet.dtrailArr;

    }

    function getDTrail(id) {
        if (typeof DTrailSet.dtrailArr == 'undefined') {
        } else {
            for (var i = 0; i < DTrailSet.dtrailArr.length; i++) {
                if (DTrailSet.dtrailArr[i].dtrailID == id) {
                    return DTrailSet.dtrailArr[i];
                }
            }
        }
        return null;

    }

    function addDTrail(dtrail) {
        isEmptyAndInit();
        DTrailSet.dtrailArr[DTrailSet.dtrailArr.length] = dtrail;
    }

    function isEmptyAndInit() {
        if (typeof DTrailSet.dtrailArr == 'undefined') {
            DTrailSet.dtrailArr = new Array();
        }
    }
}

function MarkerSet() {
    this.markers;

    this.initMarkers = initMarkers;
    this.addMarker = addMarker;
    this.getMarkers = getMarkers;

    function initMarkers() {
        this.markers = new Array();
    }

    function addMarker(marker) {
        this.markers[this.markers.length] = marker;
    }

    function getMarkers() {
        return this.markers;
    }


}

function AllVars() {
    this.typ_exhibit;
    this.typ_workshop;
    this.typ_conference;
    this.typ_talk;
    this.typ_seminar;
    this.typ_others;
    this.date_arr;

    this.initVars = initVars;
    this.getTypeValues = getTypeValues;
    //this.setTypeValue = setTypeValue;
    this.toggleTypeValue = toggleTypeValue;
    this.toggleDateArr = toggleDateArr;
    this.getDateArrString = getDateArrString;
    this.getDateArr = getDateArr;

    function initVars() {
        this.typ_exhibit = 0;
        this.typ_workshop = 0;
        this.typ_conference = 0;
        this.typ_talk = 0;
        this.typ_seminar = 0;
        this.typ_others = 0;
        this.date_arr = new Array(5);
        for (var i = 0; i < this.date_arr.length; i++) {
            this.date_arr[i] = new Array(7);
            for (var j = 0; j < this.date_arr[i].length; j++) {
                this.date_arr[i][j] = 0;
            }

        }
    }

    function getTypeValues() {
        var retarr = new Array(6);
        retarr[0] = this.typ_exhibit;
        retarr[1] = this.typ_workshop;
        retarr[2] = this.typ_conference;
        retarr[3] = this.typ_talk;
        retarr[4] = this.typ_seminar;
        retarr[5] = this.typ_others;
        retarr[6] = this.date_arr;
        return retarr;
    }

    /*
    function getTypeValue(type) {
    if (type == "1") {
    return this.typ_exhibit;
    } else if (type == "2") {
    return this.typ_worksSem;
    } else if (type == "3") {
    return this.typ_conference;
    } else if (type == "4") {
    return this.typ_others;

        }
    }
    */
    function toggleDateArr(row, col) {
        if (this.date_arr[row][col] == 0) {
            this.date_arr[row][col] = 1;
        } else {
            this.date_arr[row][col] = 0;
        }
    }

    function getDateArr() {
        return this.date_arr;
    }

    function getDateArrString() {
        var retstr = "";
        for (var row = 0; row < this.date_arr.length; row++) {
            for (var col = 0; col < this.date_arr[row].length; col++) {
                retstr += this.date_arr[row][col] + " , ";
            }
            retstr += "\n";
        }
        return retstr;

    }

    function toggleTypeValue(type) {
        if (type == "1") {
            if (this.typ_exhibit == 0) {
                this.typ_exhibit = 1;
            }
            else {
                this.typ_exhibit = 0;

            }
        } else if (type == "2") {
            if (this.typ_workshop == 0) {
                this.typ_workshop = 1;
                this.typ_seminar = 1;
                this.typ_talk = 1;
            }
            else {
                this.typ_workshop = 0;
                this.typ_seminar = 0;
                this.typ_talk = 0;

            }
        } else if (type == "3") {
            if (this.typ_conference == 0) {
                this.typ_conference = 1;
            }
            else {
                this.typ_conference = 0;

            }
        } else if (type == "4") {
            if (this.typ_others == 0) {
                this.typ_others = 1;
            }
            else {
                this.typ_others = 0;

            }
        }
    }

    /*
    function setTypeValue(type, value) {
    if (type == "1") {
    this.typ_exhibit = value;
    } else if (type == "2") {
    this.typ_worksSem = value; ;
    } else if (type == "3") {
    this.typ_conference = value; ;
    } else if (type == "4") {
    this.typ_others = value; ;

        }
    }
    */

}

//DTrail Class
function DTrail() {

    this.initDTrail = initDTrail;
    this.paint = paint;
    this.getMarkers = getMarkers;
    this.removeTrail = removeTrail;
    this.removeSummaryMarker = removeSummaryMarker;
    this.setDirection = setDirection;
    this.getDirection = getDirection;

    function initDTrail(dtrailID, name, color, description, coordinates, markersArr) {
        this.dtrailID = dtrailID;
        this.name = name;
        this.color = color;
        this.description = description;
        this.coordinates = coordinates;
        this.markersArr = markersArr;
        this.polyline = new GPolyline(this.coordinates, this.color, 5, 0.7);
        this.boundingbox = getboundingbox(markersArr);
        
        this.summaryMarker = getSummaryMarker(this.boundingbox,this.dtrailID);


    }

    function setDirection(directions) {
        this.directions = directions;
    }

    function getDirection() {
        return this.directions;
    }

    
    function getSummaryMarker(bounds,tid){
        var sumIcon = new GIcon(G_DEFAULT_ICON);
        sumIcon.image = "images/dtrailMarkers/gmarkerview.png";
        sumIcon.iconSize = new GSize(100, 24);
        sumIcon.shadowSize = new GSize(0, 0);
        sumIcon.imageMap = [1, 1, 100, 1,100,24,1,24];
        
        //blueIcon.iconAnchor = bounds.getCenter();
        markerOptions = { icon: sumIcon };
        var nemarker = bounds.getNorthEast();
		var summarkercoord = new GLatLng(nemarker.lat()-0.00139,nemarker.lng()+0.00082);
        //var marker = new GMarker(bounds.getCenter(), markerOptions);
        var marker = new GMarker(summarkercoord, markerOptions);
        GEvent.addListener(marker, "click",
	      function() {
        //alert(tid);
	          window.location.href = "designtrailSummary.aspx?id=" + tid;
	      }
	    );    
	    
	    return marker;

    }

    function getboundingbox(markersArr) {
        if (markersArr != null) {
            var maxx = 0;
            var maxy = 0;
            var minx = 10000;
            var miny = 10000;
            
            for (var i = 0; i < markersArr.length; i++) {
                var marker = markersArr[i];
                //do check for ne sw
                //alert(marker.y);
                if (marker.x > maxx) {
                    maxx = marker.x;
                }
                if (marker.x < minx) {
                    minx = marker.x;
                }
                if (marker.y > maxy) {
                    maxy = marker.y;
                }
                if (marker.y < miny) {
                    miny = marker.y;
                }
            }


            nex = maxy;
            ney = maxx;

            swx = miny;
            swy = minx;
            //alert(nex+","+ ney);

            var ne2 = new GLatLng(ney, nex);
			var ne = new GLatLng(ne2.lat()+0.0002,ne2.lng());
            var sw = new GLatLng(swy, swx);
			
			ne.lat+=0.001

            //var markerSW = new GMarker(new GPoint(swx, swy));

            return new GLatLngBounds(sw, ne);

        } 

    }

    function removeSummaryMarker(){
        var map = thismap();
        map.removeOverlay(this.summaryMarker);
    }

    function removeTrail() {
        var map = thismap();
        map.removeOverlay(this.polyline);
        map.removeOverlay(this.summaryMarker);

        if (this.markersArr != null) {
            for (var i = 0; i < this.markersArr.length; i++) {
                this.markersArr[i].removeMarker();
                //alert(this.markersArr[i].getFullDescription());
                //marker.paint();
            }

        }
    }

    function paint() {
        var map = thismap();
        map.addOverlay(this.polyline);

        if (this.markersArr != null) {
            for (var i = 0; i < this.markersArr.length; i++) {
                var marker = this.markersArr[i];
                marker.paintMarker();
            }
        }
        
        map.addOverlay(this.summaryMarker);
        map.setCenter(this.boundingbox.getCenter(), map.getBoundsZoomLevel(this.boundingbox));

        //map.setCenter(this.coordinates[this.coordinates.length - 1], 17);

    }

    function getMarkers() {
        return this.markersArr;
    }
}
