Worldmap = new Object();
Worldmap.modeMap = new Object();
Worldmap.modeRoute = new Object();
Worldmap.modeQuest = new Object();
Worldmap.modePoringsweeper = new Object();
Worldmap.active = Worldmap.modeMap;

// ==================================================================================
// Worldmap
// ==================================================================================

Worldmap.init = function()
{	
	astarond.init();
	
	this.modeMap.mapinfo_hover = document.getElementById('mapinfo_hover');
	
	var sortedMapinfo = new Array();
	
	for (var i in mapinfo)
	{
		if (MapDesc[i] != undefined && mapinfo[i][0] != '~')
		{
			sortedMapinfo.push(new Array(mapinfo[i][0], i));
		}		

		var div = document.getElementById('ID' +i);
		if (div != undefined)
		{
			div.style.textAlign = 'left';
			
			div.onclick = function()
			{
				Worldmap.active.mapClicked(this.id.substr(2));
			}
			div.onmouseover = function()
			{
				Worldmap.active.mapHover(this.id.substr(2));
			}
			div.onmouseout = function()
			{
				Worldmap.active.mapHoverOut(this.id.substr(2));
			}
		}
	}
	
	var mapSelect = document.getElementsByName('mapID');
	var option, option2;
		
	sortedMapinfo.sort();
	for (var i in sortedMapinfo)
	{
		var mapID = sortedMapinfo[i][1];
		option = document.createElement('option');
		option.value = mapID;
		option.innerHTML = mapinfo[mapID][1].replace(/<br>/, ", ") + " [" +mapinfo[mapID][0]+ "]";
		
		if (MapDesc[mapID].substr(0, 2) != 'ja')
			option.className = "select_NIY";
										
		option2 = option.cloneNode(true);
		option2.innerHTML = mapinfo[mapID][0];
		
		mapSelect[0].appendChild(option2);
		mapSelect[1].appendChild(option);
	}
	this.active.init();	
}
Worldmap.changeMode = function(modeID)
{
	this.active.exit();
	switch (modeID)
	{
		case 0:
		// Worldmap mode
			if (this.active == this.modeMap)
				return;
			this.active = this.modeMap;
			break;
		case 1:
		// ROutes mode
			if (this.active == this.modeRoute)
				return;
			this.active = this.modeRoute;
			break;
		case 2:
		// Quest mode
			if (this.active == this.modeQuest)
				return;
			this.active = this.modeQuest;
			break;
		case 3:
		// PoringSweeper mode
			if (this.active == this.modePoringsweeper)
				return;
			this.active = this.modePoringsweeper;
			break;
	}
	this.active.init();
}

Worldmap.showElement = function(objID)
{
	if (document.getElementById(objID) == null)
		return;
	document.getElementById(objID).style.display = 'block';
}

Worldmap.hideElement = function(objID)
{
	if (document.getElementById(objID) == null)
		return;
	document.getElementById(objID).style.display = 'none';
}

Worldmap.toggleElement = function(objID)
{
	if (document.getElementById(objID) == null)
		return;
	document.getElementById(objID).style.display = (document.getElementById(objID).style.display == 'none' ? 'block' : 'none');
}

// ==================================================================================
// modeMap 
// ==================================================================================
Worldmap.modeMap.init = function()
{
	Worldmap.hideElement('routes');
	Worldmap.hideElement('quests');
	document.getElementById('ChangeMode').href = "javascript:Worldmap.changeMode(1)";
}

Worldmap.modeMap.exit = function()
{
	this.mapinfo_hover.style.display = 'none';
}

Worldmap.modeMap.mapClicked = function(mapID)
{
	popMap('World(' +mapID+ ').rc');
}

