var menu = {
    timer: null,
    delay: 2500,
    item: null,
    init: function() {
        this.bind();
    },
    bind: function() {
        var obj = this;

        $("#menu div.subnav").parent().hover(function() {
            clearTimeout(obj.timer);
            $("#menu div.subnav").hide();
            $("#menu div.subnav").parent().children("a").removeClass("hover");

            if ($.browser.msie && $.browser.version.indexOf("6") != -1)
                $("select").css("visibility", "hidden");

            obj.item = $(this);
            obj.item.children(".subnav").show();
            obj.item.children("a").addClass("hover");
            home.spotlight.hide();

        },
		function() {
		    obj.timer = setTimeout("menu.hide()", obj.delay);
		});
    },
    hide: function() {
    if (this.item != null) {
        this.item.children(".subnav").hide();
        this.item.children("a").removeClass("hover");

        if ($.browser.msie && $.browser.version.indexOf("6") != -1)
            $("select").css("visibility", "visible");
        
        }

        
    }
};

var fixes = {
    init: function() {
        this.zindex();
        this.buttons();
    },
    zindex: function() {
        // http://www.vancelucas.com/blog/fixing-ie7-z-index-issues-with-jquery/
        var zIndexNumber = 1000;

        $('div:not(.opaque, #body, #breadcrumb, .form, #search, #img_pairing_1)').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    },
    buttons: function() {
        if ($.browser.msie && $.browser.version.indexOf("6") != -1)
            $(".btn").hover(function() {
                $(this).css("background-position", "left bottom");
            },
			function() {
			    $(this).css("background-position", "left top");
			});
    },
    subnav: function() {
        if ($.browser.msie && $.browser.version.indexOf("6") != -1) {
            var top = $(".subnav").css("top");
            top = parseInt(top) - 1500;

            $(".subnav, .subnav.breadcrumb").css("top", top + "px").hide();
        }
    },
    position: function() {
        //$("a").css("position", "static");
        //$("#recipes_spotlight .top, #home_spotlight .top").css("position","static");
        $("#newsletter").css("position", "static");
    }
};

var home = {
    init: function() {
        this.spotlight.init();
        this.callout.init();
    },
    callout: {
		activeItem: null,
		refTimer: null,
		relItems: [],
		init: function() {
            var _self = this;
			 
			$('#callouts .navigation .numbers').find('a').each(function(){
				_self.relItems.push($(this).attr('rel'));
			});
			
			_self.showFirst();
			_self.setEvents();
			_self.setTimer();
			 
        },
		showFirst: function(){
			$('#callouts .callout:first').show();
		},
		showItem: function(itemPos){
			var _self = this;
			var toShowClass = _self.relItems[itemPos];
			if(toShowClass && !$('#callouts .'+toShowClass).is(':visible')){
				$('#callouts .callout:visible').fadeOut(100,function(){
					$('#callouts .'+toShowClass).fadeIn(100);
					_self.activeItem = itemPos;
					_self.setBgPosition(itemPos);
				});
			}
		},
		setBgPosition: function(pos){
			var objMenu = $('#callouts .navigation');
			var bgHeight = objMenu.height();
			objMenu.css('background-position', 'left ' + ((pos * bgHeight) * -1) + 'px');
		},
		setEvents: function(){
			var _self = this;
			$('#callouts .navigation a').click(function(){
				var itemPos = _self.getItemPos($(this).attr('rel'));
				_self.showItem(itemPos);
				clearInterval(_self.timer);
				return false;
			});
		},
		setTimer: function(){
			var _self = this;
			_self.timer = setInterval(function(){
				var nextItem = ((_self.activeItem < _self.relItems.length - 1) ? _self.activeItem + 1 : 0);
				_self.showItem(nextItem);
             },5000);
		},
		getItemPos: function(classItem){
			var _self = this;
			var i = _self.relItems.length;
			while(i){
				if(_self.relItems[i-1] == classItem){
					return i-1;
				}
				i--;
			}
		}
    },
    spotlight: {
        timer: null,
        delay: 3500,
        init: function() {
            this.bind();
            $(".bubble").hide();
        },
        bind: function() {
            var obj = this;

            $("#home_spotlight .item > a > img").hover(function() {
                
                clearTimeout(obj.timer);
                $(this).parent().parent().children(".bubble").show();
                menu.hide();
            },
			function() {
			    obj.timer = setTimeout("home.spotlight.hide()", obj.delay);
			});

            if ($.browser.msie && $.browser.version.indexOf("6") != -1)
                $("#home_spotlight .item > .desc, #home_spotlight .bottom").mouseenter(function() {
                    obj.hide();
                }).mouseleave(function() {
                    obj.hide();
                });

            $("div#home_spotlight > .top").cycle({
                fx: "fade",
                speed: "fast",
                timeout: 0,
                next: "#home_spotlight .next",
                prev: "#home_spotlight .prev",
                before: obj.hide,
                after: obj.hide,
                end: obj.hide
            });
        },
        hide: function() {
            $(".bubble").show().hide();
        }
    }
};

var recipes = {
    init: function() {
        this.spotlight.init();
    },
    spotlight: {
        timer: null,
        delay: 2500,
        item: null,
        init: function() {
            this.bind();
        },
        bind: function() {
            var obj = this;

            $("#recipes_spotlight .top").hover(function() {
                clearTimeout(obj.timer);
                obj.item = $(this).children(".bubble");
                obj.item.show();
            },
			function() {
			    obj.timer = setTimeout("recipes.spotlight.hide()", obj.delay);
			});
        },
        hide: function() {
            this.item.hide();
        }
    }
};

$(function() {
    menu.init();
    fixes.init();
});

function _hbLink(){};

function _hbPageView(){};

