// alexLIB_photographers.js | Function library | VERSION 1.1
// Copyright 2006-2007 Alexander Media, Inc. - All rights reserved.
// By: 08.14.2006_rg, 02.08.2007_rg, 02.24.2007_rg, 04.06.2007_rg

// Ajax functions
function jsAjaxErrorHandler(type, error) {
	var msg = "Communication error!\n" +
	error.message;
	alert(msg);
} // jsAjaxErrorHandler()

function jsAjaxDoReturnValue(type, evaldObj) {
	return evaldObj;
} // jsOnSelectTopic_doReturnValue()

function jsAjaxCall(jRl){ // jRl = server script url
	// connect to the server
	dojo.io.bind({
		url: jRl,
		load: jsAjaxDoReturnValue,
		error: jsAjaxErrorHandler,
		mimetype: "text/javascript",
		sync: true
	}); // dojo.io.bind
} // jsAjaxCall()

uc = Date.parse(new Date()) + '_' + Math.round(100*Math.random()) + '_'; 
var ucIncrement = 0; // a value to be incremented
function jsUC(){  // return the unique value
	++ucIncrement;
	return (uc + (ucIncrement+''));
} // jsUC()

// Generic functions
function jsInitIfUndefined(variablename, defaultvalue){
	// Initialize a variable to 'defaultvalue' if not already defined
	if (typeof(window[variablename]) == "undefined") // variable is undefined
		return defaultvalue;
	return eval(variablename); // already defined, keep value
} // jsInitIfUndefined()

function jsTrim(jVl){ // jVl = value to 'trim'
	return jVl.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, "");
} // jsTrim()

function jsIsEmpty(jStrng){ // jStrng = string to check if empty
	var jStrng2 = jsTrim(jStrng);
	return ((jStrng2 == null) || (jStrng2.length == 0));
} // jsIsEmpty()

function jsIsEmptyRadio(jFrmFldNm, jFrmNmbr){ // jFrmFldNm = radio form field name | jFrmNmbr = form number
	for (i=document.forms[jFrmNmbr][jFrmFldNm].length-1; i > -1; i--)
		if(document.forms[jFrmNmbr][jFrmFldNm][i].checked) 
			return false;
	return true;
} // jsIsEmptyRadio()

var reEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i
function jsIsEmail(jMls){ // jMls = email to validate (empty is Ok)
	return reEmail.test(jMls);
} // jsIsEmail()

// Library functions
function dataobject(companyname, currentyear, createdyear, imagesnamelist,
					imagenameattributevalue, imagesortorder, imagecellidattributevalue, imagenotesidattributevalue,
					toolboxidattributevalue,
					uploadfoldername, currentimage, navigationcellid,
					navigationelementclassnameDESELECTED, navigationelementclassnameSELECTED,
					numberofnavigationlinksperrow,
					editableareas_idlist, editableareas_array){
	// function to initialize data object	
	this.companyname = companyname;
	this.currentyear = currentyear;
	this.createdyear = createdyear;
	this.imagesnamelist = imagesnamelist;
	this.imagenameattributevalue = imagenameattributevalue;
	this.imagesortorder = imagesortorder;
	this.imagecellidattributevalue = imagecellidattributevalue;
	this.imagenotesidattributevalue = imagenotesidattributevalue;
	this.toolboxidattributevalue = toolboxidattributevalue;
	this.uploadfoldername = uploadfoldername;
	this.currentimage = currentimage;
	this.navigationcellid = navigationcellid;	
	this.navigationelementclassnameDESELECTED = navigationelementclassnameDESELECTED;
	this.navigationelementclassnameSELECTED = navigationelementclassnameSELECTED;
	this.numberofnavigationlinksperrow = numberofnavigationlinksperrow;
	this.editableareas_idlist = editableareas_idlist;
	this.editableareas_array = editableareas_array;
}

function imageobject(imagepath, bgcolor, notes, thumbnailimagepath){
	// function to initialize image object	
	this.imagepath = imagepath;
	this.bgcolor = bgcolor;
	this.notes = notes;
	this.thumbnailimagepath = thumbnailimagepath;
}

function editableareaobject(idattribute, content){
	// function to initialize editable area object
	this.idattribute = idattribute;
	this.content = content;
}

