function doViewCategoryClick ( link ) 
{
	var theguid = link.attr("guid");
	var infoDiv = $("#" + theguid + " :first-child")
	var params = {
			showTableHeader : infoDiv.attr("showTableHeader")!=undefined?infoDiv.attr("showTableHeader"):"",
			linkView : infoDiv.attr("linkView")!=undefined?infoDiv.attr("linkView"):"",
			cssOddRow : infoDiv.attr("cssOddRow")!=undefined?infoDiv.attr("cssOddRow"):"",
			cssEvenRow : infoDiv.attr("cssEvenRow")!=undefined?infoDiv.attr("cssEvenRow"):"",
			containerStyle: infoDiv.attr("containerStyle")!=undefined?infoDiv.attr("containerStyle"):"",
			categoryLinkStyle: infoDiv.attr("categoryLinkStyle")!=undefined?infoDiv.attr("categoryLinkStyle"):"",
			showExpandIcon: infoDiv.attr("showExpandIcon")!=undefined?infoDiv.attr("showExpandIcon"):"",
			onMouseOver: infoDiv.attr("onMouseOver")!=undefined?infoDiv.attr("onMouseOver"):"",
			onMouseOut: infoDiv.attr("onMouseOut")!=undefined?infoDiv.attr("onMouseOut"):"",
			eventColumns: infoDiv.attr("eventColumns")!=undefined?infoDiv.attr("eventColumns"):"",
			thisView: infoDiv.attr("thisView")!=undefined?infoDiv.attr("thisView"):"",
			guid:theguid};
	//alert("params:" + jQuery.param(params));
	$('#'+ theguid,document).load(link.attr("href") + " table:first", params, function(responseText, textStatus, XMLHttpRequest) {
		//decorateCategoryLinks();
		return false;
	});
}

function doOutlineClick( link )
{
	$("#dbOuterContainer", document).parent().load(link.attr("href"), {ajax: 'true', guid: link.attr("guid")}, function(responseText, textStatus, request) { 
		//decorateOutlineLinks();
	});
	
	return false;
}
function decorateOutlineLinks()
{
	//alert("decoratingOutlineLinks");
	// The handler for the category urls.  doViewCategoryClick function is in adpunctum.js.
	$("a[outlineLink=true]").live( "click", function(){ 
		doOutlineClick( $(this));
		return false;
	});
}

function decorateCategoryLinks()
{
	//alert("decoratingCategoryLinks");
	// The handler for the category urls.  doViewCategoryClick function is in adpunctum.js.
	$("a[category=true]").live( "click", function(){ 
		doViewCategoryClick( $(this) );
		return false;
	});
}

// Event called by the outline when a view has been changed.
$("body").bind("outlineclick",function(e, guid){
	decorateCategoryLinks();
});


$(document).ready(function(){
	$.fn.media.defaults.mp3Player = "javascript/mediaplayer.swf";
	$.fn.media.defaults.flvPlayer = "javascript/mediaplayer.swf";
	$("a:contains('adpunctum_video')").each(function(){
		ahref = this.href;
		if( ahref.indexOf("noteslink.adp?notesLink=") > 0 ){
			ahref = ahref.substring(ahref.indexOf("noteslink.adp?notesLink=") + 24);
			this.href=ahref;
		}
		$jHref = $(this);
		linkText = $jHref.html();
		indexAV = linkText.indexOf("adpunctum_video:");
		indexAVEnd = linkText.indexOf( ":", indexAV + 16 );
		vText = linkText.substring( indexAV, indexAVEnd );
		width = vText.substring(16, vText.indexOf("x"));
		height = vText.substring (vText.indexOf("x") + 1 );
		$jHref.media({width:width,height:height,bgColor:'transparent'}, function(orig, newdiv, options){
			newdiv.caption = "";
			//alert($nDiv.html());
			$jNewdiv = $(newdiv);
			$jNewdiv.children().remove();
		});
	});
});

// Replace and set the target and parentview for links in richtext fields
function setLinkParentViewAndTarget( rtId, parentView, defaultTarget )
{
	// Find either the richtext field with the id rtId, or all richtextfields
	var rts;
	if( rtId )
	{
		rts = $("#" + rtId);
	}
	else
	{
		rts = $(".rtfield").filter(function(){ // Filter out the rtwebelement classes, because these are from richtext webelements and they control their links themselves.
			return !$(this).parent().hasClass("rtwebelement");
		});
	}
	
	// Set target and add parentView to url
	$(rts).each(function(){

		var $hrefs = $(this).find("a");
		$hrefs.each(function(){
			var target = $(this).attr("target").toLowerCase();
			if( parentView && parentView != "" && (target == "currentpage" || (target == "" && defaultTarget == "currentpage"))) // add parentView if not going out of frame
			{
				var url = $(this).attr("href") + "&parentView=" + parentView;
				$(this).attr("href", url);
			}
			// set target
			if( target == 'newwindow' || (target == "" && defaultTarget == "newwindow"))
			{
				target="_blank";
			}
			else
			{
				target="_self";
			}
			$(this).attr("target", target);
		});
	});
}
