function rollover(name,source){
    document.images[name].src=source;
}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=650,height=660,left = 150,top = 0');");
}


function largeImage(source) {
    if (document.images) {
         document["largeimage"].src=source; 
         //return false;
    }
}

/**
 * function full_screen_image
 *
 * This is very simple lightbox. It also creates a transparent overlay.
 * 
 * @param string url -- the url for the image to be displayed
 */
function full_screen_image(url) {
    // create the background overlay and the container
    $("body").append("<div id='popup-overlay'></div>");  
    $("body").append("<div id='popup-container'></div>");
    $("body, #popup-overlay, #popup-container").css("overflow", "hidden"); // disable scrolling
    $("#popup-overlay, #popup-container").css("position", "absolute");
    $("#popup-overlay, #popup-container").css("top", $(this).scrollTop() + "px");         
    $("#popup-overlay, #popup-container").css("left", "0px");
    $("#popup-overlay, #popup-container").css("width", $(window).width() +"px");
    $("#popup-overlay, #popup-container").css("height", $(window).height() +"px");
    $("#popup-overlay").css("background-color", "#000000");
    $("#popup-overlay").css("z-index", "9998"); 
    $("#popup-overlay").css("opacity", ".80");  // overlay transparency
    $("#popup-overlay").css("filter", "Alpha(Opacity=80)"); // overlay transparency ie
    $("#popup-container").css("text-align", "center");
    $("#popup-container").css("padding", "10px");
    $("#popup-container").css("z-index", "9999"); 
    // append the image
    $("#popup-container").append("<img id='popup-image' />");
    $("#popup-image").attr("src", url);
    // calulate the new height
    var img = new Image();
    img.src = url;
    var height = ($(window).height() - 20);
    var width = (img.width / img.height) * height;
    
    $("#popup-image").css("height",  height + "px");
    $("#popup-image").css("width", width + "px");   
    // add the remove handler
    $("#popup-image, #popup-container, #popup-overlay").click(function(e){
        $("#popup-image").remove();
        $("#popup-overlay").remove();
        $("#popup-container").remove();
        $("body, #popup-overlay, #popup-container").css("overflow-y", "auto");        
    });
    // Handle pressing escape
    $(document).keydown(function(e) {
        if(e.which == 27) {  // escape, close box
            $("#popup-image").remove();
            $("#popup-overlay").remove();
            $("#popup-container").remove();
            $("body, #popup-overlay, #popup-container").css("overflow", "auto");
        }
    });
    // add the resize handler in case the screen is resized     
    $(window).resize(function(){
        $("#popup-overlay, #popup-container").css("width", $(window).width() +"px");
        $("#popup-overlay, #popup-container").css("height", $(window).height() +"px");
        $("#popup-overlay, #popup-container").css("top", $(this).scrollTop() + "px");
        $("#popup-image").css("height", ($(window).height() - 20) + "px");
        $("#popup-image").css("width", "auto");           
    });
    $(window).scroll(function() {
        //$("#popup-overlay, #popup-container").css("top", $(this).scrollTop() + "px");
    });    
}

$(document).ready(function(e) {
    /**
     * The following code adds the popup to all elements whose 'name' attribute
     * is equal to 'largeimage'.
     */
    $('[name=largeimage]').click(function(e) {
        full_screen_image($(this).attr('src'));
    });
});