function _TourOverlay(){
	this.TourData = null;
	var TourObj = this;
	EventManager.Add(window ,'resize' , function(){
			if(typeof TourObj.MessageBubble != 'undefined')
				TourObj.MessageBubble.position();
		});
}
_TourOverlay.prototype.start = function(cXML){
	var obj = this;
	if(typeof this.MessageBubble != 'undefined'){
		this.MessageBubble.close();
	}
	Ajax.Execute(cXML , function(oXML){
			obj.TourData = oXML.documentElement;
			obj.init();
		});
}
_TourOverlay.prototype.init = function(){
	this.CurrentStepIndex = 0;
	this.Steps = getChildNodes(this.TourData.getElementsByTagName('Steps')[0])
	this.MessageBubble = new _MessageBubble('TourOverlayBubble', {XmlDataNode: this.TourData.getElementsByTagName('Assets')[0].getElementsByTagName('MessageBubble')[0]});
	this.TotalSteps = this.Steps.length;
	this.playStep();
}
_TourOverlay.prototype.playStep = function(){
	if(this.CurrentStepIndex <= this.TotalSteps-1){
		this.CurrentStep = this.Steps[this.CurrentStepIndex];
		var cTargetElementID = this.CurrentStep.getAttribute('TargetElementID');
		if(document.getElementById(cTargetElementID)){
			var cContentsHTML = this.MessageBubble.CloseLink + this.CurrentStep.getElementsByTagName('HTMLText')[0].firstChild.nodeValue + (this.CurrentStepIndex > 0 ? (this.CurrentStepIndex < (this.TotalSteps - 1) ? this.MessageBubble.NextLink : this.MessageBubble.LastStepLink) : this.MessageBubble.ContinueButton);
			var cWidth = this.CurrentStep.getAttribute('Width');
			var cOrientation = this.CurrentStep.getAttribute('Orientation');
			this.MessageBubble.showArrow = (this.CurrentStepIndex > 0 ? true :  false);
			this.MessageBubble.open(cTargetElementID, {HTMLContents: cContentsHTML, MessageParentID: 'divTourMessageBubbleText_Default', Width: cWidth , Orientation: cOrientation});
			this.CurrentStepIndex++;
		}else{
			this.CurrentStepIndex++;
			this.playStep();
		}
	}else{
		window.scrollTo(0,0);
		this.MessageBubble.close();
	}
}

