if (!redRooster)
    var redRooster = {};

redRooster.Fonts = function() {

    var rebind = function() {
        Cufon.replace('#navMain li a, #navTop li a, .banner h1, .textContainer h2, .acordeBold,  .nutritionalTable th, .cateTitle h2, .stateListNav, .locationListContainer h2, .txtCategory, .txtProduct, .txtProductSupp, .mapContainer h2, .signUpConfirm p', { fontFamily: 'Acorde-bold' });
        Cufon.replace('h2:not(h2.acordeBold), #secondaryNav a:not(#secondaryNav a.city), .intro, .acorde, .nutritionalTable td, .searchMessage h2, .thankYou', { fontFamily: 'Acorde' });
        Cufon.replace('#navFooter a:not(#navFooter .levelOne a)', { fontFamily: 'Acorde' });
    }

    return { rebind: rebind };
}

redRooster.Global = function () {

    var init = function () {
        jQuery("a[href*='/Menu/']").each(function (i, item) {
            jQuery(item).click(function (e) {
                var $that = jQuery(this);

                if ($that.parents("#foodMenu")) {
                    jQuery("#foodMenu li").removeClass("active");
                    $that.parent().addClass("active");
                }

                GATracking.TrackPageView(this.href);
                e.preventDefault();
                var re = new RegExp(/\/Menu\/([a-zA-Z0-9-]+)/g);
                // add the hash tag in between the menu and product name
                var newUrl = this.href.replace(re, "/Menu/#/\$1");
                window.location = newUrl;
            });
        });

        // unbind disabled menu categories as set in the cms
        jQuery("#foodMenu li.disabled a").unbind('click').click(function (e) {
            e.preventDefault();
        });

        // storelocator js
        $(".storeLocator input:text,.sidePanel input:text").focus(function () {
            if (this.value == this.defaultValue) {
                this.value = "";
            }
        }).blur(function () {
            if (!this.value.length) {
                this.value = this.defaultValue;
            }
        });

        jQuery(".bannerContent .storeLocator .searchSubmit").click(function (e) {
            e.preventDefault();
            var searchbox = jQuery(".bannerContent .storeLocator input:text");
            if (!(searchbox.val() == "enter postcode or suburb" || searchbox.value == "")) {
                var dealerurl = window.location + "Your-Deal/?search=" + searchbox.val();
                window.location = dealerurl;
            }

        });


        // popups for catering
        $('.listPopus').find('.toolHover').hover(function () {
            $(this).find('.tooltip').show();
        }, function () {
            $(this).find('.tooltip').hide();
        });
		
		//show phone number for Catering page
		$('.call-now .activate a').click(function() {
			$('.call-now .show-phone').slideDown('fast');
			return false;
		});
		
    } ();

}

redRooster.Menu = function() {
    // public properties
    var data = new Data();
    var isProductClick = false;
    var sliderBanner = jQuery("#sliderBanner");

    var nutritionClick = function(e) {

        if (AGLSlider) {
            AGLSlider.mouseOverride = true;
            AGLSlider.stop();
        }

        e.preventDefault();
        jQuery("#overlay").remove();

        $.ajax({
            type: "POST",
            url: "/Services/GetNutritionInfo.aspx",
            data: { "nId": $(this).attr("currentpage") },
            success: function(text) {
                jQuery("body").append(text);
                jQuery(".scroll-pane").jScrollPane();
                jQuery("#overlay").overlay({
                    expose: {
                        color: '#ccc',
                        loadSpeed: 200,
                        opacity: 0.5
                    },
                    closeOnClick: true,
                    closeOnEsc: true,
                    onClose: function() {
                        if (AGLSlider) {
                            AGLSlider.start();
                            AGLSlider.mouseOverride = null;
                        }
                    },
                    onLoad: function() {
                        jQuery("ul.nutritionalTabs").tabs(".nutritionalPanes > div.nutritionalPane");
                    },
                    api: true
                }).load();
            }
        });
    }

    var productClick = function(e) {
        e.preventDefault();
        isProductClick = true;
        if (AGLSlider.stop)
            AGLSlider.stop();

        var pRegex = new RegExp(/\/Menu\/([a-zA-Z0-9-]+)/g);
        var matches = pRegex.exec(this.href);
        if (matches.length > 1) {

            var category = data.currentHash.category;
            var product = matches[1];

            var newHash = new Hash();
            newHash.category = category;
            newHash.product = product;
            var newUrl = newHash.BuildHashUrl() + "/";

            GATracking.TrackPageView('/Menu' + newUrl);

            window.location.hash = newUrl;

            var productIndex = jQuery(this).parent().index();

            $(document).scrollTo($(".bannerMenu"), 400, function() {
                if (sliderBanner.is(":hidden")) {
                    sliderBanner.slideDown(function() {
                        AGLSlider.run(productIndex);
                    });
                }
                else
                    AGLSlider.moveTo(productIndex, false);
            });
        }
    }

    var hashChange = function(e) {
        processHashChange();
    }

    var processHashChange = function() {
        data.SetCurrentHash(window.location.hash);
        if (data.currentHash.product != null && data.currentHash.product != "")
            isProductClick = true;
        else {
            isProductClick = false;
            loadMenuCategory(false);
        }
    }



    var loadMenuCategory = function(isPageLoad) {

        jQuery("#paging").hide();
        sliderBanner.addClass("productsBanner");
        sliderBanner.slideUp(function() {
            var id = data.currentHash.category;
            var cacheItem = data.GetCacheItem(id);

            if (cacheItem != null) {
                showData(cacheItem, false);
            }
            if (cacheItem == null) {
                jQuery.get("/Menu/" + data.currentHash.category + "/", {}, function(response) {
                    var $response = jQuery(response);

                    cacheItem = new CacheItem();
                    cacheItem.id = id;

                    cacheItem.productDetail = $response.find("#slider");
                    cacheItem.productList = $response.find(".primaryContent");

                    data.AddCacheItem(cacheItem);
                    showData(cacheItem, isPageLoad);
                });
            }
        });
    }

    var showData = function(item, isPageLoad) {

        jQuery("#slider").remove();
        jQuery("#sliderWrap").append(jQuery(item.productDetail));

        var primaryContent = jQuery(".primaryContent");
        primaryContent.fadeOut(function() {
            primaryContent.remove();

            var newProductList = jQuery(item.productList);
            newProductList.hide();
            newProductList.insertAfter(sliderBanner);
            redRooster.Fonts().rebind();

            newProductList.fadeIn(function() {
                var product = getProductIndex();

                if (isProductClick) {
                    if (sliderBanner.is(":hidden")) {
                        sliderBanner.slideDown(function() {
                            AGLSlider.run(product);
                        });
                    }
                    else
                        AGLSlider.moveTo(product, false);
                }
            });
        });
    }

    var getProductIndex = function() {
        if (data.currentHash.product) {
            var item = jQuery("a[href*='/Menu/" + data.currentHash.product + "']", ".panelContainer");

            if (item && item.length > 0)
                return item.parent().index();
        }
        return null;
    }

    var pageLoad = function() {

        var hash = location.hash;
        if (hash && hash != "") {
            var trackUrl = hash.replace('#', '/Menu');
            GATracking.TrackPageView(trackUrl);

            data.SetCurrentHash(location.hash);
            if (data.currentHash.product != null && data.currentHash.product != "")
                isProductClick = true;
            else
                isProductClick = false;

            loadMenuCategory(true);
        }
        else {
            if (AGLSlider)
                AGLSlider.run(null);
        }
    }

    var initEvents = function() {
        // set category click handler
        jQuery('.templateMenu .panelContainer li a').live('click', productClick);
        jQuery(window).hashchange(hashChange);
        jQuery('.btn_overlay').live('click', nutritionClick);
    }

    var init = function() {
        pageLoad();
        initEvents();
    } ();

}