function copyright(){
	// create the copyright text
	var yrRng = dt.createdyear;
	if(yrRng != dt.currentyear && dt.currentyear != '') // year range string
		var yrRng = dt.createdyear + '-' + dt.currentyear;
	var strng = 'Copyright &copy; ' + yrRng + ' ' + dt.companyname + ' - All rights reserved.' + '<br>' +
				'Site Developed by <a title="Alexander Media" href="http://www.alexmedia.com" target="_blank">Alexander Media</a>';
	document.write(strng);
}

function toolbox(){
	// create tool box to edit content
	var strng = '&nbsp;';
	if (loginflag == 'true'){
		// editnotes button
		if (dt.imagenotesidattributevalue.length > 0) // image notes id attribute available
			var strng = strng + '<input type="button" value="Notes" title="Edit Notes" onclick="javascript:toolbox_editnotes();">';
		// background colors button
		if (dt.imagecellidattributevalue.length > 0) // image cell id attribute available
			var strng = strng + '<input type="button" value="Background" title="Edit Background" onclick="javascript:toolbox_editbackgroundcolor();">';
		// logout button
		var strng = strng + '<input type="button" value="Logout" onclick="javascript:jsAjaxCall(\'' + scriptwebaddress + '?event=logout' + '&uc=' + jsUC()+ '\');">';
	}
	if (dt.toolboxidattributevalue.length > 0) // toolbox id attribute available
		document.getElementById(dt.toolboxidattributevalue).innerHTML = strng;
}

function toolbox_editnotes(){
	// display the 'editnotes' tool
	var strng = '';
	if (dt.imagesnamelist.length >= dt.currentimage) // image information must exist
		strng = dt.imagesnamelist[dt.currentimage].notes;
	strng = '<table border="0" cellspacing="0" cellpadding="0"><tr><td>' +
			'<span style="font:Courier; font-weight:bold;">Note:&nbsp;</span><input type="text" name="_editnotesfld" id="_editnotesfld" value="' + strng +
			'" size="20" maxlength="50" style="font:Courier;"><br>' +
			'<input type="button" name="save" value=" Save" onclick="javascript:toolbox_editnotes_doit();">&nbsp;' + 
			'<input type="button" name="cancel" value="Cancel" onclick="javascript:toolbox();">' +
			'</td></tr></table>'
	document.getElementById(dt.toolboxidattributevalue).innerHTML = strng;
}

function toolbox_editnotes_doit(){
	// update notes
	dt.imagesnamelist[dt.currentimage].notes = document.getElementById('_editnotesfld').value;
	shownotes((dt.currentimage+1), dt.imagenotesidattributevalue);
	// commit changes to the database
	jsAjaxCall(scriptwebaddress + '?event=commitnotes&imageid=' + document.imagesarray[dt.currentimage].src + '&content=' + (dt.imagesnamelist[dt.currentimage].notes.replace('#', '%23')).replace('&', '%26') + '&uc=' + jsUC()); // post data
}

