function openAnnotationsWindow(querystring) {
	annotations = window.open('annotations-window.jsp?' + querystring, 'annotations', 'width=481,height=500,resizable=no,toolbar=no,location=no,directories=no,menubar=no,copyhistory=no,scrollbars=yes');
	annotations.focus();
}

function displayElementById(id) {
	document.getElementById(id).style.display = 'block';
}

function hideElementById(id) {
	document.getElementById(id).style.display = 'none';
}

function displayBubbleTip(e, text) {
	if (document.all) {
		e = event;
	}
	var obj = document.getElementById('bubble_tooltip');
	var obj2 = document.getElementById('bubble_tooltip_content');
	obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	if (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) st = 0;
	
	/* TODO: scrolling to the left dislocates tooltip! */
	var leftPos = e.clientX;
	if (leftPos < 0) leftPos = 0;
	obj.style.left = leftPos + 80 + 'px';
	obj.style.left = leftPos - 20 + 'px';
	
	obj.style.top = e.clientY + 10 + st + 'px';
}

function displayBubbleImg(e, link, imgpath) {
	if (document.all) {
		e = event;
	}
	var obj = document.getElementById('bubble_tooltip_xl');
	var obj2 = document.getElementById('bubble_tooltip_content_xl');
	
	obj2.innerHTML = '<a href=\"' + link + '\"><img style=\"border: 0; max-width: 280px;\" src=\"' + imgpath + '\"></a>';
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	if (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) st = 0;
	var leftPos = e.clientX;
	if (leftPos < 0) leftPos = 0;
	obj.style.left = leftPos - 5 + 'px';
	obj.style.top = e.clientY + 10 + st + 'px';
}

function hideBubble() {
	document.getElementById('bubble_tooltip').style.display = 'none';
	document.getElementById('bubble_tooltip_xl').style.display = 'none';
}


function displayBubbleDoc(e, docpath) {
//	if (document.all) {
//		e = event;
//	}
//	document.getElementById('bubble_tooltip_content_xl').innerHTML = '(Ladataan linkkiä...)';
//	
//	retrieveURL("bubble.jsp?document=" + docpath);
//	var obj = document.getElementById('bubble_tooltip_xl');
//	obj.style.display = 'block';
//	var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
//	if (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) st = 0;
//	var leftPos = e.clientX;
//	if (leftPos < 0) leftPos = 0;
//	obj.style.left = leftPos - 65 + 'px';
//	obj.style.top = e.clientY + 10 + st + 'px';
}

function displayBubbleAnnotation(e, annoid) {
	if (document.all) {
		e = event;
	}
	document.getElementById('bubble_tooltip_content_xl').innerHTML = '(Ladataan linkkiä...)';
	
	retrieveURL("annotation.jsp?annoid=" + annoid);
	var obj = document.getElementById('bubble_tooltip_xl');
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	if (navigator.userAgent.toLowerCase().indexOf('safari') >= 0) st = 0;
	var leftPos = e.clientX;
	if (leftPos < 0) leftPos = 0;
	obj.style.left = leftPos - 65 + 'px';
	obj.style.top = e.clientY + 10 + st + 'px';
}

var req;
var which;