Worldmap.modeMap.mapHover = function(mapID)
{
	var monsterarray = MapDesc[mapID].split("|");
	var placement = monsterarray[0].split(";");
	var mapdesc = "Mapname: " + mapinfo[mapID][0] + "<br />";
	var ingame = placement[0];
	
	for (i = 1; i < monsterarray.length-1; i++)
	{
		monsterinfo = monsterarray[i].split(";");
		if (i == 1)
			mapdesc += "<br />Monsters:<br />";
		else
			mapdesc += "<br />";
	
		if (monsterinfo[1] == -1)
			monsterinfo[1] = "??";
		else if (monsterinfo[1] < 10)
			monsterinfo[1] = '0' + monsterinfo[1].toString();
			
		mapdesc += "" + monsterinfo[1] + " <a href=\"Monster(" + monsterinfo[0] + ").rc\"";
		if (monsterinfo[4] != null && monsterinfo[4] == 'a')
			mapdesc += "class=\"agro\"";
		mapdesc += ">";
		mapdesc += monsterinfo[2] + "</a>";
		if (monsterinfo[3] > 0)
		{
			mapdesc += " (" + monsterinfo[3];
			if (monsterinfo[3] > 1)
				mapdesc += " minutes)";
			else
				mapdesc += " minute)";
		}
	}
	
	if (placement[1] != '')
	{
		var num_maps = (placement.length - 2)/2;
		mapdesc += "<br /><br />Dungeon:<br />";
	}
	for (i = 0; i < num_maps; i++)
	{
		mapdesc += "<a href='#" +placement[i+1]+ "'>" +placement[i+1+(num_maps)]+ "</a><br />";
	}
	
	if (ingame == "nein")
		mapdesc += "<p class=\"NIY\">Not implemented yet</p>";
	
	document.getElementById('hover_maptitle').innerHTML = mapinfo[mapID][1];
	document.getElementById('hover_mapdesc').innerHTML = mapdesc;
	
	this.positionHoverDiv(mapID);
}

Worldmap.modeMap.positionHoverDiv = function(mapID)
{
  	var mapdiv = document.getElementById('ID' +mapID);
  	
  	var styleLeft = parseInt(mapdiv.style.left);
 	var styleTop = parseInt(mapdiv.style.top);
 	var styleWidth, styleHeight;	
 	if (mapdiv.parentNode.id == 'dungeon')
 	{
 		styleWidth = styleHeight = 47;
 	}
 	else
 	{
 		styleWidth = parseInt(mapdiv.style.width);
 		styleHeight = parseInt(mapdiv.style.height);
 	}
 	  	
  	this.mapinfo_hover.style.left = (styleLeft + (styleLeft > 760 ? -200 : styleHeight - 15)) + "px";
  	this.mapinfo_hover.style.top = (styleTop + (styleTop > 636 && mapdiv.parentNode.id != 'dungeon' ? -140 : styleHeight - (styleTop > 1440 && styleLeft > 250 ? 190 : 15))) + "px";
  	
  	mapdiv.parentNode.appendChild(this.mapinfo_hover);
	this.mapinfo_hover.style.display = 'block';	
}

Worldmap.modeMap.mapHoverOut = function(mapID)
{
		
}

// ==================================================================================
// modeRoute
// ==================================================================================

Worldmap.modeRoute.init = function()
{
	Worldmap.showElement('routes');
	document.getElementById('ChangeMode').href = "javascript:Worldmap.changeMode(0)";
}

Worldmap.modeRoute.exit = function()
{
	
}

Worldmap.modeRoute.mapClicked = function(mapID)
{
	astarond.openMap(mapID);
}

Worldmap.modeRoute.mapHover = function(mapID)
{

}

Worldmap.modeRoute.mapHoverOut = function(mapID)
{

}

// ==================================================================================
// modeQuests
// ==================================================================================

Worldmap.modeQuest.init = function()
{
	Worldmap.showElement('quests');
	Worldmap.hideElement('routeMapSelect');
}

Worldmap.modeQuest.exit = function()
{
	Worldmap.hideElement('quests');
	Worldmap.showElement('routeMapSelect');
}

Worldmap.modeQuest.mapClicked = function(mapID)
{

}

Worldmap.modeQuest.mapHover = function(mapID)
{

}

Worldmap.modeQuest.mapHoverOut = function(mapID)
{

}


// ==================================================================================
// modePoringsweeper
// ==================================================================================

Worldmap.modePoringsweeper.init = function()
{
	Worldmap.showElement('poringsweeper');
	Worldmap.showElement('sweepercontrast');
}

Worldmap.modePoringsweeper.exit = function()
{
	Worldmap.hideElement('poringsweeper');
	Worldmap.hideElement('sweepercontrast');
}

Worldmap.modePoringsweeper.mapClicked = function(mapID)
{
	poringsweeper.mapClicked(mapID);
}

Worldmap.modePoringsweeper.mapHover = function(mapID)
{
	poringsweeper.hover(mapID);
}

