// this is a fix for the jQuery slide effects
function slideToggle(el, bShow){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 250});
  } else {
    $el.animate({height: 0}, {duration: 250, complete:function (){
        $el.hide();
      }
    });
  }
}

function FillMainMedia(value){
	$(".MainMedia").html("<div class='large-media-wrapper'>" + value + "</div>");
	$("object embed").attr("wmode", "opaque");
	$("object").attr("wmode", "opaque");
}

$(document).ready(function() {

	$("a").each(function() {
		var url = $(this).attr("href");
		if (url.indexOf("/solomon_um/Platform") != -1 || url.indexOf("solomon_um/platform") != -1) {
			url = url.split("/solomon_um/Platform").join("").split("/solomon_um/platform").join("");
			$(this).attr("href", url);
		}
	});

	//fixes media media
	$("img").each(function() {
		var path = $(this).attr("src");
		path = path.split("~/").join("/solomon_um/Platform/");
		$(this).attr("src", path);
	});

	//handles setting objects to lower zindex
	$("object").attr("wmode", "opaque");
	$("object embed").attr("wmode", "opaque");

	//handles top level navigation
	$("#main-nav .li-1").hover(function() {
		var nav2 = jQuery(this).children(".nav-2");
		//open
		if ($.browser.msie) {
			jQuery(nav2).show();
		} else {
			slideToggle(nav2, true);
		}
		//$(this).children(".nav-2").show();
	}, function() {
		var nav2 = jQuery(this).children(".nav-2");
		//open
		if ($.browser.msie) {
			jQuery(nav2).hide();
		} else {
			slideToggle(nav2, false);
		}
		//$(this).children(".nav-2").hide();	
	});

	//handles the page expanders
	$(".ExpanderLink").click(function() {
		var exSection = $(".ExpanderSection");

		if ($(exSection).css("display") == "none") {
			//open
			if ($.browser.msie) {
				$(exSection).show();
			} else {
				slideToggle(exSection, true);
			}
		}
		else if ($(exSection).css("display") == "block") {
			//close
			if ($.browser.msie) {
				$(exSection).hide();
			} else {
				slideToggle(exSection, false);
			}
		}
	});

	//if the blog sidebar is visible
	if ($(".tagcloud").size() > 0) {

		var tags = $(".tagweight");
		var maxCount = 0;

		//loop through all the tags and get the largest tag count
		$(tags).each(function() {
			var count = $(this).attr("rel");
			if (count > maxCount) {
				maxCount = count;
			}
		});

		//loop back through and calculate their weight
		$(tags).each(function() {

			//weighting value - basically divides up the total tags by the number of font scales
			var count = $(this).attr("rel");
			var percent = (count / maxCount) * 100;

			var className = "xx-small";
			if (percent < 14) {
				className = "xx-small";
			} else if (percent < 28) {
				className = "x-small";
			} else if (percent < 42) {
				className = "small";
			} else if (percent < 56) {
				className = "medium";
			} else if (percent < 70) {
				className = "large";
			} else if (percent < 84) {
				className = "x-large";
			} else {
				className = "xx-large";
			}

			$(this).attr("class", "tagweight " + className);
		});
	}

	//for media column on standard pages
	$(".column-media li a").each(function() {
		var hiddenData = $(this).find(".hiddenData").html();
		var externalLink = $(this).find(".hiddenData .external");
		var internalLink = $(this).find(".hiddenData .internal");

		if (externalLink.size() > 0) {
			$(this).attr("target", "_blank");
			$(this).attr("href", $(externalLink).text());
		} else if (internalLink.size() > 0) {
			$(this).attr("href", $(internalLink).text());
		} else if (hiddenData.indexOf(".pdf") != -1) {
			$(this).attr("target", "_blank");
			$(this).attr("href", hiddenData);
		} else {
			//if the main is empty fill it now
			if ($(".MainMedia").html() == "") {
				FillMainMedia(hiddenData);
			}
			$(this).click(function() {
				FillMainMedia(hiddenData);
				return false;
			});
		}
	});
});
