/*{{{*/function DepartmentPAFSetup( formId )
{
	var form = document.getElementById(formId);
	var map = getChildrenWithId(form, "map" )[0];
	var perim = getChildrenWithId(form,"perimiter")[0];
	var town = getFormElement(formId,"data[Department][town]");
	var xi = getFormElement(formId,"data[Department][x]");
	var yi = getFormElement(formId,"data[Department][y]");
	var textXOffset = getFormElement(formId,"data[Department][textXOffset]");
	var textYOffset = getFormElement(formId,"data[Department][textYOffset]");
	var transparentBackground = getFormElement(formId,"data[Department][transparentBackground]");
	/**
	 *	Get map total offset
	 */
	if( ! map.totalOffsetLeft ) DepartmentGetMapTotalOffset( map );
	DepartmentSetupPerimiter(perim, map);
	xi.onkeyup = function() {
		if( isNaN( this.value ) ) this.value = 0;
		perim.department.x = this.value;
		perim.alocate();
	}
	yi.onkeyup = function() {
		if( isNaN( this.value ) ) this.value = 0;
		perim.department.y = this.value;
		perim.alocate();
	}
	textXOffset.onkeyup = function() {
		if( isNaN( this.value ) ) this.value = 0;
		perim.townNode.style.left = parseInt(this.value)+"px";
	}
	textYOffset.onkeyup = function() {
		if( isNaN( this.value ) ) this.value = 0;
		perim.townNode.style.top = parseInt(this.value)+"px";
	}
	transparentBackground.onchange = function() {
		perim.townNode.style.background = ( this.value == 'true' ) ? 'none' : '#FFF';
	}
	document.getElementById("map").onclick = function(event) {
		xi.value = event.clientX - map.totalOffsetLeft;
		yi.value = event.clientY - map.totalOffsetTop;
		xi.onkeyup();
		yi.onkeyup();
		perim.alocate();
	}
	town.onkeyup = function() {
		perim.childNodes[1].innerHTML = this.value;
	}
}/*}}}*/
/*{{{*/function DepartmentAddTownToMap( formId , departments , installationPath )
{
	var map = getChildrenWithId( document.getElementById( formId ) , "map" )[0];
	if( ! map.departments ) map.departments = [];
	window.onresize = function() {
		DepartmentGetMapTotalOffset( map );
	}
	var obj = map.parentNode;
	var info = getChildrenWithId( document.getElementById( formId ) , "info" )[0];
	if( info && ! info.initialised ) {
		info.nameNode = getChildrenWithId( info , "name" )[0];
		info.townNode = getChildrenWithId( info , "town" )[0];
		info.streetNode = getChildrenWithId( info , "street" )[0];
		info.presidentNode = getChildrenWithId( info , "president" )[0];
		info.emailNode = getChildrenWithId( info , "email" )[0];
		info.wwwNode = getChildrenWithId( info , "www" )[0];
		info.phoneNode = getChildrenWithId( info , "phone" )[0];
		info.meetingsNode = getChildrenWithId( info , "meetings" )[0];
		info.show = function( node ) {
			this.style.visibility = "visible";
			this.nameNode.innerHTML = node.department.name;
			this.presidentNode.innerHTML = node.department.president;
			this.townNode.innerHTML = node.department.town;
			this.streetNode.innerHTML = node.department.street;
			this.emailNode.firstChild.innerHTML = node.department.email;
			this.emailNode.firstChild.href = "mailto:" + node.department.email;
			this.wwwNode.firstChild.innerHTML = node.department.www;
			this.wwwNode.firstChild.href = "http://" + node.department.www;
			this.phoneNode.innerHTML = node.department.phone;
			this.meetingsNode.innerHTML = node.department.meetings;
		}
		info.hide = function() {
			if( this.selectedItem ) this.show( this.selectedItem );
			else this.style.visibility = "hidden";
		}
		info.select = function( node ) {
			this.selectedItem = node;
			this.show( this.selectedItem );
		}
		info.initialised = true;
		info.style.visibility = "hidden";
	}
	if( ! map.totalOffsetLeft ) DepartmentGetMapTotalOffset( map );
	for( var i = 0 ; i < departments.length ; i++ ) {
		var node = document.createElement("div");
		node.id = departments[i].id
		node.className = "perimiter";
		node.appendChild( document.createElement("img") );
		node.appendChild( document.createElement("div") );
		node.childNodes[0].id = "dot";
		node.childNodes[0].src = installationPath+"/map-spot.png";
		node.childNodes[1].id = "town";
		node.childNodes[1].innerHTML = departments[i].name;
		node.townNode = node.childNodes[1];
		node.townNode.style.left = departments[i].textXOffset + "px";
		node.townNode.style.top = departments[i].textYOffset + "px";
		node.townNode.style.background = ( departments[i].transparentBackground == 'true' ) ? 'none' : '#FFF';
		node.department = departments[i];
		if( info ) { 
			node.townNode.onmouseover = function() {
				info.show( this.parentNode );
			}
			node.townNode.onmouseout = function() {
				info.hide();
			}
			node.townNode.onclick = function() {
				info.select( this.parentNode );
			}
		}
		DepartmentSetupPerimiter(node,map);
		node.alocate();
		obj.appendChild(node);
		map.departments.push(node);
	}
}/*}}}*/
/*{{{*/function DepartmentSetupPerimiter( perim , map )
{
	perim.map = map;
	perim.setRadius = function( radius ) {
		this.style.width = 2*radius+"px";
	}
	perim.getRadius = function() {
		return parseInt(this.style.width)/2;
	}
	perim.alocate = function() {
		this.style.left = parseInt(this.department.x) + parseInt(this.map.totalOffsetLeft) - parseInt(this.getRadius()) + "px";
		this.style.top = parseInt(this.department.y) + parseInt(this.map.totalOffsetTop) + "px";
	}
	perim.setRadius(25);
}/*}}}*/
/*{{{*/function DepartmentGetMapTotalOffset( map )
{
	var obj = map;
	map.totalOffsetLeft = 0;
	map.totalOffsetTop = 0;
	do {
		if( ! obj.offsetParent ) break;
		map.totalOffsetLeft += obj.offsetLeft;
		map.totalOffsetTop += obj.offsetTop;
		obj = obj.offsetParent;
	} while( obj.offsetParent );
	if( map.departments ) for( var i = 0 ; i < map.departments.length ; i++ ) map.departments[i].alocate();
}/*}}}*/
