var DocOverlay = function() { this.initialize(); }

$.extend(DocOverlay.prototype, {
  th: this,
  initialize: function() {
	$('body').append('<div id="dol_overlay"></div>');
	$('body').append('<div id="dol_document"></doc>');
  },
  show: function(url)
  {
	$("#dol_overlay").css({opacity:0.8, height: $(document).height()})
				 .fadeIn("slow",this.loadurl(url));
	$(document).bind("keypress", this.keyboardAction);
  },
  loadurl: function(pageurl)
  {
	var th = this;
	$.ajax({
		url: pageurl + "?a=1",
		dataType: "jsonp",
		success: this.fadein,
		error: this.hide
	});
	//$.getJSON(url, function() { alert('success'); });
  },
  fadein: function(data)
  {
	  	//.css({marginLeft:"-"+$(this).width()/2+"px"})
		
		var th = this;
	  	$("#dol_document")
	  	.html(data.content)
	  	.css({opacity: 0})
		.fadeIn("fast")
		.css({marginLeft:"-400px"})
		.fadeTo(2000,1.0);
		$('.back').bind('click', function() { th.hide(); return false;});
  },
  hide: function()
  {
	$(document).unbind("keypress", this.keyboardAction);
    $("#dol_document").fadeOut("slow", function() {$("#dol_overlay").fadeOut("slow");})
  },
  //
  //  enableKeyboardNav()
  //
  enableKeyboardNav: function() {
      document.observe('keydown', this.keyboardAction); 
  },

  //
  //  disableKeyboardNav()
  //
  disableKeyboardNav: function() {
      document.stopObserving('keydown', this.keyboardAction); 
  },
  //
  //  keyboardAction()
  //
  keyboardAction: function(event) {
	  var th = this.th;
      var keycode = event.keyCode;

      var escapeKey;
      if (event.DOM_VK_ESCAPE) {  // mozilla
          escapeKey = event.DOM_VK_ESCAPE;
      } else { // ie
          escapeKey = 27;
      }

      var key = String.fromCharCode(keycode).toLowerCase();
      
      if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close document overlay
    	  $(document).unbind("keypress", this.keyboardAction);
    	  $("#dol_document").fadeOut("slow", function() {$("#dol_overlay").fadeOut("slow");})
      } 
  },
  get_cookie: function( cookie_name )
  {
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results ) return ( unescape ( results[2] ) );
    else return null;
  },
  delete_cookie: function( cookie_name )
  {
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
  }
});

