// write EXIF, IPTC or XML data to popup window
function exportData(f) {
	arg = f.options[f.options.selectedIndex].value
	typ = arg.substr(0,3)		// csv, tag, xml
	
	if(!document.settings.tagged.checked) {
		alert('Please enable the "Tag" column and make your changes.\nIt makes no sense to export the original tag list... \n')
		return
	}
	
	alerttext = helpMsg(typ)
	alert(alerttext)
	
	if(typ == "xml") data = makeXML(arg)
	if(typ == "tag") data = makeTag(arg)
	if(typ == "csv") data = makeCSV(arg)
	
	Size     = ",width=550,height=460"
	Options  = "menubar=yes,scrollbars=yes,status=no,resizable=yes,toolbar=no" + Size
	newWin   = open("", "Popup4", Options)
	newWin.focus()
	newWin.document.write(data)
}


// generate XML file for BB´s Custom Order
function makeXML(arg) {
	txt = '<breezebrowser version="2.1">\n'
	txt+= '\t<files directory="'
	txt+= dirSlash
	txt+= '" order="custom" reverse="0">\n'
	for(i=0; i<imgdata.length; i++)
	  txt+= '\t\t<file name="' + imgdata[i].file + '"/>\n'
	txt+= '\t</files>\n'
	txt+= '</breezebrowser>\n'
	return (txt)
}

// generate Tag file for BB
function makeTag(arg) {
	txt = ""
	for(i=0; i<imgdata.length; i++)
		if(document.taggedpics.elements[i+1].checked)
			txt+= dirSlash + imgdata[i].file + '\n'
	    
	if(txt == "") txt = "no images selected"
	return (txt)
}

// generate CSV table from EXIF data
function makeCSV(cols) {
	txt = ""
	for(i=0; i<imgdata.length; i++) {
		for(var j in imgdata[i])
			txt+= imgdata[i][j] + "\t"
		txt+= "\n"
	}
	return (txt)
}
// generate CSV file for all   EXIF data
// generate CSV file for shown EXIF data
// generate CSV file for all   IPTC data
// generate CSV file for shown IPTC data


function helpMsg(arg) {
	if(arg == "xml") {
		txt = 'If you click OK on this alert an EMPTY popup window will appear. \n'
		txt+= 'Click View -> Source Code and an editor showing the code will appear. \n'
		txt+= '(Do not use File -> Save in the Browser as this will not work.) \n'
		txt+= 'Save the code from the editor as a XML text file (i.e. "myorder.xml"). \n'
		txt+= 'Open Breeze Browser and import the file as Custom Order. \n'
	}
	if(arg == "tag") {
		txt = 'If you click OK on this alert a popup window will appear. \n'
		txt+= 'Click View -> Source Code and an editor showing the code will appear. \n'
		txt+= '(Do not use File -> Save in the Browser as this will not work.) \n'
		txt+= 'Save the code from the editor as a text file (i.e. "myfavs.tag"). \n'
		txt+= 'Open Breeze Browser and import the file using "Load Tags...". \n'
	}
	if(arg == "csv") {
		txt = 'After you click OK on this alert a popup window will appear. \n'
		txt+= 'Click View -> Source Code and an editor showing the code will appear. \n'
		txt+= '(Do not use File -> Save in the browser as this will not work.) \n'
		txt+= 'Save the code from the editor as a text file (i.e. "mydata.txt"). \n'
		txt+= 'Open Excel and import the text file. \n'
	}
	return (txt)
}

function helpTags() {
	txt = 'Note: You can add/delete tags and export them\n'
	txt+= 'but a table reload will restore the original values\n'
	txt+= '(indicated by a grey frame around the checkbox).'
	alert(txt)
}


// functions for pop up windows
function popup (fn, nr) {
	Size     = ",width=550,height=450"
	Options  = "menubar=no,scrollbars=yes,status=no,resizable=yes,toolbar=no" + Size
	url_fn   = fn + "?nr=" + nr
	newWin   = open(url_fn, "Popup", Options)
	newWin.focus()
}

function exifpopup (fn, nr) {
	Size     = ",width=280,height=560"
	Options  = "menubar=no,scrollbars=yes,status=no,resizable=yes,toolbar=no" + Size
	url_fn   = fn + "?nr=" + nr
	newWin   = open(url_fn, "Popup2", Options)
	newWin.focus()
}

function iptcpopup (fn, nr) {
	Size     = ",width=350,height=470"
	Options  = "menubar=no,scrollbars=yes,status=no,resizable=yes,toolbar=no" + Size
	url_fn   = fn + "?nr=" + nr
	newWin   = open(url_fn, "Popup3", Options)
	newWin.focus()
}

// get current image postition for prev/next links in a pop-up window
function nextImgUrl (imgnr) {
	newImg = nextID(imgnr)
	return imgdata[newImg].url  + "?nr=" + imgdata[newImg].imageNumber
}

function prevImgUrl (imgnr) {
	newImg = prevID(imgnr)
	return imgdata[newImg].url  + "?nr=" + imgdata[newImg].imageNumber
}

function nextExifUrl (imgnr) {
	newImg = nextID(imgnr)
	return imgdata[newImg].url2 + "?nr=" + imgdata[newImg].imageNumber
}

function prevExifUrl (imgnr) {
	newImg = prevID(imgnr)
	return imgdata[newImg].url2 + "?nr=" + imgdata[newImg].imageNumber
}

function nextIptcUrl (imgnr) {
	newImg = nextID(imgnr)
	return imgdata[newImg].url3 + "?nr=" + imgdata[newImg].imageNumber
}

function prevIptcUrl (imgnr) {
	newImg = prevID(imgnr)
	return imgdata[newImg].url3 + "?nr=" + imgdata[newImg].imageNumber
}


function nextID (imgnr) {
	for(i=0; i<imgdata.length - 1; i++) 		// until one before last
		if(imgnr == imgdata[i].imageNumber)
			return i+1
	return 0					// else begin with first img
}

function prevID (imgnr) {
	for(i=1; i<imgdata.length; i++) 		// from second
		if(imgnr == imgdata[i].imageNumber)
			return i-1
	return imgdata.length - 1			// else go to last img
}
