function openImage(linkElement, width, height, windowTitle) {
    var imgSrc = linkElement.getAttribute("href");
    var newWindow = window.open("", "productImage", noFrameOptions(width, height));
    newWindow.document.write("<html><head><title>" + windowTitle + "</title></head><body><img src='" + imgSrc + "'/></body></html>");
    newWindow.focus();
}

var offers = new Array();
function openOffer(linkElement, index) {
    var url = linkElement.getAttribute("href");
    offers[index] = openCascade(offers[index], url, index);
}

var reviews = new Array();
function showReview(linkElement, index) {
    var url = linkElement.getAttribute("href");
    reviews[index] = openCascade(reviews[index], url, index);
}

// private
function openCascade(w, url, index) {
    return refocusOrOpenWindow(w, url, makeOptions(index));
}

// private
function refocusOrOpenWindow(w, url, options) {
    var exists = false;

    if (w != null) {
        if (!w.closed) {
            exists = true;
        }
    }

    if (exists) {
        w.focus();
    } else {
        w = window.open(url, '', options);
    }

    return w;
}

// private
function makeOptions(count) {
    var width = 475;
    var height = 390;
    var xOffset = 24;
    var yOffset = 24;
    var x = 210;
    var y = 5;

    if (screen.width > 1024) {
        width = 800;
        height = 600;
        x = 240;
    } else if (screen.width > 800) {
        width = 620;
        height = 500;
        x = 240;
    }

    return 'toolbar=yes,scrollbars=yes,location=yes,status=yes,menubar=yes,resizable=yes,left='
        + (x + (xOffset * count)) + ',top=' + y + ',width=' + width + ',height=' + height;
}

// private
function noFrameOptions(width, height, x, y) {
    var options = 'scrollbars=yes,resizable=yes';
    if (width != null) {
        options += ',width=' + width;
    }
    if (height != null) {
        options += ',height=' + height;
    }
    if (x != null) {
        options += ',left=' + x;
    }
    if (y != null) {
        options += ',top=' + y;
    }
    return options;
}

// private
// Adjusts the size of a window to make sure none of it goes off the edge of the screen.
// Takes a pointer to the window and the original desired window width and height
function adjustSizeForVisibility(w, width, height) {
    var newWidth = width;
    if (getXOffset(w) + width > screen.availWidth) {
        newWidth = screen.availWidth - getXOffset(w);
    }

    var newHeight = height;
    if (getYOffset(w) + height > screen.availHeight) {
        newHeight = screen.availHeight - getYOffset(w);
    }

    if (newWidth != width || newHeight != height) {
        w.resizeTo(newWidth, newHeight);
    }
}

// private
// returns the number of pixels away from the left side of the screen (if the browser supports it)
function getXOffset(w) {
    if (w.screenLeft) {
        return w.screenLeft;
    } else if (w.screenX) {
        return w.screenX;
    } else {
        return 0;
    }
}

// private
// returns the number of pixels away from the top of the screen (if the browser supports it)
function getYOffset(w) {
    if (w.screenTop) {
        return w.screenTop;
    } else if (w.screenY) {
        return w.screenY;
    } else {
        return 0;
    }
}

// get cookie value
/**
* Gets the value of the specified cookie.
*
* name  Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
*   or null if cookie does not exist.
*/
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
