/** @file informU.js
	@author Don Chapman
	@author AD
	@date 2006-11-27
	@notes  Written for recent browsers
	@brief Contains the javascript classes for informU */


var gServerURL_ = '/informU_server/index.php';

bInitialRequest = true;

var InformUItem = Class.create();
InformUItem.prototype = {
	
	//var occurrences_;
	//var subject_;
	//var description_;
	
	initialize: function( subject, description )
	{
		this.occurrences_ = new Array();
		this.subject_ = subject;
		this.description_ = description;
	},
	addOccurrence: function( n )
	{
		this.occurrences_.push(n);
	}
};

var InformUSearchAndReplace = Class.create();
InformUSearchAndReplace.prototype = {
	//InformU informU_
	//String informUTag_;
	//String finishedLeftsideRegex_
	//Integer idxReplaceTo_;
	
	initialize: function( informU )
	{
		this.informUStr_ = '<a href="javascript:void(0);" onmouseover="return overlib(\'{d}\', CAPTION, \'{s_title}\', ABOVE);" onmouseout="return nd();">{s}</a>';
		this.informUStrVarLengths_ = 20;
		
		this.informU_ = informU;
		this.informUTag_ = '<!--informU-->';
		this.leftsideRegex_  = '([^a-zA-Z0-9\'"!@#$%&*])';
		this.rightsideRegex_ = '([^a-zA-Z0-9\'"!@#$%&*])';
		// current position we have built the string replaced to in original body
		this.idxReplacedTo_   = 0;
		this.accumulatedAddedLength_ = 0;
	},
	replaceBodyContent: function()
	{
		var items = this.informU_.informUItems_;

		var originalBody = this.informU_.originalBody_;
		var body = this.informU_.originalBody_;

		// loop for each informU item
		for ( var i = 0; i < items.length; ++i )
		{
			var informItem = items[i];
			var s = informItem.subject_;
			var d = informItem.description_;
			
			// without <!--informU-->
			var regexSubj = s.replace(/<!--informU-->/g, "");

			//if ( i < 3 )
			//	alert(regexSubj);
			// the regex to use
			var regStr = this.leftsideRegex_ + s + this.rightsideRegex_;
			//alert(regStr);
			var ex = new RegExp( regStr, 'g' );


			var exOccurrences = ex.exec(body);
			var idx = ex.lastIndex;
			var lastIdx = idx;

			var occurrence = 1;

			var lastSubjectLength = 0;
			var lastDescriptionLength = 0;

			// loop while there are regex matches for this item
			while ( idx>0 )
			{
				var matched = exOccurrences[0];
				var matchedLength = matched.length;
				// matched occurrence for this item
				if ( informItem.occurrences_.indexOf(occurrence) > -1 || informItem.occurrences_[0] == '0' )
				{
					var ls = matched.charAt(0);
					var rs = matched.charAt(matchedLength-1);
					
					//var checkStartIdx = lastIdx-4-lastDescriptionLength-this.informUTag_.length;
					var checkStartIdx = idx-s.length-1-this.informUTag_.length;
					
					// check if it's already been <!--informU-->
					isInformU = ( body.substr(checkStartIdx, this.informUTag_.length) == this.informUTag_ );
					
					//alert( body.substr(checkStartIdx, this.informUTag_.length) );
					
					//keep skipping until we move to the next "fresh" subject
					if ( isInformU )
					{
						exOccurrences = ex.exec(body);
						idx = ex.lastIndex;
						lastIdx = idx;
						continue;
					}

					// store everything following this occurrence
					tail = body.substr(idx);
					
					// set new body as everything up to what we're replacing
					body = body.substr(0, idx-matchedLength);
					
					// add the new string to the body
					body += ls;
					
					var theInformU = this.informUStr_.replace( '{s_title}', this.informUTag_+s );
					theInformU = theInformU.replace( '{s}', this.informUTag_+s );
					theInformU = theInformU.replace( '{d}', d );
					//if ( s == 'pulmonary embolism' )
					//	alert( theInformU );
					body += theInformU;
					body += rs;

					// append the rest to the new body
					body += tail;
					
					lastIdx = idx;
					lastSubjectLength = s.length;
					lastDescriptionLength = d.length;
				}
				++occurrence;
				exOccurrences = ex.exec(body);
				idx = ex.lastIndex;
			}
		}
		document.getElementById('bodyContent').innerHTML = body;
		//alert(body);
	}
};


var InformU = Class.create();
InformU.prototype = {
	//String originalBody_;
	//String strXmlDoc_;
	//XMLDocument xmlDoc_;
	//Array informUItems_;
	//String referrerURL_;

	initialize: function( lang )
	{
		this.originalBody_	=  document.getElementById('bodyContent').innerHTML;
		this.informUItems_ = new Array();
		xmlFile = false;
		// xml file was specified
		if ( xmlFile )
		{
			alert('xml');
		}
		// use server to get xml
		else
		{
			if ( bInitialRequest )
			{
				bInitialRequest = false;
				var url = document.location.href;
				var params = 'url='+url+'&lang='+lang;
				var aj = new Ajax.Request( gServerURL_,
								  { method: 'get',
								  	parameters: params,
								  	onComplete: this.fillInformUItems,
								  	asynchronous: true
								  }
								);
			}
		}
	},
	addInformUItem: function( item )
	{
		this.informUItems_.push( item );
	},
	fillInformUItems : function( originalRequest )
	{

		informU = new InformU();
		
		informU.xmlDoc_ = originalRequest.responseXML;
		informU.strXmlDoc_ = originalRequest.responseText;
		
		informUrlElements = informU.xmlDoc_.getElementsByTagName('url');
		var url = informUrlElements[0].firstChild.data;
				
		informU.referrerURL_ = url;
		informItemsElements = informU.xmlDoc_.getElementsByTagName('item');

		for ( var i = 0; i < informItemsElements.length; ++i )
		{
			itemElement = informItemsElements[i];

			var subject = itemElement.getElementsByTagName('subject').item(0).firstChild.data;
			var description = itemElement.getElementsByTagName('description').item(0).firstChild.data;
			// create the new informUItem
			var informUItemObj = new InformUItem( subject, description );

			occurrenceElements = itemElement.getElementsByTagName('occurrence');
			// add occurrences
			for ( var j = 0; j < occurrenceElements.length; ++j )
			{
				var occurrence = occurrenceElements[j].firstChild.data;
				informUItemObj.addOccurrence( occurrence );
			}
			informU.addInformUItem( informUItemObj );
		}
		snr = new InformUSearchAndReplace( informU );

		snr.replaceBodyContent();
	}
};

function snapware_InformU( lang )
{
	informU = new InformU( lang );
}

