var DEBUG = true;

/*
 * Marker object to initialize and show a marker on a map. 
 */
function Marker(position,icon,zindex,num,drag) {
	if(num == null) num = type.DEFAULT;
	this.SetType(num);
	if(icon == null) icon = this.GenerateIcon();
	var args = new Array();
	if(position) args.push(new GLatLng(position.latitude,position.longitude));
	else args.push(new GLatLng(0,0));
	if(drag == null || !drag) drag = false;
	else drag = true;
	if(zindex != null) args.push({"icon":icon, "zIndexProcess": function(lat) { return zindex; }, "draggable":drag});
	else args.push({"icon":icon,"draggable":drag});
	GMarker.apply(this,args);
}

/*
 * Extending the GMarker interface.
 */
Marker.prototype = new GMarker(new GLatLng(0,0));

var type = {"DEFAULT":0,
		"QUEST_CLUSTER":1,
		"QUEST":2,
		"PHOTO_CUBE":3,
		"WAYPOINT":4,
		"PLAYER":5,
		"MISSION_DETAILS":6,
		"GOAL":7,
		"CHECKPOINT":8,
		"MISSION_ENTRY":9,
		"BLUE_PLAYER":10,
		"TRACK_POINT":11,
		"ITEM":12,
		"NEW_ITEM":13,
		"NPC":14};

/*
 * Marker properties.
 */
Marker.prototype.map = null;//pointer to map containing this marker
Marker.prototype.type = type.DEFAULT;//type of marker
Marker.prototype.infoBox = null;//info box for this waymark

//properties used in missionmap.tag
Marker.prototype.questid = -1;
Marker.prototype.processed = false;
Marker.prototype.polyline = null;

/*
 * Marker methods.
 */
Marker.prototype.initialize = function(map) {
	GMarker.prototype.initialize.call(this,map);
	this.map = map;
}

Marker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.call(this,this.map);
}

Marker.prototype.remove = function() {
	GMarker.prototype.remove.call(this);
}

Marker.prototype.GenerateIcon = function(data) {
	switch(this.type) {
	case type.PHOTO_CUBE: return this.GetPhotoIcon();
	case type.WAYPOINT: return this.GetWaypointIcon();
	case type.PLAYER: return this.GetPlayerIcon();
	case type.MISSION_ENTRY:
	case type.QUEST: return this.GetQuestIcon(data);
	case type.QUEST_CLUSTER: return this.GetQuestClusterIcon(data);
	case type.GOAL: return this.GetGoalIcon();
	case type.CHECKPOINT: return this.GetCheckpointIcon();
	case type.BLUE_PLAYER: return this.GetBluePlayerIcon();
	case type.TRACK_POINT: return this.GetTrackPointIcon();
	case type.ITEM: return this.GetCheckpointIcon();
	case type.NEW_ITEM: return this.GetGoalIcon();
	case type.NPC: return this.GetCheckpointIcon();
	default: return this.GetDefaultIcon();
	}
}

Marker.prototype.SetType = function(num) {
	this.type = num;
}

Marker.prototype.CreateInfoBox = function(data) {
	var options = null;
	switch(this.type) {
	case type.QUEST: options = this.GetQuestInfoBoxOptions(data);
		break;
	case type.QUEST_CLUSTER: options = this.GetQuestClusterInfoBoxOptions(data);
		break;
	case type.PLAYER: options = this.GetPlayerInfoBoxOptions(data);
		break;
	case type.PHOTO_CUBE: options = this.GetPhotoInfoBoxOptions(data);
		break;
	case type.BLUE_PLAYER: options = this.GetBluePlayerOptions(data);
		break;
	case type.MISSION_DETAILS:
	case type.MISSION_ENTRY:
	case type.CHECKPOINT:
	case type.GOAL:
		options = this.GetMissionDetailsInfoBoxOptions(data);
		break;
	case type.ITEM:
		options = this.GetItemOptions(data);
		break;
	case type.NPC:
		options = this.GetNpcOptions(data);
		break;
	}
	if(options == null) return;
	if(this.infoBox == null) this.infoBox = new InfoBox(this.getLatLng(),options);
    else this.infoBox.setContent(options);
}

