AskAQuestion = {
	instance : null,
	getInstance : function() {
		if (AskAQuestion.instance == null) AskAQuestion.instance = new _AskAQuestion();
		return AskAQuestion.instance;
	}
}
function _AskAQuestion() {
	this.vPositionElement = null;
	this.hPositionElement = null;
	this.myId = null;
	this.inDetails=null;
	this.isPointingUp = null;
}
_AskAQuestion.IFRAME_ID = "AskAQuestionIframe";
_AskAQuestion.prototype.getIframeId = function() {
	return this.myId;
}

_AskAQuestion.prototype.close = function(horizontalElement, verticalElement) {
	Transition.addEffect(document.body, Transition.FADE_IN, "duration=.1,interval=.1");
	document.body.effects.play(Transition.FADE_IN);
	// remove iframe so EAF entries will be removed from browser history
	document.body.removeChild(document.getElementById(_AskAQuestion.IFRAME_ID));
	if(this.inDetails != "true") document.getElementById("pointer").style.visibility = 'hidden';
}
_AskAQuestion.prototype.getLeft = function() {	
	if(this.inDetails == "true") {
		var iframe = document.getElementById(_AskAQuestion.IFRAME_ID);
		var element = (this.hPositionElement) ? this.hPositionElement : document.body;
		return (element.offsetWidth / 2) - (iframe.offsetWidth / 2) + getAbsOffsetLeft(element) + "px";
	} else {
		return cursor.x + 38 + "px";
	}
}
_AskAQuestion.prototype.getTop = function() {
	var getAquote = document.getElementById(_AskAQuestion.IFRAME_ID);
	if(this.inDetails != "true") {
		var windowHeight = document.body.clientHeight + document.body.scrollTop;	
		var elementBottom = getAquote.offsetHeight + getAbsOffsetTop(this.vPositionElement);
		if (elementBottom > windowHeight) {
			jQuery("#upPointer").css('display','none');
			jQuery("#downPointer").css('display','block');
			jQuery("#upPointer_right").css('display','none');
			jQuery("#downPointer_right").css('display','none');
			this.isPointingUp = false;
			return (getAbsOffsetTop(this.vPositionElement) - (elementBottom - windowHeight) - 15) + "px";	
		} else {
			jQuery("#upPointer").css('display','block');
			jQuery("#downPointer").css('display','none');
			jQuery("#upPointer_right").css('display','none');
			jQuery("#downPointer_right").css('display','none');
			this.isPointingUp = true;
			return getAbsOffsetTop(this.vPositionElement) + 30 + "px";
		}
	} else {
		var elementHeight = (this.vPositionElement) ? this.vPositionElement.offsetHeight : document.body.clientHeight;
		return (elementHeight / 2) - (getAquote.offsetHeight / 2) + document.body.scrollTop + "px";

	}
}
_AskAQuestion.prototype.getUrl = function(doc) {
	if (!doc) var doc = document;
	var eafUrlUtil = new URLUtility(doc);
	var params = eafUrlUtil.getParameterMap();
	for (var paramName in params) {
		if (!params[paramName]) eafUrlUtil.removeParameter(paramName);
	}
	return eafUrlUtil.getUrl();
}
_AskAQuestion.prototype.pageLoad = function(eafContentElement) {	
	var iframe = document.getElementById(_AskAQuestion.IFRAME_ID);		
	iframe.style.width = eafContentElement.offsetWidth;
	iframe.style.height = eafContentElement.offsetHeight;
	iframe.style.top = this.getTop();
	iframe.style.left = this.getLeft();
	var windowWidth = document.body.clientWidth + document.body.scrollLeft;
	var getAquote = document.getElementById(_AskAQuestion.IFRAME_ID);
	var elementRight = parseInt(iframe.style.left) + parseInt(iframe.style.width);
	
	if (elementRight > windowWidth) {
		iframe.style.left = parseInt(this.getLeft()) - 550;
		jQuery("#pointer").css("left", parseInt(jQuery("#pointer").css("left")) - 50);
		if (this.isPointingUp) {
			jQuery("#upPointer").css("display", "none");
			jQuery("#downPointer").css("display", "none");
			jQuery("#upPointer_right").css("display", "block");
			jQuery("#downPointer_right").css("display", "none");
		} else {
			jQuery("#upPointer").css("display", "none");
			jQuery("#downPointer").css("display", "none");
			jQuery("#upPointer_right").css("display", "none");
			jQuery("#downPointer_right").css("display", "block");
		}
	}
	
	iframe.style.visibility = "visible";
}
_AskAQuestion.prototype.open = function(verticalElement,vID,inDetail,pageName) {
	
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id", _AskAQuestion.IFRAME_ID);
	iframe.name = _AskAQuestion.IFRAME_ID;
	this.myId = _AskAQuestion.IFRAME_ID;
	this.inDetails=inDetail;
	iframe.style.position = "absolute";
	iframe.style.top = "0px";
	iframe.style.visibility = "hidden";
	iframe.style.zIndex = 1000;
	iframe.src = "about:blank";
	iframe.setAttribute("border", "0");
	iframe.setAttribute("frameBorder", "0");
	iframe.setAttribute("scrolling", "no");
	document.body.appendChild(iframe);	
	this.vPositionElement = verticalElement;	
	iframe.src = "details_askAQuestion.do?cellName=inv&widgetName=details_widget_redesign&widgetType=inventory&widgetContext=common&id=" + vID + "&pageName=" + pageName;	
	Transition.addEffect(document.body, Transition.FADE_OUT, "duration=.1,interval=.1,backgroundColor=#222222" );
	document.body.effects.play(Transition.FADE_OUT);	
}

if (!window.getAbsOffsetLeft) {
	function getAbsOffsetLeft(element) {
		if (element.offsetParent) return element.offsetLeft + getAbsOffsetLeft(element.offsetParent);
		else return element.offsetLeft;
	}
}
if (!window.getAbsOffsetTop) {
	function getAbsOffsetTop(element) {
		if (element.offsetParent) return element.offsetTop + getAbsOffsetTop(element.offsetParent);
		else return element.offsetTop;
	}
}

