var Browser = {
  Version: function() {
    var version = 999;
    if (navigator.appVersion.indexOf("MSIE") != -1)
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

var mapHandler = {
	map: null,
	marker: null,
	control: null,
	baseIcon: null,
	kiosk: false,
	width: null,
	height: null,
	init : function(key,x,y,zoom,ptype,kiosk) {
		this.kiosk = kiosk;
		
		if (GBrowserIsCompatible()&&!this.map) {
			this.map = new GMap2(document.getElementById(key));
			this.map.enableDragging();
			this.map.enableContinuousZoom();
			this.map.enableScrollWheelZoom();
			this.map.addControl(new GSmallMapControl());
			this.marker = this.createMarker(x,y,ptype);
			this.map.addOverlay(this.marker);
			this.map.setCenter(new GLatLng(x,y), zoom);			
		}
	},
    openMap: function() {
    	var mapObj = $('google_map');
	 	var html = '<p class="routetitle" style="text-align: left;">' + jsLabels.txtFrom + '</p>' +
			'<form action="javascript:mapHandler.getDirections()">' +
			'<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br/>' +
			'<input class="orderbtn" value="' + jsLabels.txtPlanRoute +'" TYPE="SUBMIT" />' +
			jsLabels.txtNoHighway + '<input type="checkbox" name="highways" id="highways" />';    	
    	mapHandler.width = mapObj.style.width;
    	mapHandler.height = mapObj.style.height;
    	mapObj.style.width = '460px';
   	 	mapObj.style.height = '400px';
    	mapObj.style.zIndex = '100';
    	var td = $('sidebar_td');
    	var div = $('directions');
    	var tbar = $('toolbar_td');
    	if (Browser.Version() <= 7) {
    		td.style.display = 'block';
    		div.style.display = 'block';
    		tbar.style.display = 'block';
    	}
    	else
    	{
    		td.style.display = 'table-cell';
    		tbar.style.display = 'table-cell';
    		div.style.display = 'block';
    	}
    	mapHandler.map.checkResize();
    	mapHandler.map.setCenter(mapHandler.marker.getLatLng());	
   		mapHandler.marker.openInfoWindowHtml(html);
	},
	
	closeMap: function() {
   		var mapObj = $('google_map');
    	mapObj.style.width = mapHandler.width;
   	 	mapObj.style.height = mapHandler.height;
    	mapObj.style.zIndex = '100';
    	var td = $('sidebar_td');
    	var div = $('directions');
    	var tbar = $('toolbar_td');
    	td.style.display = 'none';
    	div.style.display = 'none';
    	tbar.style.display = 'none';
    	mapHandler.map.checkResize();
    	mapHandler.map.setCenter(mapHandler.marker.getLatLng());
    	mapHandler.map.closeInfoWindow();
    	mapHandler.gdir.clear();
	},
    createMarker : function(x,y,ptype) {

    	mapHandler.gdir = new GDirections(mapHandler.map,$('directions'));
        var opts = {
                "icon": this.createIcon(ptype ? ptype : 'A.002.025'),
                "clickable": (this.kiosk==1 ? false : true),
                "draggable": false
        };
		var marker = new GMarker( new GLatLng(x,y),opts );
	    GEvent.addListener(marker, "click", mapHandler.openMap);
	   	GEvent.addListener(marker, "infowindowclose", mapHandler.closeMap);
		return marker;
    },
    createIcon: function(img,large) {
    
        var icon = new GIcon();
        if( img!='A.002.005' &&
        	img!='A.002.025' &&
        	img!='A.002.035' &&
        	img!='A.002.050' &&
        	img!='A.002.065' &&
        	img!='A.002.070' &&
        	img!='A.002.075' &&
        	img!='A.002.080' &&
        	img!='A.002.085' &&
        	img!='A.002.090' ) {
        	img='A.002.025';        
        }
        
        icon.image = '/map/img/'+img+'.png';
		if(large) {
	      	icon.shadow = '/map/img/shadow_large.png';
			icon.iconSize = new GSize(50.0, 33.0);
		    icon.shadowSize = new GSize(67.0, 33.0);
		    icon.iconAnchor = new GPoint(25.0, 16.0);
		    icon.infoWindowAnchor = new GPoint(25.0, 16.0);
		}
		else {
	      	icon.shadow = '/map/img/shadow_single.png';
			icon.iconSize = new GSize(28.0, 33.0);
		    icon.shadowSize = new GSize(45.0, 33.0);
		    icon.iconAnchor = new GPoint(14.0, 16.0);
		    icon.infoWindowAnchor = new GPoint(14.0, 16.0);
		}
		return icon;

    },
    getDirections : function(x,y) {
    	var opts = {};
    	/*
        if ($('walk').checked) {
           opts.travelMode = G_TRAVEL_MODE_WALKING;
        }
        */
        if ($('highways').checked) {
           opts.avoidHighways = true;
        }
    	var query = 'from: '+ $('saddr').value +' to: '+ mapHandler.marker.getLatLng() +'';
    	mapHandler.gdir.load(query, opts);
    	//ERROR HANDLING
		var reasons=[];
		reasons[G_GEO_SUCCESS]            = jsLabels.txtSuccess;
		reasons[G_GEO_MISSING_ADDRESS]    = jsLabels.txtMissingAddress;
		reasons[G_GEO_UNKNOWN_ADDRESS]    = jsLabels.txtUnknownAddress;
		reasons[G_GEO_UNAVAILABLE_ADDRESS]= jsLabels.txtUnavailableAddress;
		reasons[G_GEO_BAD_KEY]            = jsLabels.txtBadKey;
		reasons[G_GEO_TOO_MANY_QUERIES]   = jsLabels.txtManyQueries;
		reasons[G_GEO_SERVER_ERROR]       = jsLabels.txtServerError;
		reasons[G_GEO_BAD_REQUEST]        = jsLabels.txtBadRequest;
		reasons[G_GEO_MISSING_QUERY]      = jsLabels.txtMissingQuery;
		reasons[G_GEO_UNKNOWN_DIRECTIONS] = jsLabels.txtUnknownDirections;
		GEvent.addListener(mapHandler.gdir, "error", function() {
			var code = mapHandler.gdir.getStatus().code;
			var reason="Code "+code;
			if (reasons[code]) { 
				reason = reasons[code]
			}
			alert(jsLabels.txtFailed+reason);
		});
    }
}
