//------------[ AutoNailer CLASS FILE]-------------//
//This is the main and only js file for the AutoNailer script.
//Author: Bryan English
//Date: 04.24.01

//-------------[ THE AUTONAIL CLASS ]-------------//
function AutoNail(){

	//---[ CONFIG VARIABLES ]---//
	this.font = "verdana";             //font to display captions//
	this.size = 2;                     //size of the font//
	this.color = "e2dfa3";             //color of the font//
	this.width = 600;                  //width of the nail table//
	this.thumbnailwidth = null;        //width of the thumbnail [optional]//
	this.thumbnailheight = null;       //height of the thumbnail [optional]//
	this.popupwidth = 500;             //width of the popup window
	this.popupheight = 500;            //height of the popup window
	this.tableborder = 0;              //border size of the nail table//
	this.cellpadding = 2;              //cellpadding of the nail table//
	this.cellspacing = 2;              //cellspacing of the nail table//
	this.collection = "autonails";     //name of the image collection to display//
	this.columns = 3;                  //number of columns in nail table//
	this.format = "jpg";               //the file format of the images//
	this.directory = "images/";        //the directory of the images//
	this.thumbsuffix = "t";            //the suffix for thumbnail images//
	this.template = "autonailer.html"; //the filename of the html template to use//
	this.nailcaption = true;           //display captions on the nails//
	this.popupmode = false;            //display the images in a popup window//


	//---[ PRIVATE: DON'T CHANGE]---//
	this.captions = new Array(0);
	this.current = null;
	this.jsname = "AutoNailer 1.4";
	this.parent = document.location.href.substring(document.location.href.lastIndexOf("/")+1,document.location.href.length);


	//---[ CONFIG METHODS ]---//
	this.add = autonailAdd;             //call this to add an image to the thumbnail table//
	this.naildisplay = autonailDisplay; //call this to render the thumbnail table//

	//---[ TEMPLATE METHODS ]---//
	this.loadquery = autonailLoadquery;     //ignore this//
	this.fulldisplay = autonailFulldisplay; //call this to render the full image//
	this.next = autonailNext;               //prints the link to the next image//
	this.back = autonailBack;               //prints the link to the last image//
	this.wherelist = autonailWherelist;     //prints a numbered linking navigation//

	//prints where you're out of the total collection//
	this.where = function autonailWhere(){document.write("[ "+(this.current+1)+" - "+this.captions.length+" ]");}

	//prints the link back to the thumbnail page//
	this.index = function autonailIndex(text){document.write("<a href='"+ ((this.popupmode)?"javascript:close();":this.parent) +"'"+ ((this.popupmode)?"":" target=\"_top\" ") +">"+text+"</a>");}

	//prints the caption for that particular image//
	this.caption = function autonailCaption(){document.write(this.captions[this.current]);}

	//prints a nice plug for me//
	this.about = function autonailAbout(){document.write("<font size='1'><i>Generated using <a href='http://www.simplezero.com/be/' target='_new'>"+this.jsname+"</a></i></font>");}

	this.error = function autonailError(text){alert(this.jsname + " Error\n---------------------------------\n" + text);}
	this.makequery = autonailMakequery;
	this.openWindow = autonailOpenWindow;
	}

//-------------[ THE AUTONAIL METHODS ]-------------//


