$(function () {
    var docurl = location.href;
    
    // Remove logo div
    p_logo = document.getElementById('p-logo');
    p_logo.parentNode.removeChild(p_logo);

    xcontent = document.getElementById('column-content');
    
    tabs = document.getElementById("p-cactions");
    tabs.parentNode.removeChild(tabs);
    xcontent.insertBefore(tabs, xcontent.firstChild);
    
    personal = document.getElementById("p-personal");
    personal.parentNode.removeChild(personal);
    xcontent.insertBefore(personal, xcontent.firstChild);

    $('#p-navigation').before($('#toc'));
    $('#t-recentchangeslinked').before($('#n-recentchanges'));
    $('#t-whatlinkshere').before($('#n-mainpage'));
    $('#p-navigation').hide();
    $('#t-whatlinkshere').parent().before('<a title="Expand tools menu" id="tools" href="">Tools</a><span id="separator"> | </span>');
    $('#t-whatlinkshere').parent().hide();
    $('#separator').after($('#n-mainpage > a'));
    $('#p-tb-options li').css('list-style-type','none');

    modstr = $('#lastmod').text();
    modstr = modstr.replace(/.*, (.*)./,"Modified $1");
    $('#lastmod').hide();
    $('#p-cactions').css('width','auto');

    viewstr = $('#viewcount').text();
    viewstr = viewstr.replace(/This page has been accessed (.*) times./,"$1");
    if (parseInt(viewstr.replace(/,/,"")) > 10000) {
	$('#p-tb').before('<div class="portlet" id="stats"><div class="pBody">' + viewstr + ' views<br />' + modstr + '<br />' + '</div></div>');
	$('#viewcount').hide();
    } else {
	$('#p-tb').before('<div class="portlet" id="stats"><div class="pBody">' + modstr + '<br />' + '</div></div>');
	viewstr = viewstr + " views";
	$('#viewcount').text(viewstr);
    }

    $('#footer').css('padding','0px');
    $('#content').css('margin-bottom','0px');
    $('#content').css('padding-top','0px');
    $('#content').css('padding-left','1.5em');

    $('#f-poweredbyico').hide();
    $('#disclaimer').after('<li><a href="http://mediawiki.org">Powered by MediaWiki</a></li>');
    $('#n-mainpage').hide();

    $('.toctoggle').hide();
    $('#tools').toggle(function(){
	    $('#t-whatlinkshere').parent().show();
	},function (){
	    $('#t-whatlinkshere').parent().hide();
	});

    port = document.getElementById("searchGoButton");
    port = document.getElementById("n-portal");
    port.style.display = "none";
    
    port = document.getElementById("n-randompage");
    port.style.display = "none";

    port = document.getElementById("n-sitesupport");
    port.style.display = "none";
    port.innerHTML = "<a href=\"/wiki/index.php/Special:Recentchanges\">Recent Changes to Wiki</a>";
    
    port = document.getElementById("n-currentevents");
    port.style.display = "none";
    
    port = document.getElementById("n-help");
    port.style.display = "none";

    if ( docurl.match(/Image:/) ) {
	    tmpstr = $('.firstHeading').text();
	    tmpstr = "[[" + tmpstr + "]]";
	    $('.fullImageLink').before('<b><font color="red">This is a special page for uploaded images.<ul><li>To see the pages that use this image, see <a href="#filelinks">Links</a>.</li><li>To include this image in any wiki page (besides this one), edit the page and type </font>' + tmpstr + '.</li></ul></b>');
	}

$('#n-recentchanges > a').attr('href',$('#n-recentchanges > a').attr('href')+'&days=30&limit=500');
//alert("her"+$('#n-recentchanges > a'));

    if ( docurl.match(/action\=history/) ) {
	var d = new Date();
	var shiftstr = d.toString().match('GMT-');
	if (shiftstr.length > 0) {
	    var tmpstr = d.toString().replace(/.*GMT-([0-9][0-9]).*/,' The times listed below can be converted to your local time by subtracting $1 hours.');
	} else {
	    var tmpstr = d.toString().replace(/.*GMT+([0-9][0-9]).*/,' The times listed below can be converted to your local time by adding $1 hours.');
	}
	var tmpstr = tmpstr.replace(/0([0-9])/,'$1');
	$('#contentSub').before('<b><font color="red">All times are in GMT.</font></b>'+tmpstr);
	$('#pagehistory').html($('#pagehistory').html().replace(/([0-9][0-9]:[0-9][0-9])/g,'$1 <i>(GMT)</i>'));
	}
    
    // Modify edit page
    if ((docurl.match("edit") == "edit") || (docurl.match("submit") == "submit")) {
        $('#editpage-copywarn').hide();
        $('#wpSummaryLabel').hide();
        $('#wpSummary').attr("value", "Summary of edits");
        $('#wpSummary').click(function(){
            $(this).attr("value", "")
        });
	$('#editform').prepend($('#toolbar'))
        $('.editOptions').insertBefore($('#toolbar'));

	$('#content').css("margin-left", "1em");
	$('#column-one').hide();
	$('#footer').css("margin-left", "1em");    }
    else {
	$('.toclevel-1 ol').hide();
	$('.toclevel-1 > a').toggle(function ()
				    {
					$(this).siblings('ol').show();
					window.location=$(this).attr('href');
				    }
				    ,function ()
				    {
					$(this).siblings('ol').hide();
				    }
				    );
	 
        $('#globalWrapper').before('<b><div id="col1hide"><a href="" title="Show/Hide left column" id="col1hidelink">x</a></div></b>');
        $('#col1hide').css('position', 'fixed').css('z-index', '100');
        $("#col1hide").toggle(function(){		
            $('#content').css("margin-left", "1em");
            $('#column-one').hide();
            $('#col1hidelink').text('+');
	    $('#footer').css("margin-left", "1em"); 
        }, function(){
            $('#content').css("margin-left", "20.2em");
	    $('#footer').css("margin-left", "20.2em"); 
            $('#col1hidelink').text('x');
            $('#column-one').show();
        });

    }

    // Layout fix for IE
    var regExp = /MSIE/i, index = navigator.appVersion.search(regExp);
    if ($.browser.msie && regExp.test(navigator.appVersion)){
	if (navigator.appVersion.slice(index + 5, index + 6) === "7"){
	    $("#column-one").css("width", 250);
	    $("table#toc li").css("margin-left", "0.8em");
	    $("table#toc").css("width", "100%");
	    $("#p-tb, #p-search").css("width", "100%");
	    $("#content").css("margin", presentation.style.contentMargin.normalTocShown);
	}
    }

    // This may cause a problem because present mode js is loaded after layout.js.
    //setupPresentationMode();	   

    // Indent subsections    
    if (0) { // Because it breaks present.js
    text = $('#content').html();

    // Grab everything in the main content DIV
    DIVOPEN = "<DIV class='indent' style='margin-left:1em;'>";

    // Replace all </h1> with </h1><DIV ...>
    text = text.replace(/\<\/h1\>/g,"</h1>"+DIVOPEN);
    // Replace all <h1> with </DIV><h1>
    text = text.replace(/\<h1\>/g,"</DIV><h1>");

    // Undo first replacement
    text = text.replace("</DIV><h1>","<h1>");

    // Add a closing DIV at end of last section 
    text = text.replace('<div class="printfooter">','</DIV><div class="printfooter">');

    // Replace existing content with new content
    $('#content').html(text);

    // Do not indent the 0th-level.
    $('div.indent:first').css('margin-left','0em');
    $('div.indent:first').css('border-left','0');
    // For some reason the show/hide toggle reappears
    $('.toctoggle').hide();
    }
    });

