/***************************************************************\
*                                                               *
*                 Master Script Template                        *
*        Designed exclusively for CyberLeo.Net by               *
*                         CyberLeo                              *
*                                                               *
*---------------------------------------------------------------*
*                                                               *
* ToDo:                                                         *
* 1) Add cookie function to save certain variables on client    *
*       computer to emulate saved preferences (i.e. columns,    *
*       showing comments, etc..                                 *
**2) Add ability to change number of columns by users, to       *
*       accomodate different screen widths.                     *
* 3) Add ability to turn on or off display of comments.         *
**4) Add paging ability, with user-defined number of images     *
*       per page, and handle paging via URI variables.          *
* 5) Optimize code.                                             *
*                                                               *
\***************************************************************/
function debug(out){
	if(out){
		out=out.replace(/&/g,'&amp;');
		out=out.replace(/</g,'&lt;');
		out=out.replace(/>/g,'&gt;');
		document.write('<pre>'+out+'</pre>');
	}else{
		document.write('Null');
	};
};
function PrintHeader(title,basedir){

document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
document.write('<tr><td width="100" height="100" background="'+basedir+'Images/red_ul.png"></td>');
document.write('<td width="*" height="100" background="'+basedir+'Images/red_line.png"><center><font size="+5" color="white">');
document.write(title);
document.write('</font></center></td>');
document.write('<td width="50" background="'+basedir+'Images/red_ur.png"></td></tr>');
document.write('<tr><td valign="top" width="100" background="'+basedir+'Images/red_line.png">');

document.write('<img src="'+basedir+'Images/red_line.png" border="0"><img src="'+basedir+'Images/red_line.png" border="0"><br>');
document.write('<table width="*" border="0" cellspacing="0" cellpadding="0">');
document.write('<tr><td width="10"></td><td width="*">');
document.write('<a href="'+basedir+'newindex.html"><font size="0" color="#BBCC00">Home</font></a><br>');
document.write('<a href="'+basedir+'news.html"><font size="0" color="#BBCC00">News</font></a><br>');
document.write('<a href="'+basedir+'Stories/"><font size="0" color="#BBCC00">Stories</font></a><br>');
document.write('<a href="'+basedir+'Pictures/"><font size="0" color="#BBCC00">Pictures</font></a><br>');
document.write('<a href="'+basedir+'Links/"><font size="0" color="#BBCC00">Links</font></a><br>');
document.write('<a href="'+basedir+'Downloads/"><font size="0" color="#BBCC00">Downloads</font></a><br>');
document.write('<a href="'+basedir+'Songs/"><font size="0" color="#BBCC00">Songs</font></a><br>');
document.write('<a href="'+basedir+'Untitled/"><font size="0" color="#BBCC00">Untitled</font></a><br>');
document.write('<a href="http://www.livejournal.com/users/cyberleo/"><font size="0" color="#BBCC00">LiveJournal</font></a><br>');
document.write('<a href="http://books.dreambook.com/jaymann4444/furry.html"><font size="0" color="#BBCC00">Dreambook</font></a><br>');
document.write('<a href=mailto:cyberleo4444@hotmail.com><font size="0" color="#BBCC00">Contact Me</font></a><br>');

document.write('</td></tr></table></td>');
document.write('<td width="*" rowspan="2" valign="top">');
}

function PrintFooter(basedir){
document.write('</td><td width="50" background="'+basedir+'Images/red_line.png" rowspan="2"><img src="'+basedir+'Images/red_line.png" border="0"></td>');
document.write('</tr><tr><td valign="bottom" background="'+basedir+'Images/red_line.png"><center><a href="http://www.fur.com/peace/"><img src="'+basedir+'Images/fur-peace-button.gif" border="0"></a></center></td>');
document.write('</tr><tr><td width="100" height="50" background="'+basedir+'Images/red_ll.png"></td>');
document.write('<td width="*" height="50" background="'+basedir+'Images/red_line.png">');
document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>');
document.write('<td align="left"><font size="-2" color="black">Site design copyright &copy; 2001-2003 CyberLeo<br>Site last updated 19-May-2003</font></td>');
document.write('<td align="right"><a href="http://www.cyberleo.net/cyberleo/donate/"><img src="'+basedir+'Images/x-click-but04.gif" border="0" alt="Donate with PayPal - Fast, free and secure!"></a></td>');
document.write('</tr></table>');
document.write('<td width="50" height="50" background="'+basedir+'Images/red_lr.png"></td>');
document.write('</tr></table>');
};