Marker.prototype.GetBluePLayerOptions = function(data) {
	//TODO
	return null;
}

Marker.prototype.GetQuestInfoBoxOptions = function(data) {
    var infoBoxOptions = { 
		"content": "", 
		"title": "<b style='font-size:small'>"+data.name+"</b>", 
		"imgurl": BASE_URL+"/media/"+data.image+"?maxWidth=100&maxHeight=75&pad=e5e5e5", 
		"link": BASE_URL+"/mission/"+data.id, 
		"text": data.text, 
		"offsetHorizontal": 23, 
		"offsetVertical": 23 , 
		"imgWidth": 100, 
		"imgHeight": 75,
		"show": false
		};
    return infoBoxOptions;
}

Marker.prototype.GetPhotoInfoBoxOptions = function(data) {
	var infoBoxOptions = {
	    "content": "",
	    "title": "<b style='font-size:small;'>"+data.date+", </b><b style='font-size:x-small;'>"+data.time+"</b> <br/> by "+data.author,
	    "imgurl":data.url+"?maxWidth=100&maxHeight=75&pad=E5E5E5",
	    "text":"",
	    "imgWidth": 100,
	    "imgHeight": 75,
	    "offsetHorizontal": 23,
	    "offsetVertical": 10 
	};
	return infoBoxOptions;
}

Marker.prototype.GetMissionDetailsInfoBoxOptions = function(data) {
	var infoBoxOptions = {
		"title": "<b style='font-size:small';>"+data.title+"</b>",
		"imgurl": data.img+"?maxWidth=100&maxHeight=75&pad=e5e5e5",
		"text": data.text,
		"offsetHorizontal": 23,
		"offsetVertical": 23,
		"imgWidth": 100,
		"imgHeight": 75
	};
	return infoBoxOptions;
}

Marker.prototype.GetQuestClusterInfoBoxOptions = function(data) {
    var infoBoxOptions = { 
		"content": "", 
		"title": "<b style='font-size:small;'>"+data.count + " Missions</b>", 
		"imgurl": "", 
		"link": "", 
		"text": data.liststring, 
		"offsetHorizontal": 23, 
		"offsetVertical": 23, 
		"imgWidth": 0, 
		"imgHeight": 0 
	}; 
	if(this.infoBox == null) this.infoBox = new InfoBox(this.getLatLng(),infoBoxOptions);
	this.infoBox.questbounds = new GLatLngBounds(data.sw,data.ne);
	return infoBoxOptions;
}

Marker.prototype.GetPlayerInfoBoxOptions = function(data) {
	var infoBoxOptions = {
	    "content": "",
	    "title": "<b style='font-size:small'>Player: "+data.name+"</b>",
	    "imgurl": BASE_URL+"/media/"+data.media+"?maxWidth=100&maxHeight=75&pad=e5e5e5",
	    "text": "Was here: "+data.lastseen,
	    "offsetHorizontal": this.GenerateIcon().iconSize.width,
	    "offsetVertical": this.GenerateIcon().iconSize.height,
	    "imgWidth": 100,
	    "imgHeight": 75
	};
	return infoBoxOptions;
}

Marker.prototype.GetItemOptions = function(data) {
	var infoBoxOptions = {
	    "title": "<b style='font-size:small'>"+decodeURIComponent(data.name)+"</b> X "+data.count+"<br />Type: "+data.type
	    	+"<br /><a href='javascript:DeleteMe("+data.id+")'>Delete</a>",
	    "imgurl": BASE_URL+"/media/"+data.media+"?maxWidth=100&maxHeight=75&pad=e5e5e5",
	    "text": decodeURIComponent(data.description),
	    "offsetHorizontal": 20,
	    "offsetVertical": 10,
	    "imgWidth": 100,
	    "imgHeight": 75
	};
	return infoBoxOptions;
}

