// Global variables
// ------------------------------------------------------------------------
COOKIE_PATH =
    "/";
COOKIE_PREFIX         = "multiLingualSkin_";
FULLSCREEN_COOKIE     = COOKIE_PREFIX + "fullscreen";
CAPTION_ENABLE_COOKIE = COOKIE_PREFIX + "captionEnable";
SLIDE_SHOW_COOKIE     = COOKIE_PREFIX + "slideShow";
RADIO_LANGUAGE_COOKIE = COOKIE_PREFIX + "radioLanguage";
CAPTION_SIZE_COOKIE   = COOKIE_PREFIX + "captionSize";
DELAY_COOKIE          = COOKIE_PREFIX + "delay";
RESOLUTION_COOKIE     = COOKIE_PREFIX + "resolution";

// We do/don't have both low and hi res images
isResolution = false

// Utlity functions
// ------------------------------------------------------------------------

function setVal()
{
   colCount = 5;
   rowCount = 4;
   win = new Array ("w","h");
   avail = new Array ("w","h");

   if (window.innerWidth)
   {
      win["w"] = window.innerWidth;
      win["h"] = window.innerHeight;
   }

   else if (document.body.clientWidth)
   {
      win["w"] = document.body.clientWidth;
      win["h"] = document.body.clientHeight;
   }

   else
   {
      win["w"] = screen.width - 25;
      win["h"] = screen.height - 115;
   }


   avail["w"] = win["w"];
   avail["h"] = (textHeight > 0) ? textHeight : win["h"];
//alert(avail["w"] + "," + avail["h"]);
}

function sizeImg(imgW, imgH)
{
   imgRatio = imgW / imgH;

   function scaleWB() { tWidth = avail["w"]; tHeight = tWidth / imgRatio; }
   function scaleBW() { tHeight = avail["h"]; tWidth = tHeight * imgRatio; }

   if (imgRatio >= 1)        // determination of orientation of image
   {
      scaleWB();
      if (tHeight > avail["h"]) { scaleBW(); }

   }
   else
   {
      scaleBW();
      if (tWidth > avail["w"]) { scaleWB(); }
   }
}

// See if the user's browser is up-to-date
var compatibleBrowser = false;
var ie6 = false;
function checkBrowser()
{
    if (parseInt(navigator.appVersion) >= 3)
    {
        // We need Netscape 7.0 or higher (reports appVersion of 5.0)
        if ( (navigator.appName=="Netscape") && (parseInt(navigator.appVersion) >= 5) )
        {
            compatibleBrowser = true;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1)
        {
            // Look for Internet Explorer 6.0 or higher.
            var version = 0;
            if (navigator.appVersion.indexOf("MSIE")!=-1)
            {
                temp = navigator.appVersion.split("MSIE");
                version = parseFloat(temp[1]);
                if (version >= 6.0)
                {
                    ie6 = true;
                    compatibleBrowser = true;
                }
            }
        }
    }
}

function adjustAll(imageWidth, imageHeight)
{
   if (compatibleBrowser)
   {
      setVal();
      sizeImg(imageWidth, imageHeight);
      chooseBestImage(imageWidth, imageHeight);
      document.getElementById("image").width = tWidth;
      document.getElementById("image").height = tHeight;
      document.getElementById("image").src =
        chooseBestImage(imageWidth, imageHeight);
   }
}

function closeupWin(url)
{
    if (document.forms.settings.fullscreen.checked)
    {
        return(window.open(url, "fullscreen" + Math.floor(Math.random()*1e6),
            "toolbar=0,scrollbars=0,menubar=0," +
            "location=0,resizable=1,directories=0,status=0,fullscreen=yes,width=" +
            (screen.width) + ",height=" + (screen.height) + ",left=0,top=0"));
    } else {
        // Note: everything in the third arg must be specified (including
        // width and height) to change from a full-screen window to
        // a normal window.
        return(window.open(url, "screen" + Math.floor(Math.random()*1e6),
            "toolbar=1,scrollbars=1,menubar=1," +
            "location=1,resizable=1,directories=1,status=1,fullscreen=no,width=" +
            "500,height=500"));
    }
}

