﻿var divPopup = null;
var divOverlay = null;
var divWidth = '';
var divHeight = '';
var srcElement = null;
var autoClose = true;
var modal = true;
var selectorParent = null;

$(document).ready(function() {
	// we want to append the popup element to the first available form object
	// to resolve an issue where asp.net events do not fire.
	var appendTargetElement = null;
	if (document.forms[0] != null) {
		appendTargetElement = document.forms[0];
	}
	else if (document.forms.item[0] != null) {
		appendTargetElement = document.forms.item[0];
	}
	else {
		appendTargetElement = document.body;
	}

	// create and hide container for popup content
	divPopup = $('<div id=\'divPopup\' class=\'divpop\'></div>').appendTo(appendTargetElement);
	$(divPopup).hide();

	// create and hide translucent overlay for modal style
	divOverlay = $('<div id=\'widget-overlay\' class=\'ui-inline-overlay\'></div>').appendTo(appendTargetElement);
	$(divOverlay).hide();


	// attach a click event to a & img elements created by popup control.
	$('a.more-info, img.more-info').click(function(event) {
		var url = "/_popups/";
		srcElement = $(this);
		autoClose = ($(this).attr("autoclose").toLowerCase() == "true");

		// if retrieving the popup contents via a web service
		if ($(this).attr("file") != null && $(this).attr("file") != '') {
			url = "";
			if ($(this).attr("servicePath") != null && $(this).attr("servicePath") != '') {
				url = "/" + $(this).attr("servicePath");
			}
			url += "/_popups/" + $(this).attr("file") + "?" + $(this).attr("qs");
			$.ajax({ type: "GET", url: url, data: "{}", contentType: "application/x-www-form-urlencoded;", dataType: "html",
				success: function(data) {
					data = replaceChars(data, '&lt;', '<');
					data = replaceChars(data, '&gt;', '>');
					data = replaceChars(data, '&amp;', '&');
					$(divPopup).html(data);
					return renderInlineJQ(event);
				},
				error: function(request, message, exception) { $(divOverlay).hide(); }
			});
		}
		// or getting the contents of an element inside a hidden panel.
		else if ($(this).attr("selector") != null && $(this).attr("selector") != '') {
			var sel = $(this).attr("selector");
			selectorParent = $(sel).parent();
			if ($(sel).exists()) {
				$(sel).appendTo(divPopup);
				return renderInlineJQ(event);
			}
			else {
				return false;
			}
		}
		return false;
	});

	// global handling to hide the popup when user clicks outside the popup.
	$(document).click(function(event) {
		if ($(divPopup) != null
			&& $(divPopup).is(':visible')
			&& !$(event.target).hasClass('more-info')) {
			// get the coordinates of where the user clicked..
			if (IE) {
				thisX = event.clientX + $(window).scrollLeft();
				thisY = event.clientY + $(window).scrollTop();
			}
			else {
				thisX = event.pageX;
				thisY = event.pageY;
			}
			var posLeft = $(divPopup).offset().left;
			var posTop = $(divPopup).offset().top;
			var posRight = $(divPopup).offset().left + $(divPopup).outerWidth();
			var posBottom = $(divPopup).offset().top + $(divPopup).outerHeight();
			// if they clicked outside the popup, and not in a child element that extends outside the popup
			// hide the popup.
			if ($(event.target).parents(' .divpop').length == 0
                && autoClose
                && ((thisX < posLeft || thisX > posRight)
                    || (thisY < posTop || thisY > posBottom))) {
				hideDivJQ();
			}
		}
	});

	// global handling for keypress event
	$(document).keypress(function(event) {
		escKeyJQ(event);
	});
});

