/* 
	adds tracking behaviour to links contained within the element oShowProfileDescriptionContainer
*/
//if ( typeof( addLoadEvent ) == "undefined" ) {
//	function addLoadEvent(f) { var old = window.onload; if (typeof window.onload != 'function') { window.onload = f; } else { window.onload = function() { old(); f();} } }
//}

//addLoadEvent( fAssignTrackingLinks );

jQuery(document).ready( 
function ()
{
   	// check if panel exists
	if ( oShowProfileDescriptionContainer )
	{
	    var container = jQuery( "#" + oShowProfileDescriptionContainer.id );
		var oLinks = [];
		var oLinks1 = container.find( "area" );
		var oLinks2 = container.find( "a" );
		oLinks1.each( function( i )
		{
			oLinks.push( oLinks1[ 0 ] );
		} );
		oLinks2.each( function( i )
		{
			oLinks.push( oLinks2[ i ] );
		} );
		
		var oLink, i = 0;
		while( oLink = oLinks[ i++ ] ) {
		
			//alert( oLink.tagName + " " + oLink.href)
		
			// Determine which kind of link we're dealing with
			var xWWW = /^http(s)?\:/gi;
			var xEmail = /^mailto\:/gi;

			// if the link is a mailto link
			if ( oLink.href.match( xEmail ) ) {
				
				oLink.onclick = fTrackLinkClick;
				
				if ( oLink.innerHTML )
				{
				    var sHTML = oLink.innerHTML // the innerHTML trick is needed to fix error in IE where it replaces the children of the link, when you assign something to the href attribute
				}
				
				var xSubject = /subject\=/gi;
				if ( !oLink.href.match( xSubject ) ) {
					if ( oLink.href.indexOf("?") == -1 ) {
						oLink.href = oLink.href + "?";
					} else {
						oLink.href = oLink.href + "&amp;";
					}
					oLink.href = oLink.href + "subject=" + sSubject;
				} else {
					oLink.href = oLink.href.replace( /subject=[^"&]*/gi, "subject=" + sSubject );
				}
				
				if ( oLink.innerHTML )
				{
				    oLink.innerHTML = sHTML;
				}
				    
				
				
			// if the link is a http link			
			} else if ( oLink.href.match( xWWW ) ){
				// forces the link to open in a new window			
				oLink.target = "_blank";
				oLink.onclick = fTrackLinkClick;
				
			// the link is assumed to be a local link
			} else {
				// The link is not an external link, nor an email link, and will not be tracked
				break;
			}
		}
	}
});


function fTrackLinkClick() {
	// Determine which kind of link we're dealing with
	var xWWW = /^http(s)?\:/gi
	var xEmail = /^mailto\:/gi
   
	if ( this.href.match( xEmail ) ) {
		sType = "email";
	} else if ( this.href.match( xWWW ) ){
		sType = "www";
	} else {
		// The link is not an external link, nor an email link, and will not be tracked
		return;
	}
    var oImg = document.createElement("img");
    oImg.style.position = "absolute";
    oImg.style.top = "-1000px"
    oImg.style.left = "-1000px";
    document.body.appendChild( oImg );
    oImg.src = "/RegisterClick.aspx?mode=" + sType + "&id=" + sClickTrackingId;
}