function closeup(url)
{
    closeupWin(url);
}




// Menu-item callbacks
// ------------------------------------------------------------------------
function toggleFullscreen(isToggled)
{
    fs = window.document.forms.settings.fullscreen;
    // If the user clicked on the text, not the checkbox, then the state
    // of the checkbox must be toggled.
    if (!isToggled)
        fs.checked = !fs.checked;
    setCookie(FULLSCREEN_COOKIE, fs.checked, cookieTime(), COOKIE_PATH);
    if (isParentForm())
        parentForm().fullscreen.checked = fs.checked;
    // Only make slide pages full screen.  Keep the index pages
    // normal size.
    if (isSlidePage)
        reopenWindow();
}

function toggleCaption(isToggled)
{
    ce = window.document.forms.settings.captionEnable;
    // If the user clicked on the text, not the checkbox, then the state
    // of the checkbox must be toggled.
    if (!isToggled)
        ce.checked = !ce.checked;
    setCookie(CAPTION_ENABLE_COOKIE, ce.checked, cookieTime(), COOKIE_PATH);
    if (isParentForm())
        parentForm().captionEnable.checked = ce.checked;
    if (isSlidePage)
    {
        if (compatibleBrowser)
        {
            if (ce.checked)
                vis = "visible";
            else
                vis = "hidden";
            document.getElementById("comment").style.visibility = vis;
            document.getElementById("comment_shadow").style.visibility = vis;
        } else {
            window.location.reload();
        }
    }
}

function toggleSlideShow(isToggled)
{
    ss = window.document.forms.settings.slideShow;
    // If the user clicked on the text, not the checkbox, then the state
    // of the checkbox must be toggled.
    if (!isToggled)
        ss.checked = !ss.checked;
    setCookie(SLIDE_SHOW_COOKIE, ss.checked, cookieTime(), COOKIE_PATH);
    if (isParentForm())
        parentForm().slideShow.checked = ss.checked;
    if (isSlidePage)
    {
        // Reload document to fix pause/play button
        // Also starts/stops timer.
        window.location.reload();
/*            if (ss.checked)
                slide();
            else
                if (timeout)
                    clearTimeout(timeout);
*/
    }
}

function changeDelay(amount)
{
    // The currently running timer doesn't get changed.
    //  Fix this later?
    de = window.document.forms.settings.delay;
    de.value = parseInt(de.value) + amount;
    setCookie(DELAY_COOKIE, de.value);
    if (isParentForm())
        parentForm().delay.value = de.value;
}

function changeCaptionSize(amount)
{
    cs = window.document.forms.settings.captionSize;
    cs.value = parseInt(cs.value) + amount;
    setCookie(CAPTION_SIZE_COOKIE, cs.value, cookieTime(), COOKIE_PATH);
    if (isParentForm())
        parentForm().captionSize.value = cs.value
    if (isSlidePage)
    {
        if (compatibleBrowser)
        {
            document.getElementById("comment").style.fontSize = cs.value;
            document.getElementById("comment_shadow").style.fontSize = cs.value;
        } else {
            document.location.reload();
        }
    }
}

function setResolution(value)
{
    ir = window.document.forms.settings.resolution;
    ir[value].checked = true;
    setCookie(RESOLUTION_COOKIE, value, cookieTime(), COOKIE_PATH);
    if (isParentForm())
        parentForm().resolution[value].checked = true;
    if (isSlidePage)
    {
        if (compatibleBrowser)
            document.getElementById("image").src = chooseBestImage();
        else
            document.location.reload();
    }
}




// Menu/form functions
// ------------------------------------------------------------------------

// True if a the parent window contains all the
// controls in the proper form
function isParentForm()
{
    return(isFormWindow(window.opener));
}