Marker.prototype.GetNpcOptions = function(data) {
	var infoBoxOptions = {
	    "title": "<b style='font-size:small'>"+unescape(decodeURIComponent(data.name))+"</b><br />Class: "+data.cc
	    	+"<br /><a href='javascript:DeleteObject("+data.id+")'>Delete</a><br />",
	    "imgurl": BASE_URL+"/media/"+data.media+"?maxWidth=100&maxHeight=75&pad=e5e5e5",
	    "text": unescape(decodeURIComponent(data.description)),
	    "offsetHorizontal": 20,
	    "offsetVertical": 10,
	    "imgWidth": 100,
	    "imgHeight": 75
	};
	return infoBoxOptions;
}

Marker.prototype.SetMissionMapData = function(id,processed) {
	this.questid = id;
	this.processed = processed;
}

Marker.prototype.SetMarkerCallback = function () {
	var infoBox = this.infoBox;
	var polyline = this.polyline;
	this.eventhandle = GEvent.addListener(this,"click",function() { ClickHandler(infoBox); });
	if (this.type == type.QUEST_CLUSTER) {
//		GEvent.addListener(this, "mouseover", function() {
//			//if (map.borderpoly)
//			//	map.removeOverlay(map.borderpoly);
//			//map.borderpoly = polyline;
//			//map.addOverlay(map.borderpoly);
//		});
//		GEvent.addListener(this, "mouseout", function() {
//			//map.removeOverlay(polyline);
//			//if (map.borderpoly == polyline)
//			//	map.borderpoly = null;
//			});
		
	}
}

function ClickHandler(infoBox) {
	if(infoBox && map) {
	  	if(map.infoBox) 
	  		map.removeOverlay(map.infoBox);
 		map.infoBox = infoBox;
 		map.addOverlay(map.infoBox);
 		GEvent.addDomListener(document.getElementById("closebutton"),"click",function() {
 				if (map.infoBox)
 					map.removeOverlay(map.infoBox);
 		});	 
	}
}

Marker.prototype.GetPhotoIcon = function() {
	var photoCube = new GIcon(G_DEFAULT_ICON);
	photoCube.image = BASE_URL+'/gfx/gps-mission/icons/mapmarkers/photospot.png';
	photoCube.iconSize = new GSize(47,47);
	photoCube.iconAnchor = new GPoint(0,50);
	photoCube.imageMap = [0,46,3,32,10,19,18,5,34,5,40,14,35,31,19,39,7,43];
	return photoCube;
}

Marker.prototype.GetWaypointIcon = function() {
	var wayPoint = new GIcon(G_DEFAULT_ICON,BASE_URL+'/gfx/mark_01.png');
	wayPoint.iconSize = new GSize(20, 20);
	wayPoint.iconAnchor = new GPoint(10,10);
	wayPoint.shadowSize = new GSize(0, 0);
	return wayPoint;
}

Marker.prototype.GetPlayerIcon = function() {
	var iconPlayer = new GIcon(G_DEFAULT_ICON,BASE_URL+'/gfx/gps-mission/icon_player.png');
	iconPlayer.shadow = '';
	iconPlayer.iconSize = new GSize(23, 23);
	iconPlayer.iconAnchor = new GPoint(12,12);
	iconPlayer.infoWindowAnchor = new GPoint(12,4);
	iconPlayer.imageMap = [0,0,23,0,23,23,0,23];
	return iconPlayer;
}

Marker.prototype.GetBluePlayerIcon = function() {
	var iconPlayer = new GIcon(G_DEFAULT_ICON,BASE_URL+'/image/icon_player_new.png');
	iconPlayer.shadow = '';
	iconPlayer.iconSize = new GSize(40,40);
	iconPlayer.iconAnchor = new GPoint(0,40);
	iconPlayer.shadowSize = new GSize(0,0);
	iconPlayer.infoWindowAnchor = new GPoint(12,4);
	iconPlayer.imageMap = [25,0,35,5,40,15,35,25,25,28,15,34,0,40,5,25,10,13,15,5];
	return iconPlayer;
}

