// Popup Overlay JavaScript Document
/**************** The 4 functions below are performed when the popup layer is open ****************************/
function hidePW(){
 	Popup.close();
}
function initScrollPopupOverlay() {
  // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo1 = new dw_scrollObj('popupWn', 'popupLyr', null);

  // arguments: dragBar id, track id, axis ("v" or "h"), x offset, y offset
  // (x/y offsets of dragBar in track)
  wndo1.setUpScrollbar("dragBar", "track", "v", 0, 0);

  //dw_showLayers("scrollbar","popupOverlay");
  return wndo1;
}

function swapHdr(pType) { //changes the Header image in the PopupOverlay
	
	if(pType == 'gl') {
		$('popupHeader').className = 'glossaryHdr';
		
	}
	else {
		$('popupHeader').className = pType;
	}
}

var Glossary = new GlossaryLoad(); // A unique function for Glossary definitions
function GlossaryLoad() {
	var obj;
	var def_id;
}
GlossaryLoad.prototype.showGlossary = function (obj, def_id) {
	Popup.showDefinition (obj, def_id, 'gl');
};


function PopupPanel(){ //The generic PopupOverlay function
	
	var element;	//The popup panel root element
	var _cache;	//Cached popup results
	
	var html_is_loaded;	//If the html for the panel had been loaded
	var error_loading_panel;//If the panel cannot be loaded.  Disable popup if true;
	
	var currentID;
	var currentElement;
	
	this.ajaxQueueSingle = new Ajax.Queue(Ajax.Queue.Single, false);
	this.ajaxQueueFifo = new Ajax.Queue(Ajax.Queue.FIFO, true);
	
}

PopupPanel.prototype.loadPanelHTML = function(definition_id, popType){
	
	var onerror = function(){
		this.html_is_loaded = false;
		this.error_loading_panel = true;
	};
	
	var templateAjax = new Ajax.Updater(
		{success: document.body}, 
		'/lexus-share/includes/popup_panel.incl', 
		{ 
			method: 'get',
			onComplete: function() {this.oncomplete()}.bind(this),
			wait: true,
			insertion: Insertion.Bottom,
			onError: onerror.bind(this)
		});
	var glossaryAjax = this.buildAjaxUpdater(definition_id, popType);
	
	this.ajaxQueueFifo.addAction(templateAjax);
	this.ajaxQueueFifo.addAction(glossaryAjax);
	this.ajaxQueueFifo.start();			
};

PopupPanel.prototype.oncomplete = function(){
	this.html_is_loaded = true;
	this.error_loading_panel = false;
	
	this.element = $('popupOverlay');
	
	/* 
	//	These settings will have make the box scroll on hover and scroll twice as fast on click
	Event.observe($('upMover'), 'mouseover', function(){dw_scrollObj.initScroll('popupWn','up')});
	Event.observe($('upMover'), 'mouseout', function(){dw_scrollObj.stopScroll('popupWn','up')});
	Event.observe($('upMover'), 'mousedown', function(){dw_scrollObj.doubleSpeed('popupWn')});
	Event.observe($('upMover'), 'mouseup', function(){dw_scrollObj.resetSpeed('popupWn')});
	
	Event.observe($('downMover'), 'mouseover', function(){dw_scrollObj.initScroll('popupWn','down')});
	Event.observe($('downMover'), 'mouseout', function(){dw_scrollObj.stopScroll('popupWn','down')});
	Event.observe($('downMover'), 'mousedown', function(){dw_scrollObj.doubleSpeed('popupWn')});
	Event.observe($('downMover'), 'mouseup', function(){dw_scrollObj.resetSpeed('popupWn')});
	*/
	
	//	These settings will make the box scroll only when clicked
	Event.observe($('upMover'), 'mousedown', function(){dw_scrollObj.initScroll('popupWn','up')});
	Event.observe($('upMover'), 'mouseup', function(){dw_scrollObj.stopScroll('popupWn','up')});
	
	Event.observe($('downMover'), 'mousedown', function(){dw_scrollObj.initScroll('popupWn','down')});
	Event.observe($('downMover'), 'mouseup', function(){dw_scrollObj.stopScroll('popupWn','down')});

	
	Event.observe(window, 'keypress', function(event){if(event.keyCode == Event.KEY_ESC) hidePW();}.bindAsEventListener(this));

};
PopupPanel.prototype.showDefinition = function(element, definition_id, popType,dontMove){
	this.dont_move = (dontMove==1)?true:false;
	this.currentType = popType;
	this.currentElement = element;
	
	if(!PAGE_LOAD) {
		return;
	}
	
	if(this.html_is_loaded){
		this.ajaxQueueSingle.addAction(this.buildAjaxUpdater(definition_id, popType));
		this.ajaxQueueSingle.start();	
	}
	else {
		this.loadPanelHTML(definition_id, popType);
	}
	
	this.destroyScrollEvents();
	this.setupScrollEvents();
};

