// this file holds all of the functions to draw the spiral menu, and its clickable functions.

freevolution.prototype.extend(
	{
		// click handling:
		clickcursor:function (){
			this.undisplay_action();
			if(document.getElementById('water-'+this.cursorover.id+'-0')){
				this.clickedtile=document.getElementById('water-'+this.cursorover.id+'-0');
			} else {
				this.clickedtile=this.cursorover;
			}
			var url = "../xml/actions.php?id="+this.clickedtile.id;
			myAjax.addRequest(url, 'GET', handleActionfetch,this);	
			return true;
		},
		
		handleActionfetch:function (xml) {
			var usespiral=getsetting(this.quality,'menu_spiral');
			var usegrow=getsetting(this.quality,'menu_grow');
			// first add the tiles.
			var actions=xml.getElementsByTagName("action");
			var actionscount=actions.length;
			if(actionscount==1){
				this.display_action(clickedtile.id,actions[0].attributes[0].nodeValue,
					actions[0].attributes[1].nodeValue,
					-50,
					25
				);
			} else {
				var menubottomspace=(parseInt(this.Objviewport.style.height)-(parseInt(this.clickedtile.style.top)+parseInt(this.map.style.top)));
				var menutopspace=(parseInt(this.clickedtile.style.top)+parseInt(this.map.style.top));
				var menurightspace=(parseInt(this.Objviewport.style.width)-(parseInt(this.clickedtile.style.left)+parseInt(this.map.style.left)))-40; // added button width:40
				var menuleftspace=(parseInt(this.clickedtile.style.left)+parseInt(this.map.style.left)); // added button width:40
				
				var distanceperitem=15;
				var totaldistance=distanceperitem*actionscount;
				// check corners first!
				if(totaldistance>menurightspace && totaldistance>menutopspace){
					var gooddegreesright=Math.asin(menurightspace / totaldistance)/(Math.PI/180);
					var gooddegreestop=Math.asin(menutopspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(90+gooddegreesright); // 0 / 90 / 270
					var circle=90+gooddegreesright+gooddegreestop;
					var degrees=circle/actionscount;
				} else if(totaldistance>menuleftspace && totaldistance>menutopspace){
					var gooddegreesleft=Math.asin(menuleftspace / totaldistance)/(Math.PI/180);
					var gooddegreestop=Math.asin(menutopspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(0+gooddegreestop); // 270 / 180 
					var circle=90+gooddegreesleft+gooddegreestop;
					var degrees=circle/actionscount;
				} else if(totaldistance>menurightspace && totaldistance>menubottomspace){
					var gooddegreesright=Math.asin(menurightspace / totaldistance)/(Math.PI/180);
					var gooddegreesbottom=Math.asin(menubottomspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(180+gooddegreesright); 
					var circle=90+gooddegreesright+gooddegreesbottom-10;
					var degrees=circle/actionscount;
				} else if(totaldistance>menuleftspace && totaldistance>menubottomspace){
					var gooddegreesleft=Math.asin(menuleftspace / totaldistance)/(Math.PI/180);
					var gooddegreesbottom=Math.asin(menubottomspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(270+gooddegreesbottom); 
					var circle=90+gooddegreesleft+gooddegreesbottom-10;
					var degrees=circle/actionscount;
				} else if(totaldistance>menubottomspace){ // check to see if theres enough space below
					// ac=viewport['bottom']-idx;
					var gooddegrees=Math.asin(menubottomspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(270+gooddegrees); // why do we need the 115?
					var circle=360-2*(90-gooddegrees);
					var degrees=circle/actionscount;
				} else if(totaldistance>menutopspace){
					// ac=viewport['bottom']-idx;
					var gooddegrees=Math.asin(menutopspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(90+gooddegrees); 
					var circle=360-2*(90-gooddegrees);
					var degrees=circle/actionscount;	
				} else if(totaldistance>menurightspace){
					var gooddegrees=Math.asin(menurightspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(180+gooddegrees); 
					var circle=360-2*(90-gooddegrees);
					var degrees=circle/actionscount;	
				} else if(totaldistance>menuleftspace){
					var gooddegrees=Math.asin(menuleftspace / totaldistance)/(Math.PI/180);
					var degreestart=25-(0+gooddegrees); 
					var circle=360-2*(90-gooddegrees);
					var degrees=circle/actionscount;	
				} else {
					var degreestart=0;
					var degrees=360/actionscount; // amount of degrees each button should be taking up
				}
				if(usegrow){
					var distance=actionscount*1; // R distance from the cursor should be 20 pixels for each added button
				} else {
					var distance=totaldistance; // R distance from the cursor should be 20 pixels for each added button
				}
				if(usespiral){
					degreestart=degreestart-30; // offset for the spiraling sideways motion.
				}
				var buttons=new Array;
				for(var i=0;i<actionscount;i++){
					degree=degrees*i;
					// radian=degree*Math.PI/180 // cos javascript uses radians
					dx = distance*Math.cos((degreestart+degree)*Math.PI/180);
					dy = distance*Math.sin((degreestart+degree)*Math.PI/180);
					j=i+1;
					buttons[i]=this.display_action(this.clickedtile.id,actions[i].attributes[0].nodeValue,
							actions[i].attributes[1].nodeValue,
							dx,
							dy,
							j);
				}
				if(usegrow){
					for(var i=0;i<actionscount;i++){
						for(var j=2;j<distanceperitem;j++){
							if(usespiral){
								degree=degrees*i+(360/j);
							} else {
								degree=degrees*i;
							}
							var distance=actionscount*j;
							dx = distance*Math.cos((degreestart+degree)*Math.PI/180);
							dy = distance*Math.sin((degreestart+degree)*Math.PI/180);
							this.menutimers[this.menutimers.length]=setTimeout("movebutton('"+this.clickedtile.id+"','"+buttons[i]+"',"+dx+","+dy+");",j*50);
						}
					}
				}
			}
		
			var button=document.createElement("input");
			button.style.position="absolute";
			button.style.top=parseInt(this.clickedtile.style.top)+5+"px";
			button.style.left=this.clickedtile.style.left; // no parseInt, its a string, and should stay a string.
			button.style.zIndex=301;
			button.type="button";
			button.id='circlemenu_action_'+(actionscount+1);
			button.className="circlemenubutton";
			button.name="circlemenubutton";
			button.value="Close";
			button.onclick=attachEventHandler(this, "undisplay_action");
			this.viewportgui.appendChild(button);
		},

		movebutton:function (parent,id,dx,dy){
			var obj=document.getElementById(id);
			var parent=document.getElementById(parent);	
			obj.style.top=parseInt(parent.style.top)+dx+"px";
			obj.style.left=parseInt(parent.style.left)+dy+"px";
		},
	
		display_action:function (parent,value,action,x,y,i){	
			parent = document.getElementById(parent);
			action ='buttonaction_'+action;	
			
			// create objects relative to the parents position;
			var button=document.createElement("input");
			button.id='circlemenu_action_'+i;
			button.style.position="absolute";
			button.style.top=parseInt(parent.style.top)+x+"px";
			button.style.left=parseInt(parent.style.left)+y+"px";
			button.style.zIndex=600;	
			button.type="button"
			button.className="circlemenubutton";
			button.name="circlemenubutton";
			button.value=value;
			if(this.usetransparancy){
				setopacity(button,40);
			}
			button.onclick=attachEventHandler(this, action);
			this.viewportgui.appendChild(button);
			return button.id;	
		},
		
		undisplay_action:function (){	
			timerscount=this.menutimers.length; // these are the timeouts which were spawned for the zooming/spiraling of the menu
			for(var i=0;i<timerscount;i++){
				clearTimeout(this.menutimers[i]);
			}
			actions=this.viewportgui.childNodes; 
			count=actions.length;
			for(var i=0;i<count;i++){ // why! this is needed because removing the items is slower than hiding them, giving a better visual experience.
				actions[i].style.display='none';
			}
			while(actions.length>0){
				this.viewportgui.removeChild(actions[0]);
			}
		},

		buttonaction_chat:function (){	
			var  message=prompt('Type thy chat message for tile;');
			if(message){
				var xmlhttp = false;
				xmlhttp=createRequestObject();
				// cook up which range we need to get.
				var url = "xml/sendactions.php?id="+clickedtile+"&action=chat&message="+encodeURIComponent(message);
				
				xmlhttp.open("POST", url, true);
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						var response=xmlhttp.responseXML;				
					}
				}
				xmlhttp.send(null);
				
				if(fetchchangestimer==null){ // if the updater is not initialised yet, lets do so now!
					fetchchangestimer=fetchchanges();
				}		
				return true;		
			}
			undisplay_action();
			
		},
		buttonaction_walk:function (event){
			alert('Where to walk? (not supported yet);');
		}
	}
);