Worldmap.modePoringsweeper.mapHoverOut = function(mapID)
{
	
}

// ==================================================================================
// Global Functions
// ==================================================================================

function MM_preloadImages()
{
	var d=document;
	if(d.images)
	{
		if(!d.MM_p)
  			d.MM_p=new Array();
    	var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
    	for(i=0; i<a.length; i++)
    		if (a[i].indexOf("#")!=0)
    		{
    			d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
    		}
	}
}

function showdiv(objID)
{
	displayType = ( document.getElementById(objID).style.display == 'block' ) ? 'none' : 'block';
	document.getElementById(objID).style.display = displayType;
}

function checkbox (obj, class_on, class_off)
{
	if (class_on == undefined)
		class_on = "checkbutton2";
	if (class_off == undefined)
		class_off = "checkbutton";
		
	obj.className = obj.className == class_on ? class_off : class_on;
}

function popMap(URL)
{
	window.open(URL, 'MapPopup', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=670,height=630,left = 412,top = 234');
}

function PopPicture(img)
{
	foto1= new Image();
	foto1.src=(img);
	Kontrolle(img); 
}

function Kontrolle(img)
{
	if((foto1.width!=0) && (foto1.height!=0))
    	viewFoto(img);	
	else
	{
    	funktion="Kontrolle('"+img+"')";
    	intervall=setTimeout(funktion,20);
  	}
}

function viewFoto(img)
{
	weite=foto1.width+20;
	hoehe=foto1.height+20;
	string="width="+weite+",height="+hoehe;
	Fenster=window.open(img,"",string,"resizable=no");
}

 function findMonster(monsterID) 
 {      
      clearMarks(); 
      var pattern = "\|"+monsterID+";"; 
      for (i = 1; i < MapDesc.length; i++) 
      { 
           if (document.getElementById('ID' +i) != undefined) 
           { 
                if (MapDesc[i].indexOf(pattern) != -1) 
                { 
                     addMark(document.getElementById('ID' +i)); 
                } 
           } 
      } 
 } 
 
// ==================================================================================
// Map Marking Functions
// ==================================================================================

var mark_image = document.createElement('img');
mark_image.src = 'http://images.rocards.de/map/worldmap-marked.gif';
mark_image.style.width = '100%';
mark_image.style.height = '100%';

var loading_image = document.createElement('img');
loading_image.src = 'http://images.rocards.de/map/loading.gif';

var mark_start = document.createElement('img');
mark_start.src = 'http://images.rocards.de/map/routes-start.gif';

var mark_dest = document.createElement('img');
mark_dest.src = 'http://images.rocards.de/map/routes-destination.gif';

var mark_images = new Array();

function markMap(mapID, mark_obj, store_object) 
{     
	if (mark_obj == undefined)
 		clearMarks();
 	else if (mark_obj.parentNode != undefined)
 		unmark(mark_obj);
    addMark(document.getElementById('ID' +mapID), mark_obj, store_object);
}

function markMaps()
{
	for (var i = 0; i < markMaps.arguments.length; i++)
	{
		 addMark(document.getElementById('ID' +markMaps.arguments[i]));
	}
}

function addMark(node, mark_obj, store_object)
{
	if (node == undefined)
		return;
		
	node.style.backgroundImage = "url(http://images.rocards.de/map/worldmap-marked2.png)";
	node.style.filter = "alpha(opacity=60);";

	if (mark_obj != undefined)
		temp_mark_image = mark_obj;
	else	
		temp_mark_image = mark_image.cloneNode(false);
	
	temp_mark_image.style.position = 'absolute';
	temp_mark_image.style.x = '0px';
	temp_mark_image.style.y = '0px';
	
	if (store_object == undefined)
		mark_images.push(temp_mark_image);
	else
		store_object.push(temp_mark_image);
		
	node.appendChild(temp_mark_image);
}

function clearMarks(store_object)
{
	if (store_object == undefined)
		store_object = mark_images;
		
	for (var img in store_object)
	{
		unmark(store_object[img]);	
	}
	
	store_object = new Array();	
}

function unmark(markImg)
{
	if (markImg.parentNode == undefined)
		return;
			
	if (markImg.parentNode.style != undefined)
	{
		markImg.parentNode.style.backgroundImage = "url(http://images.rocards.de/blank.gif)";
	}
	markImg.parentNode.removeChild(markImg);	
}
