// JScript File
// a few useful global variables
    var hostHome = "http://rustythejunk.110mb.com/";
    // popupElement is the text box popped up when over a local link
    var popupElement;
    
    // ie is true if we are using internet explorer (oh god)
    var ie = (document.all) ? true : false;
    
    // mouseX and mouseY are the current screen positions of the cursor
    var mouseX = 0;
    var mouseY = 0;
    
    // currentSmallMap is the currently popped up small map
    var currentSmallMap;
    
    // the offset amount to correctly position buttons on maps
    var leftBoundary = 2;
    var topBoundary = 2;

// first mouse position capture
    // this turns on mouse capture
    function mousecapture()
    {
        // If NS -- that is, !IE -- then set up for mouse capture
        if (!IE) document.captureEvents(Event.MOUSEMOVE)

        // Set-up to use getMouseXY function onMouseMove
        document.onmousemove = getMouseXY;
    }


    // Main function to retrieve mouse x-y pos.s
    function getMouseXY(e) 
    {
        // Temporary variables to hold mouse x-y pos.s
        var tempX = 0
        var tempY = 0
        
        if (IE) 
        { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
        }
        else
        {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX
            tempY = e.pageY
        }  
        
        // catch possible negative values in NS4
        if (tempX < 0){tempX = 0}
        if (tempY < 0){tempY = 0} 
         
        // save the position values
        mouseX = tempX
        mouseY = tempY
        return true
    }