function toolbox_editbackgroundcolor(){
	// display the 'editbackgroundcolor' tool
	var strng = '';
	if (dt.imagesnamelist.length >= dt.currentimage) // image information must exist
		strng = dt.imagesnamelist[dt.currentimage].bgcolor;
	strng = '<table width="225" border="0" cellspacing="1" cellpadding="1"><tr height="15">' +
			'<td bgcolor="#892f1d" width="15" height="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#892f1d\');">&nbsp;</td>' +
			'<td bgcolor="#c7533b" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#c7533b\');">&nbsp;</td>' +
			'<td bgcolor="#efa599" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#efa599\');">&nbsp;</td>' +
			'<td bgcolor="#d0b7ad" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#d0b7ad\');">&nbsp;</td>' +
			'<td bgcolor="#a689a7" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#a689a7\');">&nbsp;</td>' +
			'<td bgcolor="#8aa067" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#8aa067\');">&nbsp;</td>' +
			'<td bgcolor="#5b9ebf" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#5b9ebf\');">&nbsp;</td>' +
			'<td bgcolor="#997869" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#997869\');">&nbsp;</td>' +
			'<td bgcolor="#a52a2a" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#a52a2a\');">&nbsp;</td>' +
			'<td bgcolor="#cde985" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#cde985\');">&nbsp;</td>' +
			'<td bgcolor="#613318" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#613318\');">&nbsp;</td>' +
			'<td bgcolor="#98baac" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#98baac\');">&nbsp;</td></tr><tr height="15">' +
			'<td bgcolor="#4e2029" width="15" height="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#4e2029\');">&nbsp;</td>' +
			'<td bgcolor="#b9d0dc" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#b9d0dc\');">&nbsp;</td>' +
			'<td bgcolor="#00365b" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#00365b\');">&nbsp;</td>' + 
			'<td bgcolor="#88adc3" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#88adc3\');">&nbsp;</td>' +
			'<td bgcolor="#887811" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#887811\');">&nbsp;</td>' +
			'<td bgcolor="#3d4242" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#3d4242\');">&nbsp;</td>' +
			'<td bgcolor="black" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'black\');">&nbsp;</td>' +
			'<td bgcolor="white" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'white\');">&nbsp;</td>' +
			'<td bgcolor="purple" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'purple\');">&nbsp;</td>' +
			'<td bgcolor="#e9db1b" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#e9db1b\');">&nbsp;</td>' +
			'<td bgcolor="#006600" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#006600\');">&nbsp;</td>' +
			'<td bgcolor="#ebab00" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#ebab00\');">&nbsp;</td></tr><tr height="15">' +
			'<td bgcolor="#243842" width="15" height="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#243842\');">&nbsp;</td>' +
			'<td bgcolor="#8b713c" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#8b713c\');">&nbsp;</td>' +
			'<td bgcolor="#382225" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#382225\');">&nbsp;</td>' + 
			'<td bgcolor="#891a1c" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#891a1c\');">&nbsp;</td>' +
			'<td bgcolor="#818646" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#818646\');">&nbsp;</td>' +
			'<td bgcolor="#838f97" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#838f97\');">&nbsp;</td>' +
			'<td bgcolor="#462e26" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#462e26\');">&nbsp;</td>' +
			'<td bgcolor="#fabfb7" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#fabfb7\');">&nbsp;</td>' +
			'<td bgcolor="#92785b" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#92785b\');">&nbsp;</td>' +
			'<td bgcolor="#ebd3d5" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#ebd3d5\');">&nbsp;</td>' +
			'<td bgcolor="#bcaf82" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#bcaf82\');">&nbsp;</td>' +
			'<td bgcolor="#d0c0c0" width="15" onclick="javascript:toolbox_editbackgroundcolor_doit(\'#d0c0c0\');">&nbsp;</td></tr><tr>' +
			'<td colspan="15"><input type="button" name="close" value="Close" onclick="javascript:toolbox();">&nbsp;</td>' + 
			'</tr></table>';
	document.getElementById(dt.toolboxidattributevalue).innerHTML = strng;
}

function toolbox_editbackgroundcolor_doit(c){ // c = color
	// update background color
	dt.imagesnamelist[dt.currentimage].bgcolor = c;	
	changeimagebgcolor((dt.currentimage+1), dt.imagecellidattributevalue);
	// commit changes to the database
	jsAjaxCall(scriptwebaddress + '?event=commitbackgroundcolor&imageid=' + document.imagesarray[dt.currentimage].src + '&content=' + dt.imagesnamelist[dt.currentimage].bgcolor.replace('#', '%23') + '&uc=' + jsUC()); // post data
}

function showimage(n, m){ // n = image number, m = image name attribute value
    // show image in the document
	if (document.imagesarray.length >= n){ // image must exist
		dt.currentimage = n-1;
		if (navigator.userAgent.indexOf('Safari') != -1) // Safari workaround for proper resizing
			document[m].src = '../js/clear.gif';
		document[m].src = document.imagesarray[dt.currentimage].src;
	}
	return;
}

function changeimagebgcolor(n, c){ // n = image number, c = image cell id attribute value
	// change the background color for the cell where image appears
	if (c.length > 0) // image cell id attribute available	
		if (dt.imagesnamelist.length >= n) // image information must exist
			document.getElementById(c).bgColor = dt.imagesnamelist[n-1].bgcolor;
	return;
}

