// comment
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror')) {
	browser = "Konqueror";
	OS = "Linux";
} else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible')) {
	browser = "Netscape Navigator"
	version = detect.charAt(8);
} else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS) {
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

var onloadFunctions = new Array();

function addOnloadFunction(f) {
	onloadFunctions[onloadFunctions.length] = f;
}

function execOnloadFunctions() {
	for (i = 0; i < onloadFunctions.length; i++) onloadFunctions[i]();
}

var onunloadFunctions = new Array();

function addOnunloadFunction(f) {
	onunloadFunctions[onunloadFunctions.length] = f;
}

function execOnunloadFunctions() {
	for (i = 0; i < onunloadFunctions.length; i++) onunloadFunctions[i]();
}

var compressOpt = 0;
function compressSpaces(element) {
	if (compressOpt == 0) element.value = element.value.replace(/\n[\n ]*\n[\n ]*/gm, '\n\n').replace(/[ ]+/gm, ' ');
	else element.value = element.value.replace(/\n[\n ]+/gm, '\n').replace(/[ ]+/gm, ' ');
	compressOpt++;
}

/********************************************************************/

var idxLineLength = 12;
var idxScrollBorder = 12;
var msgBodyStartOffset = 60;
var msgBodyEndOffset = 40;

var idxMaxHeight = 200;
var idxMinHeight = 50;
var bodiesMaxHeight = 1000;
var bodiesMinHeight = 300;
var idxHRatio = 0.3;
var msgTreeTopSpace = 180;
var msgTreeBottomSpace = 40;
var bodiesScrollable = false;
var bodiesRealMaxHeight = 0;
var bodiesCurrentHeight = 0;
var bodiesExtraSpace = 6;
var indexCurrentHeight = 0;

function scrollIndex(where) {
	if (document.getElementById('mindex') == null) return;
	document.getElementById('mindex').scrollTop = document.getElementById(where).offsetTop - idxScrollBorder;// - (document.all ? 0 : document.getElementById('mindex').offsetTop);
}
function scrollFixed(where, indexTop) {
	if (document.getElementById('bodies') == null) return;
	document.getElementById('bodies').scrollTop = document.getElementById(where).offsetTop - (document.all ? 0 : document.getElementById('bodies').offsetTop);
	scrollMsgWindow(indexTop);
}
var msgWindow = new Array();

function scrollMsgWindow(indexTop) {
	if (browser == "Konqueror") return false;
	if (document.getElementById('mindex') == null) return;
	var winStart = document.getElementById('bodies').scrollTop;
	var winEnd = document.getElementById('bodies').scrollTop + document.getElementById('bodies').offsetHeight;
	var idxSelStart = 0;
	var idxSelEnd = 0;
	for(var i = 0; i < msgWindow.length; i++) {
		var startpos = document.getElementById('m'+msgWindow[i]).offsetTop - (document.all ? 0 : document.getElementById('bodies').offsetTop) + msgBodyStartOffset;
		var endpos = startpos + document.getElementById('m'+msgWindow[i]).offsetHeight - msgBodyStartOffset - msgBodyEndOffset;
		if (document.getElementById('mi'+msgWindow[i]+'_bar')) {
			if (winStart < endpos && winEnd > startpos ) {
				document.getElementById('mi'+msgWindow[i]+'_bar').style.backgroundColor = 'black';
				var selPos = document.getElementById('mi'+msgWindow[i]).offsetTop;
				if (idxSelStart == 0 || selPos < idxSelStart) idxSelStart = selPos;
				if (selPos > idxSelEnd) idxSelEnd = selPos;
			} else {
				if (document.getElementById('mi'+msgWindow[i]+'_bar')!=null) document.getElementById('mi'+msgWindow[i]+'_bar').style.backgroundColor = '#c0c0c0';
			}
		}
	}
	winStart = document.getElementById('mindex').scrollTop;
	winEnd = document.getElementById('mindex').scrollTop + document.getElementById('mindex').offsetHeight;
	if (indexTop) document.getElementById('mindex').scrollTop = (idxSelStart - idxScrollBorder > 0 ? idxSelStart - idxScrollBorder : 0);
	else {
		if (idxSelStart > 0 && winStart > idxSelStart) document.getElementById('mindex').scrollTop = (idxSelStart - idxScrollBorder > 0 ? idxSelStart - idxScrollBorder : 0) + (browser == "Konqueror" ? 400 : 0);
		if (idxSelEnd > 0 && winEnd < (idxSelEnd + idxLineLength)) document.getElementById('mindex').scrollTop = idxSelEnd - document.getElementById('mindex').offsetHeight + idxLineLength + idxScrollBorder + (browser == "Konqueror" ? 400 : 0);
	}
}

function resizeMsgTree() {
	haval = document.body.clientHeight; //window.screen.availHeight
	haval -= msgTreeTopSpace + msgTreeBottomSpace;
	if (haval > bodiesMinHeight + idxMinHeight) {
//		document.getElementById('mindex_box').style.height = document.getElementById('mindex_text').clientHeight;
//		document.getElementById('mindex_box').style.height = 0;
		document.getElementById('mindex_box').style.display = 'block';
//		document.getElementById('mindex').style.height = 0;
//		document.getElementById('mindex').style.display = 'block';

		intBMaxH = document.getElementById('bodies').scrollHeight;
		if (intBMaxH > bodiesMaxHeight) intBMaxH = bodiesMaxHeight;
		if (document.getElementById('mindex')) intIMaxH = document.getElementById('mindex').scrollHeight; else intIMaxH = 0;
		if (intIMaxH > idxMaxHeight) intIMaxH = idxMaxHeight;
		if (intIMaxH < idxMinHeight) intIMaxH = idxMinHeight;
		h = Math.round(haval * idxHRatio);
		bodiesRealMaxHeight = haval - h;
		if (h > intIMaxH) h = intIMaxH;
		if (haval - h < bodiesMinHeight) h = haval - bodiesMinHeight;
		hidxextra = document.getElementById('mindex_text').clientHeight;
		if (document.getElementById('mindex_actionbar')) hidxextra += document.getElementById('mindex_actionbar').clientHeight;
		document.getElementById('mindex_box').style.height = h + hidxextra;
		if (document.getElementById('mindex')) document.getElementById('mindex').style.height = h;
		indexCurrentHeight = h;

		bodiesCurrentHeight = (haval - h < intBMaxH ? haval - h : intBMaxH  + bodiesExtraSpace);
		document.getElementById('bodies').style.height = bodiesCurrentHeight;
		//document.getElementById('mindex').style.display = 'block';
		//document.getElementById('mindex_box').style.display = 'block';
		document.getElementById('bodies').style.display = 'block';
		if (document.getElementById('bodies').scrollHeight > haval - h) {
			document.getElementById('bodies').style.borderTopWidth = 1;
			document.getElementById('bodies').style.borderBottomWidth = 1;
			bodiesScrollable = true;
		} else bodiesScrollable = false;
		//alert(document.getElementById('bodies').scrollHeight+' '+document.getElementById('bodies').style.height+' '+(haval - h));
	} else {
		document.getElementById('bodies').style.height = haval;
		document.getElementById('mindex').style.display = 'none';
		document.getElementById('bodies').style.display = 'block';
	}
}

/********************************************************************/

function loadFormTemplate(dest, id) {
	dest.innerHTML = document.getElementById('form_template').innerHTML.replace(/TEMPLATEID/gim, id);
	return false;
}

var lastprefix;
var lastid;
var lastrootid;
var lastparentid;
var lastdata;
var lasttitle;
var lastbody;
var lastnick;
var lastemail;
var lastpriority;
var lastparentmsgtype;
var lastmsgtype;
var lastskey;
var metimer = 0;

function messageEditLast() {
	return messageEdit(null, lastprefix, lastid, lastrootid, lastparentid, lastdata, lasttitle, lastbody, lastnick, lastemail, lastpriority, lastparentmsgtype, lastmsgtype, lastskey);
}

function messageEdit(obj, prefix, id, rootid, parentid, data, title, body, nick, email, priority, parentmsgtype, msgtype, skey) {
	if (!document.getElementById(prefix+'_formspace')) {
		alert('Message space non found: '+prefix);
		return false;
	}
	if (document.getElementById(prefix+'_formspace').style.display == 'block' && metimer == 0) return false;
	
	if (document.getElementById(prefix+'_formspacesep')) document.getElementById(prefix+'_formspacesep').style.display='block';
	document.getElementById(prefix+'_formspace').style.display='block';
	if (id==0 && rootid==parentid) document.getElementById(prefix+'_formspace_box').style.display='block';
	loadFormTemplate(document.getElementById(prefix+'_formspace'), prefix);
	
	lastprefix = prefix;
	lastid = id;
	lastrootid = rootid;
	lastparentid = parentid;
	lastdata = data;
	lasttitle = title;
	lastbody = body;
	lastnick = nick;
	lastemail = email;
	lastpriority = priority;
	lastparentmsgtype = parentmsgtype;
	lastmsgtype = msgtype;
	lastskey = skey;
	
	if (browser == "Konqueror") {
		if (metimer == 0) metimer = window.setTimeout("messageEditLast()", 500);
		else {
			window.clearTimeout(metimer);
			metimer = 0;
		}
	}

	if (id > 0) document.getElementById(prefix+'form_id').value = id;
	if (rootid > 0) document.getElementById(prefix+'form_rootid').value = rootid;
	if (parentid > 0) document.getElementById(prefix+'form_parentid').value = parentid;
	if (data > 0) document.getElementById(prefix+'form_data').value = data;
	if (title && document.getElementById(prefix+'form_title') != null) document.getElementById(prefix+'form_title').value = title;
	if (body) {
		document.getElementById(prefix+'textarea').value = body;
		if (id == 0) document.getElementById(prefix+'textarea').scrollTop = document.getElementById(prefix+'textarea').scrollHeight;
	}
	if (nick != null) document.getElementById(prefix+'form_nick').value = nick;
	if (email != null) document.getElementById(prefix+'form_useremail').value = email;
	if (priority) document.getElementById(prefix+'form_priority').value = priority;
	if (skey != null) document.getElementById(prefix+'form_skey').value = skey;
	if (browser != "Konqueror" && document.getElementById(prefix+'form_msgtype') != null) {
		if (parentmsgtype) fill_form_msgtype(prefix, parentmsgtype, msgtype);
		else fill_form_msgtype(prefix, -1, -1);
	}

	document.getElementById(prefix+'_msgform').style.display = 'block';
	if (!bodiesScrollable && bodiesRealMaxHeight > 0) {
		if (browser == "Internet Explorer") extra = 45;
		else extra = 0;
		if (bodiesCurrentHeight + document.getElementById(prefix+'_msgform').scrollHeight + extra > bodiesRealMaxHeight) {
		 	bodiesScrollable = true;
		 	bodiesCurrentHeight = bodiesRealMaxHeight;
		} else {
			bodiesCurrentHeight += document.getElementById(prefix+'_msgform').scrollHeight + bodiesExtraSpace + extra;
		}
	 	document.getElementById('bodies').style.height = bodiesCurrentHeight;
		
		if (id==0 && rootid==parentid) document.getElementById(prefix+'_formspace_box').scrollIntoView();
		else document.getElementById(prefix+'_msgform').scrollIntoView();
	}
	
	//document.getElementById(prefix+'form').scrollIntoView();
	if (id == 0 && rootid == parentid && document.getElementById(prefix+'form_title') != null) document.getElementById(prefix+'form_title').focus();
	else document.getElementById(prefix+'textarea').focus();

	//document.getElementById('bodies').scrollTop += document.getElementById(prefix+'_formspace').scrollHeight;

	return false;
}

function hideMessageEditor(prefix) {
	document.getElementById(prefix+'_msgform').style.display = 'none';
	document.getElementById(prefix+'_formspace').style.display = 'none';
	if (document.getElementById(prefix+'form_id').value==0 && document.getElementById(prefix+'form_rootid').value==document.getElementById(prefix+'form_parentid').value) document.getElementById(prefix+'_formspace_box').style.display='none';
}

function messageFormSubmitCheck(prefix) {
	if (document.getElementById(prefix+'form_nick') && document.getElementById(prefix+'form_nick').value.match(/^[ ]*$/) ) {
		alert('Occorre specificare un nick!');
		return false;
	}
	if (document.getElementById(prefix+'textarea') && document.getElementById(prefix+'textarea').value.match(/^[ ]*$/) ) {
		alert('Il messaggio risulta vuoto!');
		return false;
	}
	return true;
}

function clearOptions(oSelect) {
	while (oSelect.options.length > 0) oSelect.options[0] = null;
}

function addOption(oSelect, oValue, oCaption, oSelected) {
	var oOption = document.createElement("OPTION");
	oSelect.options.add(oOption);
	oOption.innerHTML = oCaption;
	oOption.value = oValue;
	oOption.selected = oSelected;
}

/********************************************************************/

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

var edButtons = new Array();
var edLinks = new Array();
var edOpenTags = new Array();

function edButton(id, display, tagStart, tagEnd, open, message1) {
	this.id = id;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.open = open;			// set to -1 if tag does not need to be closed
	this.message1 = message1;
}

function edAddTag(button) {
	if (edButtons[button].tagEnd != '') {
		edOpenTags[edOpenTags.length] = button;
		if (document.getElementById(edButtons[button].id).innerHTML) 
			document.getElementById(edButtons[button].id).innerHTML = '/' + document.getElementById(edButtons[button].id).innerHTML;
		else
			document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value;
	}
}

function edRemoveTag(button) {
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			edOpenTags.splice(i, 1);
			if (document.getElementById(edButtons[button].id).innerHTML) 
				document.getElementById(edButtons[button].id).innerHTML = document.getElementById(edButtons[button].id).innerHTML.replace('/', '');
			else
				document.getElementById(edButtons[button].id).value = document.getElementById(edButtons[button].id).value.replace('/', '');
		}
	}
}

