function addEvent (obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        return obj.attachEvent("on" + evType, fn);
    } else {
        return false;
    }
}

function imageSwap(Item,ImgSrc) {
    if (document.images) {
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1]);
        if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
            var imgName = ImgSrc.toUpperCase();
            if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
                var imgTemp = ImgSrc.split("/");
                if (ImgSrc.indexOf("/images/buttons/") > 0) {
                    newSrc = "./images/buttons/" + imgTemp[imgTemp.length-1];
                }
                if (ImgSrc.indexOf("/bookresources/") > 0) {
                    newSrc = "./bookresources/" + imgTemp[imgTemp.length-1];
                }
                if (newSrc != "") {
                    Item.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + newSrc + "', sizingMethod='scale')";
                    Item.src = "./images/1x1.gif";
                }
            }
        } else {
            Item.src = ImgSrc;
        }
    }
}

function correctPNG() {
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
      for(var i=0; i<document.images.length; i++) {
         var img = document.images[i];
         var imgName = img.src.toUpperCase();
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
            var newSrc = "";
            var imgTemp = img.src.split("/");
            if (img.src.indexOf("/images/buttons/") > 0) {
                newSrc = "./images/buttons/" + imgTemp[imgTemp.length-1];
            }
            if (img.src.indexOf("/bookresources/") > 0) {
                newSrc = "./bookresources/" + imgTemp[imgTemp.length-1];
            }
            if (newSrc != "") {
                img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + newSrc + "', sizingMethod='scale')";
                img.src = "./images/1x1.gif";
            }
         }
      }
   }
}

//------------------------------------------------------------------------------------------------

function createAgentBox(agentID, xmlPath) {
    if (DragItem['dragAgent' + agentID]) return;
    var agent = new FlashAgent(agentID, './agents/', './agents/', xmlPath);
    //agent.setOnReady( function() { agent.dragBox.show(); agent.playSequence('01'); });
    agent.autoplay = true;
	agent.ttsPath = './bookresources/';
    agent.setOnPlayStop( function() { agent.dragBox.hide(); } );
    agent.setSize(200,200);
    agent.createDragBox();
    agent.dragBox.clearOnHide = true;
    agent.dragBox.handleScroll = false;
    agent.dragBox.show();
}

function createAndPlay(agentID, xmlPath, preview) {
    //var xmlPath = sequenceFile[seqNum];
    var agBox = FlashApplet.lookupNumber(agentID);
    if (agBox == null) {
        createAgentBox(agentID, xmlPath, preview);
    } else {
        agBox.seqPath = xmlPath;
        agBox.dragBox.show();
        agBox.updateSettings();
    }
}


//------------------------------------------------------------------------------------------------
function scaleImages() {
    // Look for div with class of scale
    var divList = document.getElementsByTagName("DIV");
    for (var i=0; i < divList.length; i++) {
        if (divList[i].className.match(new RegExp("^scale", "i"))) {
            var box = divList[i];
            var imgList = divList[i].getElementsByTagName("IMG");
            if (imgList[0] != null) {
                var img = imgList[0];

                // Get image dimensions
                var imgW = img.getAttribute('width');
                var imgH = img.getAttribute('height');

                // Get box dimensions
                var boxW = box.clientWidth;
                var boxH = box.clientHeight;

                // Find which dimension is scaled the most
                var scaleW = boxW / imgW;
                var scaleH = boxH / imgH;

                // Move image into visible area
                img.style.position = "relative";
                img.style.left = "0";

                // Check to see if any scaling is needed
                if ((imgW < boxW) && (imgH < boxH)) {
                    return;
                }

                // Scale the image
                if (scaleW > scaleH) {
                    var newScale = (imgW/boxW) * scaleH * 100;
                    img.style.width = newScale.toFixed(2) + "%";
                    img.style.height = "auto";
                } else {
                    img.style.width = "100%";
                    img.style.height = "auto";
                }
            }
        }
    }
}

//------------------------------------------------------------------------------------------------

function bottomCoord(objectID) {
    if (!document.getElementById(objectID)) return false;

    var targetNode = document.getElementById(objectID);

    // Top
    tempTop = targetNode.offsetTop;

    // Check to see if offsetParent is the BODY (HTML in IE)
    // otherwise start looping and adding parent's offset until we get there
    var offsetParentCheck = targetNode.offsetParent;
    while ((offsetParentCheck.nodeName != "BODY") && (offsetParentCheck.nodeName != "HTML")) {
        tempTop += offsetParentCheck.offsetTop;
		offsetParentCheck = offsetParentCheck.offsetParent;
	}
    targetTop = tempTop;

    // Bottom
    tempHeight = targetNode.offsetHeight;
    targetBottom = targetTop + tempHeight;

    return targetBottom;
}

function fixPageHeight() {
    var bpBot = bottomCoord("bookpage");

    var txtBot = new Array();
    for(var x=0; x < txtItems.length; x++) {
        txtBot[x] = bottomCoord(txtItems[x]);
    }

    // Find largest item
    var maxVal = Math.max.apply(null, txtBot);

    // If content is longer than alloted 'bookpage', increase 'bookpage' height
    if (maxVal > bpBot) {
        bpHeight = document.getElementById("bookpage").offsetHeight + (maxVal - bpBot);
        document.getElementById("bookpage").style.height = bpHeight + "px";
    }
}