/* 
ajaxManager.js -- 7/2006 
Thanks to Eddie Traversa at DHTMLNirvana.com for the incredibly easy 
AJAX model.
Back button and bookmarking fixed using the RSH (really simple history) framework
from codinginparadise.org.
Thanks to David Cilley for the remarkably easy PNG work-
around for IE posted on his blog at Eyepatch.com. 
All other content by Rob Allen
*/
 function ajaxManager(){
		  var args = ajaxManager.arguments;
			  var menuSelect = '';
				if (args[3]){
						var mm = args[3]; //main menu item for bc nav;
						}
						else {
							var mm = 0;
						}
					if (args[4]){
						var sm = args[4]; //sub menu item for bc nav;
						}
						else {
							var sm = false;
						}
		if (document.getElementById) {
				var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if (x){		
			switch (args[0]){
				    case "load_page":
					       if (x){
							x.onreadystatechange = function(){
								 if (x.readyState == 4 && x.status == 200){
									 el = document.getElementById(args[2]);
									 el.innerHTML = x.responseText;
									  if (navigator.userAgent.indexOf("MSIE") > 0){ 
										 fixPngs();
										}
									}
							}
							x.open("GET", args[1], true);
							x.send(null);
							}
                	  break;
					case "start_up":
							ajaxManager('load_page', 'welcome.html', 'mainDisplay');
							ajaxManager('load_page', '/includes/nav.php?s=mainNav', 'nav');
							ajaxManager('load_page', '/includes/nav.php?s=footer', 'footer');
							ajaxManager('load_page', '/includes/nav.php?s=bc&mm=' + mm, 'breadcrumb');
						 	ajaxManager('load_page', 'http://stackoverflow.com/users/flair/149.html', 'flair');
							break;
					case "load_js":
						    x.onreadystatechange = function(){
							if (x.readyState == 4 && x.status == 200){
								var getheadTag = document.getElementsByTagName('head')[0];
								setjs = document.createElement('script'); 
								setjs.setAttribute('type', 'text/javascript');
								getheadTag.appendChild(setjs); 
								setjs.text = x.responseText;
								}
							}
							x.open("GET", args[1],true);
							x.send(null);
						break;
					case "nav":
							menuSelect = '&mm=' + mm;
							if (sm){
									menuSelect += '&sm=' + sm;
									ajaxManager('load_page', 'includes/nav.php?s=subNav' + menuSelect, 'subNav');										
									}
							ajaxManager('load_page', args[1], 'mainDisplay');
							ajaxManager('load_page', 'includes/nav.php?s=mainNav' + menuSelect, 'nav');						
							ajaxManager('load_page', '/includes/nav.php?s=bc' + menuSelect , 'breadcrumb');
							break; 
					} //END SWITCH
				} //END if(x)
			} //END AjaxManager

// David Cilley's IE .png Workaround functions //
// edit-Rob: Function call moved to the load_page case in the ajaxManager function so dynamically called pages will automatically have the .png's replaced as necessary
// edit-Rob: reverted the browser check back to the old MSIE indexOf trick due to issues with Opera that indexOf('Opera') didn't resolve

function fixPngs(){
// Loops through all img tags	
	   for (i = 0; i < document.images.length; i++){
		  var s = document.images[i].src;
		  if (s.indexOf('.png') > 0)  // Checks for the .png extension
			 fixPng(s, document.images[i]);
	   }
	}

function fixPng(u, o){
	// u = url of the image
	// o = image object	
	   o.src = '/images/spacer.gif';  // Need to give it an image so we don't get the red x
	   o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
	}