﻿var FlashHelper_version = 1;

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

/**************************************** FlashHelper *************************************************/

var FlashHelper = new Object();
FlashHelper.height = 1;
FlashHelper.width = 1;

FlashHelper.isFlashInstalled = function() {
    var ret;
    
    if (typeof(this.isFlashInstalledMemo) != "undefined") { return this.isFlashInstalledMemo; }
    
    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;
}

FlashHelper.getFlash = function() {
    return $("storage");
}

FlashHelper.checkFlash = function() {
    // confirm that the Flash Storage is running
    
    try {
        return (this.getFlash().ping() == "pong");
    }
    catch (e) { return false; }
}

FlashHelper.writeFlash = function() { 
    var swfName = "/cimh/cimh-page-header/Flash4AJAX.swf";
    var flashContainer = document.getElementById("Flash4AJAX");
           
    if (window.ActiveXObject && FlashHelper.isFlashInstalled())
    {
        // browser supports ActiveX
        // Create object element with 
        // download URL for IE OCX
        var obj_HTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + 
	        ' codebase="http://download.macromedia.com' + 
	        '/pub/shockwave/cabs/flash/swflash.cab#version=8,5,0,0"' + 
	        ' height="' + this.height + '" width="' + this.width + '" id="storage">' + 
	        ' <param name="movie" value="' + swfName + '">' + 
	        ' <param name="quality" value="high">' + 
	        ' <param name="swliveconnect" value="true">' + 
	        '<\/object>';
	    flashContainer.innerHTML = obj_HTML;
		//document.write(obj_HTML);
    }
    else
    {
        // browser supports Netscape Plugin API

        var obj_HTML = '<object id="storage" data="' + swfName + '"' +
	        ' type="application/x-shockwave-flash"' +
	        ' height="' + this.height + '" width="' + this.width + '">' +
	        '<param name="movie" value="' + swfName + '">' +
	        '<param name="quality" value="high">' +
	        '<param name="swliveconnect" value="true">' +
	        '<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer">' +
	        '<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer">' +
	        '<p>You need Flash for this.' +  
	        ' Get the latest version from' +
	        ' <a href="http://www.macromedia.com/software/flashplayer/">here<\/a>.' +
	        '<\/p>' +
	        '<\/object>';
   	    flashContainer.innerHTML = obj_HTML;
    }
}


FlashHelper.addLoadEvent = function(func) {
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


FlashHelper.load = function() {
    if (typeof(FlashHelper.onload) != "function") { return; } 

    if (FlashHelper.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; }
    }
    
    var fs = FlashHelper.getFlash();
    
    if ((!FlashHelper.isFlashInstalled() || this.flashLoaded) && fs) {
        if (FlashHelper.checkFlash()) {
            callAppOnLoad(fs);
        } else {
            callAppOnLoad(null);
        }
    } else {
        callAppOnLoad(null);
    }
    
    function callAppOnLoad(fs) {
        if (FlashHelper.onloadCalled) { return; }
        FlashHelper.onloadCalled = true;
        FlashHelper.onload(fs);
    }
}

function storageOnLoad() { 
    FlashHelper.flashLoaded = true;
    FlashHelper.load();
}

function storageOnError() {
    FlashHelper.flashLoaded = true;
    FlashHelper.load();
}

FlashHelper.init = function() {
    this.flashLoaded = false;
    this.documentLoaded = false;

    // attach to the window.onload event
    this.addLoadEvent(onload);
  
    function onload() {
        if (FlashHelper.isFlashInstalled()) {
            // todo: set a timer
            //setTimeout(storageOnError, 60000);
        }

        FlashHelper.documentLoaded = true;
        FlashHelper.load();
    }
}

FlashHelper.init();


/**************************************** CallbackManager *************************************************/

var CallbackManager = new Object();
CallbackManager.callbacks = new Array();

// assigns and returns a unique callback name for the input callback
CallbackManager.registerCallback = function(callback) {
    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;
    } 
}

/**************************************** FlashXmlHttpRequest ***********************************************/