function edCheckOpenTags(button) {
	var tag = 0;
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}	

function edCloseAllTags() {
	var count = edOpenTags.length;
	for (o = 0; o < count; o++) {
		edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
	}
}

function edInsertTag(myField, i) {
	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + edButtons[i].tagStart
			              + myField.value.substring(startPos, endPos) 
			              + edButtons[i].tagEnd
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagStart
				              + myField.value.substring(endPos, myField.value.length);
				edAddTag(i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagEnd
				              + myField.value.substring(endPos, myField.value.length);
				edRemoveTag(i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(i);
		}
		myField.focus();
	}
}

function edInsertContent(myField, myValue) {
	if (document.selection) {
		//IE support
		
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if (myField.selectionStart || myField.selectionStart == '0') {
		//MOZILLA/NETSCAPE support
		
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;

	} else myField.value += myValue;

	myField.focus();
}

function edLowerContent(myField) {
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = sel.text.toLowerCase();

	}	else if (myField.selectionStart || myField.selectionStart == '0') {
		//MOZILLA/NETSCAPE support
		
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		if (startPos != endPos) {
			var scrollTop = myField.scrollTop;
			myField.value = myField.value.substring(0, startPos)
			              + myField.value.substring(startPos, endPos).toLowerCase()
	                  + myField.value.substring(endPos, myField.value.length);
			myField.selectionStart = startPos;
			myField.selectionEnd = endPos;
			myField.focus();
			myField.scrollTop = scrollTop;
		} else myField.value = myField.value.toLowerCase();

	} else myField.value = myField.value.toLowerCase();

	myField.focus();
}

