/*
This file contains javascript functions for use with Michael morit's Website
*/



function morit_page_setup () {

// This function should be called from the body element of an html page as <body onload="morit_page_setuo();">

// This function performs two tasks. First it preloads a number of important images

	preload_rollover_images();

// Second, this function searches the html document for <div> elements whose id begins "tmpl_...". When it finds such an element, it will search the templates subdirectory for a template whose name matches the remainder of the div's id, and fills the div with the code in that template.

/* So, for example, if you have a template called banner.html in the templates subdirectory, all you need to do to include that as a template is to put a div element in your file with an id of "tmpl_banner" i.e. in your html document you would put

...<div id="tmpl_banner"><div>...

*/

// First get all the divs in the document
var divs = document.getElementsByTagName('div');

// Now loop through the divs to find the ones whose name begins "tmpl_"

for (var i=0; i<divs.length; i++) {
	
	
	
	if (divs[i].id.substring(0,5) == 'tmpl_') {
	
	// OK, so we've found a div whose name begins "tmpl_..."
	
	// First we're going to extract template name i.e. all the stuff that comes after "tmpl_" in the id
	
	var tmpl_identifier = divs[i].id.substring(5,divs[i].id.length);
	
	// Then we send the extracted name to the morit_include function
	morit_include(tmpl_identifier);
	}
}

	// finally we show the navigation instructions, if any
	morit_show_instructions();

}


function morit_include (identifier) {

	
  	var template_prefix = templates_dir_prefix();

	var template_suffix = ".html";
	var url_prefix = "";

    if (document.getElementById('tmpl_' + identifier)) {
	
	client_side_include('tmpl_' + identifier, template_prefix + identifier + template_suffix);	
	}

}



function preload_rollover_images() {
	
	preload_image_object = new Image();
	
	var mypics = new Array();
mypics[1] = "b1.gif"; 
mypics[2] = "b2.gif"; 
mypics[3] = "b3.gif"; 
mypics[4] = "b4.gif"; 
mypics[5] = "b5.gif"; 
mypics[6] = "b6.gif"; 
mypics[7] = "b7.gif"; 
mypics[8] = "b8.gif"; 
mypics[9] = "banner.gif"; 
mypics[10] = "bo1.gif"; 
mypics[11] = "bo2.gif"; 
mypics[12] = "bo3.gif"; 
mypics[13] = "bo4.gif"; 
mypics[14] = "bo5.gif"; 
mypics[15] = "bo6.gif"; 
mypics[16] = "bo7.gif"; 
mypics[17] = "bo8.gif"; 
mypics[18] = "bottombar.gif"; 
mypics[19] = "frontpage.gif"; 
mypics[20] = "landrbar.gif"; 
mypics[21] = "sidebargrad.gif"; 
mypics[22] = "topmenu3.gif"; 
mypics[23] = "topmenu4.gif"; 
mypics[24] = "morit1.jpg"; 
mypics[25] = "morit2.jpg"; 
	
	for(i=1; i<25;i++){
		preload_image_object.src = "images/" + mypics[i];
	}
}



function client_side_include(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}




function templates_dir_prefix () {
	
	var req = false;
	if (window.XMLHttpRequest) {
	    try {
	      req = new XMLHttpRequest();
	    } catch (e) {
	      req = false;
	    }
	  } else if (window.ActiveXObject) {
	    // For Internet Explorer on Windows
	    try {
	      req = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try {
	        req = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e) {
	        req = false;
	      }
	    }
	  }

	var base_templates_url = "templates/";
		
	if (url_exists(req, base_templates_url)) {
		return base_templates_url;
	} 
	else {return "../templates/";}
}


function url_exists (req, url) {
	
	try {
			req.open("HEAD", url, false);
			req.send(null);		
			return req.status== 200 ? true : false;
		}
		catch (er) {
			return false;
		}
}
	


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
