/*
 * $Id: AspHelp.js,v 1.1 2010/07/20 17:26:24 ggortsema Exp $
 * Copyright (C) 2007 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */

 
/**
 * @fileoverview
 *
 * Contains aspadmin.AspHelp class.
 * 
 * @author Vladimir Sakhovski
 * @version $Revision: 1.1 $
 */

// Check dependencies
jade.require("aspadmin");
jade.require("jade.Log");
jade.require("aspadmin.Resources");


/**
 * Constructs a aspadmin.AspHelp object.
 *
 * @class The aspadmin.AspHelp class
 *
 * @constructor
 * 
 */

aspadmin.AspHelp = function(){
	this.notForTransfer = true;
	this.prepareStatic();
}

/**
 * Constants
 */
aspadmin.AspHelp.helpDivId = 'helpDiv';

aspadmin.AspHelp.prototype.prepareStatic = function() {
	if(!aspadmin.AspHelp.instance) {aspadmin.AspHelp.instance = {};}
	aspadmin.AspHelp.instance = this;
};

/**
 * Set text resources, which should be used to search for help text by key.
 */
aspadmin.AspHelp.setTexts = function(resources) {
	aspadmin.AspHelp.instance.resources = resources;
};
	
/**
 * Display div with helpText at the position relative to event target.
 * 
 * @param evt
 * 				event which triggered this call
 * @param helpText  
 * 				text to be shown
 * @param helpDivClass
 * 				css class name to use for help div, if not set default class name 'help' will be used
 */
aspadmin.AspHelp.showHelpText = function(evt, helpText, helpDivClass) {
	// clear the old help windows (just in case)
	aspadmin.AspHelp._hideHelp();
	evt = (evt)? evt : ((window.event) ? window.event : null);
	//jade.Log.debug('showHelpText evt='+evt+' helpText='+helpText+' helpDivClass='+helpDivClass);
	if (!evt) return;
	if (helpText == '') return;
	
	var eventTargetElem = (evt.target)?evt.target:evt.srcElement;
	$(eventTargetElem).bind("mouseout.showHelpText", function(){
		aspadmin.AspHelp._hideHelp();
		$(this).unbind("mouseout.showHelpText");
	});
	
	var bodyScrollLeft = document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft;
	var bodyScrollTop = document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
	var mouseX = evt.pageX?evt.pageX-bodyScrollLeft:evt.clientX-document.body.clientLeft;
	var mouseY = evt.pageY?evt.pageY-bodyScrollTop:evt.clientY-document.body.clientTop;
	
	var helpOuterDiv = document.createElement("DIV");
	helpOuterDiv.className = 'help-outerDiv';
	helpOuterDiv.id = aspadmin.AspHelp.helpDivId;
	
	var helpDiv = helpOuterDiv.appendChild(document.createElement("DIV"));
	if (helpDivClass){
		helpDiv.className = helpDivClass;
	} else {
		helpDiv.className = 'help';
	}
	helpDiv.innerHTML = helpText;
	
	var xOffset = bodyScrollLeft+mouseX+10;
	var yOffset = bodyScrollTop+mouseY+10;
	
	var windowInnerWidth;
	var windowInnerHeight;
	if (document.body && (document.body.clientWidth !=0)) {
      windowInnerWidth=document.body.clientWidth;
      windowInnerHeight=document.body.clientHeight;
    }
    if (document.documentElement && (document.documentElement.clientWidth!=0) && (document.body.clientWidth + 20 >= document.documentElement.clientWidth)) {
      windowInnerWidth=document.documentElement.clientWidth;   
      windowInnerHeight=document.documentElement.clientHeight;   
    }
	
	helpOuterDiv.style.left = xOffset+"px";
	helpOuterDiv.style.top  = yOffset+"px";
	
	document.body.appendChild(helpOuterDiv);
	
	var helpDivWidth = helpOuterDiv.scrollWidth;
	if (mouseX+helpDivWidth+10 > windowInnerWidth) 
	{
		xOffset = bodyScrollLeft+mouseX-helpDivWidth;
		helpOuterDiv.style.left = xOffset+"px";
	}
	var helpDivHeight = helpOuterDiv.scrollHeight;
	if (mouseY+helpDivHeight+10 > windowInnerHeight) 
	{
		yOffset = bodyScrollTop+mouseY-helpDivHeight;
		helpOuterDiv.style.top = yOffset+"px";
	}
	//jade.Log.debug('showHelpText done');
}