function edInsertLink(myField, i, defaultValue) {
	if (!defaultValue) {
		defaultValue = 'http://';
	}
	if (!edCheckOpenTags(i)) {
		var URL = prompt(edButtons[i].message1 ,defaultValue);
		if (URL) {
			edButtons[i].tagStart = edButtons[i].tagStart + '=' + URL + ']';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

function edInsertImage(myField) {
	var myValue = prompt('Enter the URL of the image', 'http://');
	if (myValue) {
		myValue = '<img src="' 
				+ myValue 
				+ '" alt="' + prompt('Enter a description of the image', '') 
				+ '" />';
		edInsertContent(myField, myValue);
	}
}

/********************************************************************/

var map = null;
var marker = null;
var lookingForCoord = false;
var gmapDefLat = 41.903299;
var gmapDefLng = 12.481842;
//Zoom da 0, 16 ok, 20 molto vicino
var gmapDefZoom = 10;

function editorLocationGMapLoad() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));

    //map.addControl(new GSmallZoomControl());
    //map.addControl(new GSmallMapControl());
    map.addControl(new GLargeMapControl());
    //map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());
    //map.addControl(new GOverviewMapControl());

    var point = new GLatLng(gmapStartLat > 0 ? gmapStartLat : gmapDefLat, gmapStartLng > 0 ? gmapStartLng : gmapDefLng);
    
    // Imposta il centro 
    map.setCenter(point, 15);
    
    // Si muove verso un punto
    //map.panTo(new GLatLng(37.4569, -122.1569))
    
    // Aggiunge una infowin
    //map.openInfoWindow(map.getCenter(), document.createTextNode("Hello, world"));
    
    // Aggiunge un marker
    //var point = new GLatLng(44.285604, 11.880941);
    //map.addOverlay(new GMarker(point));
    
    // Gestione eventi
    /* GEvent.addListener(map, "moveend", function() {
		  var center = map.getCenter();
		  document.getElementById("lat").value = center.lat();
		  document.getElementById("lng").value = center.lng();
		}); */
		
		GEvent.addListener(map, "click", function(marker, point) {
		  if (marker) {
		    map.removeOverlay(marker);
		    marker = null;
		  } else {
		  	editorLocationGmapMoveMarker(point);
		  }
		});
		
		// Infowin con dei Tab
		/*
		var infoTabs = [
			new GInfoWindowTab("Tab #1", "This is tab #1 content"),
			new GInfoWindowTab("Tab #2", "This is tab #2 content")
		];
		
		// Place a marker in the center of the map and open the info window
		// automatically
		var marker = new GMarker(map.getCenter());
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml(infoTabs);
		});
		map.addOverlay(marker);
		marker.openInfoWindowTabsHtml(infoTabs);				
		*/

    // Marker con icona
    /*
		var icon = new GIcon();
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		map.addOverlay(new GMarker(point, icon));
		*/
		//editorLocationGmapMoveMarker(point);
  }
}

