/**
* Copyright 2008 Corella Ltd
*/
var OffsetX= 95;
var OffsetY= 0;
var old,skn,iex=(document.all),yyy=-1000;
var ns4=document.layers;
var ns6=document.getElementById && !document.all;
var ie4=document.all;
var pflag=0;
var popupPos = "contacts-popup_pos"
var popupContent = "contacts-popup_cnt";
var noContactToken = "view=biblio";
var tureToken = "target=tutk";
var ture = false;
var personlistPage = "/contacts-popup/jsp/personlist.jsp";

window.onload=initPersonSearch;

if (ns4) 
	skn=document.pdq;
else if (ns6) 
	skn=document.getElementById("pdq").style;
else if (ie4) 
	skn=document.all.pdq.style;
if (ns4) 
	document.captureEvents(Event.MOUSEMOVE);
else {
	skn.visibility="visible";
	skn.display="none";
}

/**
 * id - id of element used for HTML content
 * imgId - id of element used for positioning the popup window
 */
function popup(id, imgId){
	var msg = document.getElementById(id).innerHTML;
	var content = "<div class=boxpopup>" + msg + "</div>";
	
	var coord = findPos(document.getElementById(imgId));
	
	var posLeft = parseInt(coord[0]) + OffsetX;
	var posTop = parseInt(coord[1]) + OffsetY;
	
	skn.left= (ns6) ? posLeft + "px" : posLeft;
	skn.top= (ns6) ? posTop + "px" : posTop;	

	if(ns4){skn.document.write(content);skn.document.close();skn.visibility="visible";}
	if(ns6){document.getElementById("pdq").innerHTML=content;skn.display='';}
	if(ie4){document.all("pdq").innerHTML=content;skn.display='';}
	/**switchImage(imgId, true);*/
}

function removePopup(){
	yyy=-1000;
	if(ns4){skn.visibility="hidden";}
	else if (ns6||ie4)
	skn.display="none";
	swapStyle(false, popupPos);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;	
		} while (obj = obj.offsetParent);
	} else {
		alert("no support for offsetParent");
	}
	return [curleft,curtop];
}

function mouseOverContact(e) {
	xmlhttpPost(personlistPage);
	swapStyle(true, e.id);
}

function swapStyle(mouseOver, id) {
	var e = document.getElementById(id).style
	if (mouseOver) {
		e.fontWeight = "bold";
	} else {
		e.fontWeight = "normal";
	}
}

function xmlhttpPost(strURL) {

	// jos popupin sisalto on jo haettu, naytetaan vaan popup
	if (pflag == 1) {
		popup(popupContent, popupPos);
		return;	
	}
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        		// asetetaan haettu sisalto maaritetylle elementille
            updatepage(self.xmlHttpReq.responseText);
            popup(popupContent, popupPos);
            // asetetaan flagi paalle
            pflag = 1;
        }
    }
    
    var authors = encodeURIComponent(document.getElementById("authors").innerHTML);
    
    // NOTE: no '?' before querystring
    self.xmlHttpReq.send("a=" + authors + (ture ? "&ture=1" : ""));
}

function updatepage(str){
	document.getElementById(popupContent).innerHTML = str;
}

function initPersonSearch() {
	pflag = 0;
	var loc = "" + window.location;
	
	// remove contact image from certain views
	if (loc.indexOf(noContactToken) > -1 || !document.getElementById("authors")) {
		document.getElementById(popupPos).style.display = "none";
	} 
	
	// set TURE flag on
	if (loc.indexOf(tureToken) > -1) {
		ture = true;
	}
}
