 /**
  * Open a window popup
  * @Author: Francesco Rabottini <dev@webzone.it>
  *
  * @param	(String) url	the url to be opened
  * @param	(int) width		the width of the window
  * @param	(int) height	the height of the window
  * @param	(String) mode	"[stlmrS]" default: ""
  *				possible values:
  *				o	s: status
  *				o	t: toolbar
  *				o	l: location
  *				o	m: menubar
  *				o	r: resizable
  *				o	S: scrollbars=yes
  *
  * @return	(Void)	focus the last opened window popup
  */
 function openpopup(url, w, h, mode)
 {
 	_top  = (screen.height - h) / 2
 	_left = (screen.width  - w) / 2;
 	option = "top="+_top+",left="+_left+",width="+w+",height="+h;
 	if (! mode) { mode = ""; }
 	opt = new Array("status","toolbar","menubar","location","resizable", "Scrollbars");
 	for (i=0; i< opt.length;i++) {
 		if (mode.indexOf(opt[i].substring(0,1)) < 0) {
 			option += ","+opt[i]+"=no";
 		} else {
 			option += ","+opt[i]+"=yes";
 		}
 	}
 	win = window.open(url, null, option.toLowerCase());
 	win.focus();
 }

/* * * * * * * * * * * * * * */
function checkAll(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length)	{
		for (i=0; i<itemId.length; i++)	{
			itemId[i].checked = btn.checked;
		}
	} else {
		itemId.checked = btn.checked;
	}
}

function deleteSelected(btn, notConfirmAction)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un elemento da eliminare');
		return;
	}
	if (notConfirmAction)	{
		btn.form.submit();
		return;
	}
	if (confirm("\n\nHai scelto di Eliminare [ "+itemChecked+" ] elemeto/i\n\nVuoi davvero completare l'operazione?\n\n"))	{
		btn.form.submit();
	}
}

function syncToRole(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un utente da sincronizzare');
		return;
	}
	if (btn.form.role.value == '')	{
		alert('Devi selezionare un ruolo');
		return;
	}
	if (confirm('Vuoi davvero sincronizzare i ['+itemChecked+'] utenti selezionati ?'))	{
		btn.form.action = btn.form.action.replace('Delete','SyncToRole');
		btn.form.submit();
	}
}

function help(id, show)	{
	dhelp = document.getElementById(id);
	if (dhelp)	{
		dhelp.style.display = show ? "block":"none";
	}
	return false;
}

/**
 * FCKeditor inizialized
 */
var _FCK_EDITOR_ENABLED_ = true;
// var _FCK_EDITOR_ENABLED_ = false;

function ini_fck(fck_field_name, fck_width, fck_height) {

	if (_FCK_EDITOR_ENABLED_)	{

		var oFCKeditor = new FCKeditor(fck_field_name);
		oFCKeditor.Height = (fck_height) ? fck_height : "300";
		oFCKeditor.Width = (fck_width) ? fck_width : "100%";
		oFCKeditor.BasePath = "/FCK/"
		oFCKeditor.ReplaceTextarea() ;
  }
}


// Files associati
function displayAssociatedFiles(h3) {

	var AssociatedFilesStatus = !AssociatedFilesStatus
	divs = $('attachments').getElementsByTagName('div')
	for (i=0;divs.length;i++) {
		divs[i].style.display = AssociatedFilesStatus ? "block" : "none"
	}
}

/***********/
function getGooglePosition(form) {
	var geocoder = new GClientGeocoder()
	address = $F('address')+", "+ $F('postal_code')+" "+ $F('city')+" "+ $F('state');
	geocoder.getLocations(address, addPointToField)
}

function addPointToField(response) {

	if (response.Status.code == G_GEO_SUCCESS) {
		place = response.Placemark[0].Point
		$('gposition').value = place.coordinates[1]+", "+place.coordinates[0]
	} else if (response.Status.code == 602) {

		alert("Impossibile trovare l'indirizzo:\""+response.name+"\"");
	} else {
		alert("Impossibile contattare il server Google.");
	}
}
/**** LIst **********/


var map
var geocoder

function googleMapsInitialize() {

	if ($("map_canvas") && GBrowserIsCompatible()) {

		$("points_list").hide();


		var map_canvas = $("map_canvas")
		map_canvas.show();
		var map = new GMap2(map_canvas)
		var geocoder = new GClientGeocoder()
		var pointidx = 0

		map.addControl(new GSmallMapControl())
		map.addControl(new GMapTypeControl())
// 		map.enableScrollWheelZoom()
		map.setCenter(new GLatLng(41.869561, 12.568359), 6)

		// Creates a marker at the given point
		function createMarker(point, number) {
			var marker = new GMarker(point)
			marker.value = number
			p = points[number - 1]
			GEvent.addListener(marker,"click", function() {

				var myHtml = $("point_"+(number - 1)).innerHTML;
				map.openInfoWindowHtml(point, myHtml)
				}
			);
			return marker
		}

		for (var i = 0; i < points.length; i++) {
			p = points[i]
			pointidx++
			if (p.latitude && p.longitude) {
				point = new GLatLng(p.latitude, p.longitude);
				map.addOverlay(createMarker(point, pointidx))
			}
		}
	}//~compatible
}//~initialize

window.onload = googleMapsInitialize;


