﻿// show starting image
show_image = new Image();
show_image.src = "images/inspiration_gallery/" + image_list[current_image];
document.getElementById("gallery_image").src = show_image.src;

getSavedImages();

function goBack()
{
    show_image = new Image();
    
    if (current_image <= 0)
    {
        show_image.src = "images/inspiration_gallery/" + image_list[image_list.length - 1];
        current_image = image_list.length - 1;
    }
    else
    {
        show_image.src = "images/inspiration_gallery/" + image_list[current_image - 1];
        current_image--;
    }
    
    document.getElementById("gallery_image").src = show_image.src;
}

function goNext()
{
    show_image = new Image();

    if (current_image < image_list.length - 1)
    {
        show_image.src = "images/inspiration_gallery/" + image_list[current_image+1];
        current_image++;
    }
    else
    {
        show_image.src = "images/inspiration_gallery/" + image_list[0];
        current_image = 0;
    }
    
    document.getElementById("gallery_image").src = show_image.src;
}

function getSavedImages()
{
    sendRequest("consumer_inspiration_gallery_save.aspx" + "?z_cachebuster=" + cacheBuster(), showSavedList);
}

function saveImage()
{
    url = "consumer_inspiration_gallery_save.aspx?action=save&id=" + current_image + "&z_cachebuster=" + cacheBuster();
    sendRequest(url, showSavedList);   
}

function removeImage(image_id)
{
    url = "consumer_inspiration_gallery_save.aspx?action=delete&id=" + image_id + "&z_cachebuster=" + cacheBuster();
    sendRequest(url, showSavedList);
}

function emailImage()
{

}

function cacheBuster()
{
    return (new Date).valueOf();
}

function sendRequest(url, callback)
{
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url(url);
    wRequest.set_httpVerb("GET");
    wRequest.add_completed(callback);
    wRequest.invoke();    
}

function showSavedList(executor, eventArgs)
{
    label_control = document.getElementById("div_mySavedImages");
    doc = executor.get_xml();

    results = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">" + 
        "<tr><td><img src=\"images/buckets/bucket_grey_top.png\" border=\"0\" /></td>" + 
        "</tr><tr><td style=\"background-image: url(images/buckets/bucket_grey_bg.png); background-repeat: repeat-x; background-position: center center; padding-left: 8px;\" align=\"left\">" +
        "<div style=\"padding-bottom: 8px; color: #6e6e70;\"><strong>MY SAVED IMAGES</strong></div>" +
        "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
        
    if (doc.documentElement.childNodes.length == 0)
    {
        results += "<tr><td style=\"padding-left: 5px; padding-top: 30px; padding-bottom: 45px; color: #FF0000;\">You have no saved images</td></tr>";
    }
    else
    {
        results += "<tr><td style=\"padding-bottom: 20px; \"><ul style=\"padding-left: 0px; margin-left: 15px;\">";
        
        for (i = 0; i < doc.documentElement.childNodes.length; i++)
        {
            results += "<li><a href=\"?id=" + doc.documentElement.childNodes[i].firstChild.nodeValue + "\">Saved Image #" + (i+1) + "</a> " + 
                "(<a href=\"javascript:removeImage('" + doc.documentElement.childNodes[i].firstChild.nodeValue + "');\">Remove</a>)</li>";
        }

        results += "</ul></td></tr>";
    }
        
    results += "</table>" +
        "</td></tr><tr><td><img src=\"images/buckets/bucket_grey_bottom.png\" border=\"0\" /></td></tr></table>";

    label_control.innerHTML = results;
}
