// popup class
function popup(anchor) {
	anchorPointLeft=0;
	anchorPointRight=0;
	poppedUp=false;
	var popUpElement=null;
	var popUpCloseElement=null;
	var popUpBodyElement=null;
	var popUpTop=null;
	var popUpMiddle=null;
	var popUpBottom=null;
	var padtop=null;
	var popUpInnerElement=null;
	var padbottom=null;

               var title=jQuery(anchor).attr('title');

                if (jQuery(anchor).hasClass("divflag")) 
                {
                               ajaxRef="/inc/ajax_acl.asp?c=" + title;
                } 
                else if (jQuery(anchor).hasClass("goalicon"))
                {
                              ajaxRef="/inc/ajax_acl.asp?ga=" + title;
                }
	
	//ajaxRef=jQuery(anchor).attr('href');

               	// get popup content via ajax
	jQuery.ajax({
	                 url: ajaxRef,
	                 success: function(data) {
			createpopup(data);
		  },
                                  error: function(jqXHR,ts,e) {
                                                // do nothing
                                                return;
                                  }
	});

                
                function disableHyperLinks() {
                                jQuery(anchor).click(function(){
		        //jQuery(".popup").css("visibility", "hidden");
		        jQuery(".popup").css("display", "none");
		        showPopup();
		        return false;
	                });
                } 
	
	 function setanchorpoints() {
		var anchorLeft = jQuery(anchor).offset().left;
		var anchorTop = jQuery(anchor).offset().top;
		var anchorWidth = jQuery(anchor).width();
		var anchorHeight = jQuery(anchor).height();
		anchorPointLeft = anchorLeft+((anchorWidth/2)-57);
		anchorPointTop = anchorTop+(anchorHeight);
	}
	
	function createpopup(content) {
		
		// create HTML div to hold popup image
		popUpElement = document.createElement("div");
		jQuery(popUpElement).addClass('popup');
		//jQuery(popUpElement).addClass(determinePosition());
		
		// create HTML for close button
		popUpCloseElement=document.createElement("a");
		jQuery(popUpCloseElement).addClass('popupclose');
		jQuery(popUpCloseElement).click(function(){hidePopup();});
		
		// create HTML div to create padding within popup
		popUpInnerElement=document.createElement("div");
		jQuery(popUpInnerElement).addClass('inner');
		
		// create HTML div to hold header content
		popUpHeaderElement=document.createElement("div");
		jQuery(popUpHeaderElement).addClass('popupheader');
		
		// create HTML div to hold body content
		popUpBodyElement=document.createElement("div");
		jQuery(popUpBodyElement).addClass('popupbody');
		
		// create HTML h3 to hold title
		popUpTitle = document.createElement("h3");
		popUpTitle.appendChild(document.createTextNode(title));
		
		// add close button and title to header
		popUpHeaderElement.appendChild(popUpTitle);
		
		// add header and body to inner container
		popUpInnerElement.appendChild(popUpCloseElement);
		popUpInnerElement.appendChild(popUpHeaderElement);
		popUpInnerElement.appendChild(popUpBodyElement);
		
		// create HTML div to hold balloon top image
		popUpTop = document.createElement("div");
		jQuery(popUpTop).addClass('top');
		
		// create HTML div to hold balloon middle image
		popUpMiddle = document.createElement("div");
		jQuery(popUpMiddle).addClass('middle');
		
		// create HTML div to hold balloon bottom image
		popUpBottom = document.createElement("div");
		jQuery(popUpBottom).addClass('bottom');
		
		// add top\bottom padding divs inner container to popup middle
		padtop=document.createElement("div");
		jQuery(padtop).addClass('padder');
		padbottom=document.createElement("div");
		jQuery(padbottom).addClass('padder');
		
		popUpMiddle.appendChild(padtop);
		popUpMiddle.appendChild(popUpInnerElement);
		popUpMiddle.appendChild(padbottom);
		
		// add container to popup
		popUpElement.appendChild(popUpTop);
		popUpElement.appendChild(popUpMiddle);
		popUpElement.appendChild(popUpBottom);
		
		// add popup to page body
		document.getElementsByTagName("body")[0].appendChild(popUpElement);
		
		// hide popup
		//jQuery(popUpElement).css("visibility", "hidden");
                                jQuery(popUpElement).css("display", "none");

                                //Populate popup with content from remote file
                                jQuery(popUpBodyElement).html(content);

                                //route icon link clicks 
                                disableHyperLinks(); 

                                jQuery(window).resize(function() {determinePosition()});
 
	}
	
	function showPopup () {
                                jQuery(popUpElement).css("display", "block");
		determinePosition();
		//jQuery(popUpElement).fadeTo(0,0).css('visibility','visible').fadeTo(400,1);
		poppedUp=true;
	}
	
	function hidePopup() {
		//jQuery(popUpElement).css("visibility", "hidden");
                                jQuery(popUpElement).css("display", "none");
		poppedup=false;
	}
	
	function determinePosition() {
	// pixel to top of view port
	viewPortTop = jQuery(window).scrollTop()+jQuery(window).height();
	
	// pixel to top of anchor
	anchorLeft = jQuery(anchor).offset().left;
	anchorTop = jQuery(anchor).offset().top;
	anchorWidth = jQuery(anchor).width();
	anchorHeight = jQuery(anchor).height();
	anchorPointLeft = anchorLeft+((anchorWidth/2)-57);
	anchorPointTop1 = anchorTop+anchorHeight;

	// anchor top plus balloon height
	
	var totalTop = anchorPointTop1 + (jQuery(popUpElement).height()+56);

	jQuery(popUpElement).removeClass('top_left_ie top_left_not_ie bottom_left_ie bottom_left_not_ie');

	if (totalTop<viewPortTop) {
		// detect browser, 
		// if ie add class which will use gif images instead of png
		// else use png for all other browsers
		

			jQuery(popUpElement).addClass('top_left_not_ie');

		anchorPointTop = anchorTop+(anchorHeight);		
		
	} else {
		

			jQuery(popUpElement).addClass('bottom_left_not_ie');
		
		anchorPointTop = anchorTop-jQuery(popUpElement).height();
	}
	
	// position popup 
	jQuery(popUpElement).offset({ top: anchorPointTop, left: anchorPointLeft });
}
}