function shownotes(n, i){ // n = image number, i = image notes id attribute value
    // show image notes
	if (i.length > 0){ // image notes id attribute available	
		if (dt.imagesnamelist.length >= n) // image information must exist
			document.getElementById(i).innerHTML = dt.imagesnamelist[n-1].notes;
		else	
			document.getElementById(i).innerHTML = '';
	} //if
	return;
}

function togglenavigationelementclassname(ln){ // ln = link number
	// set the element 'className' attribute
	document.getElementById(dt.navigationcellid + dt.currentimage).className = dt.navigationelementclassnameDESELECTED;
	document.getElementById(dt.navigationcellid + dt.currentimage + '_link').className = dt.navigationelementclassnameDESELECTED;
	document.getElementById(dt.navigationcellid + ln).className = dt.navigationelementclassnameSELECTED;
	document.getElementById(dt.navigationcellid + ln + '_link').className = dt.navigationelementclassnameSELECTED;	
}

function navigation(n, m){ // n = NUMBERS or THUMBNAILS | method = onclick, onmouseover, href, etc.
	// show the navigation bar for images using a hyperlink for each image
	var strng = '';
	// create html string to display navigation links	
	if (dt.imagesnamelist.length >= 1){ // there are images to be shown
		var cnt = 0;
		for(k=0; k<dt.imagesnamelist.length; k++){
			if (dt.numberofnavigationlinksperrow >= 1){ // start new row when needed
				cnt = cnt+1;
				if (cnt == 1 && k != 0)
					var strng = strng + '</tr><tr>';
				if (cnt == dt.numberofnavigationlinksperrow)
					cnt = 0;
			}
			var clss = dt.navigationelementclassnameDESELECTED;
			if (k == 0)
				var clss = dt.navigationelementclassnameSELECTED;
			var prestrng = '<td class="' + clss + '" id="' + dt.navigationcellid + k + '">';
			var posstrng = '</td>';
			var lnkstrng = (k+1);			
			if (n == 'THUMBNAILS')
				var lnkstrng = '<img src="' + 'http://' + webaddress + '/' +
							   dt.imagesnamelist[k].thumbnailimagepath + '" border="0">';
			var strng = strng + prestrng + 
						'<a class="' + clss + '" ' +
					    'id="' + dt.navigationcellid + k + '_link" ' +
						m + '="javascript:togglenavigationelementclassname(' + k +
					    ');showimage(' + (k+1) + ',\'' + dt.imagenameattributevalue + '\');' +
						'changeimagebgcolor(' + (k+1) + ',\'' + dt.imagecellidattributevalue + '\');' +
						'shownotes(' + (k+1) + ',\'' + dt.imagenotesidattributevalue + '\');toolbox();">' + lnkstrng + '</a>' + 
						posstrng;
		} // for
	} // if	
	document.write('<table width="100%" border="0">' + strng + '</tr></table>');
}

function navigationbynumbers(){
	navigation('NUMBERS', 'href');
}

function navigationbythumbnailsONHOVER(){
	navigation('THUMBNAILS', 'onmouseover');
}

function navigationbythumbnailsONCLICK(){
	navigation('THUMBNAILS', 'href');
}

function preloadimages(){
  // preload images onto an array under the document object
  mgsrry = new Array();
  if (dt.imagesnamelist.length >= 1){ // there are images to be preloaded
  	for(k=0; k<dt.imagesnamelist.length; k++){
		mgsrry[k] = new Image;
		mgsrry[k].src = 'http://' + webaddress + '/' + dt.imagesnamelist[k].imagepath;
	} // for
  } // if
  if (document.images && mgsrry.length > 0){
  	document.imagesarray = mgsrry;
	return true;
  }	
  return false;
}

function editablearea_show(i){
	var cntntstrng = dt.editableareas_array[i].content;
	if (loginflag == 'true')
		var cntntstrng = cntntstrng + '<div><input type="button" value="Edit" title="Edit Content" onclick="javascript:editablearea_edit(\'' + i + '\');"></div>';
	document.getElementById(dt.editableareas_array[i].idattribute).innerHTML = cntntstrng;
	return;
}

