/* Cobalt.Website.VehicleDetail.InTransitDisclaimerPopup.js */

/* some of the comments are exceptionally verbose so as to reduce/remove ambiguity between which module (Sandbox vs jQuery vs Native JS) we're envoking. */
Cobalt.Website.Common.InTransitDisclaimerPopupModule = function(sandbox){

	
/* PRIVATE MEMBER DATA  & METHODS */
	
	// create a reference to this class so these below ... private? .... functions have access to it
	
	return {
           init: function(){
		var _this_class = this;
		var listenLiveObjs = sandbox.dom(".inTransitNotice");           
		   
		listenLiveObjs.listenLive("mouseenter", function(e) {
			_this_class.showFor.apply(_this_class, [e]);
		}); 
	
		listenLiveObjs.listenLive("mouseleave", function(e) {
			_this_class.mouseOff.apply(_this_class, [e]);
		});           
		
           }
	   
           ,destroy: function(){
		// do nothing
           }
	   ,showFor: function(e){
		
		// To better know the physical location on the page, the "popup" div is attached
		// to the element it is to appear next to.
		
		// SHORT-CIRCUIT:  If we're already attached to this currentObject, do nothing
		if(jQuery(e.currentTarget)==this.currentObject)   return; 
		
		   
		// Set a reference to the "current target" the user is hovering over
		this.currentObject = jQuery(e.currentTarget);
		
		/* To improve control of positioning, attach the disclamer bubble to the element of relative position*/
	
		// reference the popup dom object via Core JS Fnx
		var dom_disclaimerPopUp = document.getElementById(this.popupId);
	
		// remove the popup dialogbox form the current parent
		dom_disclaimerPopUp.parentNode.removeChild(dom_disclaimerPopUp); 
	
		// Attach popup dialogbox to new current object via a jQuery Append
		this.currentObject.append(dom_disclaimerPopUp);
		
		
		/* Determine the position of the popup disclaimer */
		var newLeft = this.currentObject.position().left + this.currentObject.width() + 3;
		
		/* this 97 is based off the location of the left tick + about 1/2 the height of the tick*/
		var newTop = this.currentObject.position().top - 97;
		
		// use SANDBOX layer to transparently pass new CSS => jQuery => domObject
		sandbox.dom("#" + this.popupId).addStyle({top:newTop, left: newLeft});
		
		/* if we were about to do a fade-out at another location on the page, cancel that */
		clearTimeout(this.fadeOutTimer);
		
		/* cancel the fade-out if one was started */
		this.fadingOut = false;
		
		jQuery("#" + this.popupId).fadeIn(200);
	}
	
	, mouseOff: function(e){
		// note that wer're about to fade out
		this.fadingOut = true;
		
		this.fadeNow(e);

/*		
		// create a reference to this object so it can be accessed in a set-timeout (where "this" means something entirely different) 
		// track the setTimeout so it can be cleared if needed.
		// this.fadeOutTimer = setTimeout(function(){this.fadeNow(e)}, 500);
		this.fadeOutTimer = setTimeout(
			function(){
				_this_class.fadeNow.apply(_this_class, [e])
			}
			, 500
			);
*/
	}
	,fadeNow:function(e){
		// only fade if we're still supposed to be fading, or maybe we just moved
		// the disclaimer box to another locaiton, because we're hovering over another 
		// box, in wich case we just need to reposition the dialog.
		
		if(!this.fadingOut)
			return;
		

		
		// placed before the fade-out incase user manages to generate an avoidable race
		// condition with the asynchronous fading
		this.currentObject = null;
		
		//fadeout
		// not using jquery fade-out because IE7 doesn't play well with it.
		document.getElementById(this.popupId).style.display="none";

	}
	
		
	,popupId : "inTransitDisclaimerPopup"
	,fadeOutTimer: null
	,currentObject: null
	,fadeOutTimer: null
	,fadingOut: false
	
   }
}
