/*  this file in intended to replace utils.js as code is updated using jquery
*/

/*******************************************************************************
* this code has been developed specifically for disclaimers and footnote type
* tooltips, it needs to be extended to be more general
* 1) bindTooltip function should be extracted and created as an object that 
*    inherits base class containing showTooltip, killTooltip, createTtMarkup
* 2) create distinct bind function for definition or help type tooltips
* 3) placement to depend on position of cursor for definition or help type tooltips
* 
* looks for a specific html object type to apply tooltips to, and expects a 
* corresponding div tag that contains text for tooltip.      
* uses very specific graphics and styles defined according to reskin standards
* found in /MMNA/images/global_assets/  /MMNA/css/styles-oop.css            
* element:   an html element type that contains the footnote or disclaimer symbol
* that when rolled over will display a tooltip                                    
* reference: a div tag that defines the upper left corner of the page (used
*            to calculate whether a tool tip should render left or right) 
* see switch statement for naming convention for divs containing tooltip text
* see page /jsp/owners/own.regular.content.jsp for sample usage         
*******************************************************************************/

function Tooltip(element, reference) {

	this.element   = element;
	this.reference = reference;
	if (arguments.length == 3 ) {
		this.parent = parent;
	} else {
		this.parent = null;
	}
	
	this.origin    = $(this.reference).offset();
	var thisObj    = this;
	
	// the extended character codes (128+) do not have the unicode equivalent when doing character matching
	// use: var code = str.charCodeAt(0); alert("charCode: "+code) to determine what charCode to match;
	dagger  = String.fromCharCode(8224);	// use &#134;
	dDagger = String.fromCharCode(8225);	// must use &#135; in mark-up and NOT &dagger;&dagger;
	
	this.bindTooltip = function() {
		
		$(thisObj.element).each( function() {

			var str = $(this).text();
			var footnote;
			
			switch (str) {
				case "*":
					footnote = '#asterick';
					break;
				case dagger:
					footnote = '#dagger';
					break;
				case dDagger:
					footnote = '#double-dagger';		
					break;
				case "**":
					footnote = '#double-asterick';	
					break;
				default:
					footnote = "#footnote_" + str;
			} 
			
			if (thisObj.parent) {
				var thisJqueryObj = $(this).parent();
			} else {
				var thisJqueryObj = $(this);
			} 

			function onMouseEnter(e) {

				if ($('#tooltip').length==0) {
					// be sure not to create a tooltip if previous has not yet been killed
					// guards against really fast user action
					if($(footnote).length == 0) {
						var copy = "Please accept our apologies, the text associated with this object is missing.";
					} else {
						var copy = $(footnote).html();
					}
					thisObj.showTooltip( thisJqueryObj, copy, thisJqueryObj.offset().left);
				}
			}	
			
			function onMouseLeave(e) {
				thisObj.killTooltip();
			}	
			
			thisJqueryObj.bind("mouseenter", onMouseEnter);
			thisJqueryObj.bind("mouseleave", onMouseLeave);
		});

	} // END bindToolTip	

	
	this.createTtMarkup = function () {
		var ttDiv = ""
		ttDiv += "<div id='tooltip'>";
		ttDiv += "<div class='ttTop'></div>";
		ttDiv += "<div class='ttCenter'><p></p></div>";
		ttDiv += "<div class='ttBottom'></div>";
		ttDiv += "</div>";
		return ttDiv;
	} // END bindToolTip

	this.showTooltip = function (obj, copy, offsetLeft) {	
		// create the HTML structure for the tooltip
		var ttDiv = thisObj.createTtMarkup();
		// append to thisJqueryObj and wrap contents in a relatively positioned div
		// this allows use of position: absolute for tooltip inside any element (including td)
		// this is a work around to an offset().top bug in jQuery/Firefox
		$(obj).append(ttDiv);
		$(obj).wrapInner("<div style='position:relative'></div>");
		$('div.ttCenter p').html(copy);
		
		// check to see it tool tip will extend beyond the page (1000px)
		// if so, then render the tooltip from the right hand corner instead  
		// then add class tooltip-right and use the righthand graphic 
		// offsetLeft is from browser window, need to find left relative to this.origin.left
		var left  = offsetLeft - this.origin.left + 308;
		if (left > 1000) { // width of page
			left = -308;
			$('#tooltip').addClass("tooltip-right");
		} else {
			left = 0 +  $(obj).outerWidth();
		}
		top  = $(obj).outerHeight();
		
		// animating opacity of png in IE is not pretty, if IE skip animation
		if (window.event) { 
			 $('#tooltip').css({left: left, top: top, position: 'absolute'});
		} else {
			$('#tooltip').css({left: left, top: top, opacity: '0', position: 'absolute'}).animate({opacity: '1'}, 500, 'swing');
		}

	} // END showToolTip
		
	this.killTooltip = function (e) {
		// animating opacity of png in IE is not pretty, if IE skip animation
		if (window.event) { 
			$('#tooltip').remove();
		} else {
			$('#tooltip').animate({opacity: '0'}, 100, 'swing', function(){
				$('#tooltip').remove();
			});
		}
	} // END killToolTip

} // END Tooltip constructor function


/*******************************************************************************
* alternate rows of a table between light and dark
* assumes use of reskin oop styles
* expects a list of table rows (ie 'table.someClass tr' or '#myTable tr')
*
*******************************************************************************/

function stripeTable( element ) {
	var lightRow = "even";
	//alert("element: "+element)
	$(element).each( function(i){
		if( $(this).children('td').hasClass('header') || $(this).children('td').hasClass('row_subHeader') ){
			// if row contains a header check if its and even or odd row
			// we want the row following a header to be light (by default even rows are light)
			// if the header is an even row we need to adjust and make odd rows light
			// also skips applying the class to this row
			// sample pages with tables with multiple headers: owners/regular or owners/severe
			if (i%2) { lightRow = "odd" } else { lightRow = "even" };
		} else {
			if (lightRow == "even") {
				if (i%2) {	//even
					$(this).addClass('row_light');
				}else {		// odd
					$(this).addClass('row_dark');
				};	
			} else {
				if (i%2) {	//even
					$(this).addClass('row_dark');
				}else {		// odd
					$(this).addClass('row_light');
				};				
			}
		}   
	});
}