/*	
 *	GoToLinkFix.js	// REQUIRES jQuery 1.3.2 or higher
 *
 *	2009-12-28
 *	andrew.wright@iomer.com
 *	
 *	Implementation to allow browsers to render visited link colors and effects on 
 *	links that use the SharePoint javascript function 'GoToLink' and 'GoToDiscussion'
 *	which override the recording of a particular URL as having been visited.
 *
 */


/* <debug.remove> */
/*
function GetSource() { return window.location; }
function STSPageUrlValidation(url) { return url; }
function isPortalTemplatePage(url) { return false; }
function OnLink() { return; }
*/
/* </debug.remove> */

/*
 *	convertToStsLink : a utility function for imitating the creation of the 
 *	URL and target attribute for the link.
 */
function convertToStsLink (elm, param) {
	if (elm.href==null)
		return {};

	var ch=elm.href.indexOf("?") >=0 ? "&" : "?",
		srcUrl=GetSource(); // GetSource is located in core.js

	if (srcUrl !=null && srcUrl !="")
		srcUrl=ch+param+"="+srcUrl,
		targetUrl=elm.href+srcUrl,
		_target = '_self';
		
	// Source
	// TopicsView

	targetUrl = STSPageUrlValidation(targetUrl); // STSPageUrlValidation is located in core.js
	
	if (isPortalTemplatePage(targetUrl)) // isPortalTemplatePage is located in core.js
		_target = '_top';

	return { url: targetUrl, target: _target };
}
/*
 *	replaceLink : Clone a link element and replace it in the DOM to force IE to repaint it and thus mark 
 *	correctly the visited links.
 */
function replaceLink( _$link, attributes ){
	var _$clone = _$link.clone( false ),
		tempHtml = _$link.html();

	for( var attr in attributes )
		_$clone.attr( attr, attributes[attr] );

	_$link.replaceWith( _$clone );
	_$clone.html( tempHtml );
}
/*
 *	initLinkFix : When the DOM is ready, convert all of the offending anchors on the current page.
 */
function initLinkFix( jQueryNamespace ){
	// utilize the passed in jQueryNamespace
	jQueryNamespace(function () {
		//note: jQueryNamespace('a[onclick^=GoToLink]') // doesn't work in IE
		jQueryNamespace('a[onclick]')
			.filter(function(){
				var _$this = jQueryNamespace( this ),
					onclickStr = _$this.attr( 'onclick' ).toString(),
					linkMatch = /\bgotolink\b/gi.test( onclickStr ),
					discussionMatch = /\bgotodiscussion\b/gi.test( onclickStr ),
					positiveMatch =  linkMatch || discussionMatch;
				
				if( linkMatch ){
					_$this.data( 'linkType', 'Source' );
				} else if ( discussionMatch ){
					_$this.data( 'linkType', 'TopicsView' );	
				}
				return positiveMatch;
			})
			.removeAttr( 'onclick' ) // get rid of the function that breaks 'visited' urls
			.unbind( 'click' ) // doubled up to ensure removal of the click event
			.each (function () {
				var _$this = jQueryNamespace(this),
					linkType = _$this.data( 'linkType' ) || 'Source', // default to Source since it is much more common.
					_urlTargetPair = convertToStsLink ( this, linkType ); // convert this link to to an STS link
				
				replaceLink( _$this, { 'href' : _urlTargetPair.url, 'target': _urlTargetPair.target } );
			});
	});
}