/**
 * popupTitle.js
 * The popupTitle object displays a custom pop of an elements 'title' attribute value.
 * This file is included in application.php and has been incorporated into controller.js
 * for easily attaching the popupTitle object to any element.
 *
 * How To Use:
 * 1. Populate the title address of the chosen element.
 * 2. Add the class e_popupTitle as the first class of the chosen element.
 *
 * @date 22 Oct 2008
 * @author Matt Kosolofski
 */

var popupTitle = { 
	
	// Static offsets of the popup from the cursor.
	popupStaticOffsetLeft : 5,
	popupStaticOffsetTop : 5,

	// The max number of times each popup title can display. -1 means that this feature is disabled.
	popUpMaxDisplayTimes : -1,
	
	// Keeps track of the pop titles already viewed as well as how many times a title has been viewed
	titlesViewed : new Object(),
	
	// Contains a list element ids and their 'title' attribute values.
	elements : new Object(),
	
	/**
	* Displays the title popup with a specified elements 'title' attribute value.
	*
	* @param (int) cursorXCoord  The x position of the cursor.
	* @param (int) cursorYCoord  The y position of the cursor.
	* @param (element object) activeElement  The element containing the title that will be used 
	*                                        to display in the popup.
	**/
	displayPopUp : function(cursorXCoord, cursorYCoord, activeElement) {
		// Retreives the active elements id. If the element does not have
		// an id, the prototype 'identify' functions creates and assigns a 
		// unique one.
		var activeElementID = activeElement.identify();
		
    	// Store the active elements text if it already isn't.
		if(!popupTitle.elements[activeElementID]) popupTitle.elements[activeElementID] = activeElement.getAttribute('title');
		
		// Store that the title has been viewed as well as increment that it has been viewed.
        if(!popupTitle.titlesViewed[activeElementID]) {
            popupTitle.titlesViewed[activeElementID] = 1;
        } else {
            popupTitle.titlesViewed[activeElementID] += 1;
        }

		// Clear the active elements title so that the user's browser does
		// not display it as a tool tip. 
		activeElement.writeAttribute('title','');

		// Initialize the popup div if it does not already exist.
		if(!$('popUpTitleContainer')) $('wrapper').insert({bottom:'<div id="popUpTitleContainer" style="top:0px;display:none;"></div>'});

		// If popUpMaxDisplayTimes is enabled and the user has reached the view limit for this pop up, 
		// don't display the pop up.
        // e_popupTitleAlwaysShow will counter the max number of times the pop up appears.
		if (popupTitle.titlesViewed[activeElementID] > popupTitle.popUpMaxDisplayTimes && 
		    popupTitle.popUpMaxDisplayTimes > -1 &&
            activeElement.className != 'e_popupTitleAlwaysShow'
		) {
		    return;
		} else {
  				
    		// Update the pop up with the elements title attribute value.
    		$('popUpTitleContainer').update("<div>" + popupTitle.elements[activeElement.id] + "</div>");
    		
    		// Set the pop up position on the page.
    		popupTitle.setPopUpPosition(cursorXCoord, cursorYCoord);
    		
    		// Actually display the pop up.
    		$('popUpTitleContainer').style.display = '';
		}
	},
	
	/**
	* Hides the title popup.
	*
	* @param (element object) activeElement  The element that caused the title popup to display.
	*/
	hidePopUp : function(activeElement){
		$('popUpTitleContainer').style.display = 'none';
		
		// Set the 'title' attribute of the element since it was erased when the
		// popup was displayed (prevented tool tip from appearing).
		activeElement.writeAttribute('title',popupTitle.elements[activeElement.id]);
        
        // take out the popup title class if we no longer need to show it again
        if (popupTitle.titlesViewed[activeElement.id] > popupTitle.popUpMaxDisplayTimes && 
		    popupTitle.popUpMaxDisplayTimes > -1 &&
            activeElement.hasClassName('e_popupTitle')
		) {
		    activeElement.removeClassName('e_popupTitle');
		}
	},
	
	/**
	* Calculates the 'absolute' position of the title pop-up on the page.
	*
	* @param (int) cursorXCoord  The x position of the cursor.
	* @param (int) cursorYCoord  The y position of the cursor.
	*/	
	setPopUpPosition : function(cursorXCoord, cursorYCoord){
		var topOffset = parseInt(cursorYCoord + popupTitle.popupStaticOffsetTop),
			leftOffset = parseInt(cursorXCoord + popupTitle.popupStaticOffsetLeft);
			
		//Set the left position of the pop up title object.
		if ( parseInt( document.documentElement.clientWidth + document.documentElement.scrollLeft ) < 
		     parseInt( $('popUpTitleContainer').offsetWidth + leftOffset ) 
		   ) 
		{
			$('popUpTitleContainer').style.left = parseInt( leftOffset - $('popUpTitleContainer').offsetWidth )+'px';
		} else {
			$('popUpTitleContainer').style.left = leftOffset + 'px';
		}
		
		//Set the top position of the pop up title object.
		if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < 
		     parseInt($('popUpTitleContainer').offsetHeight + topOffset) 
		   ) 
		{
			$('popUpTitleContainer').style.top = parseInt( topOffset - $('popUpTitleContainer').offsetHeight )+'px';
		} else {
			$('popUpTitleContainer').style.top = topOffset+'px';
		}		
	}	
};

