
		/* 
		 * Global Variables
		 */

		 
		var tipstate = "";
		var basetip = "";//"Click sinistro: ora successiva<br/>Shift-CTRL-Click sin.: ora precedente<br/>rotellina: zoom in-out"

		var canZoom = true;

		var Zoom = 1.0;			// Image Zoom factor
		var	oldZoom = 1.0;
		var oldleft = 0.0;
		var oldtop = 0.0;
		var	mouseDownPos = {
			X: 0,
			Y: 0
		};
		var	initialMousePos = {
			X: 0,
			Y: 0
		};

		var ffversion;

		var fullSet;


		var origTblHt;
		var imgHt;
		var	imgSiz;

		var enabletip=false;
		var tipobj;
		var pointerobj;
		var	mapObj;
    var	map_headerObj;
    var	map_footerObj;
    var map_headerDiv;
    var map_footerDiv;
    var iframeObj;
    
		var offsetfromcursorX=1;		 	//Customize x offset of tooltip
		var offsetfromcursorY=1;		 	//Customize y offset of tooltip

		var offsetdivfrompointerX=00;	//Customize x offset of tooltip DIV relative to pointer image
		var offsetdivfrompointerY=14;	//Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).


 /***********************
 * initIt()             *
 *                      *
 * Initialise Variables *
 * Build Menus, etc     *
 ***********************/
function initIt()
{
    var localobj;

		/* There is a bug in FF 1.5 & 2 with DOMMouseScroll - look for ffversion below */
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
			ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		}

		/***********************/
		/* Set Default Options */
		/***********************/

		document.getElementById("theMap").onload = setSize;
		document.getElementById("theMap").onerror = badImage;
		document.getElementById("theBlipspot").onerror = badBlipspot;

		whichBrowser();	// Determine punter's Browser

		mapObj = document.getElementById("theMap");
		map_headerObj = document.getElementById("theMap_header");
		map_footerObj = document.getElementById("theMap_footer");
		map_headerDiv = document.getElementById("map_header");
		map_footerDiv = document.getElementById("map_footer");
		iframeObj = document.getElementById("theBlipspot");
		localobj=mapObj;
		setSize();

		//loadImage(1);	// Assume forwards welcome_windgram.png
		//mapObj.src = "/rasp/welcome_windgram.png";
		mapObj.src = "/rasp/legendaWG.jpg";
		map_headerObj.src = mapObj.src;
		map_footerObj.src = mapObj.src;


		/********************* Material below adapted from *************************************
		* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
		* This notice MUST stay intact for legal use
		* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
		****************************************************************************************/


		document.write('<div id="dhtmltooltip" style="align: left;" onload="ddrivetip(\'Click successivo</br>Shift-Click precedente\')" ></div>') //write out tooltip DIV
		document.write('<img id="dhtmlpointer" src="arrow2.gif" >') //write out pointer image


		if (ie){
			tipobj		 = document.all["dhtmltooltip"];
			pointerobj = document.all["dhtmlpointer"];
		}
		else{
			tipobj		 = document.getElementById("dhtmltooltip") ;
			pointerobj = document.getElementById("dhtmlpointer") ;
		}


		unfreezetip(basetip);

		mapObj.onmousemove = positiontip;
		mapObj.onmouseout	= obscuretip;
		mapObj.onmouseover	= revealtip;
		pointerobj.onmouseover = positiontip;
		tipobj.onmouseover = positiontip;
		
		mapObj.onmousedown = getPos;
		mapObj.onmouseup   = doclick;

		/* Mouse Wheel Initialization code */
		// Note that order is important: Chrome has both onmousewheel and addEventListener

    if(typeof(mapObj.onmousewheel) != 'undefined' )
		  mapObj.onmousewheel = wheel;
	  else if(mapObj.addEventListener)
	    mapObj.addEventListener('DOMMouseScroll', wheel, false);
	  else
	    alert("No mouse wheel interface");

}


/****************************************
 *      END OF INITIALISATION STUFF     *
 *      Start of functions              *
 ****************************************/

