$(document).ready( function() {

    /* BOUND FUNCTION: initialisePromotionWidget() by Alex Kearns
     *
     * This funciton is bound to all Welcome widgets. It automatically sets up all the
     * controls, ajax calls, etc for a particular Welcome widget. Welcome widgets
     * should have this function attached and then called to initialise them.
     ************************************************************************************************/

    $("div.product-promotion-widget-holder").each( initialisePromotionWidget = function() {

        var thispromotionwidget=this;
        var currentpromotionimage=0;
        var numproducts=$(this).find("div.promotion-gallery-stage div.product-promotion").length;
        var swappingstopped=false;
        var currentlyswapping=false;
        var loadnewproduct;
        var parentWidget=$(this).parent().parent();

        // Initialisation

        $(thispromotionwidget).find("div.promotion-gallery-stage div.product-promotion").each( function() {
            $(this).find("div.hold-graphic").css({background: "url(/assets/ui/product-selector/"+$(this).find("span.hold-graphic").text()+")"});
            $(this).find("h4").css({color: "#"+$(this).find("span.headline-colour").text()});
        });


        $(this).find("p.current-product-number").text("1 of "+numproducts);

        $(this).find("span.minimise-function").click( function() {
            swappingstopped=true;
        });

        $(this).find("span.unminimise-function").click( function() {
            swappingstopped=false;
        });

        $(this).find("span.drag-function").click( function() {
            swappingstopped=true;
        });

        $(this).find("span.drop-function").click( function() {
            swappingstopped=false;
        });


        $(this).find("div.main-promotion-product").click( function() {
            var producturl=$(thispromotionwidget).find("div.product-promotion:eq("+currentpromotionimage+") span.product-link").text();
            window.open(producturl);
        });

        $(this).find("a.more").click( function() {
            window.open("/products/");
            return false;
        });

        setTimeout( loadnewproduct=function() {
            if(swappingstopped || currentlyswapping) {
                setTimeout( function() {
                    loadnewproduct();
                }, 5000);
                return false;
            }
            currentlyswapping=true;
            currentpromotionimage++;

            if (currentpromotionimage == numproducts) {
                currentpromotionimage=0;
            }
            $(thispromotionwidget).find("p.current-product-number").text((currentpromotionimage+1)+" of "+numproducts);
            var gallerywidth=$(thispromotionwidget).find("div.main-promotion-product").width();
            var galleryheight=$(thispromotionwidget).find("div.main-promotion-product").height();
            $(thispromotionwidget).find("div.promotion-gallery-stage").css({ width:gallerywidth});
            $(thispromotionwidget).find("div.promotion-gallery-stage").css({top:(-galleryheight*currentpromotionimage)});
            $(thispromotionwidget).find("div.main-promotion-product").animate({opacity: 0}, 500, function() {
                var newpromotionimage=$(thispromotionwidget).find("div.promotion-gallery-stage img:eq("+currentpromotionimage+")").attr("src");
                var oldpromotionimage=$(this).find("img.promotion-gallery-main-image").attr("src");
                if (newpromotionimage != oldpromotionimage) {
                    $(this).find("img.promotion-gallery-main-image").attr("src", newpromotionimage);
                }
                $(this).find("h5").text($(thispromotionwidget).find("div.promotion-gallery-stage h5:eq("+currentpromotionimage+")").text());
                $(this).find("h4").text($(thispromotionwidget).find("div.promotion-gallery-stage h4:eq("+currentpromotionimage+")").text()).css({color: "#"+$(thispromotionwidget).find("div.promotion-gallery-stage span.headline-colour:eq("+currentpromotionimage+")").text()});
                $(this).find("h6").text($(thispromotionwidget).find("div.promotion-gallery-stage h6:eq("+currentpromotionimage+")").text());
                $(this).find("p").text($(thispromotionwidget).find("div.promotion-gallery-stage p:eq("+currentpromotionimage+")").text());
                $(this).find("div.hold-graphic").css({background: "url(/assets/ui/product-selector/"+$(thispromotionwidget).find("div.promotion-gallery-stage span.hold-graphic:eq("+currentpromotionimage+")").text()+")"});
                var mainpromotionproduct=this;
                setTimeout(function() {
                    $(mainpromotionproduct).css({opacity: 1});
                }, 150);
                currentlyswapping=false;

                // Make sure future images are loaded
                if ((currentpromotionimage+1) <numproducts) {
                    var newimagesrc=$(thispromotionwidget).find("div.promotion-gallery-stage span.image-src:eq("+(currentpromotionimage+1)+")").text();
                    $(thispromotionwidget).find("div.promotion-gallery-stage img:eq("+(currentpromotionimage+1)+")").attr("src", newimagesrc);
                }

                setTimeout( function() {
                    loadnewproduct();
                }, 5000);
            });
        }, 10000);

    });
    
});