function _MessageBubble(cUniqueID , opts){
	this.XmlDataNode = (typeof opts['XmlDataNode'] != 'undefined' ? opts['XmlDataNode'] : null);
	this.showArrow = false;
	this.mainSpan = null;
	this.init();
}
_MessageBubble.prototype.init = function(){
	var MessageBubbleElements = getChildNodes(this.XmlDataNode);
	for(var i=0;i<MessageBubbleElements.length;i++){
		this[MessageBubbleElements[i].tagName] = MessageBubbleElements[i].firstChild.nodeValue
		var NodeAttributes = MessageBubbleElements[i].attributes;
		for(var j=0;j<NodeAttributes.length;j++){
			this[MessageBubbleElements[i].tagName + MessageBubbleElements[i].attributes[j].name] = MessageBubbleElements[i].attributes[j].value;
		}
	}
}
_MessageBubble.prototype.open = function(cTargetElementID, opts){
	this.close();
	this.mainSpan = document.createElement('SPAN');
	this.mainSpan.innerHTML = this.MainHTML;
	var cHTMLContents = opts['HTMLContents'];
	document.body.appendChild(this.mainSpan);
	document.getElementById(opts['MessageParentID']).innerHTML = cHTMLContents;
	this.ArrowDiv = document.createElement('DIV');
	this.ArrowDiv.style.display = 'inline'; //fix FF bug
	this.mainSpan.appendChild(this.ArrowDiv);
	this.Width  = opts['Width'];
	this.Orientation =  opts['Orientation'];
	this.TargetElement = document.getElementById(cTargetElementID);
	this.appendIframeShim();
	this.position();
	if(!isElementInView(this.TargetElement.id)){
		this.TargetElement.scrollIntoView(false)
	}
}
_MessageBubble.prototype.position = function(){
	if(this.mainSpan == null)
		return false;
	var arrTargetElementPosition = findPos(this.TargetElement);
	var cTargetElementPositionX = arrTargetElementPosition[0];
	var cTargetElementPositionY = arrTargetElementPosition[1];
	this.FirstChild = getChildElements(this.mainSpan)[0];
	this.FirstChild.style.width = this.Width + 'px';
	var nHeight = this.FirstChild.offsetHeight;
	var nArrowHeight = parseInt(this.ArrowUpHeight);
	var nArrowWidth =  parseInt(this.ArrowUpWidth);	
	switch (this.Orientation){
		case "bottom":
			var nTop = cTargetElementPositionY + this.TargetElement.offsetHeight;
			var nLeft = cTargetElementPositionX - Math.floor((this.Width / 2)) + Math.floor((this.TargetElement.offsetWidth / 2));
			this.FirstChild.style.left = nLeft + 'px';
			if(this.showArrow){
				this.FirstChild.style.top = (nTop + nArrowHeight - (this.MainHTMLOffsetTop * 1)) +  'px';
				this.ArrowDiv.style.position = 'absolute';
				this.ArrowDiv.style.zIndex = '11';
				this.ArrowDiv.style.top = nTop + 'px';
				this.ArrowDiv.style.left = nLeft + Math.floor((this.Width / 2)) + 'px';
				this.ArrowDiv.innerHTML = this.ArrowUp;
			}else{
				this.FirstChild.style.top = (nTop - (this.MainHTMLOffsetTop * 1)) +  'px';
			}
		break;
		case "top":
			var nTop = cTargetElementPositionY - nHeight;
			var nLeft = cTargetElementPositionX - Math.floor((this.Width / 2)) + Math.floor((this.TargetElement.offsetWidth / 2))
			this.FirstChild.style.left = nLeft  + 'px';
			if(this.showArrow){
				this.FirstChild.style.top = (nTop - nArrowHeight + (this.MainHTMLOffsetBottom * 1)) + 'px';
				this.ArrowDiv.style.position = 'absolute';
				this.ArrowDiv.style.zIndex = '11';
				this.ArrowDiv.style.top = ( nTop + nHeight - nArrowHeight ) + 'px';
				this.ArrowDiv.style.left = nLeft + Math.floor((this.Width / 2))   + 'px';
				this.ArrowDiv.innerHTML = this.ArrowDown;
			}else{
				this.FirstChild.style.top = (nTop - (this.MainHTMLOffsetBottom * 1)) + 'px';
			}
		break;
		case "left":
			var nTop  = cTargetElementPositionY - Math.floor((nHeight / 2))  + Math.floor((this.TargetElement.offsetHeight / 2))
			var nLeft = cTargetElementPositionX - ((this.Width * 1) + 2)
			this.FirstChild.style.top = nTop + 'px';
			if(this.showArrow){
				this.FirstChild.style.left = (nLeft  - nArrowWidth + (this.MainHTMLOffsetRight * 1))+'px';
				this.ArrowDiv.style.position = 'absolute';
				this.ArrowDiv.style.zIndex = '11';
				this.ArrowDiv.style.top = nTop + Math.floor((nHeight / 2)) - (nArrowHeight - 5) + 'px';
				this.ArrowDiv.style.left = (nLeft + (this.Width * 1) - nArrowWidth) + 'px';
				this.ArrowDiv.innerHTML = this.ArrowRight;
			}else{
				this.FirstChild.style.left = (nLeft  + (this.MainHTMLOffsetRight * 1) )+'px';
			}
		break;
		case "right":
			var nTop = cTargetElementPositionY  -  Math.floor((nHeight / 2)) + Math.floor((this.TargetElement.offsetHeight / 2));
			var nLeft = cTargetElementPositionX + this.TargetElement.offsetWidth;
			this.FirstChild.style.top = ( nTop )+  'px';
			if(this.showArrow){
				this.FirstChild.style.left = (nLeft + nArrowWidth - (this.MainHTMLOffsetLeft * 1)) + 'px' ;
				this.ArrowDiv.style.position = 'absolute';
				this.ArrowDiv.style.zIndex = '11';
				this.ArrowDiv.style.top = nTop + Math.floor((nHeight / 2)) - (nArrowHeight - 5) + 'px';
				this.ArrowDiv.style.left = (nLeft) + 'px';
				this.ArrowDiv.innerHTML = this.ArrowLeft;
			}else{
				this.FirstChild.style.left = (nLeft - (this.MainHTMLOffsetLeft * 1)) + 'px' ;
			}
		break;
		default:
			this.FirstChild.style.top = cTargetElementPositionY + 'px';
			this.FirstChild.style.left = cTargetElementPositionX + 'px';
		break;		
	}
	this.positionIframeShim();
}
_MessageBubble.prototype.appendIframeShim = function(){
	if (typeof document.body.style.maxHeight == "undefined"){
		this.Iframe = document.createElement('IFRAME');
		this.Iframe.style.position = 'absolute';
		this.Iframe.style.zIndex = '1';
		this.Iframe.style.top = '0px';
		this.Iframe.style.left = '0px';
		this.Iframe.src="javascript:false;"
		this.Iframe.frameBorder="0";
		this.Iframe.scrolling="no";
		this.Iframe.style.height = '1px';
		this.Iframe.style.width = '1px';
		this.Iframe.id = 'StdDynamicInfoBoxIframe';
		this.mainSpan.appendChild(this.Iframe);		
	}else{
		this.Iframe = null;
	}
}	
_MessageBubble.prototype.positionIframeShim = function(){
	if (this.Iframe != null){
		this.Iframe.style.top = this.FirstChild.style.top;
		this.Iframe.style.left = this.FirstChild.style.left;		
		var obj = this;		
		if(jsTourOverlay.CurrentStepIndex == 0){
			setTimeout(function(){obj.resizeIframeShim();} , "300");
		}else{		
			obj.resizeIframeShim();
		}
	}
}
_MessageBubble.prototype.resizeIframeShim = function(){
	if (this.Iframe != null){
		if(this.MainHTMLIframeOffset){
			this.Iframe.style.width = (this.FirstChild.style.width.replace(/\D/g,''))+ 'px';		
			this.Iframe.style.height = this.FirstChild.offsetHeight + 'px';		
		}else{
			var nBorder = this.MainHTMLOffsetTop * 1;
			this.Iframe.style.width = (this.FirstChild.style.width.replace(/\D/g,'')) - ((this.MainHTMLOffsetRight * 1) - nBorder)  + 'px';		
			this.Iframe.style.height = this.FirstChild.offsetHeight  - ((this.MainHTMLOffsetBottom * 1) - nBorder)  + 'px';
		}
	}
}

