﻿
/**************************************** xmlHttpRequest ***************************************************/
var xmlHttpRequest = new Object();

  xmlHttpRequest.isFlashInstalled = function() {
  var ret;
  
  //alert(typeof(this.isFlashInstalledMemo) + '3333');
  
  if (typeof(this.isFlashInstalledMemo) != "undefined") { return this.isFlashInstalledMemo; }
  
  //alert(typeof(ActiveXObject) + '444');
  
  if (typeof(ActiveXObject) != "undefined") {
      try {
          var ieObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
      } catch (e) { }
      ret = (ieObj != null);
  } else {
      var plugin = navigator.mimeTypes["application/x-shockwave-flash"];
      ret = (plugin != null) && (plugin.enabledPlugin != null);
  }
  
  this.isFlashInstalledMemo = ret;

  return ret;
}

xmlHttpRequest.getFlash = function() {
  return document.getElementById("storage");
}

xmlHttpRequest.checkFlash = function() {
  try {
      return (this.getFlash().ping() == "pong");
  }
  catch (e) { return false; }
}

xmlHttpRequest.writeFlash = function(swfName) { 
	document.write('<div id="divStorage" style="POSITION:absolute;top:-500px;height:0px;">');
  if (window.ActiveXObject && !xmlHttpRequest.isFlashInstalled())
  {
      document.write('<object id="storage" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
      document.write(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,5,0,0"');
      document.write(' height="' + this.height + '" width="' + this.width + '">');
      document.write(' <param name="movie" value="' + swfName + '">');
      document.write(' <param name="quality" value="high">');
      document.write(' <param name="swliveconnect" value="true">');
      document.write('<\/object>');
  }
  else
  {
      document.write('<object id="storage" data="' + swfName + '"');
      document.write(' type="application/x-shockwave-flash" height="' + this.height + '" width="' + this.width + '">');
      document.write('<param name="movie" value="' + swfName + '">');
      document.write('<param name="quality" value="high">');
      document.write('<param name="swliveconnect" value="true">');
      document.write('<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer">');
      document.write('<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer">');
      document.write('<p>You need Flash for this. Get the latest version from');
      document.write(' <a href="http://www.macromedia.com/software/flashplayer/">here<\/a>.');
      document.write('<\/p>');
      document.write('<\/object>'); 
  }
  document.write('</div>');    
}


xmlHttpRequest.addLoadEvent = function(func) {
	var oldonload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


xmlHttpRequest.load = function() {
	if (typeof(xmlHttpRequest.onload) != "function") { return; } 

	if (xmlHttpRequest.isFlashInstalled()) {
			// if we expect Flash to work, wait for both flash and the document to be loaded
			var finishedLoading = this.flashLoaded && this.documentLoaded;
			if (!finishedLoading) { return; }
	}
	// todo: cancel timer

	var fs = xmlHttpRequest.getFlash();

	if ((!xmlHttpRequest.isFlashInstalled() || this.flashLoaded) && fs) {
			if (xmlHttpRequest.checkFlash()) {
					callAppOnLoad(fs);
			} else {
					callAppOnLoad(null);
			}
	} else {
			callAppOnLoad(null);
	}

	function callAppOnLoad(fs) {
			if (xmlHttpRequest.onloadCalled) { return; } // todo: figure out why this case gets hit
			xmlHttpRequest.onloadCalled = true;
			xmlHttpRequest.onload(fs);
	}
}

xmlHttpRequest.init = function() {
	this.flashLoaded = false;
	this.documentLoaded = false;

	this.addLoadEvent(onload);

	function onload() {
			xmlHttpRequest.documentLoaded = true;
			xmlHttpRequest.load();
	}
}

xmlHttpRequest.init();
xmlHttpRequest.writeFlash("/HanbitSoftXHR.swf");

function storageOnLoad() { 
  xmlHttpRequest.flashLoaded = true;
  xmlHttpRequest.load();
}

function storageOnError() {
	alert("storageOnError"); 
	xmlHttpRequest.flashLoaded = true;
	xmlHttpRequest.load();
}
/**************************************** CallbackManager ***************************************************/

var CallbackManager = new Object();
CallbackManager.callbacks = new Array();

// assigns and returns a unique callback name for the input callback
CallbackManager.registerCallback = function(callback) {
  // todo: could be improved (look for the first available spot in the callbacks table, if necessary, expand it)
  var length = this.callbacks.push(selfDeleteCallback);
  var callbackID = length - 1;
  
  return "CallbackManager.callbacks[" + callbackID + "]";
  
  function selfDeleteCallback(obj) {
      delete CallbackManager.callbacks[callbackID];
      setTimeout(function() { callback(obj); }, 0);
      return;
  } 
}
