var SCROLL_WRAPPER_ID = "#scroll-wrapper";
var SCROLL_BOX_ID = '#scroll-box';

var container;
var content;
var hidden;

var SCROLL_CONTROL_HTML = "<ul id=\"scroll-nav\"><li id=\"scroll-up\">Scroll Up</li><li id=\"scroll-down\" >Scroll Down</li></ul>";

$(document).ready(function() {
    if ($(SCROLL_WRAPPER_ID).length) {
        $(SCROLL_WRAPPER_ID).css("overflow", "hidden");
        $(SCROLL_WRAPPER_ID).wrapInner("<div id=\"scroll-box\"></div>");
        $(SCROLL_WRAPPER_ID).after(SCROLL_CONTROL_HTML);

        setScrollDimensions();

        $('#scroll-up').click(function() { return false; });
        $('#scroll-down').click(function() { return false; });

        $('#scroll-down').hover(function() {
            if (hidden > 0) {
                var current = getTop();
                $(SCROLL_BOX_ID).animate({ top: -hidden - 15 }, Math.abs(current - hidden) * 8);
            }
        }, function() {
            $(SCROLL_BOX_ID).stop();
        });

        $('#scroll-up').hover(function() {
            if (hidden > 0) {
                var current = getTop();
                $(SCROLL_BOX_ID).animate({ top: "0" }, Math.abs(current) * 8);
            }
        }, function() {
            $(SCROLL_BOX_ID).stop();
        });
    }



    if ($('#consumer-rotator').length) {
        $('#wheat-stalk-alt-content').hide();
        window.setTimeout("consumerRotatorSwitch()", 3000);
    }


    $('.clear-on-focus').clearonfocus();

    //consumer gallery
    if ($('#flash-gallery-1').length) {
        var flashvars = {};
        flashvars.xmlPathUrl = "/media/112/consumer-gallery.xml";
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        params.allowScriptAccess = "always";
        var attributes = {};
        swfobject.embedSWF("/media/160/consumergallery.swf", "flash-gallery-content", "513", "449", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("flash-gallery-content", "outline:none");
    }
    if ($('#consumer-bread-navigation').length) {
        var flashvars = {};
        flashvars.xmlPathUrl = "/media/109/consumer-bread-selector.xml" + cacheBuster();
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        params.allowScriptAccess = "always";
        var attributes = {};
        swfobject.embedSWF("/media/157/consumer-bread-selector.swf", "bread-flash-content", "950", "160", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("bread-flash-content", "outline:none");
    }

    //trade gallery
    if ($('#flash-gallery-3').length) {
        var flashvars = {};
        flashvars.xmlPathUrl = "/media/118/trade-gallery.xml";
        var params = {};
        params.menu = "false";
        params.allowScriptAccess = "always";
        var attributes = {};
        swfobject.embedSWF("/media/2345/tradegallery.swf", "flash-gallery-content", "516", "400", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("flash-gallery-content", "outline:none");
    }
    //the factory tour gallery
    if ($('#the-factory-tour-gallery').length) {
        var flashvars = {};
        flashvars.xmlPathUrl = "/media/115/the-factory-tour.xml";
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        params.allowScriptAccess = "always";
        var attributes = {};
        swfobject.embedSWF("/media/169/small-366x306-gallery.swf", "the-factory-tour-gallery-flash-content", "366", "306", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("the-factory-tour-gallery-flash-content", "outline:none");
    }

    //trade products by location
    if ($('#region-product-selector-both').length) {
        var flashvars = {};
        flashvars.xmlPathUrl = "/media/121/trade-region-breads.xml" + cacheBuster();
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        var attributes = {};
        swfobject.embedSWF("/media/175/trade-region-product-selector-000.swf", "product-flash-content", "950", "175", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("product-flash-content", "outline:none");
    }
    //the buzz media player
    if ($('#the-buzz-media-player').length) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        var attributes = {};
        swfobject.embedSWF("/media/172/the-buzz.swf", "the-buzz-flash-content", "950", "451", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("the-buzz-flash-content", "outline:none");
    }
    //every thing amorosos
    if ($('#every-thing-media-player').length) {
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        params.menu = "false";
        var attributes = {};
        swfobject.embedSWF("/media/163/everything-amorosos.swf", "the-buzz-flash-content", "950", "451", "9.0.0", "false", flashvars, params, attributes);
        swfobject.createCSS("the-buzz-flash-content", "outline:none");
    }

    if ($('#recipe-popup').length) {
        $('#recipe-popup').hide();
        $('#recipe-content div').hide();

        $('#r-p-close-button').click(function() {
            $('#recipe-content div').hide();
            $('#recipe-popup').hide();
        });
        $('#recipe-list-box a').click(function() {
            $('#recipe-content div').hide();
            $('#recipe-popup').show();
            $($(this).attr('href')).show();
            return false;
        });
    }

});

function cacheBuster() {
    var junk = Math.floor(Math.random() * 1101);

    return "?r=" + junk;
}

function getAnchor() {
    var anchorValue;
    var url = document.location;
    var strippedUrl = url.toString().split("#");
    if (strippedUrl.length > 1) {
        anchorValue = strippedUrl[1];
        return anchorValue;
    }
}



function getTop() {
    var top = $(SCROLL_BOX_ID).position().top;
    return top;
}

function getHeight(id) {
    var height = $(id).height();
    var paddingTop = trimPx($(id).css("padding-top"));
    var paddingBottom = trimPx($(id).css("padding-bottom"));

    return height + paddingTop + paddingBottom;
}

function trimPx(value) {
    var pos = value.indexOf("px");
    if (pos != 0)
        return parseInt(value.substring(0, pos));
    else
        return 0;
}

function setScrollDimensions() {
    container = getHeight(SCROLL_WRAPPER_ID);
    content = getHeight(SCROLL_BOX_ID);
    hidden = content - container;
}

function resetScroller() {
    setScrollDimensions();
    $(SCROLL_BOX_ID).css('top', 0);
}


function consumerRotatorSwitch() {
    if ($('#flash-wheat-stalks').is(':visible')) {
        $('#flash-wheat-stalks').hide();
        $('#wheat-stalk-alt-content').show();
        window.setTimeout("consumerRotatorSwitch()", 3000);
        return;
    }
    if ($('#wheat-stalk-alt-content').is(':visible')) {
        $('#wheat-stalk-alt-content').hide();
        $('#flash-wheat-stalks').show();
        window.setTimeout("consumerRotatorSwitch()", 3000);
        return;
    }
}