_MessageBubble.prototype.close = function(){
	if(this.mainSpan != null){
		var oParent = this.mainSpan.parentNode;
		oParent.removeChild(this.mainSpan);
		this.mainSpan = null;
	}
}
function isElementInView(cElementID){
	var oElement=document.getElementById(cElementID);
	var nElementWidth = oElement.offsetWidth;
	var nElementHeight = oElement.offsetHeight;
	var arrPos = findPos(oElement);
	var nPosX = arrPos[0]
	var nPosY = arrPos[1]
	var arrViewPort = getViewportDimensions();
	var nWidth = arrViewPort[0];
	var nHeight = arrViewPort[1];
	var nScrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
	var nScrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
	var RightEdgeElement = (nPosX+nElementWidth);
	var BottomEdgeOfElement = (nPosY+nElementHeight);	
	//if(RightEdgeElement < (nWidth-nScrollX) && BottomEdgeOfElement > (nHeight-nScrollY)){
	if(BottomEdgeOfElement > (nScrollY) &&  BottomEdgeOfElement < (nHeight-nScrollY)){
		return true;
	}else{
		return false;
	}
}
function getViewportDimensions() {
      if (window.innerHeight!=window.undefined)
      	return [window.innerWidth, window.innerHeight];
      if (document.compatMode=='CSS1Compat')
      	return [document.documentElement.clientWidth , document.documentElement.clientHeight];
      if (document.body)
      	return [document.body.clientWidth , document.body.clientHeight];
      return [0,0];
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function getChildNodes(oXmlNode){
	var arrChildNodes=[];
	for(var i=0; i<oXmlNode.childNodes.length;i++){
		if(oXmlNode.childNodes[i].nodeType != 3){
			arrChildNodes.push(oXmlNode.childNodes[i]);
		}
	}
	return arrChildNodes;
}
var jsTourOverlay = new _TourOverlay();