var GB_DONE = false;

function GB_show(caption, url) {
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img src='/img/close.gif' alt='Close window'/></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").prepend("<iframe id='GB_frame' src='"+url+"'></iframe>");
  
  // $("#GB_window").prepend('\
  // <object id="GB_frame" CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="900" height="540" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">\
  // <param name="qtsrc" value="http://audio.veganradio.com/gc/groundcrew-salesdemo.mov">\
  // <param name="autoplay" value="true">\
  // <param name="loop" value="false">\
  // <param name="controller" value="true">\
  // <embed src="sample.mov" qtsrc="http://audio.veganradio.com/gc/groundcrew-salesdemo.mov" width="900" height="540" autoplay="true" loop="false" controller="true" scale="aspect" pluginspage="http://www.apple.com/quicktime/"></embed>\
  // </object>\
  // ');
  

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();
  $("#GB_window").fadeIn("slow")
  // $("#GB_window").show();
}

function GB_hide() {
  $("#GB_frame").remove();
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var GB_HEIGHT = 590;
  var GB_WIDTH = 940;
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}


// descends from Klaus Hartl's "cookie plugin"
$.cookie = function(name, value) {
  if (typeof value != 'undefined') { 
    // name and value given, set cookie
    document.cookie = [name, '=', encodeURIComponent(value)].join('');
  } else { 
    // only name given, get cookie
    if (!document.cookie || document.cookie == '') return null;
    var cookies = document.cookie.split('; ');
    for (var i in cookies) {
      if (cookies[i].split) {
        var part = cookies[i].split('=');
        if (part[0] == name) return decodeURIComponent(part[1].replace(/\+/g, ' '));
      }
    }
    return null;
  }
};


$(document).ready(function(){
  $("a.greybox").click(function(){
    var t = this.title || this.innerHTML || this.href;
    GB_show(t,this.href);
    return false;
  });
});
