﻿function prepareGallery()
{
    //Check we can get to the "vehicle_viewer" div via the dom.
    if( !document.getElementsByTagName) return false;
    if( !document.getElementById) return false;
    if( !document.getElementById( "vehicle_viewer")) return false;
    var gallery = document.getElementById( "vehicle_viewer");
    //Now we have the vehicle viewer get all links inside it.
    var links = gallery.getElementsByTagName("a");
    
    //Loop through the links and assign the showPic function to all links accept the large image.
    //This image opens the pop-up so we treat it differently.
    /*
    for( var i = 0; i < links.length; i++)
    {
        if(links[i].getAttribute("id") != "largeImageUrl")
        {
            links[i].onclick = function()
            {
                return showPic(this);
            }
        }
    }
    */
    
}

function showPic(whichPic, medImage)
{
    //Check we have a large image before we try and set it with our new value.
    if(!document.getElementById("largeImage")) return true;
    if(!document.getElementById("largeImageUrl")) return true;
    //Get the src for our large image, this is in the href tag.
    //When JS is disabled then this opens when the image is clicked.
    var lgeImage = whichPic.getAttribute("href");
    //Go and get the dom elements for the medium image and it's container link.
    var placeholderUrl = document.getElementById("largeImageUrl");
    var placeholder = document.getElementById("largeImage");
    //Check the placeholder is an img before we try and replace it.
    if( placeholder.nodeName != "IMG") return true;
    placeholderUrl.onclick = function()
    {
        return showPicWindow(lgeImage);
    }
    placeholderUrl.setAttribute("href", lgeImage);
    placeholder.setAttribute("src", medImage);
    //Now go and set our active id for css purposes.
    var gallery = document.getElementById("vehicle_viewer");
    var links = gallery.getElementsByTagName("a");
    
    for(var i = 0; i < links.length; ++i)
    {
        if(links[i].getAttribute("id") != "largeImageUrl")
        {
            links[i].setAttribute("id", "");
        }
    }
    whichPic.setAttribute("id", "active");
    
    return false;
}

function showPicWindow(img)
{
    window.open(img,'image','width=800,height=600,toolbar=no,location=no,resize=no,scrollbars=no,status=yes');
    return false;
}
