/*
 File $Workfile: jfgallery.js $ 
 $Archive: /IMatch/scripts/WebGallery/jfgallery/jfgallery.js $ 
 Last changed by $Author: Jürgen Failenschmid $ 
 Last modified   $Modtime: 7/29/05 2:10p $ 
 Last checked in $Date: 7/29/05 2:36p $ 
 $Revision: 11 $ 
*/

/*
  If condition is true, create a link for trueImage with given width/height,
  otherwise just reference the falseImage.
  trueAlternate, falseAlternate are alternative representations of the corresponding images, used for balloon text when hovering over button. Note: "title=" is for
  the balloon text in Firefox.
*/
function button(condition, trueImage, falseImage, width, height, link, trueAlternate, falseAlternate) {
	if (condition) {
		document.write('<div class="navbutton"><a href="')
		document.write(link)
		document.write('"><img src="')
		document.write(trueImage)
		document.write('" width="')
		document.write(width)
		document.write('" height="')
		document.write(height)
		document.write('" alt="')
		document.write(trueAlternate)
		document.write('" title="')
		document.write(trueAlternate)
		document.write('"></a></div>')
	}
	else {
		document.write('<div class="navbutton">')
		document.write('<img src="')
		document.write(falseImage)
		document.write('" width="')
		document.write(width)
		document.write('" height="')
		document.write(height)
		document.write('" alt="')
		document.write(falseAlternate)
		document.write('" title="')
		document.write(falseAlternate)
		document.write('"></div>')
	}
}

// Answer the minimum of the argument and the actual screen width
function widthLimited(desiredWidth) {
	return Math.min(desiredWidth, screen.availWidth);
}

// Answer the minimum of the argument and the actual screen height
function heightLimited(desiredHeight) {
	return Math.min(desiredHeight, screen.availHeight);
}

//If the window opener is still open, it is brought to the top.
//Otherwise the opener is re-opened to display the URL.
//Note: the main window must have its name set to "jfmain".
function refocus(url) {
	if (window.opener.closed) {
		opener = window.open(url,'jfmain');
	} 
	else {
		window.opener.focus();
	}
	return false;
}

/*
  Constructs a URL from the given arguments and opens it in the current window/frame.
  The resulting URL looks like:
  '<#Base#>medium<#CurCatID#>/cat_single<#CurCatID#>-<#CurImageNo#>.htm'
  where the variables are replaced with calculated values. Function arguments:
  - base is the folder containing the HTML source file, e.g. "../"
  - catId is the ID of the current category, e.g. 12
  - picNum is the index of the current image, e.g. 3
  - subset (optional) is the subset index, e.g. 0
  Expects definitions of arrays that correspond to values for the subset choices:
  - subsets, e.g. new Array("thumbs", "small", "medium", "large", "max")
  - templates, e.g. new Array("cat-thumbs.htm", "cat-small.htm", "cat-single.htm", "cat-single.htm", "cat-single.htm");
  - numPics, e.g. new Array(12, 2, 1, 1, 1) 
  The values with array index 0 are for the thumbnail pages that are selected from the category
  navigation frame. When the user selects a thumbnail, a detailed view is opened. Detailed views
  are linked by previous/next page navigation and are called a subset. Each array must contain 
  the same number of elements.
  - Array subsets: elements are subset names and the same as the folder names used in the ProcessFile
    statements.
  - Array templates: elements are the file names of the HTML templates used in the ProcessFile statements. 
  - Array numPics: elements are the maximum number of pictures per generated page. For the GridPage
    generator, multiply the number of rows by the number of columns. For ImagePage use 1. None of the 
    entries of array numPics may be zero.
  A cookie is used to remember the subset chosen by the user. If the cookie does not exist,
  then it is created with a default value. This default value can be defined by 
  setting variable defaultSubset to the default index, e.g. 2. The cookie is kept for the
  time specified in variable subsetSeconds (default is 30 days).
  The Javascript file cookie.js must be included if this function is used.
  Documentation of cookie functions are at http://javascript.about.com/library/blcookie.htm
  NOTE: This function currently only works if generated pages are in subfolders of the base folder.
*/
function showDetail(base, catId, picNum, subset) {
	var myCookie = "currentSubset";
	var currentSubset;
	if (showDetail.arguments.length > 3) 
		currentSubset = subset;
	else {
		currentSubset = get_cookie(myCookie);
		//window.alert("cookie value = " + currentSubset);
		if (currentSubset == null) {
			//window.alert("first time: default = " + defaultSubset);
			currentSubset = defaultSubset;
		}
		else if (0 > currentSubset || currentSubset > subsets.length - 1) {
			//overrides invalid value found in cookie
			currentSubset = defaultSubset;
		}
	}
	if (0 > currentSubset || currentSubset > subsets.length - 1) 
		throw("Invalid currentSubset value: " + currentSubset);
	if (currentSubset > 0) {
		var timeToKeep;
		if (subsetSeconds) {
			//window.alert("subsetSeconds = " + subsetSeconds);
			timeToKeep = subsetSeconds * 1000;
		}
		else {
			timeToKeep = 60000*60*24*30; // default is 30 days
		}
		var expires = new Date();
		expires.setTime(expires.getTime() + timeToKeep);
		set_cookie(myCookie, currentSubset, expires); 
	}
	var htmlParts = templates[currentSubset].split(".");
	var url = base + subsets[currentSubset] + catId + "/" + htmlParts[0] + catId
		+ "-" + Math.floor((picNum + numPics[currentSubset] - 1) / numPics[currentSubset]) 
		+ "." + htmlParts[1]
	//window.alert(url);
	window.location.href = url;
}
