﻿//************************************************************************************* 
// File     :   dlme_product_gallery v1.1
// Requires :   mf_domLibrary_0.1.js, prototype.js (1.6 or higher), scriptaculous.js (1.8.1 or higher), lightbox.js(2.04 or higher)
// Author   :   Kyle Weems (ksw)
// Origin   :   mindfly.com
// Created  :   June 4, 2008
// Modified :   August 18, 2008
// Purpose  :   Provides image-switching functionality to the Dream List Media product galleries.
//*************************************************************************************

// Version History
// 0.0 - 06/04/2008(KSW) - Script creation started.
// 1.0 - 06/05/2008(KSW) - Initial script finished with functionality listed in Purpose above.
// 1.1 - 08/18/2008(KSW) - Modified script so that clicking on a small image in a gallery results
//                         In a Lightbox popup, instead of swapping with the large image.
var disableCart = 1;

// Initialize the Dream List Media Product Galleries
function loadGallery()
{
    var i, j;
    // get a list of all gallery 'main' images (where the small images appear when clicked)
    var galleryImage = $$('.imgProductGallery');
    // If there are any, then...
    if(galleryImage.length > 0)
    {
        // Loop through all such images.
        for(i=0;i<galleryImage.length;i++)
        {
            // Add a specific class name so we can reference them later.
            galleryImage[i].addClassName('ipg_'+i);
        }
    }
    // Get a list of all ul galleries
    var gallery = $$('.ulProductGallery');
    // If there are any, then...
    if(gallery.length > 0)
    {
        // Loop through all the galleries.
        for(i=0;i<gallery.length;i++)
        {
            // Add a specific class name to each ul so we can reference them later.
            gallery[i].addClassName('upg_' + i);
            // Get a list of all images in the list.
            var galleryLink = $$('.upg_' + i + ' li img');
            // If there are any such images, then...
            if(galleryLink.length > 0)
            {
                // Set the main image associated with this gallery's src and alt attributes
                // to match the first image found in this list.
                $$('.ipg_'+i)[0].src = galleryLink[0].src;
                $$('.ipg_'+i)[0].alt = galleryLink[0].alt;
                // Loop through all the images.
                for(j=0;j<galleryLink.length;j++)
                {
                    var thisImage = $$('.upg_' + i + ' li img')[j];
                    thisImage.id = 'lpg_' + i + '_' + j;
                    if (thisImage.src.indexOf(".jpg") != -1) {
                        var thisAnchor = new Element('a', { href: thisImage.src });
                        thisImage.insert({ after: thisAnchor });
                        thisAnchor.insert({ bottom: thisImage });
                        thisAnchor.writeAttribute("rel", 'lightbox[lpg_' + i + ']');
                    }
                } //end for j
            }
        } // end for i
    } // end if
    
    var cartButton = $$('.addToCart');
    var productGallery = $$('.productGallery');
    for(i=0;i<cartButton.length;i++)
    {
        productGallery[i].insert({bottom: cartButton[i]});
    }
    
    var cartItem = $$('table#tblYourCart td.tdName');
    if(cartItem.length > 0)
    {
        for(i=0;i<cartItem.length;i++)
        {
            var itemName = cartItem[i].innerHTML;
            var itemNameBits = itemName.split("&nbsp;");
            itemName = ""
            for(j=0;j<itemNameBits.length;j++)
            {
                itemName = itemName + itemNameBits[j];
            }
            if($$('span.'+itemName).length > 0)
            {
                $$('span.'+itemName)[0].removeClassName('displayNone');
                $$('span.'+itemName)[0].addClassName('displayBlock');
            }
        }
    }
    //disableTheCart();
} // end of function loadGallery()


function disableTheCart()
{
    if(disableCart > 0)
    {
        var cartButton = $$('.addToCart');
        var productGallery = $$('.productGallery');
        if(cartButton.length > 0)
        {
            var i;
            for(i=0;i<cartButton.length;i++)
            {
                var p = Element.extend(document.createElement('p'));
                p.innerHTML = "Available August 2008 <br/>Call to pre-order. <br/>(360)676-6718";
                productGallery[i].appendChild(p);
                cartButton[i].remove();
            }
        
        }
    }
} // end of function disableTheCart()


// When the page is loaded, run the function loadGallery
addLoadEvent(loadGallery);