function editablearea_edit(i){
	// display the 'editablearea' tool
	var strng = '<table border="0" cellspacing="0" cellpadding="0"><tr><td>' +
				'<textarea rows="7" name="_editableareafld_' + i + '" id="_editableareafld_' + i + '">' + 
				dt.editableareas_array[i].content + '</textarea><br>' +
				'<input type="button" name="save" value=" Save" onclick="javascript:editablearea_edit_doit(\'' + i + '\');">&nbsp;' + 
				'<input type="button" name="cancel" value="Cancel" onclick="javascript:editablearea_show(\'' + i + '\');">' +
				'</td></tr></table>'
	document.getElementById(dt.editableareas_array[i].idattribute).innerHTML = strng;
}

function editablearea_edit_doit(i){
	// update the editable area
	dt.editableareas_array[i].content = document.getElementById('_editableareafld_' + i).value;
	editablearea_show(i);
	jsAjaxCall(scriptwebaddress + '?event=commiteditablearea&contentid=' + (dt.editableareas_array[i].idattribute.replace('#', '%23')).replace('&', '%26') + '&content=' + (dt.editableareas_array[i].content.replace('#', '%23')).replace('&', '%26') + '&uc=' + jsUC()); // post data
}

function showalleditableareas(){
  if (dt.editableareas_array.length >= 1) // there are editable areas
  	for(k=0; k<dt.editableareas_array.length; k++)
		editablearea_show(k);
  return;
}

function startup(){
    // start the system
	if (preloadimages()){
	    showimage(1, dt.imagenameattributevalue);
		changeimagebgcolor(1, dt.imagecellidattributevalue);
		shownotes(1, dt.imagenotesidattributevalue);
	}
	showalleditableareas();
}

// Variable definitions
loginflag = 'false';
servertype = 'php';
if (typeof(window['enhancedversion']) != "undefined")
	if (enhancedversion)
		servertype = 'cfm';

// Init	
uploadfoldername = jsInitIfUndefined('uploadfoldername', '');
imagenameattributevalue = jsInitIfUndefined('imagenameattributevalue', '');
imagesortorder = jsInitIfUndefined('imagesortorder', '');
imagecellidattributevalue = jsInitIfUndefined('imagecellidattributevalue', '');
imagenotesidattributevalue = jsInitIfUndefined('imagenotesidattributevalue', '');
toolboxidattributevalue = jsInitIfUndefined('toolboxidattributevalue', '');
navigationelementclassnameDESELECTED = jsInitIfUndefined('navigationelementclassnameDESELECTED', '');
navigationelementclassnameSELECTED = jsInitIfUndefined('navigationelementclassnameSELECTED', '');
numberofnavigationlinksperrow = jsInitIfUndefined('numberofnavigationlinksperrow', '');
editableareas_idlist = jsInitIfUndefined('editableareas_idlist', '');

dt = new dataobject('', '', '', new Array(),
					imagenameattributevalue, imagesortorder, imagecellidattributevalue, imagenotesidattributevalue, toolboxidattributevalue, uploadfoldername, -1, 'photocell',
					navigationelementclassnameDESELECTED, navigationelementclassnameSELECTED, numberofnavigationlinksperrow,
					editableareas_idlist, new Array());
if (dt.imagenameattributevalue.length > 0 && dt.uploadfoldername.length > 0) // fetch images only if needed
	dt.uploadfoldername = '&uploadfoldername=' + dt.uploadfoldername;
if (dt.editableareas_idlist.length > 0) // when editable areas are specified
	dt.editableareas_idlist = '&editableareas_idlist=' + dt.editableareas_idlist;	
if (dt.imagesortorder.length > 0) // when the images sort order is specified
	dt.imagesortorder = '&imagesortorder=' + dt.imagesortorder;
webaddress = window.location.host;
scriptwebaddress = 'http://' + webaddress + '/' + servertype + '/alexLIB_photographers.' + servertype; // webaddress & serverype must be declared outside and both are required.

// Fetch initialization data from server (currentyear & imagesnamelist (if needed))
jsAjaxCall(scriptwebaddress + '?event=initializeData' + dt.uploadfoldername + dt.editableareas_idlist + dt.imagesortorder + '&uc=' + jsUC()); // Get data