// presentation script
// callo@bostockandpollitt.com

var slideIs, id, one, message, presnum, getcurrent, plus, minus, pageIs, themax, start; // some globals
var passed  = false; // don't show anything unless there is something to show!
var message = 'No presentation has been specified.'; // message if no presentation has been passed.
var title = ''; // title for pop up

// unless specified, make the links hashes (anchors)
var prev = '#'; 
var next = '#';

// grab the current location
var url = document.location.href;

// if a presentation is specified, make passed true, and grab it
if(url.indexOf('?pres=')!=-1){
	one = url.split('?pres=')[1];
		if(one.indexOf('&')!=-1){
			id = one.split('&')[0];
		}else{
			id = one;
		}
		for(x=0;x<presentation.length;x++){
			if(presentation[x].name==id){
				passed = true;
				title = presentation[x].title;
				var totalSlides = presentation[x].slides.length;
				presnum = x;
				break;
			}else{
				passed = false;
				continue;
			}
		}
}

// if a page is specified, which one is it? if there isn't, make it "1"
if(url.indexOf('page=')!=-1){
	pageIs = url.split('page=')[1];
	if(pageIs.indexOf('&')!=-1){
		pageIs = pageIs.split('&')[0]
	}
}else{
	pageIs = 1;
}

// if a slide is specified, which one is it? if there isn't, make it "1"
if(url.indexOf('slide=')!=-1){
	slideIs = url.split('slide=')[1];
	if(slideIs.indexOf('&')!=-1){
		slideIs = slideIs.split('&')[0]
	}
}else{
	slideIs = 01;
}

// format the number to two digits ("02" and not "2") Part I
function Prfx(Q, L, c){ 
	var s = Q + ""; 
	if(c.length>0){
		while(s.length<L){
			s = c+s; 
		}
	}
	return s; 
}

// format the number to two digits ("02" and not "2") Part II
function StrU(X, M, N){ 
	var T, S = new String(Math.round(X*Number("1e"+N)));
	if(/\D/.test(S)){ 
		return '' + X; 
	}
	with(new String(Prfx(S, M+N, '0'))){
		return substring(0, T=(length-N)) + substring(T);
	}
}

// calculate previous and next slides
if(passed==true){
	getcurrent = parseInt(StrU(StrU(slideIs, 1, 0), 1, 0));
	minus = StrU(getcurrent - 1,2,0);
	plus = StrU(getcurrent + 1,2,0);
	if(getcurrent>01){
		prev = "?pres=" + id + "&page=" + pageIs + "&slide=" + minus;
	}
	if(!(parseInt(slideIs)>=totalSlides)){
		next = "?pres=" + id + "&page=" + pageIs + "&slide=" + plus;
	}
}

// now put the whole thing into a string ready for outputting	
var text = '';
if(passed){
	text += '	<tr>';
	text += '		<td colspan="3"><a href="' + prev + '" onMouseOver="MM_swapImage(\'previous\',\'\',\'images/button_tabs/icon_button_left_whitebg_o.gif\',1);" onMouseOut="MM_swapImgRestore()"><img src="images/button_tabs/icon_button_left_whitebg.gif" alt="Previous" name="previous" width="12" height="12" vspace="4"  border="0" id="previous"></a><img src="images/common/spacer.gif" alt="" width="8" height="1"><a href="' + next + '" onMouseOver="MM_swapImage(\'next\',\'\',\'images/button_tabs/icon_button_right_whitebg_o.gif\',1);" onMouseOut="MM_swapImgRestore()"><img src="images/button_tabs/icon_button_right_whitebg.gif" alt="Next" width="12" height="12" vspace="4" border="0" name="next" id="next"></a></td>';
	text += '	</tr>';
	text += '	<tr>';
	text += '		<td colspan="3"><img src="images/slides/' + presentation[presnum].folder + '/slide' + StrU(slideIs, 2, 0) + '.jpg" alt="Slide No. ' + StrU(slideIs, 2, 0) + ' of ' + totalSlides + '" width="640" height="480"></td>';
	text += '	</tr>';
}else{
	text += '	<tr>';
	text += '		<td colspan="3">&nbsp;</td>';
	text += '	</tr>';
	text += '	<tr>';
	text += '		<td colspan="3" style="font-family: Arial, Helvetica,Verdana, sans-serif; font-size: 11px; font-style: normal; font-weight: normal; color: #333333;">' + message + '</td>';
	text += '	</tr>';
}

// makes the list function as dhtml
startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}

// write out the contents
var contents = '';
if(passed){
	if(pageIs>1){
		
		if(eval(eval(pageIs*26)+1)<presentation[presnum].slides.length){
			themax = eval(eval(pageIs*26)+1);
		}else{
			themax = presentation[presnum].slides.length;
		}
		start = eval(eval(eval(pageIs-1)*26)+1);
		contents += '<li class="drops"><a href="?pres='+id+'&page='+eval(parseInt(pageIs)-1)+'&slide='+slideIs+'" class="subs"><span class="black">...back</a></li>';
		for(i=start;i<themax;i++){
			contents += '<li class="drops"><a href="?pres='+id+'&page='+pageIs+'&slide='+StrU(eval(i+1), 2, 0)+'" class="subs"><span class="black">' + eval(i+1) + '.</span> ' + presentation[presnum].slides[i] + '</a></li>';
		}
		if(presentation[presnum].slides.length>themax){
			contents += '<li class="drops"><a href="?pres='+id+'&page='+eval(parseInt(pageIs)+1)+'&slide='+slideIs+'" class="subs"><span class="black">more...</a></li>';
		}
	}else{
		if(presentation[presnum].slides.length<27){
			for(i=0;i<presentation[presnum].slides.length;i++){
				contents += '<li class="drops"><a href="?pres='+id+'&page='+pageIs+'&slide='+StrU(eval(i+1), 2, 0)+'" class="subs"><span class="black">' + eval(i+1) + '.</span> ' + presentation[presnum].slides[i] + '</a></li>';
			}
		}else{
			for(i=0;i<27;i++){
				contents += '<li class="drops"><a href="?pres='+id+'&page='+pageIs+'&slide='+StrU(eval(i+1), 2, 0)+'" class="subs"><span class="black">' + eval(i+1) + '.</span> ' + presentation[presnum].slides[i] + '</a></li>';
			}
			contents += '<li class="drops"><a href="?pres='+id+'&page='+eval(parseInt(pageIs)+1)+'&slide='+slideIs+'" class="subs"><span class="black">more...</a></li>';
		}
	}
}

// horrible rollover script courtesy of 'dream'weaver
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_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_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];}
}