/*
*/
function URI_Parse(p_uri){
	// declare the variables
	var j=0;
	var p_url="";
	var p_buf="";
	var p_index=1;
	var p_pointer=0;
	var p_variable=[];
	var p_value=[];
	var p_dictbuf={};
	
	// get the base URL
	p_buf=p_uri.indexOf('?');
	if(p_buf<0){
		p_url=p_uri;
	}else{
		p_url=p_uri.slice(0,p_buf);
	};
	p_buf="";
	
	// get the variables
	p_variable[p_index]="";
	p_value[p_index]="";
	// URI=location.href?
	if(p_uri.indexOf("?")>-1){
		for(j=p_uri.indexOf("?")+1;j<=p_uri.length;j++){
			p_buf=p_uri.charAt(j);
			if(p_buf=="&"){
				// transition to next variable
				p_index++
				p_pointer=0
				p_variable[p_index]="";
				p_value[p_index]="";
			}else{
				if(p_buf=="\="){
					// transition to value from variable name
					p_pointer=1;
				}else{
					if(p_pointer){
						p_value[p_index]+=p_buf//.toLowerCase();
					}else{
						p_variable[p_index]+=p_buf//.toLowerCase();
					};
				};
			};
		};
	}else{
		p_index=0;
	};
	p_dictbuf={index:p_index,URL:p_url,variable:p_variable,value:p_value};
	return p_dictbuf;
};
function URI_Make(p_dictbuf){
	// Pass me the URIdict dictionary, and I'll generate URL?variable=value&variable=value...
	// Any entries where variable="" will not be included. However, entries where value="" will be included.
	var j=0;
	var p_url="";
	var p_index=0;
	var p_buf="";
	var p_variable="";
	var p_value="";
	
	// begin generation
	p_url=p_dictbuf.URL;
	p_index=p_dictbuf.index;
	p_variable=p_dictbuf.variable;
	p_value=p_dictbuf.value;
	if(p_index){
		// run through variables, adding the ones that have a valid method and ignoring those that don't
		for(j=1;j<=p_index;j++){
			if(p_variable[j]!=""){
				p_buf+=p_variable[j];
				p_buf+="\=";
				p_buf+=p_value[j];
				if(j<p_index){
					p_buf+="&";
				};
			};
		};
		if(p_buf){
			// if we have something to show for it, add a ? and string together.
			p_url+="?"+p_buf;
		};
	};
	return p_url;
};
function URI_GetVar(p_dictbuf,p_method){
	// Pass me the URIdict dictionary and the name of the method to look for, and I'll return the value, or false if not found.
	var j=0;
	var p_index=p_dictbuf.index;
	var p_variable=p_dictbuf.variable;
	var p_value=p_dictbuf.value;
	var p_buf="";
	var p_dictbuf;
	
	// Locate the variable and return the value.
	for(j=1;j<=p_index;j++){
		// loop through the existing values
		if(p_variable[j]==p_method){
			p_buf=p_value[j];
		};
	};
	if(!p_buf){
		return false;
	}else{
		return p_buf;
	};
};
function URI_SetVar(p_dictbuf,p_method,p_data){
	// pass me URIdict, the method 
	var j=0;
	var p_url="";
	var p_index=0;
	var p_variable=[];
	var p_value=[];
	var p_buf=0;
	var p_dictbuf;
	p_url=URI_Make(p_dictbuf);
	p_dictbuf=null;
	p_dictbuf=URI_Parse(p_url);
	p_url=p_dictbuf.URL;
	p_index=p_dictbuf.index;
	p_variable=p_dictbuf.variable;
	p_value=p_dictbuf.value;
	
	// look for it first.
	for(j=1;j<=p_index;j++){
		if(p_method==p_variable[j]){
			// found it?
			p_buf=j;
			break;
		};
	};
	if(p_buf){
		// found the entry
		p_value[p_buf]=p_data;
	}else{
		// Didn't find the entry. Add a new one and increment index appropriately.
		p_variable[p_index+1]=p_method;
		p_value[p_index+1]=p_data;
		p_index++;
	};
	// rebuild the dictionary...
	p_dictbuf={URL:p_url,index:p_index,variable:p_variable,value:p_value};
	// ...and return the finished product.
	return p_dictbuf;
};
function URI_DelVar(p_dictbuf,p_method){
	// pass me URIdict and the method you wish deleted, and if it exists I'll delete it.
	var j=0;
	var p_buf=0;
	var p_url="";
	var p_index=0;
	var p_variable=[];
	var p_value=[];
	var p_dictbuf;
	p_url=URI_Make(p_dictbuf);
	p_dictbuf=null;
	p_dictbuf=URI_Parse(p_url);
	p_url=p_dictbuf.URL;
	p_index=p_dictbuf.index;
	p_variable=p_dictbuf.variable;
	p_value=p_dictbuf.value;
	// Look for it first.
	for(j=1;j<=p_index;j++){
		if(p_method==p_variable[j]){
			p_buf=j;
			break;
		};
	};
	if(p_buf){
		// found the entry
		p_variable[p_buf]="";
		p_value[p_buf]="";
		// shift everything southwards.
		for(j=p_buf+1;j<=p_index;j++){
//			alert('Index '+j+'\n'+p_variable[j-1]+' <- '+p_variable[j]+'\n'+p_value[j-1]+' <- '+p_value[j]);
			p_variable[j-1]=p_variable[j];
			p_value[j-1]=p_value[j];
		};
		p_variable[p_index]="";
		p_value[p_index]="";
		// decrease the index
		p_index--;
	};

	// rebuild the dictionary
	p_dictbuf={URL:p_url,index:p_index,variable:p_variable,value:p_value};
	// and return the finished product.
	return p_dictbuf;
};
function ReverseString(p_stringbuf){
	// Reverses a text string: 'abcd' -> 'dcba'
	var j=0;
	var p_buf="";
	for(j=p_stringbuf.length;j>-1;j--){
		p_buf+=p_stringbuf.charAt(j);
	};
	return p_buf;
};
function FindExt(p_filename){
	// Returns everything from the last decimal point in the filename to the end of the string (the extension)
	var j=0;
	var p_buf="";
	// Find the last decimal point and return the characters after that.
	for (j=p_filename.length;j>0;j--){
		if(p_filename.charAt(j)!="."){
			p_buf+=p_filename.charAt(j);
		}else{
			break
		};
	};
	// reverse the letters
	p_buf=ReverseString(p_buf);
	p_buf=p_buf.toUpperCase();
	return p_buf;
};
function ShowPicture(Image,URIdict){
	// Shows the requested image
	var p_buf=0;
	var p_stringbuf="";
	var p_htmlbuf="";
	var p_dictbuf={};
	var p_index=URI_GetVar(URIdict,'index');
	with(document){
		open();
		document.write('<html><head><title>'+Image.ttl[p_index]+'</title></head><body>');
		document.write('<center><font size="+2">'+Image.ttl[p_index]+'</font><br>');
		
		// back one image
		p_stringbuf='<font color="black">&lt;--</font>';
		if(p_index>1){
			p_buf=URI_SetVar(URIdict,'index',p_index-1);
			p_buf=URI_Make(p_buf);
			p_stringbuf='<a href="'+p_buf+'">'+p_stringbuf+'</a>';
		};
		p_htmlbuf+=p_stringbuf;
		// back to index
		p_buf=URI_DelVar(URIdict,'index');
		p_buf=URI_Make(p_buf);
		p_htmlbuf+='<a href="'+p_buf+'"><font color="black">Back</font></a>';
		// forward one image
		p_stringbuf='<font color="black">--&gt;</font>';
		if(p_index<Image.images){
			p_index++;
			p_buf=URI_SetVar(URIdict,'index',p_index);
			p_index--;
			p_buf=URI_Make(p_buf);
			p_stringbuf='<a href="'+p_buf+'">'+p_stringbuf+'</a>';
		};
		p_htmlbuf+=p_stringbuf;
		document.write(p_htmlbuf);
		document.write('<br><a href="'+Image.url[p_index]+'"><img alt="'+Image.ttl[p_index]+'" border="0" src="'+Image.url[p_index]+'"></a><br>');
		document.write(p_htmlbuf);
		document.write('<br><font size="-2">Javascript PictureGallery by CyberLeo</font>');
		document.write('</center></body></html>');
		close();
	};
};
function ShowIndex(Image,URIdict){
	var j=0;
	var p_buf="";
	var p_buf2=0;
	var p_pointer=0;
	var p_htmlbuf="";
	var p_htmlbuf2="";
	var p_uri="";
	var p_perrow=0;
	var p_perpage=0;
	var p_current=0;
	var p_maxpages=0;
	var l_highcolor="#FFEEEE";
	var l_lowcolor="#FFCCCC";
	
	// Figure out where we are
	p_perrow=URI_GetVar(URIdict,"perrow");
	if(!p_perrow){
		if(Image.PerRow){
			p_perrow=Image.PerRow; // initilize
		}else{
			p_perrow=3; // default.
		};
	};
	p_perrow=p_perrow++; // convert to number
	p_perpage=URI_GetVar(URIdict,"perpage");
	if(!p_perpage){
		if(Image.PerPage){
			p_perpage=Image.PerPage; // initilize
		}else{
			p_perpage=6; // default.
		};
	}else{
		if(p_perpage.toLowerCase()=='all'){
			p_perpage=Image.images/p_perrow; // set to maximum
		};
	};
	p_perpage=p_perpage++; // convert to number
	p_maxpages=Math.ceil(Image.images / (p_perpage*p_perrow));
	p_current=URI_GetVar(URIdict,"page");
	if(!p_current){
		p_current=1; // Initilize to first page
	};
	if(p_current>p_maxpages){
		p_current=p_maxpages; // Set to maximum
	};
	p_current=p_current++; // covnert to number
	// Generate menu thingie
	p_htmlbuf='[ Images/row: ';
	for(j=1;j<7;j++){
		if(j!=p_perrow){
			p_htmlbuf+='<a href="'+URI_Make(URI_SetVar(URIdict,'perrow',j))+'">'+j+' </a>';
		}else{
			p_htmlbuf+='<font color="#FF0000">'+j+'</font> ';
		};
	};
	p_htmlbuf+='| Rows/page: ';
	for(j=1;j<11;j++){
		if(j!=p_perpage){
			p_htmlbuf+='<a href="'+URI_Make(URI_SetVar(URIdict,'perpage',j))+'">'+j+' </a>';
		}else{
			p_htmlbuf+='<font color="#FF0000">'+j+'</font> ';
		};
	};
	if(p_perpage!=Image.images/p_perrow){
		p_htmlbuf+='<a href="'+URI_Make(URI_SetVar(URIdict,'perpage','all'))+'">all </a>';
	}else{
		p_htmlbuf+='<font color="#FF0000">all</font> ';
	};
	p_htmlbuf+='| Save settings ]<br>';
	document.write(p_htmlbuf);	

	// Generate header/footer thingie
	p_htmlbuf2='';
	p_buf='&lt;--';
	if(p_current>1){
		p_htmlbuf2+='<a href="'+URI_Make(URI_SetVar(URIdict,"page",p_current-1))+'">'+p_buf+'</a>';
	}else{
		p_htmlbuf2+=p_buf;
	};
	p_htmlbuf2+=' ';
	if(p_maxpages==1){
		p_htmlbuf2+=' All images ';
	}else{
		//p_htmlbuf2+=' Page '+p_current+' ';
		for(j=1;j<=p_maxpages;j++){
			p_buf=j
			if(j!=p_current){
				p_htmlbuf2+=' <a href="'+URI_Make(URI_SetVar(URIdict,"page",j))+'">'+p_buf+'</a> ';
			}else{
				p_htmlbuf2+=' <font color="#FF0000">'+p_buf+'</font> ';
			};
		};
	};
	p_htmlbuf2+=' ';
	p_buf='--&gt;';
	if(p_current<p_maxpages){
		p_htmlbuf2+='<a href="'+URI_Make(URI_SetVar(URIdict,"page",p_current+1))+'">'+p_buf+'</a>';
	}else{
		p_htmlbuf2+=p_buf;
	};
	document.write('<br><a href="../">Back</a>  '+p_htmlbuf2+'  Click to enlarge.<br>');
	document.write('<table border="0" cellpadding="6" cellspacing="0" width="*"><tr>');
	p_buf2=((p_perpage*p_perrow)*(p_current-1)+1);
	for(j=p_buf2;(j<(p_buf2+(p_perpage*p_perrow)))&&(j<=Image.images);j++){
		p_uri=URI_SetVar(URIdict,'index',j);
		p_uri=URI_Make(p_uri);
		p_buf=FindExt(Image.url[j]);
		p_htmlbuf='<td align="center" bgcolor="';
		if(p_pointer){
			p_htmlbuf+=l_highcolor;
		}else{
			p_htmlbuf+=l_lowcolor;
		};
		p_htmlbuf+='" height="100" width="100">';
		p_htmlbuf+='<a href="'+p_uri+'"><img alt="'+Image.ttl[j]+'" border="0" src="t/'+Image.url[j]+'"></a>';
		p_htmlbuf+='</td><td align="left" bgcolor="';
		if(p_pointer){
			p_htmlbuf+=l_highcolor;
		}else{
			p_htmlbuf+=l_lowcolor;
		};
		p_htmlbuf+='" height="100" width="100">';
		p_htmlbuf+='<a href="'+p_uri+'">'+Image.ttl[j]+'</a><br>';
		p_htmlbuf+=Image.pix[j]+' '+p_buf+'<br>';
		p_htmlbuf+=Image.bytes[j]+' bytes<br>';
		p_htmlbuf+=Image.date[j]+'<br>';
		p_htmlbuf+='</td>';
		document.write(p_htmlbuf);
		p_pointer=!p_pointer;
		if( (j) % p_perrow == 0 ){
			document.write('</tr><tr>');
			if( ! ( p_perrow % 2 ) ){
				p_pointer=!p_pointer;
			};
		};

	};
	document.write('</tr></table>'+p_htmlbuf2+'<br><font size="-2">Javascript PictureGallery by CyberLeo. Layout emulated from Nicol\'s great site.</font>');
};
function PictureGallery(Image,g_title,g_base,g_headertext,g_footertext){
	var URIdict={};
	var p_uri="";
	var p_index=0;
	URIdict=URI_Parse(location.href);
	p_index=URI_GetVar(URIdict,'index');
	if((URIdict.index!=0) && (p_index>0) && (p_index<=Image.images)){
		ShowPicture(Image,URIdict);
	}else{
		PrintHeader(g_title,g_base);
		document.write('<center>');
		document.write(g_headertext+'<br>');
		ShowIndex(Image,URIdict);
		document.write('<br>'+g_footertext);
		document.write('</center>');
		PrintFooter(g_base);
	};
};
/*************************************************\
*        Original code below this point.          *
\*************************************************/