// popup a text box for a local link
    // the real version (not ie)
    // popupName:       the story to display
    // smallMapId:      the small map that is currently up (not used any more)
    // evt:             the current mouse event
    function popupText(popupName, smallMapId, evt)
    {
    	var mainBox = document.getElementById("mainContents");
    	var mainLeftOffset = mainBox.offsetLeft;
    	var mainTopOffset = mainBox.offsetTop;
        // get the small map element
        var smallMapElement = document.getElementById(smallMapId);
        // our popup text box goes in the top level contents box element
        var contentsBoxElement = document.getElementById("contentsBox");
        var currentOffsetParent = contentsBoxElement.offsetParent;
        // find out the screen offset to the parent box
        var offsetparentoffsetx = contentsBoxElement.offsetLeft;;
        var offsetparentoffsety = contentsBoxElement.offsetTop;;
        while (currentOffsetParent != null) 
        {
            offsetparentoffsetx = offsetparentoffsetx + currentOffsetParent.offsetLeft;
            offsetparentoffsety = offsetparentoffsety + currentOffsetParent.offsetTop;
            currentOffsetParent = currentOffsetParent.offsetParent;
        }
        // work out where we want to put the box from the offset and the mouse position
        var lefter = evt.pageX - offsetparentoffsetx + mainLeftOffset;
        var topper = evt.pageY - offsetparentoffsety + mainTopOffset;
        // create the popup box
        popupElement = document.createElement('p');
        popupElement.setAttribute("style",
         "position: absolute; left: " + lefter + "px; top: " + topper + "px; z-index: 10; border-top-style: ridge; border-right-style: ridge; border-left-style: ridge; background-color: #ccccff; border-bottom-style: ridge;");
        var aElement = document.createTextNode(popupName);
        popupElement.appendChild(aElement);
        // and add it to the contents box
        contentsBoxElement.appendChild(popupElement);
    }
    
    // the ie version
    function iepopupText()
    {
        // ie pops up the alt text anyway, so we don't need to do anything here
    }
    
    // remove the current popup text box
    function removePopup()
    {
        // if we have one
        if (popupElement) 
        {
            // if we are using ie just hide it
            if (ie) 
            {
                // we didn't pop anything up so leave it alone
            }
            // otherwise really delete it
            else
            {
                popupElement.parentNode.removeChild(popupElement);
            }
            // and show that it has gone
            popupElement = null;
        }
    }
    
    // utility function to divide an int by 2 and get an int
    function intdiv2(x)
    {
        var y = x % 2;
        y = x - y;
        return y/2;
    }
    
    function centreMap(mapId)
    {
     /* get the main map of areas element */
        var mapElement = document.getElementById(mapId);
        
        var lefter = mapElement.ownerDocument.width - mapElement.width;
        lefter = intdiv2(lefter);
        mapElement.style.left = lefter+'px';
    }
    
    // add a new are to the main map
    function addHotArea(imgId, mapId, x, y, size, altName, smallMap, arrow, side)
    {
        var smallMapId = smallMap + "Id";
        var smallMapImgId = smallMapId + "Map";
        var smallMapButton = smallMap + "Button";
        
        var baseImage = document.getElementById(imgId);
    
    /* our structure is
    contents box
        p main map 
            map of areas ----- (mapId is this element)
                area 1 --> submap 1
                area 2 ...
            main map image
        p submap 1
            map of areas
                area 1 --> popup text and link
                area 2 ...
            submap 1 image
        p ...
    */
    /* get the main map of areas element */
        var mapElement = document.getElementById(mapId);
        
    /* create a new button */
        var newButton = document.createElement('img');
        newButton.setAttribute("id", smallMapButton);
        newButton.src = '/Boats/Travels/marker.png';
        newButton.style.position = "absolute";
        newButton.width = 6;
        newButton.height = 6;
        var lefter = x - intdiv2(newButton.width);
        /* offset position by boundary width */
        lefter = lefter + leftBoundary;
        topper = topper + topBoundary;
        var topper = y - intdiv2(newButton.height);
        newButton.style.left = lefter + 'px';;
        newButton.style.top = topper + 'px';
        newButton.style.visibility = "visible";
        newButton.style.zIndex = 3;
        newButton.onmouseover = popupSmallMap;
        mapElement.parentNode.appendChild(newButton);
        
    /* create a new area */
        var newSmallMap = document.getElementById(smallMapImgId);
        lefter = x - intdiv2(newSmallMap.width);
        if (lefter < 0)
        {
            lefter = 0;
        } else if ((lefter + newSmallMap.width) >= baseImage.width)
        {
            lefter = baseImage.width - newSmallMap.width - 1;
        }
        topper = y - intdiv2(newSmallMap.height);
        if (topper < 0)
        {
            topper = 0;
        } else if ((topper + newSmallMap.height) >= baseImage.height)
        {
            topper = baseImage.height - newSmallMap.height - 1;
        }
        newSmallMap.parentNode.style.left = lefter + 'px';
        newSmallMap.parentNode.style.top = topper + 'px';
        if (ie) 
        {
            newSmallMap.onmouseout = iepopunderSmallMap;
        }
        else
        {
            newSmallMap.setAttribute("onmouseout", 'popunderSmallMap("' + smallMapImgId + '", event);');
        }
        
        /* if appropriate place the where are we now arrow here */
        if (arrow) 
        {
            var arrowElement = document.getElementById(arrow);
            if (arrowElement) 
            {
            	if (side == "left")
            	{
                	var arrowLeft = x - arrowElement.width - 5;
                	var arrowTop = y - intdiv2(arrowElement.height);
                }
                if (side == "right")
            	{
                	var arrowLeft = x + 5;
                	var arrowTop = y - intdiv2(arrowElement.height);
                }
                if (side == "top")
            	{
                	var arrowLeft = x - intdiv2(arrowElement.width);
                	var arrowTop = y - arrowElement.height - 5;
                }
                if (side == "bottom")
            	{
                	var arrowLeft = x - intdiv2(arrowElement.width);
                	var arrowTop = y + 5;
                }
                
                arrowElement.style.left = arrowLeft + 'px';
                arrowElement.style.top  = arrowTop + 'px';
                arrowElement.style.visibility = "visible";
            }
        }
    }
    
    // add a local hot spot
    function addSubArea(smallMapId, x, y, size, story, url)
    {
    /* get the map of areas for this submap */
        var areaElement = document.getElementById(smallMapId);
        
        var smallMapArrayId = smallMapId + "Array";
        var smallMapButtonId = smallMapId + x + y;
        
        /* create a new button */
        var newButtonAnchor = document.createElement('a');
        newButtonAnchor.setAttribute("id", smallMapButtonId);
        newButtonAnchor.setAttribute("href", url);
        
        var newButton = document.createElement('img');
        newButton.src = '/Boats/Travels/marker.png';
        newButton.style.position = "absolute";
        newButton.setAttribute("alt", story);
        newButton.width = 6;
        newButton.height = 6;
        var lefter = x - intdiv2(newButton.width);
        var topper = y - intdiv2(newButton.height);
        /* offset position by boundary width */
        lefter = lefter + leftBoundary;
        topper = topper + topBoundary;
        newButton.style.left = lefter + 'px';;
        newButton.style.top = topper + 'px';
        newButton.style.zIndex = 6;
        if (ie)
        {
            newButton.onmouseover = iepopupText;
            newButton.onmouseout = removePopup;
        }
        else
        {
            newButton.setAttribute("onmouseover", 'popupText("' + story + '", "' + smallMapId + 'IdMap", event);');
            newButton.setAttribute("onmouseout", "removePopup();");
        }
        newButtonAnchor.appendChild(newButton);
        areaElement.appendChild(newButtonAnchor);
    }
    
    // pop up the small map associated with this button
    function popupSmallMap()
    {
        // get the button id
        var smallMap = this.id;
        // work out the small map id
        var smallMapImg = smallMap.substr(0, smallMap.length - 6);
        smallMapImg = smallMapImg + "IdMap";
        // get the small map
        var smallMapElement = document.getElementById(smallMapImg);
        // if there is a small map already up lose it
        if (currentSmallMap != null)
        {
            reallyPopunderSmallMap();
        }
        // show the small map
        smallMapElement.parentNode.style.visibility = "visible";
        // and keep track of it
        currentSmallMap = smallMapElement.parentNode;
    }
    
    // remove the small map (non ie version) NB we have to check that the cursor has really left
    function popunderSmallMap(smallMapId, e)
    {
        var smallMapElement = document.getElementById(smallMapId);
        var currentOffsetParent = smallMapElement.offsetParent;
        var offsetparentoffsetx = 0;
        var offsetparentoffsety = 0;
        while (currentOffsetParent != null) 
        {
            offsetparentoffsetx = offsetparentoffsetx + currentOffsetParent.offsetLeft;
            offsetparentoffsety = offsetparentoffsety + currentOffsetParent.offsetTop;
            currentOffsetParent = currentOffsetParent.offsetParent;
        }
        var topLeftX = smallMapElement.offsetLeft + offsetparentoffsetx;
        var topLeftY = smallMapElement.offsetTop + offsetparentoffsety;
        var bottomRightX = topLeftX + smallMapElement.clientWidth;
        var bottomRightY = topLeftY + smallMapElement.clientHeight;
        if ((e.pageX < topLeftX) || (e.pageY < topLeftY) || (e.pageX > bottomRightX) || (e.pageY > bottomRightY))
        {
            reallyPopunderSmallMap();
        }
    }
    
    // remove the small map (ie version)
    function iepopunderSmallMap()
    {
        var e = window.event;
            if ((event.offsetX < 0) || (event.offsetY < 0) || (event.offsetX > this.clientWidth) || (event.offsetY > this.clientHeight))
            {
                reallyPopunderSmallMap();
            }
    }
    
    // really hide the small map
    function reallyPopunderSmallMap()
    {
        if (currentSmallMap != null)
        {
            currentSmallMap.style.visibility = "hidden";
            currentSmallMap = null;
        }
    }
    
    function mouseOverBuildingLink()
    {
        this.className = 'overBuildingLink';
    }
 
    function mouseOutBuildingLink()
    {
        this.className = 'buildingLink';
    }   



    function doCopyrightLine()
    {
    }

    function doHeading(subHead)
    {
    	if (top.titleFrameLoaded != 0)
    	{
    	top.title.changeTitle(subHead);
    	}
    }

	function toggleLinks(linkId)
	{
        var linkElement = document.getElementById(linkId);
        var vis = (linkElement.style.display == 'block');
        if (vis)
        {
        	linkElement.style.display = 'none';
        }
        else
        {
        	linkElement.style.display = 'block';
        }
    }
        	
		    
