
$(document).ready(function() {
	// Functions to run if Javascript is enabled.
	tidyLanguageSwitcher();

	// Old functions from pre-jquery site. 
	finallyInit(); 

})


// Make the langauge switcher look nicer.
function tidyLanguageSwitcher(){
	$("#langswitcher input[type=submit]").remove();
	$("#langswitcher select").css("background", 
		$("#langswitcher option[selected]").css("background")
	); 

	// Remove text to display only image. 
	// Opera does not support displaying images in select options. 
	if(!window.opera){ 
		$("#langswitcher option").text("");
		$("#langswitcher option").css("padding-left", "20px");
		$("#langswitcher p").remove();
	}
}



// Old javascript to make the styler look nicer. Requires testUtils.js
// Compare all this to the jquery-enabled stuff above. What a differnce. 

function finallyInit(){
	showhack();		/* Show the CSS hack note (is this still being used?) */
	jsInit_internal();	/* Initialize testUtils */
	var styleNode = myGet('styler');
	// This changes style in place but does not affect links:
	//initStylerHack(styleNode, "setStyle(seekChildNodeNamed(myGet('styler'), 'select').value);");
	// Simpler redirect:
	initStylerHack(styleNode, "stylerRedirect(seekChildNodeNamed(myGet('styler'), 'select').value);", "blk2");
}

function showhack(){
	var n;

	var style = "blk2";

	if((n = document.getElementById("csshacknote"))
	 && typeof(document.styleSheets) != 'undefined'){
		// n.innerHTML = "testing..";
		n.innerHTML = "With"
		+ " <abbr title=\"Cascading Style Sheets\">CSS</abbr>:"
		+ " <a href=\"?node=index&amp;style=bare\""
		+ " title=\"You will need to use your browser's 'back'"
		+ " feature to return\">view only main window</a>";
	}
}




function initStylerHack(nodePtr, onChangeCall, selectedName, doKillSubmit){
	//alert("Nodeptr is " + nodePtr); 
	selectedName = typeof(selectedName) != 'undefined' ? selectedName : "";
	doKillSubmit = typeof(doKillSubmit) != 'undefined' ? doKillSubmit : 1;
	// nodePtr should a the result of a getElementById call. 
	// TODO: support passing of a string nodeId?
	if(!nodePtr){
		nodePtr = myGet("styler");
		if(!nodePtr){ return; }
	}

	// Set the selected item to Selected.
	// OBSOLETE: Replaced by using selected="selected" in PHP.
/*	if(selectedName){
		// Get the first <option> tag and iterate through options
		var selectPtr = seekChildNodeNamed(nodePtr, "select"); 
		var optPtr = seekChildNodeNamed(selectPtr, "option"); 
		var selectedIndex = 0; 
		nodeDescribe(nodePtr);
		while(optPtr != null){ 
		// Find the matching @value attribute and set it selected
			var myName = nattr(optPtr, "value"); 
			if(myName == selectedName){
				optPtr.setAttribute("selected","selected");
				optPtr = null;
			} else { 
				optPtr = seekSiblingByName(optPtr, "option");
				selectedIndex ++; 
			}
		}
		// Since setting selected="selected" does not update
		// the form in every browser...
		if(selectedIndex < selectPtr.length){
			selectPtr.selectedIndex = selectedIndex; 
		}
	}
*/

	// Set an onchange handler to reset the main_style
	nodePtr.setAttribute("onchange", onChangeCall); 

	// Kill the submit button 
	if(doKillSubmit){
		//alert("In initStylerHack, node is " + nodeDescribe(nodePtr));
		killSubmit(seekChildNodeNamed(nodePtr, "input"));
	}
}

// Remove the submit button from the style changer form. 
// TODO: fix calling method, it stinks. Base cause is poorly written
// function. The function should get a collection of "inputs", 
function killSubmit(nodePtr){
	//alert("In killSubmit, node is " + nodeDescribe(nodePtr));
	if(nodePtr){
		if(nattr(nodePtr, "type") == "submit"){
			nodePtr.setAttribute("style", "display:none;");
		} else {
			killSubmit(seekSiblingByName(nodePtr, "input")); 
		}
	}
}


// setStyle not used -- it changes the style for the current page
// but the URLs are not changed, so any link you click on is the
// old style. 
function setStyle(styleName){
	var myNode = myGet("main_style");
	//alert("MyNode is " + myNode); 
	if(!myNode) return ; 
	var relAttr = myNode.attributes.getNamedItem("href"); 
	//alert("RelAttr is " + relAttr + " Value " + relAttr.value); 
	if(!relAttr) return ; 
	relAttr.value="styles/" + styleName + ".css"; 
	//alert("New style is is " + relAttr.value); 
	//alert("Finally style is: " + myNode.attributes.getNamedItem("href").value);
	return; 
}

// stylerRedirect() replaces setStyle(). It causes the browser to fetch
// the page again, but it saves me the trouble of writing Javascript to
// rewrite all the URLs the way mangleHrefs does in PHP. 
function stylerRedirect(newStyle){
        var url = window.location.toString();
        // toString() is needed above or else "match() is not a function".
        if(url.match(/style=.*/)){
                url = url.replace(/style=[^&]*/, "style=" + newStyle);
        } else if (url.match(/\?/)){
                url = url + "&style=" + newStyle;
        } else {
                url = url + "?style=" + newStyle;
        }
        window.location = url;
}

