PortalPopup = {
	overrideReportingInfo : false,
	setOverrideReportingInfo : function(applyDefault) {
		this.overrideReportingInfo = applyDefault;
	},
	/*
	 * Sets up the event handlers for the scrollbar, widget container and popup
	 * container
	 */
	popupReady : function() {
		PortalPopup.scrollerSetup();
		/*
		 * Attaches the event handlers for popup overlay, widget close button,
		 * widget container and popup container
		 */
		PortalPopup.triggerPopupEvents();
	},
	/*
	 * Event handlers for the child popup container, scroll bar Gripper and drag
	 * object
	 */
	scrollerSetup : function() {
		/*
		 * When we are keeping our mouse on the store locator widget then only
		 * we need to scroll the widget, so mouseover event for the widget
		 * activates the scroll area.
		 */
		var childPopupContainerObj = jQuery('#childPopup_Container');
		childPopupContainerObj.mouseover(function() {
			if (jQuery('#childPopup_ScrollBar').css('display') == 'block') {
				CustomScroller.setScrollAreaActive('childPopup');
			}
		});
		// De-Activates the scroll area of the store locator widget
		childPopupContainerObj.mouseout(function() {
					CustomScroller.setScrollAreaInactive('childPopup');
				});
		// onclick event for the scroll bar track allows the scroll action.
		jQuery('#childPopup_GripperContainer').click(function(event) {
					CustomScroller.scrollBarTrackClick('childPopup', event);
				});
		// Starts the scrolling action on mouse down of the scroll bar gripper.
		jQuery('#childPopup_Drag').mousedown(function(event) {
					CustomScroller.scrollBarDragStart(event, this.id);
				});
	},
	/*
	 * Shows the Schedule Service popup Parameters:- 1.dealerIndex : Dealer
	 * Index
	 */
	showPopup : function(dealerIndex, url, formName, popupPixelData) {
		dealerIndex += 1;
		var popupBodyObj = jQuery("#popupBody");
		jQuery("div.directionsPrint").addClass('hideLayer');

		// Sets the header for popup
		jQuery("#popupHeader").html(jQuery("#dealerInfoHeader" + dealerIndex).html());
		popupBodyObj.html("").removeClass('hideLayer').addClass('showElement');
		// Hides the Thank you message
		jQuery('#popupThankYouMessage').removeClass('showElement').addClass('hideLayer');
		/*
		 * Build query string for the popup pixel data. The pixel data will be
		 * passed to emailform as a query string.
		 */
		if (popupPixelData) {
			if (this.overrideReportingInfo) {
				var pixelDataQS = "&pixelWidgetName=" + popupPixelData.widgetName
								+ "&pixelPopupName=" + popupPixelData.popupName
								+ "&applyDefault=" + this.overrideReportingInfo;
			} else {
				var pixelDataQS = "&pixelWidgetName="
						+ popupPixelData.widgetName + "&pixelPopupName="
						+ popupPixelData.popupName;
			}
			// url will already have the querystring so append with & symbol
			url = url + pixelDataQS;
		}
		// loads 'Schedule Services' form in the popup
		WSCore.load("popupBody", url, function() {
					PortalPopup.popupCallBack(formName, dealerIndex);
				});
	},
	/*
	 * Shows the popup overlay and popup Parameters:- 1.formName: Form Name
	 * 2.dealerIndex : Dealer Index
	 */
	popupCallBack : function(formName, dealerIndex) {
		jQuery('#popupOverlay').removeClass('hideLayer').addClass('showElement');
		jQuery('#dgpPopup').removeClass('hideLayer').addClass('showElement');

		// selects the Web-Id of the dealer in the hidden dropdown
		if (jQuery("form[name='" + formName + "'] select[name='web_id']").length > 0) {
			jQuery("form[name='" + formName + "'] select[name='web_id']")
					.val(jQuery("#dealerWebId" + dealerIndex).html());
		}

		PortalPopup.showHideScrollbar();

		// At the time of popup load, scrolls the popups to the position 0.
		PortalPopup.scrollTo(0, 'childPopup');
	},
	/*
	 * Shows/Hides the scrollbar
	 */
	showHideScrollbar : function() {
		/*
		 * If the scroll bar is required then setting the scroll height
		 * otherwise hiding the scroll bar. If the height of scrolling Area of
		 * Child popup is greater than content height, the scrollbar will appear
		 * else scrollbar will be hidden.
		 */
		var childPopupScrollBarObj = jQuery('#childPopup_ScrollBar');
		var childPopupContentObj = jQuery('#childPopup_Content');
		if (jQuery('#childPopup_ScrollingArea').height() > childPopupContentObj.height()) {
			childPopupContentObj.removeClass().addClass('popupWithScroll');
			childPopupScrollBarObj.show();
			CustomScroller.setScrollbarHeight('childPopup');
		} else {
			childPopupScrollBarObj.hide();
			// At the time of scroll bar hide, de-activating the mouse wheel scroll.
			CustomScroller.setScrollAreaInactive('childPopup');
			childPopupContentObj.removeClass().addClass('popupWithoutScroll');
		}

		// Scrolls the popups to the position 0.
		PortalPopup.scrollTo(0, 'childPopup');
	},
	/*
	 * Upon clicking the marker, highlights the appropriate dealer from the
	 * nearest dealership widget, if we have more dealers and the selected
	 * dealer is not in the viewable area then we have to scroll the nearest
	 * dealership widget automatically, the below function performs this
	 * operation.
	 */
	scrollTo : function(scrlAmt, elem) {
		jQuery("#" + elem + "_ScrollingArea").css('top', '0px');
		var scrollPos = CustomScroller.getTopScrollAmount(elem) - scrlAmt;
		scrollHeight = CustomScroller.getScrollHeight(elem);
		if (scrollPos > 0 && scrollPos < (-1 * scrollHeight))
			scrollPos = (-1 * scrollHeight);
		jQuery("#" + elem + "_ScrollingArea").css('top', scrollPos + 'px');
		CustomScroller.setScrollBarPercentage(elem, CustomScroller
						.getScrollPercentage(elem));
	},
	triggerPopupEvents : function() {
		/*
		 * Event handler to identify click event for widget container and popup
		 * contianer
		 */
		jQuery('#dgpWidgetContainer, #dgpPopupContainer').click(function(e) {
					e.stopPropagation();
				});
		/*
		 * Event handler to identify the click event for popup overlay and
		 * widget close button
		 */
		jQuery('#popupOverlay, #popupCloseButton').click(function(e) {
			e.stopPropagation();
			jQuery('#dgpPopup').removeClass('showElement')
					.addClass('hideLayer');
			jQuery('#popupOverlay').removeClass('showElement')
					.addClass('hideLayer');
		});
	}
};