// hides the popup when ESC is pressed
function escKeyJQ(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	if (evt.keyCode == 27) {
		hideDivJQ();
	}
}
// posts data to a URL for custom content, used with liveHelp
$.inlineObjPostJQ = function(url, data, event) {
	$.ajax({ type: "POST", url: url, data: data, contentType: "application/x-www-form-urlencoded;", dataType: "html",
		success: function(e) {
			return renderInlineJQ(e, event);
		}
	});
}
// renders the modal or popup
function renderInlineJQ(event) {
	var bodyHeight = $(document).height();
	var bodyWidth = $(document).width();
	var windowTop = $(window).scrollTop();

	if (divPopup != null) {
		
		var xPos = -1;
		var yPos = -1;
		var animate_roll = false;
		var animate_fade = false;
		yPos = parseInt($(srcElement).attr("yCoordinate"));
		xPos = parseInt($(srcElement).attr("xCoordinate"));
		animate_roll = ($(srcElement).attr("animate_roll").toLowerCase() == "true");
		animate_fade = ($(srcElement).attr("animate_fade").toLowerCase() == "true");

		if ($(srcElement).attr("popWidth") == 'auto') {
			divWidth = 'auto';
		}
		else {
			divWidth = $(srcElement).attr("popWidth");
			//alert($(srcElement).attr("popWidth"));
		}
		divPopup.width(divWidth);
		divPopup.height('auto');

		//alert('divWidth: ' + $(divPopup).width() + '; actual: ' + $(divPopup).width() + ';');
		// overlay is always shown
		$(divOverlay).css({ 'z-index': 999, 'width': $(document).width(), 'height': $(document).height() });
		$(divPopup).css({ 'z-Index': 1000, 'position': 'absolute'  });
		// if modal, display a translucent overlay beneath the popup and above the body
		$(divOverlay).show();
		
		// static x-y positioning
		if (yPos > 0 && xPos > 0) {
			yPos = yPos + $(window).scrollTop();
			xPos = xPos + $(window).scrollLeft();
			// position the popup
			$(divPopup).css({ 'left': xPos, 'top': yPos });
		}
		// center if modal
		else if (modal) {
			$(divPopup).center();
		}
		else {
			// position popup around source element
			if (yPos < 0) {
				yPos = ($(srcElement).offset().top + $(srcElement).height());
			}
			if ((yPos + $(divPopup).height() > $(window).scrollTop() + $(window).height()) && $(divPopup).height() < $(window).height()) {
				yPos = (yPos - $(srcElement).height()) - $(divPopup).height();
			}
			if (yPos < $(window).scrollTop()) {
				yPos = $(window).scrollTop() + 15;
			}
			if (yPos <= 0) {
				yPos = 0;
			}
			if (xPos < 0) {
				xPos = $(srcElement).offset().left;
			}
			if ((xPos + $(divPopup).width()) >= $(document).width()) {
				xPos = (xPos + $(srcElement).width()) - $(divPopup).width();
			}
			else if ((xPos + $(srcElement).width() + $(divPopup).width()) >= $(document).width()) {
				xPos = (xPos + $(srcElement).width()) - $(divPopup).width() - $(srcElement).width();
			}
			if (xPos <= 0) {
				xPos = 0;
			}
			// end get location of source element
			// position the popup
			$(divPopup).css({ 'left': xPos, 'top': yPos });
		}
		// after positioning, show the popup
		if (animate_roll == true) {
			$(divPopup).slideDown(1000);
		}
		else if (animate_fade == true) {
			$(divPopup).fadeIn(1500);
		}
		else {
			$(divPopup).fadeIn(350);
		}
		//show all input boxes in inline
		hideAllSelects();
	}
}
// called by legacy inline popup controls
// overrides hideDiv function defined in global.js
function hideDiv() {
	hideDivJQ();
}

// hides the popup div
// jQuery port of hideDiv from global.js
function hideDivJQ() {
	yPos = 0;
	xPos = 0;
	showFlashJQ();
	showAllSelects();
	$('#media').hide();
	$(divOverlay).hide();

	// if popup-by-selector, append content back to it's original location
	if (selectorParent != null) {
		$($(divPopup).children()[0]).appendTo(selectorParent);
		selectorParent = null;
	}
	// empty the contents of the popup after selector content is put back
	$(divPopup).html('');
	$(divPopup).hide();
	if (modal) {
		$('body').css({ 'overflow': 'visible' });
	}
}
// jQuery override of the same function from global.js to parse the DOM and hide all selects.
function hideAllSelects() {
	if (IE) {
		$('select').hide();
		$('#divPopup select').show();
	}
}
// jQuery override of the same function from global.js to parse the DOM and show all selects.
function showAllSelects() {
	$('select').show();
}

// hides the object with the id "flashcontent"
function hideFlashJQ() {
	$('#flashcontent').hide();
}
// shows the object with the id "flashcontent"
function showFlashJQ() {
	$('#flashcontent').show();
}

// returns true if the element exists, otherwise false
jQuery.fn.exists = function() { return jQuery(this).length > 0; }

// centers an element in the viewable portion of the screen.
jQuery.fn.center = function() {
	this.css("position", "absolute");
	this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
	this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
	return this;
}