function Data() {
    this.currentHash;
    this.cache = {};

    this.SetCurrentHash = function(hash) {
        var t = hash.split('#/')[1];
        if (t) {
            var a = t.split('/');

            this.currentHash = new Hash();
            this.currentHash.category = a[0];

            if (a.length > 1 && a[1] != "")
                this.currentHash.product = a[1];
        }
    }

    this.AddCacheItem = function(cacheItem) {
        this.cache[cacheItem.id] = cacheItem;
    }

    this.GetCacheItem = function(id) {
        if (id in this.cache)
            return this.cache[id];
        return null;
    }
}

function CacheItem() {
    this.id;
    this.productDetail;
    this.productList;
}

function Hash() {
    this.category;
    this.product;

    this.BuildHashUrl = function() {
        var url = "";
        url = "/" + this.category;

        if (this.product != null && this.product != "")
            url = url + "/" + this.product;

        return url;
    }
}

// Enter key event.
function WebForm_FireDefaultButton(event, target) {
    var __defaultFired = false;
    var __nonMSDOMBrowser =
    window.navigator.appName.toLowerCase()
    if (!__defaultFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;
        if (__nonMSDOMBrowser) {
            defaultButton = document.getElementById(target);
        }
        else {
            defaultButton = document.all[target];
        }
        if (defaultButton && typeof (defaultButton.click) != "undefined") {
            __defaultFired = true;
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

jQuery(document).ready(function() {
    jQuery(window).resize(function() {
        var viewportWidth = jQuery(window).width();
        if (viewportWidth < 924) {

            jQuery('.container').addClass('banner930px');
            jQuery('.footer').addClass('width930px');

        } else {
            jQuery('.container').removeClass('banner930px');
            jQuery('.footer').removeClass('width930px');
        }
    });
    jQuery(window).width(function() {
        var viewportWidth = jQuery(window).width();
        if (viewportWidth < 924) {

            jQuery('.container').addClass('banner930px');
            jQuery('.footer').addClass('width930px');

        } else {
            jQuery('.container').removeClass('banner930px');
            jQuery('.footer').removeClass('width930px');
        }
    });

    jQuery(function() {
        var pagingWidth = 0.5 * jQuery("#sliderBanner #paging").width();
        pagingWidth = 465 - pagingWidth;
        jQuery("#sliderBanner #paging").css("left", pagingWidth);
    });

    redRooster.Global();
});

var GATracking = {

    TrackEvent: function(category, action, label, link) {
        if (_gaq) {
            _gaq.push(['_trackEvent', category, action, label]);
        }

        if (link) {
            setTimeout('document.location = "' + link.href + '"', 100);
        }
    },

    TrackPageView: function(url) {
        if (_gaq) {
            if (url.indexOf('http://' + window.location.host) > -1)
                url = url.replace('http://' + window.location.host, '');

            _gaq.push(['_trackPageview', url]);
        }
    }

}