function editorLocationGmapMoveMarker(point) {
	if (marker != null) map.removeOverlay(marker);
	marker = new GMarker(point);
  map.addOverlay(marker);
  document.getElementById("lat").value = point.lat();
  document.getElementById("lng").value = point.lng();
}

function editorLocationFind() {
	var street = document.getElementById('location_address_input').value;
	var city = document.getElementById('location_city_input').value;
	var zip = document.getElementById('location_cap_input').value;
	var s = document.createElement( "script" );
	// &cb=CallBack&cbe=CallBackError
	//s.src="http://www.localsearchmaps.com/geo/?country=IT&use=google2&level=16" +
	s.src="http://www.localsearchmaps.com/geo/?country=IT&cb=editorLocationFindGotCoord&cbe=editorLocationFindGotCoordError" +
		"&street=" + street + 
		"&city=" + city + 
		"&zip=" + zip + 
		"";
	//alert(s.src);
	s.type = "text/javascript";
	//s.onreadystatechange = gotCoordStateChange;
	lookingForCoord = true;
	document.getElementById('gmapfindbutton').disabled = true;
	document.getElementsByTagName( "head" )[0].appendChild(s);
}

function editorLocationFindGotCoord(lat, lng) {
	lookingForCoord = false;
	document.getElementById('gmapfindbutton').disabled = false;
	//alert(lat + ' ' + lng);
  var point = new GLatLng(lat, lng);
	map.panTo(point);
	editorLocationGmapMoveMarker(point);
  //map.addOverlay(new GMarker(point));
}