Marker.prototype.GetQuestIcon = function(data) {
	var url = BASE_URL+'/gfx/gps-mission/icons/mapmarkers/mission.png';
	var wayPoint = null;
	if(data != null && data.region != null && (data.region == "US" || data.region == "NL"
		|| data.region == "GB" || data.region == "DE")) {
		url = BASE_URL+'/gfx/gps-mission/icons/mapmarkers/wappen_home_'+data.region.toLowerCase()+'.png';
		wayPoint = new GIcon(G_DEFAULT_ICON,url);
		wayPoint.iconSize = new GSize(23,23);
		wayPoint.iconAnchor = new GPoint(10,10);
		wayPoint.shadowSize = new GSize(0,0);
		wayPoint.imageMap = [11,0,17,3,21,6,23,11,21,18,15,22,11,23,4,21,2,18,0,11,2,5];
	} else {
		wayPoint = new GIcon(G_DEFAULT_ICON,url);
		wayPoint.iconSize = new GSize(47,47);
		wayPoint.shadowSize = new GSize(0,0);
		wayPoint.imageMap = [0,46,3,32,10,19,18,5,34,5,40,14,35,31,19,39,7,43];
	}
	return wayPoint;
}

Marker.prototype.GetQuestClusterIcon = function(data) {
	var url = BASE_URL+'/gfx/gps-mission/icons/mapmarkers/mission_multiple.png';
	var wayPoint = null;
	if(data != null && data.region != null && (data.region == "US" || data.region == "NL"
		|| data.region == "GB" || data.region == "DE")) {
		url = BASE_URL+'/gfx/gps-mission/icons/mapmarkers/wappen_cluster_'+data.region.toLowerCase()+'.png';
		wayPoint = new GIcon(G_DEFAULT_ICON,url);
		wayPoint.iconSize = new GSize(23,23);
		wayPoint.iconAnchor = new GPoint(10,10);
		wayPoint.shadowSize = new GSize(0,0);
		wayPoint.imageMap = [11,0,17,3,21,6,23,11,21,18,15,22,11,23,4,21,2,18,0,11,2,5];
	} else {
		wayPoint = new GIcon(G_DEFAULT_ICON,url);
		wayPoint.iconSize = new GSize(50,50);
		wayPoint.shadowSize = new GSize(0,0);
		wayPoint.imageMap = [24,46,9,40,4,29,4,19,7,12,14,5,24,3,33,5,39,9,45,17,45,33,39,40,31,45];
	}
	return wayPoint;
}

Marker.prototype.GetGoalIcon = function() {
	var wayPoint = new GIcon(G_DEFAULT_ICON,BASE_URL+'/gfx/gps-mission/icons/mapmarkers/finish.png');
	wayPoint.iconSize = new GSize(47,47);
	wayPoint.iconAnchor = new GPoint(0,50);
	wayPoint.imageMap = [0,46,3,32,10,19,18,5,34,5,40,14,35,31,19,39,7,43];
	return wayPoint;
}

Marker.prototype.GetCheckpointIcon = function() {
	var wayPoint = new GIcon(G_DEFAULT_ICON,BASE_URL+'/gfx/gps-mission/icons/mapmarkers/checkpoint.png');
	wayPoint.iconSize = new GSize(47,47);
	wayPoint.iconAnchor = new GPoint(0,50);
	wayPoint.imageMap = [0,46,3,32,10,19,18,5,34,5,40,14,35,31,19,39,7,43];
	return wayPoint;
}

Marker.prototype.GetTrackPointIcon = function() {
	var icon = new GIcon(G_DEFAULT_ICON,BASE_URL+'/image/tracks/track_point_default.png');
	icon.iconSize = new GSize(15,15);
	icon.shadowSize = new GSize(0,0);
	icon.iconAnchor = new GPoint(7,7);
	icon.imageMap = [7,0,13,2,15,7,13,12,7,15,2,13,0,7,2,2];
	return icon;
}

Marker.prototype.GetDefaultIcon = function() {
	return new GIcon(G_DEFAULT_ICON);
}

function Translate(str) {
	if(str.toLowerCase() == "quest")
		return type.QUEST;
	if(str.toLowerCase() == "questcluster")
		return type.QUEST_CLUSTER;
	else
		return type.DEFAULT;
}