function retrieveURL(url) {
	if (window.XMLHttpRequest) {
		// Non-IE browsers
		req = new XMLHttpRequest();
		req.onreadystatechange = processStateChange;
		try {
			req.open("GET", url, true);
		}
		catch (e) {
			alert(e);
		}
		req.send(null);
	} else if (window.ActiveXObject) {
		// IE
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processStateChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processStateChange() {
	if (req.readyState == 4) {
		// Complete
		if (req.status == 200) {
			// OK response
			document.getElementById("bubble_tooltip_content_xl").innerHTML = req.responseText;
		} else {
			document.getElementById("bubble_tooltip_content_xl").innerHTML = "<b>Linkin lataus epäonnistui. Kyseiseen tuotteeseen ei ole käyttöoikeutta, linkkiä napsauttamalla pääsee hallintasivulle.</b>";
		}
	}
}

var timeout = 500;
var closetimer = 0;
var mainmenuitem = 0;

// open hidden layer
function menuopen(id) {
	// cancel close timer
	mcancelclosetime();
	
	// close old layer
	if (mainmenuitem) mainmenuitem.style.visibility = 'hidden';
	
	// get new layer and show it
	mainmenuitem = document.getElementById(id);
	mainmenuitem.style.visibility = 'visible';
}
// close showed layer
function menuclose() {
	if (mainmenuitem) mainmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
	closetimer = window.setTimeout(menuclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function addToUserCollection(annotationid, path, usercollectionid) {
	var loc = document.location.href;
	
	var text = document.getElementById('PART_usercollection-title').firstChild.nodeValue;
	
	loc = loc.replace(new RegExp("addToUserCollection=1", "g"), '');
	loc = loc.replace(new RegExp("removeFromUserCollection=1", "g"), '');
	loc = loc.replace(new RegExp("annotationid=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("path=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("usercollectionid=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("text=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("#", "g"), "");
	loc = loc.replace(new RegExp("&+", "g"), "&");
	if (loc.indexOf('?') < 0) {
		loc = loc + '?';
	}
	document.location.href = loc + "&addToUserCollection=1&annotationid=" + annotationid + "&path=" + path + "&usercollectionid=" + usercollectionid + "&text=" + text+"#"+annotationid;
}

function removeFromUserCollection(annotationid, path, usercollectionid) {
	var loc = document.location.href;
	
	loc = loc.replace(new RegExp("removeFromUserCollection=1", "g"), '');
	loc = loc.replace(new RegExp("addToUserCollection=1", "g"), '');
	loc = loc.replace(new RegExp("annotationid=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("path=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("usercollectionid=[^&]*", "g"), '');
	loc = loc.replace(new RegExp("#", "g"), "");
	loc = loc.replace(new RegExp("&+", "g"), "&");
	if (loc.indexOf('?') < 0) {
		loc = loc + '?';
	}
	document.location.href = loc + "&removeFromUserCollection=1&annotationid=" + annotationid + "&path=" + path + "&usercollectionid=" + usercollectionid + "#" + annotationid;
}

function highlightPartsInUserCollection() {
	var divs = document.getElementsByTagName("span");
	for (var i = 0; i < divs.length; i++) {
		// alert('element['+i+']: '+divs[i]+', element.id: '+divs[i].id+', element.className: '+divs[i].className+', divs[i].id.substring(0, 21)='+divs[i].id.substring(0, 21));
		if (divs[i].id.substring(0, 21) == 'is_in_usercollection_') {
			var annotationId = divs[i].id.substring(21);
			// alert('highlight annotationId: '+', element['+i+']: '+divs[i]+', element.id: '+divs[i].id+', element.className: '+divs[i].className);
			// alert('Highlighting '+annotationId+' ('+document.getElementById(annotationId)+')');
			// document.getElementById(annotationId).style.borderLeft="thick double red";
			
			var e = document.getElementById("PART_" + annotationId);
			for (var j = 0; j < 100000; j++) {
				if (e!=null && e.style != undefined) {
					// e.style.borderLeft="medium double black";
					// e.style.paddingLeft="5px";
					e.style.color = "green";
				}
				
				e = e.nextSibling;
				
				if (e == null) {
					break;
				} else if (e.id != undefined && e.id != null && e.id != "") { // && e.id.substring(0, 3)==annotationId.substring(0, 3)){
					break;
				}
			}
		}
		// if(i++>10){ return; }
	}
}


// Escape Lucene special characters
function escapeLuceneChars(str) {
    if (str.length > 0) {
        if (str[0] == '@')
            return str.substring(1);
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\:/g,'\\\:');
    str=str.replace(/\?/g,'\\?');
    str=str.replace(/\*/g,'\\*');
    str=str.replace(/\~/g,'\\~');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\^/g,'\\^');
    str=str.replace(/\]/g,'\\]');
    str=str.replace(/\[/g,'\\[');
    str=str.replace(/\}/g,'\\}');
    str=str.replace(/\{/g,'\\{');
    str=str.replace(/\)/g,'\\)');
    str=str.replace(/\(/g,'\\(');
    str=str.replace(/\!/g,'\\!');
    str=str.replace(/\-/g,'\\-');
    str=str.replace(/\+/g,'\\+');
    }
return str;
}

// close layer when click-out
document.onclick = menuclose;



