﻿$(document).ready(function() {
    hideLPGoGreenErrorPanel();
    setImageButton();
    setImageSwitcher();
});
/* Purpose: hide Go Green Landing Page error cells after they are clicked  */
function hideLPGoGreenErrorPanel() {
    $(".lpGoGreenErrors").click(
        function() {
            this.style.display = "none";
        }
    );
}
function newGallery(settings) {
    var i;
    var newGallery = {
        baseDir: "",
        images: [],
        randomImages: [],
		index: 0,
        preloadImage: function(imgData) {
            preloadImg = document.createElement("img");
            preloadImg.src = this.baseDir + imgData.filename;
            imgData.img.parentNode.insertBefore(preloadImg, imgData.img);
            preloadImg.style.display = "none";
        },
        preloadImages: function(img) {
            var preloadImg;
            var i;
            for(i = 0; i < this.randomImages.length; i++) {
                this.preloadImage({
                    img: img,
                    filename: this.randomImages[i]
                });
            }
            for(i = 0; i < this.images.length; i++) {
                if(typeof(this.images[i].filename) != "undefined") {
                    this.preloadImage({
                        img: img,
                        filename: this.images[i].filename
                    });
                }
            }
        },
        getRandomImage: function() {
            return this.randomImages[(Math.floor(Math.random() * this.randomImages.length))];
        },        
        getNextImage: function() {
            this.incrementIndex();
			return this.baseDir + (this.images[this.index].filename || this.getRandomImage());
        },
        incrementIndex: function() {
            this.index++;
            if(this.index >= this.images.length) {
                this.index = 0;
            }
        },
        setRandomIndex: function() {
            this.index = Math.floor(Math.random() * this.images.length);
        },
        nextSlide: function(img) {
            var $tmpImage;
            var thisObj = this;

            $tmpImage = $(img).clone();
            $tmpImage.appendTo("#homePageFlash");
            img.src = this.getNextImage();
            if(typeof(thisObj.images[thisObj.index].url) == "undefined") {
                $(img).removeClass("imageSwitcherWithURL");
                $(img).unbind("click");
            } else {
                $(img).addClass("imageSwitcherWithURL");
                $(img).bind("click", function() {
                    window.location = thisObj.images[thisObj.index].url;
                });
            }
            $tmpImage.fadeOut("slow", function() {
                $tmpImage.remove();
            });
            setTimeout(function() {
                thisObj.nextSlide(img);
            }, (thisObj.images[thisObj.index].duration || thisObject.duration) * 1000);    
        },
        duration: 5
    };
    for(i in newGallery) {
        if(typeof(settings[i]) != "undefined") {
            newGallery[i] = settings[i];
        }
    }
    
    return newGallery;
}

function setImageSwitcher() {
    $(window).load(function() {
        $(".imageSwitcher").each(function() {
            var gallery = galleries[this.id];
            var img = this;
            gallery.preloadImages(img);
            gallery.setRandomIndex();
            gallery.nextSlide(img);
        });
        
        
        
    });
}
function setImageButton() {
    // Pre: Class name is imgButton.  Source pair of images where the rest image contains the string "-rest".  And
    // the hover image is identical, except that "-rest" is replaced with "-hover".  ex. (image1-rest.png, image1-hover.png)
    // Post: Hover images are preloaded
    // Purpose: Performs image preload on all images badged with the class imgButton for their associated hover img.
    // This will change any instance of "-rest" to "-hover" within the source of the preloading image.  So, path and filename
    // needs to be named accordingly.  This will try to act on anything with the imgButton class.  Only img tags are
    // supported.
    var preloadImg;
    $(".imgButton").each(function() {
        preloadImg = this.cloneNode(true);
        preloadImg.src = preloadImg.src.replace("-rest", "-hover");
        this.parentNode.insertBefore(preloadImg, this);
        preloadImg.style.display = "none";
    });
    // Pre: Class name is imgButton.  Source pair of images where the rest image contains the string "-rest".  And
    // the hover image is identical, except that "-rest" is replaced with "-hover".  ex. (image1-rest.png, image1-hover.png)
    // Post: Rest and hover images are swapped appropriately
    // Purpose: Performs hover image swap on all images badged with the class imgButton
    // This will change any instance of "-rest" or "-hover" within the source.  So, path and filename needs to
    // be named accordingly.  This will try to act on anything with the imgButton class.  Only img tags are supported.
    $(".imgButton").hover(
        function() {
            this.src = this.src.replace("-rest", "-hover");
        },
        function() {
            this.src = this.src.replace("-hover", "-rest");
        }
    );
        $(".imgButton").click(function(e) {
            this.src = this.src.replace("-hover", "-rest");
            switch (this.id) {
                case "newsletterFormButton": {
					openNewsletterForm(e);
                }
                case "landingFormSubmit": {
					submitLandingForm(e);
                } break;
            }
        });
}


