// JavaScript Document
$(document).ready(function() {
    //$(document.body).css("overflow-x","hidden");
    $('#site_wrap').css("overflow-x","hidden");
    //for homepage
    var times = null,times1 = null;
    $('.Navigation UL LI').mouseenter(function(){
		var coords = $(this).backgroundPosition().split(" ")[0];
		$(this).css({ backgroundPosition: coords +' -76px' });
    }).mouseleave(function(){
		var coords = $(this).backgroundPosition().split(" ")[0];
    	$(this).css({ backgroundPosition: coords+' 0px' });
    });

    $("#better_oats").mouseenter(function() {
        if (times) {
            clearTimeout(times);
        }
        times = setTimeout(function() {
        	$("#better_oats .sub_nav").css({display:'block'});
            $("#better_oats .sub_nav UL").animate({top:'0px'},400,"swing");//.slideDown("fast")
        }, 50);
    }).mouseleave(function() {
    	
        if (times) {
            clearTimeout(times);
        }
        times = setTimeout(function() {
            $("#better_oats .sub_nav UL").animate({top:'-100%'},250,"swing", function(){ $("#better_oats .sub_nav").css({display:'none'}); });//.slideUp("fast")
        }, 100);
    });
	
	$("#stuff").mouseenter(function() {
        if (times) {
            clearTimeout(times);
        }
        times = setTimeout(function() {
        	$("#stuff .sub_nav").css({display:'block'});
            $("#stuff .sub_nav UL").animate({top:'0px'},400,"swing");//.slideDown("fast")
        }, 50);
    }).mouseleave(function() {
    	
        if (times) {
            clearTimeout(times);
        }
        times = setTimeout(function() {
            $("#stuff .sub_nav UL").animate({top:'-100%'},250,"swing", function(){ $("#stuff .sub_nav").css({display:'none'}); });//.slideUp("fast")
        }, 100);
    });
	
    $("#product").mouseenter(function() {
        if (times1) {
            clearTimeout(times1);
        }
        times1 = setTimeout(function() {
        	$("#product .sub_nav").css({display:'block'});
            $("#product .sub_nav UL").animate({top:'0px'},400,"swing");//.slideDown("fast")
        }, 50);
    }).mouseleave(function() {
    	
        if (times1) {
            clearTimeout(times1);
        }
        times1 = setTimeout(function() {
            $("#product .sub_nav UL").animate({top:'-100%'},250,"swing", function(){ $("#product .sub_nav").css({display:'none'}); });//.slideUp("fast")
        }, 100);
    });

//    $(".arrow_L,.arrow_R").fadeOut("fast");
//    $(".slideshow").mouseenter(function() {
//        $(".arrow_L,.arrow_R").fadeIn("fast");
//    }).mouseleave(function() {
//        $(".arrow_L,.arrow_R").fadeOut("fast");
//    });



    //	$("ul#home_pro").tabs("div.home_tab_m"//, {effect: 'fade', fadeOutSpeed: 400}
    //						   )
    var spd = "fast";
    var delay = 6000;
    var s = setTimeout("$('#why_oats').click()");
	//$("ul#home_pro").css({left:"-4px"});

	$("#why_oats").click(function() {
        clearTimeout(s);

        $(this).parent().css({ backgroundPosition: '0px 0px' });        
		$("#why_oats_show").fadeIn(spd);
        $("#find_oats_show").hide();
        $("#feeding_show").hide();
		$("#taste_show").hide();
		$("#grains_show").hide();
        s = setTimeout("$('#find_oats').click()", delay);
    })

    $("#find_oats").click(function() {
        clearTimeout(s);

        $(this).parent().css({ backgroundPosition: '-254px 0px' });
		$("#why_oats_show").hide();
        $("#find_oats_show").fadeIn(spd);
        $("#feeding_show").hide();
		$("#taste_show").hide();
		$("#grains_show").hide();
        s = setTimeout("$('#feeding').click()", delay);
    })
	
    $("#feeding").click(function() {
        clearTimeout(s);

        $(this).parent().css({ backgroundPosition: '-508px 0px' });
		$("#why_oats_show").hide();
        $("#find_oats_show").hide();
        $("#feeding_show").fadeIn(spd);
		$("#taste_show").hide();
		$("#grains_show").hide();		
        s = setTimeout("$('#taste').click()", delay);
    })
		
     $("#taste").click(function() {
        clearTimeout(s);

        $(this).parent().css({ backgroundPosition: '-762px 0px' })       	
		$("#why_oats_show").hide();
        $("#find_oats_show").hide();
        $("#feeding_show").hide();
		$("#taste_show").fadeIn(spd);
		$("#grains_show").hide();
        //        $("#tester").animate({ alpha: 1 }, delay, function() { $("#why_oats").click() })
        s = setTimeout("$('#grains').click()", delay);
    })
	 
	$("#grains").click(function() {
        clearTimeout(s);

        $(this).parent().css({ backgroundPosition: '-1016px 0px' });        
		$("#why_oats_show").hide();
        $("#find_oats_show").hide();
        $("#feeding_show").hide();
		$("#taste_show").hide();
		$("#grains_show").fadeIn(spd);

        s = setTimeout("$('#why_oats').click()", delay);
    })
	
    $("#why_oats").click();

    $(".box_tab_list ul").tabs("div.box_tab_c", {tabs:'li', onClick:ie6RedrawHook});
    //$(".pro_tab_list ul").tabs("div.M_content")//, {effect: 'fade', fadeOutSpeed: 400}

    //for product page

    //for product tab_list
    $(".box_tab_list ul li").each(function(i) {
        $(this).click(function() {
            //var url = $(".slide_img img").eq(i);
            $(".box_tab_list ul li").removeClass("active")
            $(this).addClass("active");
        });
    });
	
	//for pop-up on nutrition.php page
	$(".pop-up").css({display:"none"})
	$("#pop-up").click(function(){
		$(".pop-up").fadeIn("slow");
	})
	$(document).bind('keydown', function(e) {
		if (e.keyCode == 27) $(".pop-up").fadeOut("slow");
		return true
  	});
	$(".pop-up").click(function(){
		$(this).fadeOut("slow");
	})
	
	
	//te();
	
	//var w = window.screen.availWidth + "px";
	var originalFlavor = $("#otherLikeText a").text();
	var originalFlavorURL = $("#otherLikeText a").attr("href");
	// for individual.php page
	$("#otherLike li img").each(function(i){
		var imgactiveurl = $("#otherLike li.active img").attr("src");
		var imgurl = $(this).attr("src");
		var newurl = $(this).attr("src").replace("gray_oats", "gray_oats/gray");
		$(this).attr("src",newurl);
		$("#otherLike li.active img").attr("src",imgactiveurl);
	
		$(this).mouseenter(function(){
			$(this).attr("src",imgurl);
			var proName = $(this).attr("alt");
			var proURL = $(this).parent().attr("href");
			
			$("#otherLikeText a").text(proName);
			$("#otherLikeText a").attr("href",proURL);
		}).mouseleave(function(){
			$(this).attr("src",newurl);
			$("#otherLike li.active img").attr("src",imgactiveurl);
			$("#otherLikeText a").text(originalFlavor);
			$("#otherLikeText a").attr("href", originalFlavorURL);
		});
	});



});    

(function($) {
  jQuery.fn.backgroundPosition = function() {
    var p = $(this).css('background-position');

    if(typeof(p) == 'undefined') {
		return $(this).css('backgroundPosition-x') +  ' ' + $(this).css('backgroundPosition-y');
	}
    else 
		return p;
  };
})(jQuery);

