SITE_URL = "/";
_onload_functions = new Array();
_calendar_date_format = "%m/%d/%Y";

function calcMaxProportions(width, height, maxWidth, maxHeight) {
  if(width > maxWidth || height > maxHeight) {
    if(width / height > maxWidth / maxHeight) {
      var k = maxWidth / width;
      width = width * k;
      height = height * k;
    } else {
      var k = maxHeight / height;
      width = width * k;
      height = height * k;
    }
  }
  return {width: width, height: height};
}

function hideMaximized(url, width, height) {
  var el = document.getElementById("previewContainer");
  el.style.display = "none";
  el.parentNode.removeChild(el);
  unshadeBody();
  return false;
}

function showMaximized(container, width, height) {
  var url = container.href;
  if ("" == url || "#" == url) {
    return false;
  }
  var newWindow = (typeof($.fn.nyroModal) == "undefined" || document.compatMode == "BackCompat");  
  var base = document.getElementsByTagName("base")[0];
  if(!url.match(/^https?:\/\//i)) url = base.href + url;
  var htmlStart = "", htmlEnd = "", html = "";
  
  if (url.match(/.*\.htm*$/i) || url.match(/.*\.jsp*$/i)) {
    html = "<iframe src='" + url + "' width='" + width + "' height='" + height + "' frameborder='0'></iframe>";
  } else if (url.match(/.*\.jpg$/i)) {
    html = "<img src='" + url + "' width='" + width + "' height='" + height + "' alt='' border='0' />";
  } else if(url.match(/.*\.mov$/i)) {
    height+=16;
    html =
    "<object width='" + width + "' height='" + height + "' " +
    " classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B'" +
    " codebase='http://www.apple.com/qtactivex/qtplugin.cab'>" +
    " <param name='src' value='" + url + "'>" +
    " <param name='autoplay' value='true'>" +
    " <param name='controller' value='true'>" +
    "<embed src='" + url + "' width='" + width + "' height='" + height + "'" +
    " autoplay='true' controller='true'" +
    " pluginspage='http://www.apple.com/quicktime/download/'></embed></object>";
  } else if(url.match(/.*\.flv$/i)) {
    height+=20;
    html =
    "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' " +
    " width='" + width + "' height='" + height + "'>" +
    "  <param name='movie' value='../../resources/flvplayer.swf?file=" + url + "&autostart=true'>" +
    "  <param name='autostart' value='true'>" +
    "  <param name='file' value='" + url + "'>" +
    "  <embed src='../../resources/flvplayer.swf?file=" + url + "&autostart=true' width='" + width + "' height='" + height + "' " +
    "   pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' " +
    "   file='" + url + "' autostart='true'/>" +
    "</object>";
  } else {
    height+=45;
    html =
    "<OBJECT width='" + width + "' height='" + height + "'" +
    " classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'" +
    " codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'" +
    " standby='Loading player components...' type='application/x-oleobject'>" +
    " <param name='fileName' value='" + url + "'>" +
    " <param name='animationatStart' value='true'>" +
    " <param name='transparentatStart' value='true'>" +
    " <param name='showControls' value='true'>" +
    " <EMBED type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/'" +
    "   showcontrols='true' width='" + width + "' height='" + height + "' src='" + url + "'>" +
    " </EMBED>" +
    " </OBJECT>";
  }

  if(newWindow) {
    htmlStart += "<base href='" + base.href + "'/>";
    var links = document.getElementsByTagName("link");
    for(var i=0; i<links.length; i++) {
      htmlStart += "<link rel='" + links[i].rel + "' href='" + links[i].href + "' type='" + links[i].type + "'/>";
    }
    htmlStart +=
      "<table width='100%' height='100%' cellpadding='0' cellspacing='0'><tr valign='middle'><td align='center'>";
    htmlEnd = "</td></tr></table>";
    var coords = calcMaxProportions(width, height, screen.width, screen.height-60);
    width = coords.width;
    height = coords.height;
    var left = (screen.width-width)/2;
    var top = (screen.height-60-height)/2;
    if(left < 0) left=0;
    if(top < 0) top = 0;
    var openedWindow = window.open("about:blank", "_blank", "height="+height+", width="+width+", left="+left+", top="+top+", location=no, menubar=no, resizable=yes, status=no, toolbar=no, scrollbars=no");
    openedWindow.document.open();
    openedWindow.document.write(htmlStart + html + htmlEnd);
    openedWindow.document.close();
    return false;
  } else {
    if(url.match(/.*\.jpg|jpeg$/i)) {
      $(container).attr({target: "_self"}).nyroModalManual();
    } else {           
      $.nyroModalManual({
        content: html,
        width: width,
        height: height
      });
    }
    return false;
  }
  return false;
}

function openNewWindow(url, target) {
  window.open(url, target);
}
