function parseLinks() {
	try {
		var links = dojo.query("a.rblink");
		
		dojo.forEach(links, function(link) {
			var href = dojo.attr(link, "href");
			
			if (href) {
				if (dojo.isIE < 8) {
					// special för IE 6+7
					var exp = /href="(.*?)"/;
					try {
						var groups = exp.exec(link.outerHTML);
						href = groups[1];
					}
					catch (e) {
					}
				}
				href = "MainServlet?query=rbdoc&sf=HY&link=" + encodeURIComponent(href);
				dojo.attr(link, "href", "javascript:payLinkLogin('" + href + "')");
			}
		});
	}
	catch (e) {
		alert(e);
	}
}
function initInputField(input, startText) {
	if (typeof input == 'string') input = dojo.byId(input);
	if (input && (input.value == "" || input.value == startText)) {
		input.onfocus = function() {
			if (this.className == this.initClassName) {
				this.value = "";
				this.className = this.realClassName;
				dojo.style(input,"color","black");
				dojo.style(input,"fontStyle","normal");
			}
		};
		input.startText = startText;
		input.value = startText;
		if (!input.className) {input.className = "noclass";}
		input.realClassName = input.className;
		input.initClassName = input.className = input.className + " initInputField";
		input.onblur = function() {
			if (this.className == this.realClassName && this.value == "") {
				this.className = this.className + " initInputField";
				this.value = this.startText;
				dojo.style(input,"color","#767676");
				dojo.style(input,"fontStyle","italic");
			}
		};
		var prevOnKeyDown = input.onkeydown;
		input.onkeydown = function(e) {
			if (prevOnKeyDown) prevOnKeyDown(e);
			if (this.className == this.initClassName) {
				this.value = "";
				this.className = this.realClassName;
			}
		}
	}
}

function showInfo(link, id) {
	showInfoMain(link, id, 0, 0);
}

function showInfoMain(link, id, offsetX, offsetY) {
	var item = document.getElementById(id);
	if (item) {
		var dx = findAbsoluteParentX(link);
		var dy = findAbsoluteParentY(link);
		var absParent = (dx != 0 || dy != 0);		
		if (!absParent) {
			var maincontainer = document.getElementById("maincontainer");
			if (maincontainer) {
				dx = findX(maincontainer);
			}
		}
		else {
			if (document.getElementById(id + "_outer")) {
				item = document.getElementById(id + "_outer");
			}
			else {
				var outsideItem = document.createElement("div");
				outsideItem.id = id + "_outer";
				outsideItem.className = "infoText"; 
				
				outsideItem.innerHTML = item.innerHTML;
				document.body.appendChild(outsideItem);
				item = outsideItem;				
			}			
		}
		item.style.display = 'block';
		if (item.offsetHeight > 20) {
			if(item.offsetWidth < 225) {
				item.style.width = '225px';
			}else{
				item.style.width = item.offsetWidth+'px';
			}
		}
		var x = (findX(link) + 20 - dx);
		var y = findY(link) - dy;
		
		var winHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
    	winHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
		winHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winHeight = document.body.clientHeight;
		}
		if ((x + item.offsetWidth) > 800) {
			if(y+item.offsetHeight>winHeight){
				item.style.top = (y + offsetY - item.offsetHeight - 8)+'px';
			}else{
				item.style.top = (y + offsetY  + 20)+ 'px';
			}
			item.style.left = (dx + 770-item.offsetWidth+offsetX)+'px'; //539+offsetX+'px';
		} else {
			if(y+item.offsetHeight>winHeight){
				item.style.top = (y + offsetY - item.offsetHeight - 8 + dy)+'px';
			}else{
				item.style.top = (y + offsetY - 5 + dy)+ 'px';
			}
			item.style.left = (dx + x + offsetX) + 'px';
		}
		item.style.visibility = 'visible';
		showIEMenuFixSub(item);
	}
}
function showInfoOver(link, id) {
	showInfoOverMain(link, id, 0, 0);
}
function showInfoOverMain(link, id, offsetX, offsetY) {
	var item = document.getElementById(id);
	if (item) {
		if(item.offsetWidth < 225) {
			item.style.width = '225px';
		}else{
			item.style.width = item.offsetWidth+'px';
		}
		item.style.visibility = 'visible';
		dx = findAbsoluteParentX(link);
		dy = findAbsoluteParentY(link);
		x = findX(link);
		if ((x + item.offsetWidth) > 800) {
			item.style.top = (findY(link) - item.offsetHeight + offsetY - 5 - dy)+ 'px';
			item.style.left = (770-item.offsetWidth-dx+offsetX) + 'px';
		}
		else {
			item.style.top = (findY(link) - item.offsetHeight + offsetY - 5 - dy)+ 'px';
			item.style.left = (x-dx+offsetX) + 'px';
		}
		showIEMenuFixSub(item);
	}
}

