// Popup Overlay JavaScript Document
/**************** The 4 functions below are performed when the popup layer is open ****************************/
var DealerPopupPanel = Class.create();
Object.extend(DealerPopupPanel.prototype, {
	initialize: function(element){
		this.element = null;	//The popup panel root element
		this._cache = null;	//Cached popup results

		this.html_is_loaded = null;	//If the html for the panel had been loaded
		this.error_loading_panel = null;//If the panel cannot be loaded.  Disable popup if true;

		this.currentID = null;
		this.currentElement = null;

		this.ajaxQueueSingle = new Ajax.Queue(Ajax.Queue.Single, false);
		this.ajaxQueueFifo = new Ajax.Queue(Ajax.Queue.FIFO, true);
		this.scroll_events = false;
	},
	loadPanelHTML: function(definition_id, popType){

		var onerror = function(){
			this.html_is_loaded = false;
			this.error_loading_panel = true;
		};

		var templateAjax = new Ajax.Updater(
			{success: 'wrapper'}, 
			'/lexus-share/includes/popup_panel_dealer.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();			
	},
	oncomplete: function(){
		this.html_is_loaded = true;
		this.error_loading_panel = false;

		this.element = $('popupOverlay');

		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')});

		Event.observe(window, 'keypress', function(event){if(event.keyCode == Event.KEY_ESC) DealerPopup.close();}.bindAsEventListener(this));

	},
	showDefinition: function(element, definition_id, popType){
		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();
	},
	close: function(){
	  if(!$('popupOverlay')) {
	  	return;
	  }
	  $('popupOverlay').style.display = 'none';
	  $('popupOverlay').style.visibility = 'hidden'; 
	  this.destroyScrollEvents();
	},
	buildAjaxUpdater: function(definition_id, popType) {
		
		var defURL 	=	'';
		
		if (popType == 'adhours') {
				defURL = '/lexus/jsp/pub/dealers/find_results/additional_hours.jsp?dealer_id=' + definition_id
				//defURL = '/lexus/jsp/pub/dealers/find_results/sample_add_hours.html';
		}
		else if(popType != 'gl') {
				defURL = '/lexus-share/includes/' + definition_id.toLowerCase() + '.incl'
		}
		else { 
			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)
			});
	},
	preparePopupOvlay: function() {
		this.swapHdr(this.currentType);
		this.reposition(this.currentElement);
		this.initScrollPopupOverlay();
		if(document.all){
			//Fix IE drawing problems by forcing it to redraw this element
			this.element.style.display = 'none';
			this.element.style.display = 'block';
		}
	},
	swapHdr: function(pType) { //changes the Header image in the PopupOverlay
		
		if(pType == 'adhours')
		{
			$('popupHeader').className = 'adhours';
		}
		else if(pType == 'gl') {
			$('popupHeader').className = 'glossaryHdr';
		}
		else {
			$('popupHeader').className = pType;
		}
	},
	initScrollPopupOverlay: function(){
		// 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;

	},
	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];
		}
	},
	destroyScrollEvents: function(){
		if(this.scroll_events){
			Event.stopObserving(this.scroll_events[0]);
			Event.stopObserving(this.scroll_events[1]);
			this.scroll_events = false;
		}
	},
	reposition: function(element){

		$('popupOverlay').style.display = 'block';
		Element.addClassName($('popupOverlay'), 'dealer');
		var left = Position.cumulativeOffset(element)[0];
		var top = Position.cumulativeOffset(element)[1];
		var leftpos = (left - (this.element.offsetWidth + (element.offsetWidth *1.5)));
		if(document.all) {
			var leftpos = (left - (this.element.offsetWidth + (element.offsetWidth *4)));
		}
		var toppos = (top - (this.element.offsetHeight));
		if(document.all) {
			var toppos = (top - (this.element.offsetHeight - (element.offsetHeight *.5)));
		}

		var scrollX = (document.all)?document.documentElement.scrollLeft:window.pageXOffset;
		var scrollY = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
		var window_width = (document.all)?document.documentElement.clientWidth:window.innerWidth;

		if(leftpos + this.element.offsetWidth >= (window_width + scrollX)){
			leftpos = ((window_width + scrollX) - this.element.offsetWidth) - 15;
		}

		$('popupOverlay').style.left = (leftpos > scrollX ? leftpos : scrollX) + 'px';
		
		//for additional hours window
		if ($('popupHeader').className == 'adhours')
			$('popupOverlay').style.left = '450px';
	
		$('popupOverlay').style.top = (toppos > scrollY ? toppos - 20 : scrollY) + 'px';

		// Initially hidden
		$('popupOverlay').style.visibility = 'visible';
	}
});

var DealerPopup = new DealerPopupPanel();