/**********************************
* Get Mouse Position on mousedown *
**********************************/
function getPos(E)
{
			if(!E)
				E = window.event;

			initialMousePos.X = mouseDownPos.X = E.clientX;
			initialMousePos.Y = mouseDownPos.Y = E.clientY;
			if(Zoom != 1){
				if(window.addEventListener){
					mapObj.removeEventListener('mousemove', positiontip, false);
					mapObj.addEventListener('mousemove', dragIt, false);
				}
				else{
					mapObj.detachEvent('onmousemove', positiontip);
					mapObj.attachEvent('onmousemove', dragIt);
				}
				if (E.preventDefault)
					E.preventDefault();
				E.returnValue = false;
			}
}


function dragIt(E)
{
			if(!E)
				E = window.event;

			var coords = {
				X: E.clientX,
				Y: E.clientY
			};

			if(ie)
				mapObj.releaseCapture();
			positiontip(E);
			oldleft = oldleft + coords.X - initialMousePos.X;

			oldleft = (oldleft > 0 ? 0 : oldleft); 	// Prevent moving Img too far Right
			

			theSiz = Number(document.getElementById("theMap").style.height.replace(/px/, ""));
			
			if(theSiz < (imgHt - oldleft)){					// Prevent moving Img too far Left
				oldleft = imgHt - theSiz ;
			}
			
			oldtop  = oldtop  + coords.Y - initialMousePos.Y;
			oldtop = (oldtop > 0 ? 0 : oldtop);			// Prevent moving Img too far Down

			if(theSiz < (imgHt - oldtop)){					// Prevent moving Img too far Left
				oldtop = imgHt - theSiz ;
			}
			
			initialMousePos.X = coords.X;
			initialMousePos.Y = coords.Y;
			mapObj.style.left = oldleft + "px";
			mapObj.style.top  = oldtop + "px";
			if (E.preventDefault)
				E.preventDefault();
			E.returnValue = false;
}