function editorLocationFindGotCoordError(error) {
	alert(error);
	lookingForCoord = false;
	document.getElementById('gmapfindbutton').disabled = false;
}

/*
function editorLocationFindGotCoordStateChange() {
	alert("SC");
	lookingForCoord = false;
	document.getElementById('gmapfindbutton').disabled = false;
}
*/

function detailLocationGMapLoad() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    
    //map.addControl(new GSmallZoomControl());
    //map.addControl(new GSmallMapControl());
    map.addControl(new GLargeMapControl());
    //map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());
    //map.addControl(new GOverviewMapControl());

    var point = new GLatLng(gmapStartLat > 0 ? gmapStartLat : gmapDefLat, gmapStartLng > 0 ? gmapStartLng : gmapDefLng);
    
    // Imposta il centro
    map.setCenter(point, gmapDefZoom);
    map.addOverlay(new GMarker(point));
  }
}

/********************************************************************/

var _img_maxwidth = 460;
var _img_maxheight = 300;
var _img_midpoint = 478; //410
var _img_minpoint = 10; //10
var _img_bordersize = 1;

var imgdims=new Array();

function zoomimg(imgid,zm) {
	embedimg=document.getElementById('embedimg' + imgid);
	imgwid=embedimg.width;
	imghei=embedimg.height;

	if(imgdims.length<imgid) imgdims[imgid]=[imgwid,imghei];
	else {
		if(!imgdims[imgid]) imgdims[imgid]=[imgwid,imghei]
	}

	if(zm>0) {
		embedimg.width=imgwid*zm;
		embedimg.height=imghei*zm;
		expandimg(imgid,3);
	} else {
		embedimg.width=imgdims[imgid][0];
		embedimg.height=imgdims[imgid][1];
		expandimg(imgid,3);
	}
}