PopupPanel.prototype.close = function(){
  if(!$('popupOverlay')) {
  	return;
  }
  $('popupOverlay').style.display = 'none';
  $('popupOverlay').style.visibility = 'hidden'; 
  this.destroyScrollEvents();
}

PopupPanel.prototype.buildAjaxUpdater = function(definition_id, popType) {
	if (popType != 'gl') {
			var defURL = '/lexus-share/includes/' + definition_id.toLowerCase() + '.incl'
	}
	else { 
		var defURL = '/lexus-share/includes/glossary/' + definition_id.toUpperCase() + '.incl' 
	}
	
	return new Ajax.Updater(
		{success: 'popupLyr'},
		defURL, 
		{ 
			method: 'get',
			wait: true,
			onComplete: function() {this.preparePopupOvlay()}.bind(this)
		});
};
PopupPanel.prototype.preparePopupOvlay = function() {
	swapHdr(this.currentType);
	if(!this.dont_move){
		this.reposition(this.currentElement);
	}
	initScrollPopupOverlay();
		//Fix IE drawing problems by forcing it to redraw this element
		this.element.style.display = 'none';
		this.element.style.display = 'block';
};

PopupPanel.prototype.setupScrollEvents = function(){
	if(!this.scroll_events){
		var scroll_event = Event.observe(window, 'scroll', function(){this.reposition(this.currentElement)}.bind(this));
		var resize_event = Event.observe(window, 'resize', function(){this.reposition(this.currentElement)}.bind(this));
		this.scroll_events = [scroll_event, resize_event];
	}
};

PopupPanel.prototype.destroyScrollEvents = function(){
	if(this.scroll_events){
		Event.stopObserving(this.scroll_events[0]);
		Event.stopObserving(this.scroll_events[1]);
		this.scroll_events = false;
	}
};

PopupPanel.prototype.reposition = function(element){

	$('popupOverlay').style.display = 'block';
	var left = Position.cumulativeOffset(element)[0];
	var top = Position.cumulativeOffset(element)[1];
	
	var leftpos = (left - (this.element.offsetWidth / 2 ) - 5);
	var toppos = (top - (this.element.offsetHeight));
	
	var scrollX = (document.all)?document.body.scrollLeft:window.pageXOffset;
	var scrollY = (document.all)?document.body.scrollTop:window.pageYOffset;
	var window_width = (document.all)?document.body.clientWidth:window.innerWidth;
	
	if(MODEL_ID == 'CPO Dealerized') {
		//The following applies only to the Dealerized Model Detail pages. This prevents the Glossary overlay from appearing in the area reserved for the Dealer left navigation.
		leftpos = left;
		}
		
	if(leftpos + this.element.offsetWidth >= (window_width + scrollX)){
		leftpos = ((window_width + scrollX) - this.element.offsetWidth) - 15;
	}
	
	
	$('popupOverlay').style.left = (leftpos > scrollX ? leftpos : scrollX) + 'px';
	$('popupOverlay').style.top = (toppos > scrollY ? toppos : scrollY) + 'px';
	
	// Initially hidden
	$('popupOverlay').style.visibility = 'visible';	
	
	
};

var Popup = new PopupPanel();