function ietruebody()
{
			return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function ddrivetip(thetext, thewidth, thecolor)
{
			if (typeof thewidth != "undefined")
				tipobj.style.width = thewidth+"px"
			if (typeof thecolor != "undefined" && thecolor != "")
				tipobj.style.backgroundColor = thecolor
			tipobj.innerHTML = thetext
			enabletip = true
			return false
}

function positiontip(e)
{
			if(tipstate === "frozen")
				return;
			if (enabletip){
				var nondefaultpos=false
				var curX=(ns) ? e.pageX : event.clientX+ietruebody().scrollLeft;
				var curY=(ns) ? e.pageY : event.clientY+ietruebody().scrollTop;

				//Find out how close the mouse is to the corner of the window
				var winwidth  = ie && !window.opera ? ietruebody().clientWidth : window.innerWidth-20
				var winheight = ie && !window.opera ? ietruebody().clientHeight : window.innerHeight-20

				var rightedge = ie &&!window.opera ? winwidth  - event.clientX - offsetfromcursorX : winwidth - e.clientX - offsetfromcursorX
				var bottomedge= ie &&!window.opera ? winheight - event.clientY - offsetfromcursorY : winheight - e.clientY - offsetfromcursorY

				var leftedge=(offsetfromcursorX<0) ? offsetfromcursorX*(-1) : -1000

				//if the horizontal distance isn't enough to accomodate the width of the context menu
				if (rightedge < tipobj.offsetWidth){
					//move the horizontal position of the menu to the left by it's width
					tipobj.style.left = curX - tipobj.offsetWidth + "px"
					nondefaultpos = true
				}
				else if (curX < leftedge)
					tipobj.style.left = "5px"
				else{
					//position the horizontal position of the menu where the mouse is positioned
					tipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px"
					pointerobj.style.left = curX + offsetfromcursorX + "px"
				}

				//same concept with the vertical position
				if (bottomedge<tipobj.offsetHeight){
					tipobj.style.top = curY - tipobj.offsetHeight - offsetfromcursorY + "px"
					nondefaultpos = true
				}
				else{
					tipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px"
					pointerobj.style.top = curY + offsetfromcursorY + "px"
				}
				tipobj.style.visibility = "visible"
				if (!nondefaultpos)
					pointerobj.style.visibility = "hidden"
			}
}


function obscuretip()
{
			if(tipstate != "frozen")
				hidetip();
}

function unfreezetip(Str)
{
			revealtip();
			tipobj.innerHTML = Str;
			tipobj.onmouseover  = positiontip;
			pointerobj.onmouseover  = positiontip;
			mapObj.onmousemove = positiontip;
			tipstate = "";
}

function freezetip()
{
			if(window.addEventListener){
				mapObj.removeEventListener("mousemove", positiontip, false);
				mapObj.removeEventListener("mouseout", obscuretip, false);
			}
			else{
				mapObj.detachEvent("onmousemove", positiontip);
				mapObj.detachEvent("onmouseout", obscuretip);
			}
			pointerobj.onmouseover = "";
			tipobj.onmouseover = "";
			tipstate = "frozen";
}

function revealtip()
{
			enabletip=true
			tipobj.style.visibility="visible"
			pointerobj.style.visibility="hidden"
}

function hidetip()
{
			enabletip=false
			tipobj.style.visibility="hidden"
			pointerobj.style.visibility="hidden"
}




/********************************/
/* CallBack for onclick (image) */
/********************************/
function doclick(E)
{
			var buttn;
			var newValue;
			var dirn = 1; // forwards
			var pos = {	// current mouse posn
				X: 0,
				Y: 0
			};
      // if TOPO or WINDGRAM DO NOTHING!
      //if(currMapID.match("topo") ||
      //   currMapID.match("windgram")
      //) return;
      
			if(window.addEventListener){
				mapObj.removeEventListener('mousemove',   dragIt,   false);
				mapObj.removeEventListener('mousemove',     positiontip,  false);
			}
			else{
				mapObj.detachEvent('onmousemove', dragIt);
				mapObj.detachEvent('onmousemove', positiontip);

			}

			// Catch a stupid selection
			if (currMapID === "nope1") {
				return false;
			}

			if(!E)	// IE stupidity
				E = window.event;

			// Grab mouse posn immediately
			pos.X = E.clientX;
			pos.Y = E.clientY;

			/*	buttn	IE		NS (etc?)
			 *  L			1			1
			 *  M			4			2
			 *  R			2			3
			 */
			var LEFT 	= 1;
			var MID  	= 2;
			var RIGHT	= 3;
			if ( ie ) {
				shiftPressed	= event.shiftKey;
				ctrlPressed		= event.ctrlKey;
				altPressed		= event.altKey;
				switch(event.button){
				case 1:	buttn = LEFT;  break;
				case 2: buttn = RIGHT; break;
				case 3:	buttn = MID; 	 break;
				case 4:	buttn = MID;	 break;
				default:
					alert("I don't understand which butoon you pressed!\nButton # = " + event.button);
					break;
				}
			}
			else if (ns || ko || op ) {
				shiftPressed	= E.shiftKey;
				ctrlPressed		= E.ctrlKey;
				altPressed		= E.altKey;
				switch(E.which){
				case 1:	buttn = LEFT; 	break;
				case 2:	buttn = MID;		break;
				case 3: buttn = RIGHT;	break;
				default:	alert("Which button was that?\nE.which = " + E.which); break;
				}				
			}
			else {
				alert("Which Button?\nE.which = " + E.which);
			}
			

			switch (buttn) {
			case 1:
				/**** ORDINARY CLICK -> NEXT TIME IN DAY / DRAG ****/
				if(!shiftPressed && !ctrlPressed){
					// This is a bit kludgy: movement < 5 px == No Movement!
					if( (Zoom == 1) || (Math.abs(mouseDownPos.X - pos.X) < 5 && Math.abs(mouseDownPos.Y - pos.Y) < 5)){
						// No movmt -> NextTimeInDay dayGroup
						//newValue = select_next_radio("timeGroup", 1)
						newValue = select_next_radio("dayGroup", 1)
					}
					//unfreezetip("ore "+newValue+"<br/>"+basetip);
					unfreezetip(newValue+"<br/>"+basetip);
				}

				/**** CTRL-SHIFT -> PREVIOUS TIME IN DAY ****/
				else if(shiftPressed && ctrlPressed){
					//newValue = select_next_radio("timeGroup", -1)
					newValue = select_next_radio("dayGroup", -1)
					dirn = -1; // backwards;
					//unfreezetip("ore "+newValue+"<br/>"+basetip);
					unfreezetip(newValue+"<br/>"+basetip);
					//alert(i);
				}


				break; // Case 1: -> LEFT BUTTON

			
			case 2:	// MID - was Previous time in day: Now Reset Zoom
			Zoom = 1.0;
			setSize();
			break;


			/* NOT NEEDED
			case 3:	// RIGHT - was BLIPspot
			alert("Use Ctrl-LeftClick for Parameter Value\nShift-LeftClick for PopUp");
			break;
			*/

			/* NO default: - We are not interested in any other Buttons! */

			}

			loadImage(dirn);

			if (E.preventDefault)
				E.preventDefault();
			E.returnValue = false;
			return false;		// Defeat normal contextmenu for Firefox
}

var txtPlace = "tip";





/******************/
/* Which Browser? */
/******************/
var ns;
var ko;
var ie;
var op;

function whichBrowser()
{
			// alert("Browser = " + navigator.appName);
			ns = navigator.appName === 'Netscape' ;
			ie = navigator.appName === 'Microsoft Internet Explorer' ;
			ko = navigator.appName === 'Konqueror' ;
			op = navigator.appName === 'Opera' ;
			//alert (navigator.appName);
}


/** Event handler for mouse wheel event */
function wheel(event)
{
			
      var delta = 0;
      
      if (!canZoom) return; // do not zoom un-zoomable maps

			if (!event) /* For IE. */
				event = window.event;
			if (event.wheelDelta) { /* IE/Opera. */
				delta = event.wheelDelta/120;
				//alert("ie case. delta = " + delta);
				/** In Opera 9, delta differs in sign as compared to IE */
				if (window.opera)
					delta = -delta;
			} else if (event.detail) { /** Mozilla case. */
				/*
				 * In Mozilla, sign of delta is different than in IE.
				 * Also, delta is multiple of 3.
				 */
				delta = -event.detail/3;
        //alert("mozilla case. delta = " + delta);
			}
				
			/* Bug in FF < 3
			 * Correct values for clientX appear as screenX 
			 * See http://extjs.com/forum/showthread.php?t=20298
			 */
			if(ns && ffversion < 3){
				var eventCoords = {
					X: event.screenX,
					Y: event.screenY
				};
				//alert("ff version <3 "+event.screenX+" "+event.screenY);
			}
			else if(event.clientX){
				var eventCoords = {
					X: event.clientX,
					Y: event.clientY
				};
				//alert("event.clientX "+event.clientX+" "+event.clientY);
			}

			/* If delta is nonzero, handle it.
			 * delta > 0 if wheel was scrolled up,
			 * delta < 0 if wheel was scrolled down.
			 */

			if (delta) {
				oldZoom = Zoom;
				if (delta < 0){
					Zoom /= 1.05;
				if(Zoom < 1)
					Zoom = 1.0;
				}
				else{
					Zoom *= 1.05;
				}
				//alert("X="+eventCoords.X+" Y="+eventCoords.Y);
        zoomIt(eventCoords);				
			}

			/* Prevent default actions caused by mouse wheel */
			if (event.preventDefault)
				event.preventDefault();
			event.returnValue = false;
}


function zoomIt(coords)
{
			var	mapDiv;
			var	X, Y;
			

			unfreezetip(basetip);

			imgSiz = Number(document.getElementById("imgBox").style.height.replace(/px/, ""));
			imagesize = Number(mapObj.style.width.replace(/px/, ""));
			oldleft = oldleft - 830*(Zoom - oldZoom)/2;
			oldtop = oldtop - 830*(Zoom - oldZoom)/2;
			
			
			if(Zoom == 1){	// Reset to centre posn;
				oldleft = 0;
				oldtop  = 0;
			}
			mapObj.style.left = oldleft + "px";
			mapObj.style.top  = oldtop + "px";
      mapObj.style.height   = Zoom * 830 + "px";
      mapObj.style.width    = Zoom * 830 + "px";
			//alert("W = " + mapObj.style.width + " " + Zoom + " * " + imgSiz);

}


/***************************/
/* Set Initial Image Size  */
/***************************/
function setSize()
{
			var newMap;
			var mapBox;
			var mapDiv;
//alert( "setsize");
			if (Zoom != 1.0)
				return;		// Don't mess with it if zoomed

      imgHt = 830;
      
			oldleft = 0;
			oldtop  = 0;

			mapBox = document.getElementById("imgBox");
			mapBox.style.left     = 0  + "px" ;
			mapBox.style.top      = 0  + "px";
			mapBox.style.height   = imgHt + "px";
			mapBox.style.width    = imgHt + "px";
			mapBox.style.overflow = "hidden" ;

			mapObj.style.height = imgHt + "px";
			mapObj.style.width  = imgHt + "px";
			mapObj.style.left   = 0  + "px";
			mapObj.style.top    = 0  + "px";
			mapObj.style.position = "absolute";

}


/*******************************/
/* CallBack from the selectors */
/*******************************/
function doChange()
{
	if (currMapID =="none") return 0;
	
  //alert ('MapType: '+currMapType+' MapID: '+currMapID+' Day: '+selected_radio('dayGroup')+' Time: '+selected_radio('timeGroup'))
	
  unfreezetip(basetip);

	if (currMapID.match("sounding") ||
      currMapID.match("boxwmax") ||
      currMapID.match("windgram")) {

    Zoom=1;
		oldleft = 0;
		oldtop  = 0;
		mapObj.style.left = oldleft + "px";
		mapObj.style.top  = oldtop + "px";
    mapObj.style.height   = Zoom * 830 + "px";
    mapObj.style.width    = Zoom * 830 + "px";
    //map_headerDiv.style.display = "none";
    map_footerDiv.style.display = "none";
  } else {
    canZoom = true;
    map_headerDiv.style.display = "inline";
    map_footerDiv.style.display = "inline";          
  }

	/* Load images */
	if(currMapID.match("topo") ||
     currMapID.match("windgram")
  ) {
		loadImage(0);
	}
	else {
		loadImage(1); // forwards
	}
}

/************************************/
/* Load the Image                   */
/************************************/

function loadImage(dirn)
{

			var	imgURL;
			var	blipspotURL = "";

      if (currMapType == "windgram") {
					  imgURL =  Server 
							+ "type=WINDGRAMS"
							+ "&par="+currMapID
							+ "&day="+selected_radio("dayGroup")
							+ "&region="+currRegion
							+ "&dummy="+(new Date()).getTime() //Cache Buster
              ;
							//alert(imgURL);
						blipspotURL = imgURL.replace("windgram", "blipspot");
						blipspotURL = blipspotURL.replace("WINDGRAMS", "BLIPSPOTS");
						//blipspotURL = blipspotURL.replace("png", "html");
						//alert(blipspotURL);
					
			} else {
      				imgURL = Server
					    + "type=FCST"
							+ "&par="+currMapID
							+ "&region="+currRegion
							+ "&day="+selected_radio("dayGroup")
							+ "&time="+selected_radio("timeGroup") + "lst.w2"
							+ "&dummy="+(new Date()).getTime() //Cache Buster
              ;
							//alert(imgURL)
			}
			mapObj.src = imgURL;
			map_headerObj.src = imgURL;
			map_footerObj.src = imgURL;
			if (blipspotURL == "") {
         iframeObj.style.display = "none";
      } else {
         iframeObj.src = blipspotURL;
         iframeObj.style.display = "inline";
      }
			
}

/**************************
 * badimage()             *
 * Put up a warning image *
 * if image not found etc *
 **************************/
function badImage()
{
				alert("No Image!");
}

function badBlipspot()
{
  alert("noblipspot!");
	iframeObj.style.display = "none";
}