function hideInfo(id) {
	var item = document.getElementById(id + "_outer");
	if (!item) item = document.getElementById(id);
	if (item) {
		item.style.visibility = 'hidden';
		item.style.width = '';
		hideIEMenuFixSub(item);
	}
}

function showIEMenuFixSub(theDiv) {
	var iefix = document.getElementById("iefixsub");
	if (iefix) {
		iefix.style.width = theDiv.offsetWidth;
		iefix.style.height = theDiv.offsetHeight;		
		iefix.style.top = findY(theDiv) + 'px';
		iefix.style.left = findX(theDiv) + 'px';
		iefix.style.display = "block";
		iesubFixedFor = theDiv;
	}	
}

function hideIEMenuFixSub(theDiv) {
	if (theDiv == iesubFixedFor) {
		var iefix = document.getElementById("iefixsub");
		if (iefix) {
			iefix.style.display = "none";
			iesubFixedFor = null;
		}
	}
}
function findY(item) {
	var y = 0;
	while (item != null) {
		y += item.offsetTop;
		item = item.offsetParent;
	}
	return y;
}

function findX(item) {
	var x = 0;
	while (item != null) {
		x += item.offsetLeft;
		item = item.offsetParent;
	}
	return x;
}

function findAbsoluteParentX(item) {
	item = item.offsetParent;
	while (item != null) {
		if (getStyle(item, "position") == 'absolute')
			return findX(item);
		item = item.offsetParent;
	}
	return 0;
}

function findAbsoluteParentY(item) {
	item = item.offsetParent;
	while (item != null) {
		if (getStyle(item, "position") == 'absolute')
			return findY(item);
		item = item.offsetParent;
	}
	return 0;
}

function getStyle(x,styleProp,fromParent)
{
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	if (y || !fromParent)
		return y;
	if (x.offsetParent) {
		return getStyle(x.offsetParent, styleProp, fromParent);
	}
	return y;
}

// webfraga
function createWebfraga(contextUrl, questionNodeID, resultNodeID, totalNodeID, articleId) {
	dojo.require("dojo.fx");
	var wf = {
		webfragaresult: null,
		articleId: articleId,
		qNodeID: questionNodeID,
		rNodeID: resultNodeID,
		totNodeID: totalNodeID,
		url: contextUrl,
		barScale: 1.0,
		wfWipeOut: dojo.fx.wipeOut({node: questionNodeID,duration: 500}),
		wfWipeIn: null,
		wfRevealResult: function() {
				dojo.fx.chain([this.wfWipeOut,this.wfWipeIn]).play();
			},
		
		disable:function(value) {
				dojo.forEach(dojo.query("#" + this.qNodeID + " input"), function(input) { input.disabled = value; });
			},
		setResult: function(data) {
				this.webfragaResult = data;	
				for (var i = 0; i < data.counters.length; i++) {
					var svarDiv = document.getElementById("svar"+this.articleId+"_"+(i+1));
					if (svarDiv) {
						var pct = Math.round(100*data.counters[i]/(data.total == 0 ? 1 : data.total));
						svarDiv.innerHTML = pct + "%";
						document.getElementById("svarBar"+this.articleId+"_"+(i+1)).style.width = (this.barScale * pct) + "px";
					} 
				}
				document.getElementById(this.totNodeID).innerHTML = "" + data.total;
			},
			
		fetchResult: function() {
				var wf = this;
				dojo.xhrGet({
			 			url: wf.url + 'WebfragaServlet?id=' + wf.articleId,
			 			handleAs: "json",
			 			load: function (data) {
			 				wf.webfragaResult = data;
			 				wf.setResult(data);
			 			},
			 			error: function (error) {
			 				alert(error);
			 				wf.disable(false);
			 			}
				});
			},
			
		answer: function (n) {
				this.disable(true);
				var wf = this;
				dojo.xhrPost({
			 			url: this.url + 'WebfragaServlet',
			 			handleAs: "json",
			 			content: {
			 				id : wf.articleId,
			 				n : n
			 			},
			 			headers: {
			 				"Content-Encoding" : "utf-8",
			 				"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
			 			},
			 			load: function (data) {
			 				wf.webfragaResult = data;
			 				wf.wfRevealResult();
			 			},
			 			error: function (error) {
			 				alert(error);
			 				wf.disable(false);
			 			}
					
				});
			}

	};
	wf.wfWipeIn = dojo.fx.wipeIn({node: resultNodeID,duration: 500, onEnd: 
		function() { 
			try {
				if (wf.webfragaResult == null) {
					wf.fetchResult();
				}
				else {
					wf.setResult(wf.webfragaResult);
				}
			}
			catch (e) {
				alert(e);
			}	
		}
	})
	return wf;
}




