﻿// -------------------------------------------------------------------------------------------------------------
// GLOBAL.JS : contains global javascript functions for McMillan Binch Site
// -------------------------------------------------------------------------------------------------------------

// ROLLOVER CODE
function image_on(pimage) {
    pimage.before = pimage.src; // remember what the original image was
    pimage.src = eval(pimage.name + "_on.src");
}

function image_off(pimage) {
    pimage.src = pimage.before; // switch it back
}


// MISC
function popup_close(pURL) {
    if (pURL != "") {
        if (window.opener) { window.opener.location = pURL; }
    }
    window.close();
}

// GOES BACK IN HISTORY
function goBack() {
    /*
    if(history.length > 2)
    {
    if(history.previous == history.current)
    {
    history.go(-2)
    }
    else
    {
    history.go(-1);
    }
    }
    else
    {
    history.go(-1);
    }
    */
    history.back();
}

// POPUP CODE
var popups = new Array;

function popup(pname, pURL, pwidth, pheight, pfeature, pscreenx, pscreeny) {
    //Pops up a window. If window is already popped up, it just sets focus to it.
    // 1. pname 	-> giving 2 popups the same name (including "") causes them to overwrite each other.
    //					-> giving 2 popups different names will cause 2 different popups
    // 2. pURL		-> URL you want to popup, relative URLS work.
    // 3. pwidth 4. pheight	-> Width of popup. Height of popup. Passing in 0, 0 causes a full-screen popup
    // 5. pfeature -> feature to override default feature.
    // 6. pscreenx 7. pscreeny -> Position of popup. If not passed in, centers the popup.
    // returns     -> pointer to window that was opened
    // Usage: openWin("jimdaily", "http://www.dailyradar.com", 400, 300, "scrollbars=yes", 100, 200)

    var width = 0, height = 0;
    var deltawidth = 0, deltaheight = 0;
    var screenx = 0, screeny = 0;
    var i = 0, popupindex = -1, newwindow = null;

    var is_ie = ((navigator.appVersion.toLowerCase().indexOf('msie')) != -1);
    var is_mac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);
    var version = parseFloat(navigator.appVersion);
    var is_ie4 = ((is_ie) && (version == 4) && (navigator.userAgent.toLowerCase().indexOf("msie 4") != -1));

    var feature = "scrollbars=no,menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no";

    // search through the list of open popups to see if it's already open
    for (i = 0; i < popups.length; i++) {
        if ((popups[i].window != null) && (!(popups[i].window.closed))) {
            if (popups[i].name == pname) {
                // find current position of popup (in case we need to close and repopen popup)
                if (popups[i].url.substring(0, 7) != "http://") {
                    // if we're on the same domain, re-use the CURRENT position of the pop-up window (not the saved position)
                    // since user may have moved popup
                    screenx = (popups[i].window.screenX) ? popups[i].window.screenX : popups[i].window.screenLeft;
                    screeny = (popups[i].window.screenY) ? popups[i].window.screenY : popups[i].window.screenTop - 22;
                } else {
                    // if we're in a different domain (indicated by the URL having an absolute as opposed to relative address),
                    // can't even look at .screenX or .screenLeft, so default to memorized screenX and screenY
                    screenx = popups[i].screenx;
                    screeny = popups[i].screeny;
                }
                if ((popups[i].width != pwidth) || (popups[i].height != pheight)) {
                    // window exists, but it's a different width and height so can't resuse it.
                    popups[i].window.close();
                } else {
                    popupindex = i; // re-use the window
                }
            }
            if (screenx == 0) { screenx = popups[i].screenx + 25; screeny = popups[i].screeny + 25; } // cascade multiple open popups
        }
    }

    // in IE4, it's a security violation to touch a popup after it's been open (and points to a URL on a different site)
    // if all popups are on the same site as the page that called them, this code can be removed.
    if ((popupindex != -1) && ((pURL.substring(0, 7) == "http://") && (is_ie4))) {
        popups[popupindex].window.close(); popupindex = -1;
    }

    if (popupindex == -1) {
        // the requested window is NOT already open. open it, and add it to the list of popups.
        popupindex = popups.length;

        // configure the feature of the window (including scrollbars)
        if (pfeature) {
            if (typeof (pfeature) != "string") { alert("BUG:Popup.js was passed an invalid feature. It MUST be a string."); return false; }
            if (pfeature.toString() != "") { feature = pfeature.toString().replace(" ", ""); }
        }

        // tweak the passed in width & height depending on requested features - this is far from perfect
        deltawidth = 10;
        deltawidth += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;

        deltaheight = 29;
        deltaheight += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;
        deltaheight += (((feature.search("menubar=yes")) != -1) && (is_ie)) ? 48 : 0;
        deltaheight += (((feature.search("status=yes")) != -1) && (is_ie)) ? 20 : 0;

        // build the feature list with proper screenx and width
        if ((pwidth == 0) && (pheight == 0)) {
            // full screen popup
            width = screen.availWidth - deltawidth; height = screen.availHeight - deltaheight;
            screenx = 0; screeny = 0;
        } else {
            width = pwidth - deltawidth; height = pheight - deltaheight;
            // center the new window on the screen
            if ((typeof (pscreenx) == "number") && (typeof (pscreeny) == "number") && (pscreenx != 0) && (pscreeny != 0)) {
                screenx = pscreenx; screeny = pscreeny;
            } else if ((screenx == 0) && (screeny == 0)) {
                if ((is_ie) && (is_mac)) { pwidth -= 15; }
                screenx = parseInt((screen.availWidth - (pwidth + deltawidth)) / 2); screeny = parseInt((screen.availHeight - (pheight + deltaheight)) / 2);
            }
            if (screenx < 0) screenx = 0; if (screeny < 0) screeny = 0;
        }
        // if (!is_ie) {screeny -= 30;}
        feature += ",left=" + screenx + ",screenX=" + screenx + ",top=" + screeny + ",screenY=" + screeny + ",width=" + width + ",height=" + height + ";";
        /// tack on seconds to gurantee a new window. new Date().getTime()
        // newwindow = window.open(pURL, pname, feature); good, but it has problems

        newwindow = window.open(pURL, pname, feature, false); // may capture the ghost
        // doesn't work very well with .pdfs - need to close & re-open to remove ghosts
        // newwindow.close();	// kill any ghosts, this captures and kills and old floaters caused by refreshing of page or pdf (not sure how it operates on 56K)
        // newwindow = window.open(pURL, pname, feature); // finally gauranteed to be new

        if (newwindow != null) {
            // doesn't work if we're switching between an .PDF and HTML (because it transforms from a PDF viewer to late so there is no focus event to call)
            if (!((pURL.substring(0, 7) == "http://") && (is_ie4))) { newwindow.focus(); } // security violation in IE4 if URL points to a different site

            // memorize the passed-in parameters for later reference
            popups[popupindex] = new Object();
            popups[popupindex].window = newwindow;
            popups[popupindex].name = pname;
            popups[popupindex].url = pURL;
            popups[popupindex].screenx = screenx; popups[popupindex].screeny = screeny;
            popups[popupindex].width = pwidth; popups[popupindex].height = pheight;
        }

    } else {
        // it's already open, so reuse the popup that's already out there (browser will NOT let us override feature, only URL).
        if (popups[popupindex].url != pURL) {	// are we changing URLs in currently open window?
            popups[popupindex].window.location = pURL;
            popups[popupindex].url = pURL;
        }
        popups[popupindex].window.focus();
    }

    return popups[popupindex].window;
}
;