var FlashXMLHttpRequest = function() {
    var self = this;
    var _method, _url, _contentType = null;
    var _headers = new Array();
        
    this.open = function(method, url, async, user, password) { 
        _method = method;
        _url = url;
    }
    this.send = function(body) {
        var fs = FlashHelper.getFlash();
        
        function callback(varName) {
            var response = FlashHelper.getFlash().GetVariable(varName);
            self.responseText = response;
            
            if (self.onload) {
                self.onload();
            }
        }

        fs.XmlHttp(_url, CallbackManager.registerCallback(callback), _method, body, _contentType, _headers);
    }
    
    this.setRequestHeader = function(header, value) {
        if (header.toLowerCase() == "Content-Type".toLowerCase()) {
            _contentType = value;
            return;
        }
        
        _headers.push(header);
        _headers.push(value);
    }
    
    this.getRequestHeader = function() {
    }
    this.getResponseHeader = function(a) { alert("not supported"); }
    this.getAllResponseHeaders = function() { alert("not supported"); }
    this.abort = function() { alert("not supported"); }
    this.addEventListener = function(a, b, c) { alert("not supported"); }
    this.dispatchEvent = function(e) { alert("not supported"); }
    this.openRequest = function(a, b, c, d, e) { this.open(a, b, c, d, e); }
    this.overrideMimeType = function(e) { alert("not supported"); }
    this.removeEventListener = function(a, b, c) { alert("not supported"); }
    
}

/**************************************** Page Functions *********************************************/

FlashHelper.onload = startApp;

function startApp(fs) {
    if (!fs) { alert("Flash not loaded"); return; }
    fs.Debug();
    var cimhPath = "http://www.cimh.org/cimh-page-header/";
    var headerLinksHTML = "cimh-header-links.asp";
    var menuLinksHTML = "cimh-header-menu.asp";
    // get header links and display
    getContent(cimhPath+headerLinksHTML,"updateHeaderLinks")
    // get menu links and display
    getContent(cimhPath+menuLinksHTML,"updateMenuLinks")
    
    // add Google Analytics to page at bottom
    var gaScript = document.createElement('script');
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	gaScript.setAttribute('type','text/javascript');
	gaScript.setAttribute('src',gaJsHost+'google-analytics.com/ga.js');
	document.getElementsByTagName('body')[0].appendChild(gaScript);

    var gaTracking = document.createElement('script');
	gaTracking.setAttribute('type','text/javascript');
	gaTracking.setAttribute('src','http://www.cimh.org/cimh-page-header/gaTracking.js');
	document.getElementsByTagName('body')[0].appendChild(gaTracking);
}

function getContent(url, functionOut){
	var strAlpha = "abcdefghijklmnopqrstuvwxyz";
	var numStrLen = 6;
	var strRandom;
	for (x=0;x<numStrLen;x++){
		strRandom = strRandom+strAlpha.charAt(Math.floor(Math.random()*strAlpha.length));
	}
	var method="get";
	var body=""; // not sure what this does
	var contentType="text/html";
    var fs = FlashHelper.getFlash();
    fs.XmlHttp(url+"?"+strRandom, functionOut, method, body, contentType);
}

function updateHeaderLinks(){
    var response = FlashHelper.getFlash().GetVariable("retText");
    $("cimh_header_links").innerHTML = response;
}
function updateMenuLinks(){
    var response = FlashHelper.getFlash().GetVariable("retText");
    $("cimh_menu_wrapper").innerHTML = response;
}
function insert_cimh_header(){
	var cimh_header = document.getElementById("cimh_header");
	cimh_header.innerHTML = page_HTML;
	FlashHelper.writeFlash();
}
sfHover = function() {
var sfEls = document.getElementById("cimh_menu").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
	sfEls[i].onmouseover=function() {
		this.className+=" sfhover";
		}
	sfEls[i].onmouseout=function() {
		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

/**************************************** Page Setup ***********************************************/

var page_HTML = '<div id="cimh_header_wrapper">' +
				'<a onclick="pageTracker._link(this.href); return false;" href="http://www.cimh.org/Home.aspx" id="cimh_homeLink"><img border="0" src="http://www.cimh.org/Portals/0/Skins/cimh02/images/_spacer.gif" width="125" height="132" /></a>' +
				'<div id="cimh_header_links"></div>' +
			'</div>' +
			'<div id="cimh_search">' +
				'<form onSubmit="javascript:pageTracker._linkByPost(this)" action="http://www.cimh.org/SearchResults/tabid/37/Default.aspx" method="get">' +
				'<input type="text" maxlength="200" name="xsq" class="NormalTextBox" /> ' +
				'<input type="image" src="http://www.cimh.org/DesktopModules/XSSearchInput/images/search_go.gif" alt="Search" align="absmiddle" style="border-width:0px;" />' +
				'</form>' +
			'</div>'+
			'<div id="cimh_menu_wrapper" onmouseover="if (window.attachEvent){sfHover();}">' +
				'<ul id="cimh_menu">' +
				'</ul>' +
			'</div>' +
			'<div id="Flash4AJAX"></div>';