var dialogConnections = [];
var frame_src = "";

function popOut(url) {
  // get size info for popout window
  var popOutHeight = (screen.availHeight < 800) ? 575 : 1075;
  var popOutWidth = (screen.availHeight < 800) ? 575 : 1075;
  var sidDim = (screen.availHeight < 800) ? 500 : 1000;
  var x = (screen.availWidth - popOutWidth)/2;
  var y = (screen.availHeight - popOutHeight)/2;
  var newUrl = sidUrl(sidDim, sidDim, url);

  var myWin = window.open(newUrl, "GaStateFair", "toolbar=0,location=0,directories=0,menubar=1," +
          "status=1,scrollbars=1,resizable=1,copyhistory=0,width=" + popOutWidth +
          ",height=" + popOutHeight + ",left=" + x + ",top=" + y);
  myWin.focus();
}

function sidUrl(width, height, href) {
  var id = href.match(/item=(.*)\.sid/);
  return "http://dlgcsm.galib.uga.edu/StyleServer/calcrgn?browser=ns&cat=gsfr&wid=" +
      width + "&hei=" + height + "&style=gsfr/gsfr.xsl&item=" + id[1] + ".sid";
}

dojo.addOnLoad(function() {
  var viewportWidth;
  var viewportHeight;
  // Create dialog and set iframe for SIDs
  var sidDialog = new dijit.Dialog( {
    title: "Georgia State Fair",
    content: dojo.byId("sid_frame"),
    refocus: false,
    draggable: false,
    autofocus: false
  });
  var popOut = document.createElement("a");
  popOut.style.position = "absolute";
  popOut.innerHTML = "Larger Image";
  popOut.title = "Popout for a larger image.";
  popOut.style.backgroundColor = "black";
  popOut.style.color = "white";
  popOut.style.fontSize = "smaller";
  //popOut.style.textDecoration = "none";
  //popOut.style.backgroundImage = "url(/gastatefair/images/new-window-icon.gif)";
  //popOut.style.backgroundPosition = "0 50%";
  //popOut.style.backgroundRepeat = "no-repeat";
  //popOut.style.padding = "1px 4px 1px 4px";
  sidDialog.domNode.appendChild(popOut);

  function updateDialog(url) {
    if (typeof window.innerWidth != 'undefined') {
      // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
      viewportWidth = window.innerWidth,
      viewportHeight = window.innerHeight
    } else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0) {
          // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
          viewportWidth = document.documentElement.clientWidth,
          viewportHeight = document.documentElement.clientHeight
    } else {
      // older versions of IE
      viewportWidth = document.getElementsByTagName('body')[0].clientWidth,
      viewportHeight = document.getElementsByTagName('body')[0].clientHeight
    }

    var sidFrameHeight = (viewportHeight < 655) ? "330px" : "585px";
    var sidFrameWidth = (viewportHeight < 655) ? "350px" : "585px";
    var sidHeight = (viewportHeight < 655) ? "250" : "500";
    var sidWidth = (viewportHeight < 655) ? "312" : "500";
    var popOutTop = (viewportHeight < 655) ? "31.4em" : "56em";
    var popOutRight = (viewportHeight < 655) ? "1em" : "4em";

    popOut.style.top = popOutTop;
    popOut.style.right = popOutRight;

    //if (sidFrameHeight != dojo.byId("sid_frame").style.height) {
      dojo.style("sid_frame", "height", sidFrameHeight);
      dojo.style("sid_frame", "width", sidFrameWidth);
    //}
    if (dojo.byId("sid_frame").src == "" || url != sidUrl(sidWidth, sidHeight, dojo.byId("sid_frame").src)) {
        //dojo.byId("sid_frame").src = sidUrl(sidWidth, sidHeight, url);
        dojo.byId("sid_frame").contentWindow.location.replace(sidUrl(sidWidth, sidHeight, url));
        popOut.href = "javascript:popOut('" + url + "')";
        dojo.style("sid_frame", "border-width", "0");
        dojo.style("sid_frame", "opacity", "0");
        dojo.style("sid_frame", "display", "inline");
        window.setTimeout( "dojo.fadeIn({ node: 'sid_frame', duration: 1000 }).play()", 500);
    }
  }
 
  var pdf_r = /pdf$/; 
  dojo.query(".full_image_link").forEach(function(node) {
      if (!node.href.match(pdf_r) ) {
        // add dialog
        dialogConnections.push(
          dojo.connect(node, "onclick", function(event) {
            event.stopPropagation();
            event.preventDefault();
            //dojo.byId("sid_frame").src = this.href;
            //dojo.style("sid_frame", "height", "585px");
            //dojo.style("sid_frame", "width", "575px");
            frame_src = this.href;
            updateDialog(frame_src);
            sidDialog.show();
          })
        );
      }
  });

  dojo.connect(document, "onclick", function(event) {
      if (event.target.className && event.target.className.match(/^dijitDialogUnderlay/)) {
        sidDialog.hide();
      }
  });

  dojo.connect(popOut, "onclick", function(event) {
      sidDialog.hide();
  });

  window.onresize = function() {
    if (sidDialog.open) {
      updateDialog(frame_src);
    }
  }
});
