// Start from caiolib
function isFunction(a)  { return typeof a == 'function' }

function isObject(a)    { return (a && typeof a == 'object') || isFunction(a) }

function isString(a)    { return typeof a == 'string' }

function isElement(o, strict) {
    return o && isObject(o) && ((!strict && (o==window || o==document)) || o.nodeType == 1 || o.nodeType == 3);
}

function getElem(el) {
    var ge = (document.getElementById && function(id){return document.getElementById(id)} ) ||
             (document.all && function(id){return document.all[id]} ) ||
             function(){return null};
    return isElement(el)? el : isString(el) ? ge(el) : null;
}
// End caiolib

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" : "");
  // alert(curCookie);
  document.cookie = curCookie;
}

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));
}

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";
  }
}

function Write_How_Long_Ago(epoch_time)
{
   var hours = (new Date() - new Date(epoch_time)) / 3600000;
   document.write('(' + How_Long_Ago(hours) + ')');
}

function How_Long_Ago(hours)
{
   if (hours >= 24)
   {
      var days = Math.round(hours/24);
      if (days > 365)
      {
	 var years = Math.floor(days / 365);
         var how_long = years + ((years == 1) ? " year" : " years");
	 var days = days % 365;
	 if (days > 0)
	    how_long += ", " + How_Long_Ago(days * 24)
	 return how_long;
      }
      else
      {
         return days + ((days == 1) ? " day" : " days") + " ago";
      };

   }
   else
      return Math.round(hours) + " hours ago";

}

function Create_Res_Select()
{
   var Names = new Array("small","medium","large");
   var Buff = "";
   var I, Name;
   var Res = Get_Cookie_Res();

   for (I in Names)
   {
      Name = Names[I];
      Buff += "<option ";
      if (Name == Res)
         Buff += " selected ";
      Buff += ">" + Name + "</option>";
   };
   document.write(Buff);
}

function Get_Cookie_Res()
{
   var Res = getCookie("PAScreenRez");

   if ((Res != "small") && (Res != "medium") && (Res != "large"))
      Res = "medium";
   return Res;
}


function Create_Img_Tag(src)
{
   var size = Max_Size(); // in per-photo template
   document.write('<img border="0" alt="Click to view next photo in folder" src="' +
      src + '/' + size.width + 'x' + size.height + '.jpg">');
}

function Set_Resolution()
{
   var Sizer = document.size_form.sizer;
   var Res = Sizer.options[Sizer.selectedIndex].text;
   setCookie("PAScreenRez", Res, new Date(2099, 1, 1), 
      "/", ".muegel.org");
   window.location.reload(true);
}

