(function($){// convert file inputs & trigger event for ALL reloadable changes
$(document).one('popup-dialog-available', function(){
 var popup_content_init = function(){
	//debugger;
//	$(this).convert_file_inputs();
//	$(document).trigger('popup-content-changed', this);
	var _this = $("."+popup_dialog.content_class);
	_this.convert_file_inputs();
	$(document).trigger('popup-content-changed', _this);
 };
 popup_dialog.reloadable_options_adjust = function(options){
	if (options.__adjusted)
		return options;
	else if (typeof options.on_load == 'function') {
		var load_function = options.on_load;
		// keep 'this' as it would be for on_load w/o this function merge
		var _this = $("."+popup_dialog.content_class);
		options.on_load = function(){
		 $.each([ popup_content_init, load_function ], function(i, fx){
			_this.___go = fx;
			_this.___go();
		 });
		 _this.___go = undefined;
		};
	}
	else
		options.on_load = popup_content_init;
	options.__adjusted = true;
	return options;
 };
});
})(jQuery);


function showDesc(e) {
	thisId = this.getAttribute("id").match(/\d+$/);

	theMap = document.getElementById('map_'+thisId);
	theDesc = document.getElementById('info_'+thisId);

	xpos = positionX(theMap);
	ypos = positionY(theMap);

	theDesc.style.top = ypos + 12 + "px";
	theDesc.style.left = xpos + 15 + "px";
	theDesc.style.display = 'block';
}

function hideDesc(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement.parentNode : e.target.parentNode;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	while (reltg != tg && reltg.nodeName != 'BODY') {
		reltg = reltg.parentNode
	}
	if (reltg == tg) {
		return;
	}

	thisId = this.getAttribute("id").match(/\d+$/);
	theDesc = document.getElementById('info_'+thisId);
	theDesc.style.display = 'none';
}

function showDetails() {
	descs = document.getElementById('gallery').getElementsByTagName('div');
	for (i = 0; i < descs.length; i++ ) {
		if (descs[i].className == "map") {
			descs[i].parentNode.className = "mapbox-ex";
			descs[i].onmouseover = null;
			descs[i].onmouseout = null;

			infos = descs[i].childNodes[1];

			infos.style.top = 0;
			infos.style.left = 0;
			infos.style.display = 'block';
			infos.className = "mapInfoDetail";

			descs[i].firstChild.style.cursor = "pointer";
			descs[i].firstChild.onclick = function() {
				findlink = this.parentNode.getElementsByTagName('a');
				for (j = 0; j < findlink.length; j++) {
					if (findlink[j].className == "viewLink")
						location.href = findlink[j].getAttribute("href");
				}
			};
		}
	}
	document.getElementById('detailsLink').innerHTML = "hide details";
	document.getElementById('detailsLink').setAttribute("href", "javascript:hideDetails();");
}

function hideDetails() {
	descs = document.getElementById('gallery').getElementsByTagName('div');
	for (i = 0; i < descs.length; i++ ) {
		if (descs[i].className == "mapInfoDetail") {
			descs[i].style.display = 'none';
			descs[i].className = "mapInfo";
		}
		if (descs[i].className == "map") {
			descs[i].parentNode.className = "mapbox";
			descs[i].onmouseover = showDesc;
			descs[i].onmouseout = hideDesc;
			descs[i].firstChild.style.cursor = "default";
			descs[i].firstChild.onclick = null;
		}
	}
	document.getElementById('detailsLink').innerHTML = "show details";
	document.getElementById('detailsLink').setAttribute("href", "javascript:showDetails();");
}

function positionX(item) {
	var leftPos = 0;
	if (item.offsetParent) {
		while (item.offsetParent) {
			leftPos += item.offsetLeft;
			item = item.offsetParent;
		}
	}
	else if (item.x)
		leftPos += item.x;
	return leftPos;
}

function positionY(item) {
	var topPos = 0;
	if (item.offsetParent) {
		while (item.offsetParent) {
			topPos += item.offsetTop;
			item = item.offsetParent;
		}
	}
	else if (item.y)
		topPos += item.y;
	return topPos;
}

function deleteIt(type, id, origId) {
	confirm("Are you sure you want to delete this "+type+"?", function(a){
		if (a) window.location = "delete.php?type="+type+"&id="+id+"&origId="+origId;
	}, "Yes, delete", "No, retain");
}

function startPrint() {
	window.print()
}

function adjustMap() {
	if (window.innerHeight) {
		windowHeight = window.innerHeight;
		thisOffset = 232;
	} else if (document.documentElement && document.documentElement.clientHeight) { // for ie
		windowHeight = document.documentElement.clientHeight;
		thisOffset = 235;
	}/* else if (document.body) { // for older ie
		windowHeight = document.body.clientHeight;
		thisOffset = 232;
	} */
	if (windowHeight < 1200) {
		document.getElementById('map').style.height = (windowHeight - thisOffset) + "px";
	} else {
		document.getElementById('map').style.height = "900px";
	}
}

