function QuickHelpWindow(id,left,top,lang,staticContentMode,staticContentSource){
	this.config = {
		windowId:id,
		windowLeft:left,
		windowTop:top,
		contentLang:lang,
		iconWidth:16,
		windowMinWidth:350,
		windowMinHeight:15,
		staticContentMode: staticContentMode,
		staticContentSource: staticContentSource
	};
	
	this.createCloseLinkFor=function(windowId){
		return function(){
			var windowDiv=document.getElementById('quickHelp'+windowId);
			document.body.removeChild(windowDiv);
		}
	};
	
	this.createMoveWindowFor=function(windowId){
		return function(e){
			e=e||event;
			var windowDiv=document.getElementById('quickHelp'+windowId);
			var startEventPosX=e.clientX;
			var startEventPosY=e.clientY;
			var startWindowLeft=windowDiv.offsetLeft;
			var startWindowTop=windowDiv.offsetTop;
			
			document.documentElement.onmousemove=function(e){
				e=e||event;
				windowDiv.style.left=startWindowLeft+e.clientX-startEventPosX+'px';
				windowDiv.style.top=startWindowTop+e.clientY-startEventPosY+'px';
			};
		
			document.documentElement.ondragstart=function(e){
				return false;
			};
			
			document.documentElement.onselectstart=function(e){
				return false;
			};
			
			document.documentElement.onmouseup=function(e){
				e=e||event;
				document.documentElement.onmousemove=null;
				document.documentElement.onmouseup=null;
			};
		};
	};
	
	this.createResizeFor=function(windowId){
		var windowMinWidth=this.config.windowMinWidth;
		var windowMinHeight=this.config.windowMinHeight;
		
		return function(e){
			e=e||event;
			var windowDiv=document.getElementById('quickHelp'+windowId);
			var windowContentDiv=document.getElementById('quickHelpContent'+windowId);
			var startEventPosX=e.clientX;
			var startEventPosY=e.clientY;
			var startWidth=windowContentDiv.offsetWidth;
			var startHeight=windowContentDiv.offsetHeight;
			
			document.documentElement.onmousemove=function(e){
				e=e||event;
				var newWidth=Math.max(windowMinWidth,startWidth+e.clientX-startEventPosX);
				var newHeight=Math.max(windowMinHeight,startHeight+e.clientY-startEventPosY);
				windowDiv.style.width=newWidth+'px';
				windowContentDiv.style.height=newHeight+'px';
			};
			
			document.documentElement.ondragstart=function(e){
				return false;
			};
			
			document.documentElement.onselectstart=function(e){
				return false;
			};
			
			document.documentElement.onmouseup=function(e){
				e=e||event;
				document.documentElement.onmousemove=null;
				document.documentElement.onmouseup=null;
			};
		};
	};
	
	this.createWindow=function(){
		var quickHelpDiv=document.createElement('DIV');
		quickHelpDiv.className='quickhelpWindow';
		document.body.appendChild(quickHelpDiv);
		quickHelpDiv.id='quickHelp'+this.config.windowId;
		
		var quickHelpTopDiv=document.createElement('DIV');
		quickHelpTopDiv.className='quickhelpWindowTop';
		quickHelpTopDiv.onmousedown=this.createMoveWindowFor(this.config.windowId);
		quickHelpTopDiv.style.cursor="move";
		quickHelpDiv.appendChild(quickHelpTopDiv);
		
		var topButtonsDiv=document.createElement('DIV');
		topButtonsDiv.className='quickhelpWindowButtons';
		topButtonsDiv.style.cursor="pointer";
		quickHelpTopDiv.appendChild(topButtonsDiv);
		
		var imgClose=document.createElement('IMG');
		imgClose.src='groups/public/@l102/@news/@p/documents/tekn/img_gif_quickhelp_close.gif';
		imgClose.className='quickhelpWindowCloseButton';
		imgClose.onclick=this.createCloseLinkFor(this.config.windowId);
		topButtonsDiv.appendChild(imgClose);
		
		var quickHelpMiddleDiv=document.createElement('DIV');
		quickHelpMiddleDiv.className='quickhelpWindowMiddle';
		quickHelpDiv.appendChild(quickHelpMiddleDiv);
		
		var quickHelpContentDiv=document.createElement('DIV');
		quickHelpContentDiv.className='quickhelpWindowContent';
		quickHelpContentDiv.id='quickHelpContent'+this.config.windowId;
		quickHelpMiddleDiv.appendChild(quickHelpContentDiv);
		
		var quickHelpBottomDiv=document.createElement('DIV');
		quickHelpBottomDiv.className='quickhelpWindowBottom';
		quickHelpDiv.appendChild(quickHelpBottomDiv);
		
		var imgResize=document.createElement('IMG');
		imgResize.src='groups/public/@l102/@news/@p/documents/tekn/img_gif_quickhelp_resize.gif';
		imgResize.className='quickhelpWindowResizeImage';
		imgResize.onmousedown=this.createResizeFor(this.config.windowId);
		imgResize.style.cursor='nw-resize';
		quickHelpBottomDiv.appendChild(imgResize);
		
		quickHelpContentDiv.innerHTML=this.getContentOnLoading();
		quickHelpDiv.style.left=(this.config.windowLeft+this.config.iconWidth)+'px';
		quickHelpDiv.style.top=this.config.windowTop+'px';
		
		return quickHelpDiv;
	};
	
	this.loadContent=function(){
		var windowContentDiv=document.getElementById('quickHelpContent'+this.config.windowId);
		
		if(this.config.staticContentMode){
			
			windowContentDiv.innerHTML=$("#"+this.config.staticContentSource).val()
		
		}else{

			var xmlHttp = new XHConn();
		
			if(typeof(g_HttpRelativeWebRoot)!=="undefined"){
				var webRoot = g_HttpRelativeWebRoot;
			}else{
				var webRoot = urlVariables.serviceRoot.replace("idcplg","");
			}
			
			var url = webRoot+"groups/public/@l102/@news/@p/documents/tekn/st_pikaohje.hcsp";
			
			if(this.config.contentLang==undefined){
				this.config.contentLang = "";
			}
			
			xmlHttp.connect(
				url, 
				"GET", 
				"dDocName="+this.config.windowId+"&lang="+this.config.contentLang,
				function(XML){
					windowContentDiv.innerHTML=XML.responseText||this.getContentNotFoundInfo();
				}
			);
		}
	};
	
	this.getContentNotFoundInfo=function(){
		if(this.config.contentLang=='en'){
			return 'Help not found for component.';
		}
		return 'Pikaohjetta ei löytynyt.';
	};
	
	this.getContentOnLoading=function(){
		if(this.config.contentLang=='en'){
			return 'Loading help...';
		}
		return 'Ladataan ohjetta...';
	};
	
	this.getContentNotLoadedInfo=function(){
		if(this.config.contentLang=='en'){
			return 'Could not view help.';
		}
		return 'Pikaohjetta ei voinut näyttää.';
	};
	
	this.open=function(){
		var quickHelpDiv=this.createWindow();
		this.loadContent();
	};
	
	this.toString=function(){
		return 'QuickHelp: {windowId='+this.config.windowId
				+', windowWidth='+this.config.windowWidth
				+', windowHeight='+this.config.windowHeight
				+', windowLeft='+this.config.windowLeft
				+', windowTop='+this.config.windowTop
				+', contentUrl='+this.config.contentUrl
				+', contentLang='+this.config.contentLang
				+', imagePath='+this.config.imagePath
				+'}';
	};
}

function openQuickHelpWindow(obj,id,lang, staticContentMode, staticContentSource){
	var left=findObjectPosX(obj);
	var top=findObjectPosY(obj);
	var windowDiv=document.getElementById('quickHelp'+id);
	if(!windowDiv){
		var quickHelpWindow=new QuickHelpWindow(id,left,top,lang,staticContentMode,staticContentSource);
		quickHelpWindow.open();
	}
}

function findObjectPosX(obj){
	var curleft=0;
	if(obj.offsetParent){
		while(1){curleft+=obj.offsetLeft;
			if(!obj.offsetParent){
				break;
			}
			obj=obj.offsetParent;
		}
	}else if(obj.x){
		curleft+=obj.x;
	}
	return curleft;
}

function findObjectPosY(obj){
	var curtop=0;
	if(obj.offsetParent){
		while(1){
			curtop+=obj.offsetTop;
			if(!obj.offsetParent){
				break;
			}
			obj=obj.offsetParent;
		}
	}else if(obj.y){
		curtop+=obj.y;
	}
	return curtop;
}

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}
