/*
 * Copyright (c) 2007, White Dactyl Labs
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of White Dactyl Labs nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WHITE DACTYL LABS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL WHITE DACTYL LABS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

var max_detected = null;
var max_width = null;
var max_height = null;

function do_startup()
{
	if(is_nav && !is_nav6up)
		return;

	max_width = getCookie('gnr_max_width');
	max_height = getCookie('gnr_max_height');

	if(max_width == null || max_height == null) {
		if(winW ==0)
			return;
		max_width = winW-25;
		max_height = winH-125;
		max_detected = 1;
	} else {
		max_detected = 0;
	}
	fit();
}

function redetect()
{
	if(max_detected != null
	&& max_detected == 1)
	{
		if(is_nav4up) {
			winW = window.innerWidth;
			winH = window.innerHeight;
		} else {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
		max_width = winW-25;
		max_height = winH-125;
	}
}

function fit()
{
	var width = large_width;
	var height = large_height;
	var use_large = 1;

	if(max_width < width) {
		width = max_width;
		height = width * large_height / large_width;
	}

	if(max_height < height) {
		height = max_height;
		width = height * large_width / large_height;
	}

	if(width < 10 || height < 10)
	{
		width = small_width;
		height = small_height;
	}

	if((width > small_width || height > small_height) && is_large)
		document.gallery.src='large/' + picture + '.jpg';
	else
		document.gallery.src= picture +'.jpg';

	document.gallery.width = width;
	document.gallery.height = height;
}

function bigger()
{
	if(is_nav && !is_nav6up)
		return;

	var width = document.gallery.width;

	width += 100;
	if(width < 512)
		width = 512;
	if(width > large_width)
		width = large_width;

	var height = width * large_height / large_width;

	if(width > small_width && is_large)
		document.gallery.src='large/' + picture + '.jpg';
	else
		document.gallery.src=picture + '.jpg';

	document.gallery.width = width;
	document.gallery.height = height;
}

function smaller()
{
	if(is_nav && !is_nav6up)
		return;

	var width = document.gallery.width;

	width -= 100;
	if(width < 512)
		width = 512;
	if(width > large_width)
		width = large_width;

	var height = width * large_height / large_width;

	if(width > small_width && is_large)
		document.gallery.src='large/' + picture + '.jpg';
	else
		document.gallery.src=picture + '.jpg';

	document.gallery.width = width;
	document.gallery.height = height;
}

do_startup();