/**
 * Display help text found by key.
 * 
 * @param evt
 * 				event which triggered this call
 * @param helpKey 
 * 				help text id for message to show
 * @param helpDivClass
 * 				css class name to use for help div, if not set default class name 'help' will be used
 */
aspadmin.AspHelp.showHelpByKey = function(evt, helpKey, helpDivClass) {
	evt = (evt)? evt : ((window.event) ? window.event : null);
	//jade.Log.debug('showHelpByKey evt='+evt+' helpKey='+helpKey+' helpDivClass='+helpDivClass);
	if (!evt) return;
	
	// check if we have requested key in resources
	if (typeof aspadmin.AspHelp.instance.resources == 'undefined' || !aspadmin.AspHelp.instance.resources.hasKey(helpKey))
	{
		// help text is not found, return
		throw ('Help text string with key '+helpKey+' is not found');
	}

	var helpText = aspadmin.AspHelp.instance.resources.getValue(helpKey);
	aspadmin.AspHelp.showHelpText(evt, helpText, helpDivClass);
}


/**
 * Hide ASP help. Element to hide is always found by known id.
 */
aspadmin.AspHelp._hideHelp = function() {
	var helpDiv = document.getElementById(aspadmin.AspHelp.helpDivId);
	if (helpDiv) {
		helpDiv.parentNode.removeChild(helpDiv);
	}
}

/**
 * Bind helpText to the specified element.
 */
aspadmin.AspHelp.bindHelpText = function(elementOrId, helpText) {
	var el = elementOrId;
	if (typeof elementOrId == 'string') { el = document.getElementById(elementOrId); }
	if (!el) {
		throw ('Element to bind help to is not found by specified elementOrId '+elementOrId);
		return;
	}
	
	if (typeof helpText != 'undefined' && helpText != '') { 
		$(el).bind("mouseover", helpText, function(event){
			aspadmin.AspHelp.showHelpText(event,event.data);
		});
	}
}

/**
 * Bind helpKey to the specified element.
 */
aspadmin.AspHelp.bindHelpKey = function(elementOrId, helpKey) {
	var el = elementOrId;
	if (typeof elementOrId == 'string') { el = document.getElementById(elementOrId); }
	if (!el) {
		throw ('Element to bind help to is not found by specified elementOrId '+elementOrId);
		return;
	}
	
	if (typeof helpKey != 'undefined' && helpKey != '') { 
		$(el).bind("mouseover", helpKey, function(event){
			aspadmin.AspHelp.showHelpByKey(event,event.data);
		});
	}
}

aspadmin.AspHelp.showTooltip = function(evt, tooltipText, helpDivClass)
{
	evt = (evt)? evt : ((window.event) ? window.event : null);
	if (!evt) return;
	if (!tooltipText) return;
	
	var eventTargetElem = (evt.target)?evt.target:evt.srcElement;
	var pendingTooltip = setTimeout(
			(function(evt, tooltipText, helpDivClass){
				return function(){aspadmin.AspHelp.showHelpText(evt, tooltipText, helpDivClass)};
			})(evt, tooltipText, helpDivClass),1000);
	$(eventTargetElem).bind("mouseout.showTooltip", {pendingTooltip: pendingTooltip}, function(event){
		clearTimeout(event.data.pendingTooltip);
		aspadmin.AspHelp._hideHelp();
		$(this).unbind("mouseout.showTooltip");
	});

	
};

//init singleton
new aspadmin.AspHelp();