function expandimg(imgid,flag,imgpath) {
	imgcontain=document.getElementById('imgcontain' + imgid);
	if (imgcontain.style.display == 'inline') return;
	bufferimg=document.getElementById('bufferimg' + imgid);
	embedimg=document.getElementById('embedimg' + imgid);
	imgicon=document.getElementById('iconimg'+imgid);

	if(flag==2)	if(imgcontain.style.position=='absolute') { flag=0 } else { flag=1 };
	if(flag==3)	if(imgcontain.style.position=='absolute') { flag=1 } else { flag=0 };
	
	
	imgwid=embedimg.width;
	imghei=embedimg.height;

	if(imgwid>0) {
		if(flag==1) {
			if(imgwid>_img_maxwidth) {
				midpoint=_img_midpoint-Math.round(imgwid/2);
				if(midpoint<_img_minpoint)  midpoint=_img_minpoint;

				imgcontain.style.width=imgwid+_img_bordersize*2;
				imgcontain.style.height=imghei+_img_bordersize*2;
				//imgcontain.style.left=midpoint;
				imgcontain.style.position='absolute';
				imgcontain.style.borderStyle='solid';
				imgcontain.style.borderColor='#333333';
				imgcontain.style.borderWidth=_img_bordersize;
				bufferimg.style.height=imghei+1;
				if(imgicon.width==22) imgicon.src=imgpath+"imgexpand1.gif";
			} else {
				imgcontain.style.width=_img_maxwidth;
				imgcontain.style.height = imghei < _img_maxheight ? imghei + 15 : _img_maxheight;
				bufferimg.style.height=1;
				imgcontain.style.borderStyle='none';
				imgcontain.style.position='static';
				if(imgicon.width==22) imgicon.src=imgpath+"imgexpand0.gif";
			}
		} else {
			imgcontain.style.width=_img_maxwidth;
			imgcontain.style.height = imghei < _img_maxheight ? imghei + 15 : _img_maxheight;
			bufferimg.style.height=1;
			imgcontain.style.borderStyle='none';
			imgcontain.style.position='static';
			if(imgicon.width==22) imgicon.src=imgpath+"imgexpand0.gif";
		}
	}
}

function mouseoverimg(imgid, imgpath) {
		imgcontain=document.getElementById('imgcontain' + imgid);
		imgicon=document.getElementById('iconimg'+imgid);
		embedimg=document.getElementById('embedimg' + imgid);
		imgwid=embedimg.width;
		
		if (imgwid>_img_maxwidth)	{
			imgicon.width=22;
			if(imgcontain.style.position=='absolute') {
				imgicon.src=imgpath+"imgexpand1.gif";
			} else {
				imgicon.src=imgpath+"imgexpand0.gif";
			}
		}
}

function mouseoutimg(imgid, imgpath) {
		imgicon=document.getElementById('iconimg'+imgid);
		imgicon.src=imgpath+"spacer.gif";
		imgicon.width=13;
}

function initimg(imgid) {
	imgcontain=document.getElementById('imgcontain' + imgid);
	embedimg=document.getElementById('embedimg' + imgid);
	imgicon=document.getElementById('iconimg'+imgid);
	bufferimg=document.getElementById('bufferimg' + imgid);
	//navimg=document.getElementById('navimg' + imgid);
	
	imgwid=embedimg.width;
	imghei=embedimg.height;
	if(imgwid>_img_maxwidth) {
		if (imgcontain.previousSibling && imgcontain.previousSibling.nodeName == '#text') {
			imgcontain.parentNode.insertBefore(document.createElement('BR'), imgcontain);
		}
		imgcontain.style.display = 'block';
		imgcontain.style.width = _img_maxwidth;
		imgcontain.style.height = imghei < _img_maxheight ? imghei + 15 : _img_maxheight;
		//imgcontain.style.overflow = 'auto';
		imgcontain.style.zoom = 1;
		imgicon.style.cursor = 'hand';
		embedimg.style.cursor = 'pointer';
		//if (navimg) navimg.style.display = 'block';
		
	} else {
		imgcontain.style.display = 'inline';
		imgcontain.style.width = null;
		imgcontain.style.height = null;
		//imgcontain.style.overflow = null;
		imgicon.style.display = 'none';
		bufferimg.style.display = 'none';
		imgicon.style.cursor = null;
		embedimg.style.cursor = null;
		//if (navimg) navimg.style.display = 'none';
	}

}