function parentForm()
{
    return(window.opener.document.forms.settings);
}

function reopenWindow()
{
    // Ask the index page to reopen this page
    if (window.opener && !window.opener.closed)
        newWin = window.opener.window.closeupWin(currentPath);
    else
        newWin = closeupWin(document.location);
    closeWindowAfter();
}

// Sometimes closing too soon causes problems.  Use this
// to wait until the new window is open before closing
// this one.
function closeWindowAfter()
{
    if (!isFormWindow(newWin))
        setTimeout("closeWindowAfter();", 200);
    else
        window.close();
}

// Only do something for slide pages
function closeWindow()
{
    if (isSlidePage)
    {
        if (window.opener && !window.opener.closed)
            window.opener.focus();
        window.close();
    }
}

// First, functions to test for the existence of a web page
// with the necessary forms and controls.
// True if a the given window contains all the
// controls in the proper form and after the window has been
// fully loaded
function isFormWindow(win)
{
    form = undefined;
    try {        
        // Trying to get win.document if this window was
        // opened by a secure (https) site results in an
        // error.  Just catch it and assume we can't get
        // to the form. 
        if (win && !win.closed && win.document &&
            win.document.forms &&
            win.document.forms.settings)
            form = win.document.forms.settings;
    } catch (er) {
    }
    if (form)
    {
        res = isResolution ? form.resolution : true;
        return (form && form.fullscreen && form.slideShow &&
            form.delay && form.captionEnable && form.captionSize &&
            form.radioLanguage && form.loaded &&
            form.loaded.checked && res);
    } else
        return false;
}

// Assumes the settings form is loaded and valid in this web page.
function updateFormData()
{
    form = window.document.forms.settings;
    if (getCookie(FULLSCREEN_COOKIE) != null)
        form.fullscreen.checked = getBooleanCookie(FULLSCREEN_COOKIE);
    if (getCookie(CAPTION_ENABLE_COOKIE) != null)
        form.captionEnable.checked = getBooleanCookie(CAPTION_ENABLE_COOKIE);
    if (getCookie(SLIDE_SHOW_COOKIE) != null)
        form.slideShow.checked = getBooleanCookie(SLIDE_SHOW_COOKIE);
    if (getCookie(RADIO_LANGUAGE_COOKIE) != null)
    {
        i = getLanguageIndex(getCookie(RADIO_LANGUAGE_COOKIE));
        if ((i != -1) && form.radioLanguage.length)
            form.radioLanguage[i].checked = true;
        else
            form.radioLanguage.checked = true;
    }
    if (getCookie(DELAY_COOKIE) != null)
        form.delay.value = getCookie(DELAY_COOKIE);
    if (getCookie(CAPTION_SIZE_COOKIE) != null)
        form.captionSize.value = getCookie(CAPTION_SIZE_COOKIE);
    if (isResolution && (getCookie(RESOLUTION_COOKIE) != null))
        form.resolution[parseInt(getCookie(RESOLUTION_COOKIE))].checked = true;
}



// Cookie functions, copied from
// http://www.webreference.com/js/column8/functions.html
// -------------------------------------------------------
// Notes: use / for the path, that's easiest

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// Make all my cookies last for 365 days:
// 365 days * 24 hours * 60 minutes * 60 seconds * 1000 ms
function cookieTime()
{
    return(fixDate(new Date()) + 365*24*60*60*1000);
}


function getBooleanCookie(name)
{
    return ((getCookie(name) == "true") ? true : false);
}


// A cute trick I found on the web
// Make IE4 work with getElementById
if (document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

// Another cute web trick:
// load a dynamically-named script from a web page.
// Doesn't work with Mozilla 1.4
//  <script type="text/javascript" id="dynscript">
//  document.getElementById("dynscript").src = "whatever script you want";
//  </script>


// Set the language strings
setLanguageStrings(getLanguage());

