// ***********************************************************************
//
// projectpage.js
//
// Copyright 2008 Optodesign, inc.
//
// Javascript routines for the Project page
//
// showmainimage -- swaps the main image on the project page for one of the thumbnails
// showlinktext -- swaps the right hand text column with the incoming link
// getlink -- returns a link
// nextmainimage -- swaps the main image for the next image in the thumbnail series
//

	// Constants

var STARTLINK	= "[";
var LINKSEP		= "|";
var ENDLINK		= "]";


//
// showmainimage -- swaps the main image on the project page for one of the thumbnails
//
function showmainimage( thumbnail, cellid ) {
	var debug	= false;

	if ( debug ) {
		alert( "In showmainimage:\n" +
				"thumbnail = [" + thumbnail + "]\n" +
				"cellid = [" + cellid + "]\n" +
				"" );
	}

		// Dim the thumbnail cells

	var curcell		= undefined;
	var classname	= "";
	for ( var curcellnum = 1; curcellnum <= 8; curcellnum++ ) {
		if (( curcell = document.getElementById( "thumbcell" + curcellnum )) != undefined ) {

			if ( debug ) {
				alert( "swapping bright for dimmed" );
			}
			classname	= curcell.className;
			if ( debug ) {
				alert( "classname = [" + classname + "]" );
			}
			classname	= classname.replace( "bright", "dimmed" );
			curcell.setAttribute( "class", classname );
			curcell.setAttribute( "className", classname );

		}
	}

		// Brighten this cell

	curcell = document.getElementById( cellid );
	classname	= curcell.getAttribute( "class" );
	classname	= classname.replace( "dimmed", "bright" );
	curcell.setAttribute( "class", classname );
	curcell.setAttribute( "className", classname );

		// Set the mainphoto src to the thumbnail src

	document.getElementById( "mainphoto" ).src	= thumbnail.src;

		// Save the currentimage num thumbcell

	curimagenum	= parseInt( cellid.substr( 9 ));
	if ( debug ) {
		alert( "curimagenum = [" + curimagenum + "]" );
	}

	if ( debug ) {
		alert( "Done showmainimage" );
	}
}


//
// showlinktext -- swaps the right hand text column with the incoming link
//
function showlinktext( name, number ) {
	var debug	= false;

	if ( debug ) {
		alert( "In showlinktext:\n" +
				"name = [" + name + "]\n" +
				"number = [" + number + "]\n" +
				"" );
	}

		// Get the link

	/*
	name	= name.replace( /<br>/g, "\n" );

	if ( debug ) {
		alert( "name now = [" + name + "]" );
	}
	*/

	var text	= getlink( name, number );
	if ( text != "" ) {

			// Change the inner HTML on the div

		text	= text.replace( /\n/g, "<br>\n" );
		document.getElementById( "specifications" ).innerHTML = text;

			// Hide/show the close link

		if ( name == "Right Column" ) {
			document.getElementById( "closelink" ).style.visibility	= "hidden";
		} else {
			document.getElementById( "closelink" ).style.visibility	= "visible";
		}
	} else {
		if ( debug ) {
			alert( "Can't find link" );
		}
	}

	if ( debug ) {
		alert( "Done showlinktext" );
	}
}


//
// getlink -- returns a link
//
function getlink( name, number ) {
	var retval	= "";
	var debug	= false;

	if ( debug ) {
		alert( "In getlink:\n" +
				"name = [" + name + "]\n" +
				"number = [" + number + "]\n" +
				"" );
	}

	var	curlinks	= document.getElementById( "linktext" ).value;
	if ( debug ) {
		alert( "curlinks = [" + curlinks + "]" );
	}

	var startpos	= -1;
	var endpos		= -1;

		// Find the start

	linkid	= STARTLINK + name + LINKSEP + number + LINKSEP;
	if (( startpos = curlinks.indexOf( linkid )) == -1 ) {
		if ( debug ) {
			alert( "Can't find this link" );
		}
	}

		// Find the end

	if (( startpos != -1 ) && (( endpos	= curlinks.indexOf( "]", startpos )) == -1 )) {
		if ( debug ) {
			alert( "Can't find end this link" );
		}
	}

		// Get the text

	if (( startpos != -1 ) && ( endpos != -1 )) {
		retval 	= curlinks.substring( startpos, ( endpos + 1 ))
		retval	= retval.replace( linkid, "" );
		retval	= retval.substring( 0, ( retval.length - 1 ));

		if ( debug ) {
			alert( "retval = [" + retval + "]" );
		}

	}

	if ( debug ) {
		alert( "Done getlink" );
	}

	return retval;

}


//
// nextmainimage -- swaps the main image for the next image in the thumbnail series
//
function nextmainimage() {
	var debug	= false;

	if ( debug ) {
		alert( "In nextmainimage:\n" +
				"curimagenum = [" + curimagenum + "]\n" +
				"" );
	}

		// Set to next image num

	curimagenum++;
	var curcellid	= "thumbcell" + curimagenum;

	if ( !document.getElementById( curcellid )) {
		curimagenum	= 1;
		curcellid	= "thumbcell" + curimagenum;
	}


		// Get the next thumbnail image & cellid

	var thumbid			= "$projectphoto" + curimagenum;
	var curthumbnail	= document.getElementById( thumbid );

	if ( debug ) {
		alert( "curthumbnail = [" + curthumbnail + "]\n" +
				"curcellid = [" + curcellid + "]\n" +
				"" );
	}

		// Call showmainimage

	showmainimage( curthumbnail, curcellid );


	if ( debug ) {
		alert( "Done nextmainimage" );
	}
}



