;(function($)
{
	var dstr = "http://"+document.domain;
    // $.fn is the object we add our custom functions to
    $.fn.postAsAjax = function()
    {
		var aitems    = $("a:[href^='"+dstr+"']"),
			formitems = $('form');

		aitems.each(function(i){
			resetAnchorEvent(aitems[i]) 
			});
    };

	$.fn.roundBlock = function()
	{
		$item = $(this);
		$('<b class="top"><b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b></b>').insertBefore($item);	
		$('<b class="bottom"><b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b></b>').insertAfter($item);
	};

	$.fn.addIndex = function()
	{
		$item=$(this);
		var aitems = $('ol.faqlist > li > a[name^="m"]');
		$ci = $('<ol class="faqlist" style="margin-left:10px;"></ol>').insertBefore($item);	
		aitems.each(function(i){
			$ci.append('<li class="faqq"><a href="#'+$(aitems[i]).attr('name')+'">'+$(aitems[i]).html()+'</a></li>');
			});
	}

	function resetAnchorEvent(item) {
		if (item) {
			if (!($(item).attr("target")) && ($(item).attr("href").indexOf('AJXOK')>=0)){
				$(item).click(function(event){
					event.preventDefault();
					$.ajax({
						type: "GET",
						url: $(item).attr("href")+"&MCONLY=1",
						success: function (data) { 
							$("#mcenter").html(data);
							var anewItems = $("#mcenter a:[href^='"+dstr+"']")
							anewItems.each(function(i) {
								resetAnchorEvent(anewItems[i]);
							});
						},
						complete:function(data) {
							adjustFooterPosition();
						}
					});
				});
			}
		}
	};
})(jQuery);

function adjustFooterPosition(){
	if ($("#mcenter") && $("#mwrapleft")){
		if ($("#mcenter").outerHeight() < $("#mwrapleft").outerHeight()){
			$("#mcenter").css("min-height", $("#mwrapleft").height()+"px");
			if ($("#mcenter").outerHeight() < $("#mwrapleft").outerHeight()){
				// for IE 6
				$("#mcenter").css("height", $("#mwrapleft").height()+"px");
			}
		}
	}
};

$.initMathPresent = function()
{
	// init menu
    $("#msmenu").css("border","solid 2px #7EC0EE").roundBlock();
    $("#msmenu dt:[id^='mdt']").each(function(){
				$(this).mouseover(
					function(event) {
						$("#mul"+$(this).attr("id").substring(3)).show('slow');
				});
			});
    $("#msmenu dd  ul").each(
			function() {
				$(this).mouseleave(function(event){ $(this).hide('slow'); });
			});
    $("#mheader").roundBlock();
    $("ol.faqlist").addIndex();
    $("div.mindexmenus > ul").roundBlock();
    $("div.qodd").roundBlock();
    $("div.qeven").roundBlock();
	adjustFooterPosition();
	// init ajax
    $("#page").postAsAjax();
	switchJSGSVG();
};
$.initJsTree = function(treeContainer, bdropable)
{
	$(treeContainer).treeview({
		animated: "fast",
		collapsed: false,
		unique: false});
};

function switchJSGSVG()
{
	$("#JSGSVGSWITCH").each(function(){
		$(this).click(
		function(event){
			var cval = $(this).val();
			if (cval){
				var stext = $("#QCONTENT").val();
				if (stext) {
					if (cval=="jsg"){
						$("#QCONTENT").val(stext.replace(/jsg>/g,"svg>"));
						$(this).val("svg");
					} else if (cval == "svg"){
						$("#QCONTENT").val(stext.replace(/svg>/g,"jsg>"));
						$(this).val("jsg");
					}
				}
			}
		});
	});
};