function autonailDisplay(){
	//if this is being loaded in the frameset//
	if(parent.autonailer_core){parent.autonailer_template.location = this.template + parent.location.search;return};
	if(this.captions.length <= 0){this.error("No Thumbnails Added");return;}
	var rows = Math.ceil(this.captions.length/this.columns);
	var buffer = "";
	var rowcnt = colcnt = cnt = 0;

	buffer += "<table border='"+ this.tableborder +"' cellpadding='"+this.cellpadding+"' cellspacing='"+this.cellspacing+"' width='"+this.width+"'>";

	for(rowcnt;rowcnt<rows;rowcnt++){
		buffer += "<tr>";
		for(colcnt=0;colcnt<this.columns;colcnt++){
			if(cnt < this.captions.length)
				if(this.popupmode)
					if(this.nailcaption)
						buffer += "<td align='center'><font face='"+this.font+"' size='"+this.size+"' color='"+this.color+"'><a href=\"javascript:void('');\" onMouseOver=\"window.status='';return(true);\" onClick=\"autonail.openWindow('"+ this.template + this.makequery() + "current=" + cnt +"&');\"><img border='0' "+ ((this.thumbnailwidth)?"width='"+this.thumbnailwidth+"' ":"") + ((this.thumbnailheight)?"height='"+this.thumbnailheight+"' ":"") +" src='"+ this.directory + this.collection + (cnt+1) + this.thumbsuffix + "." + this.format +"'></a>" + "<br>" + this.captions[cnt] + "</font></td>";
					else
						buffer += "<td align='center'><font face='"+this.font+"' size='"+this.size+"' color='"+this.color+"'><a href=\"javascript:void('');\" onMouseOver=\"window.status='';return(true);\" onClick=\"autonail.openWindow('"+ this.template + this.makequery() + "current=" + cnt +"&');\"><img border='0' "+ ((this.thumbnailwidth)?"width='"+this.thumbnailwidth+"' ":"") + ((this.thumbnailheight)?"height='"+this.thumbnailheight+"' ":"") +" src='"+ this.directory + this.collection + (cnt+1) + this.thumbsuffix + "." + this.format +"'></a></font></td>";
				else
					if(this.nailcaption)
						buffer += "<td align='center'><font face='"+this.font+"' size='"+this.size+"' color='"+this.color+"'><a href='autonailer-frameset.html" + this.makequery() + "current=" + cnt +"&'><img border='0' "+ ((this.thumbnailwidth)?"width='"+this.thumbnailwidth+"' ":"") + ((this.thumbnailheight)?"height='"+this.thumbnailheight+"' ":"") +" src='"+ this.directory + this.collection + (cnt+1) + this.thumbsuffix + "." + this.format +"'></a>" + "<br>" + this.captions[cnt] + "</font></td>";
					else
						buffer += "<td align='center'><font face='"+this.font+"' size='"+this.size+"' color='"+this.color+"'><a href='autonailer-frameset.html" + this.makequery() + "current=" + cnt +"&'><img border='0' "+ ((this.thumbnailwidth)?"width='"+this.thumbnailwidth+"' ":"") + ((this.thumbnailheight)?"height='"+this.thumbnailheight+"' ":"") +" src='"+ this.directory + this.collection + (cnt+1) + this.thumbsuffix + "." + this.format +"'></a></font></td>";
			else
				buffer += "<td>&nbsp;</td>";
			cnt++;
			}
		buffer += "</tr>";
		}

	buffer += "</table>";
	document.write(buffer);
	}


function autonailLoadquery(){
	var q = unescape(document.location.search);
	if(q.indexOf("?") == -1){this.error("No Query String");return;}
	this.collection = extVal(q,"collection=","&");
	this.format = extVal(q,"format=","&");
	this.directory = extVal(q,"directory=","&");
	this.popupmode = ((extVal(q,"popupmode=","&")=="true")?true:false);
	if(this.popupmode)
		this.captions = opener.autonail.captions;
	else
		this.captions = parent.autonailer_data.autonail.captions;
	this.current = parseInt(extVal(q,"current=","&"));
	this.parent = extVal(q,"parent=","&");
	this.template = extVal(q,"template=","&");
	}

function autonailMakequery(){
	var q = "?";
	q += "collection=" + this.collection + "&";
	q += "popupmode=" + this.popupmode + "&";
	q += "format=" + this.format + "&";
	q += "directory=" + this.directory + "&";
	q += "parent=" + escape(this.parent) + "&";
	q += "template=" + this.template + "&";
	return q;
	}

function autonailFulldisplay(){
	document.write("<img border='0' src='"+ this.directory + this.collection + (this.current+1) + "." + this.format +"'>");
	}

function autonailNext(text){
	if(this.current < this.captions.length-1)
		document.write("<a href='"+ this.template + this.makequery() + "current=" + (1+this.current) + "&'>"+text+"</a>");
	}

function autonailBack(text){
	if(this.current > 0)
		document.write("<a href='"+ this.template + this.makequery() + "current=" + (this.current-1) + "&'>"+text+"</a>");
	}

//Add a pic to the collection//
function autonailAdd(caption){
   var cnt,length;

	if(isNaN(caption)){
		this.captions[this.captions.length] = caption;}
	else
		{
		if(caption <= 0){this.error("Invalid Number of Adds");return;}
		length = this.captions.length;
		for(cnt=0;cnt<caption;cnt++)
			this.captions[length+cnt] = "";
		}
	}

//display a list of where you are//
function autonailWherelist(){
	var cnt, buffer = "[ ";
	for(cnt=0;cnt<this.captions.length;cnt++)
		if(cnt!=this.current)			
			buffer += "<a href='"+ this.template + this.makequery() + "current=" + cnt + "&'>" + (cnt+1) + "</a> ";
		else
			buffer += "<b>"+ (cnt+1) +"</b> ";
	buffer += "]";
	document.write(buffer);
	}

//open a popup window//
function autonailOpenWindow(url){
	var win = window.open(url,"autonail","width="+this.popupwidth+",height="+this.popupheight);
	}

//-------------[ MISC ]-------------//

function extVal(data,key,delim)
   {
   var index;

   index = data.indexOf(key);
   if(index != -1)
      return(data.substring(index + key.length, data.indexOf(delim,index+1)));
   else
      return -1;
   }

//create an AutoNail object, so you don't//
var autonail = new AutoNail();


