//images for rollover states preload
var preloadImg = [
	"/images/locale/en-us/menu/menu_1_hover.gif",
	"/images/locale/en-us/menu/menu_2_hover.gif",
	"/images/locale/en-us/menu/menu_4_hover.gif",
	"/images/locale/en-us/menu/menu_1_on.gif",
	"/images/locale/en-us/menu/menu_2_on.gif",
	"/images/locale/en-us/menu/menu_3_on.gif",
	"/images/14x14_page_on.gif",
	"/images/bg_selectedProduct.gif",
	"/images/bg_selectedProductSpecific.gif",
	"/images/bg_selecterLetter.gif",
	"/images/locale/en-us/btn_findOut_on.gif",
	"/images/locale/en-us/btn_share_blue_on.gif",
	"/images/locale/en-us/btn_wasThisHelpful_on.gif",
	"/images/locale/en-us/bg_promo_greenList_on.png",
	"/images/locale/en-us/bg_promo_productIngredients_on.png",
	"/images/locale/en-us/bg_promo_whyTheseIngredientNames_on.png",
	"/images/locale/en-us/tab-find_1_hover.gif",
	"/images/locale/en-us/tab-find_2_hover.gif",
	"/images/locale/en-us/tab-find_1_on.gif",
	"/images/locale/en-us/tab-find_2_on.gif",
	"/images/locale/en-us/tab_1_on.gif",
	"/images/locale/en-us/tab_2_on.gif",
	"/images/locale/en-us/tab_1_hover.gif",
	"/images/locale/en-us/tab_2_hover.gif"
];

var tmpImg;

for(i = 0; i < preloadImg.length; i++) {
	tmpImg = new Image();
	tmpImg.src = preloadImg[i];
}


// function for AddThis buttons
function addthis_proxy(arg1, arg2, arg3, arg4, left) {
    addthis_pub = 'scjmonty';
    addthis_logo = 'http://www.whatsinsidescjohnson.com/images/logo.gif';
    addthis_logo_background = 'ffffff';
    addthis_logo_color = '1666A5';
    addthis_brand = 'SC Johnson';
    addthis_options = 'email, favorites, facebook, digg, delicious, twitter';

    if (left) {
        addthis_offset_left = 0;
        addthis_offset_top = 0;
    }
    else {
        addthis_offset_left = -160;
        addthis_offset_top = 3; // ie6 fix
    }

    addthis_open(arg1, arg2, arg3, arg4);
}



$(document).ready(function() {
    //fix for ie6 - drop down menu
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

        $("#menu > li").mouseover(function() {
            $(this).addClass("over");
        }).mouseout(function() {
            $(this).removeClass("over");
        });

    }

    //menu hover states
    $("#menu > li").hover(

		function() {

		    var image = $(this).find("div a img")[0];

		    if ($(this).hasClass("noDropDown")) {

		        if ($(this).attr("id") == "activeMenu") {
		            return;
		        }
		        else {
		            image.src = image.src.replace("_off", "_on");
		        }
		    }

		    else {

		        if ($(this).attr("id") == "activeMenu") {
		            image.src = image.src.replace("_on", "_hover");
		        }
		        else {
		            image.src = image.src.replace("_off", "_hover");
		        }
		    }

		},

		function() {

		    var image = $(this).find("div a img")[0];

		    if ($(this).hasClass("noDropDown")) {
		        if ($(this).attr("id") == "activeMenu") {
		            return;
		        }
		        else {
		            image.src = image.src.replace("_on", "_off");
		        }
		    }
		    else {
		        if ($(this).attr("id") == "activeMenu") {
		            image.src = image.src.replace("_hover", "_on");
		        }
		        else {
		            image.src = image.src.replace("_hover", "_off");
		        }
		    }
		}
	);


    //first/last element in the menu
    $(".menuDiv ul li ul li:last-child").addClass("lastDDItem");
    $(".menuDiv ul li ul.alertDD li:first-child").addClass("firstAlertDDItem");
    $(".menuDiv ul li ul.alertDD li:last-child").addClass("lastAlertDDItem");

    if ($(".menuDiv ul li ul.alertDD li").length == 1) {

        $(".menuDiv ul li ul.alertDD li").removeClass();
        $(".menuDiv ul li ul.alertDD li").addClass("singleAlertDDItem");
    }

    //info popups
    $(".infoLinkDiv").hover(
		function() {
		    var id = $(this).attr("id");
		    $("#" + id + "Popup").fadeIn("fast");
		},
		function() {
		    var id = $(this).attr("id");
		    $("#" + id + "Popup").fadeOut("fast");
		}
	);


    // product view (slide effect)
    var imageBase = "/images/";
    $(".productsPaging").css("display", "block");
    $("div.productsContentDiv").css("overflow-x", "hidden");

    var curId = 0;

    // Get max number of slides 
    var maxId = 0;
    $(".btn").each(
	    function(setMax) {
	        var id = $(this).attr("id").split('_')[1];
	        if (id > maxId) {
	            maxId = id;
	            $(".forward").children().attr("src", imageBase + "arrow_right.gif");
	        }
	    }
    );

    var slideW = ($(".slideHolder div").width()) * 4;
    var slideDiv = $(".slideHolder").width();


    $(".btn").click(function() {

        var id = $(this).attr("id").split('_')[1];
        var mult = Math.abs(id - curId);

        // switch paging images from off to on
        $(".btn img").attr("src", imageBase + "14x14_page_off.gif");
        $("#item_" + id).children().attr("src", imageBase + "14x14_page_on.gif");


        if (id != curId) {

            if (id > curId) {
                $("#item_" + curId + " img").attr("alt", "previous page");
                $(".slideHolder").animate({ "left": "-=" + slideW * mult + "px" }, "slow");
            }

            else {
                $("#item_" + curId + " img").attr("alt", "next page");
                $(".slideHolder").animate({ "left": "+=" + slideW * mult + "px" }, "slow");
            }
        }

        // set the alt attribute for current dot button image
        $("#item_" + id).children().attr("alt", "current page");

        if (id > 0) {
            $(".back").children().attr("src", imageBase + "arrow_left.gif");
        }
        else {
            $(".back").children().attr("src", imageBase + "arrow_left_off.gif");
        }

        if (id < maxId) {
            $(".forward").children().attr("src", imageBase + "arrow_right.gif");
        }
        else {
            $(".forward").children().attr("src", imageBase + "arrow_right_off.gif");
        }

        curId = id;
        return id;
    });

    $(".back").click(function() {
        if (curId > 0) {

            var id = curId - 1;

            var mult = Math.abs(id - curId);
            $("#item_" + curId + " img").attr("alt", "previous page");
            $(".slideHolder").animate({ "left": "+=" + slideW * mult + "px" }, "slow");
            curId = id;

            $(".forward").children().attr("src", imageBase + "arrow_right.gif");

            if (id == 0) {
                $(".back").children().attr("src", imageBase + "arrow_left_off.gif");
            }

            // switch paging images from off to on
            $(".btn img").attr("src", imageBase + "14x14_page_off.gif");
            $("#item_" + id).children().attr("src", imageBase + "14x14_page_on.gif");

            // Set alt attributes of dot buttons
            $("#item_" + (id - 1) + " img").attr("alt", "previous page");
            $("#item_" + (id + 1) + " img").attr("alt", "next page");
            $("#item_" + id).children().attr("alt", "current page");
        }
        else {
            $(".back").children().attr("src", imageBase + "arrow_left_off.gif");
        }
    });

    $(".forward").click(function() {
        if (curId < maxId) {

            var id = curId + 1;

            var mult = Math.abs(id - curId);
            $("#item_" + curId + " img").attr("alt", "previous page");
            $(".slideHolder").animate({ "left": "-=" + slideW * mult + "px" }, "slow");
            curId = id;

            $(".back").children().attr("src", imageBase + "arrow_left.gif");

            if (id == maxId) {
                $(".forward").children().attr("src", imageBase + "arrow_right_off.gif");
            }

            // switch paging images from off to on
            $(".btn img").attr("src", imageBase + "14x14_page_off.gif");
            $("#item_" + id).children().attr("src", imageBase + "14x14_page_on.gif");

            // Set alt attributes of dot buttons
            $("#item_" + (id - 1) + " img").attr("alt", "previous page");
            $("#item_" + (id + 1) + " img").attr("alt", "next page");
            $("#item_" + id).children().attr("alt", "current page");
        }
        else {
            $(".forward").children().attr("src", imageBase + "arrow_right_off.gif");
        }
    });

    //rollover effect for products by brand
    $(".slideHolder div").hover(
		function() {
		    $(".slideHolder div").css({ 'opacity': '.50', 'filter': 'alpha(opacity=50)' });
		    $(this).css({ 'opacity': '1', 'filter': 'alpha(opacity=100)', 'background': 'url(/images/bg_selectedProduct.gif) no-repeat top left' });
		},
		function() {
		    $(".slideHolder div").css({ 'opacity': '1', 'filter': 'alpha(opacity=100)', 'background': 'none' });
		}
	);

    //rollover effect for products by brand specific
    $(".productsSpecificContent div").hover(
		function() {
		    $(".productsSpecificContent div").css({ 'opacity': '.50', 'filter': 'alpha(opacity=50)' });
		    $(this).css({ 'opacity': '1', 'filter': 'alpha(opacity=100)', 'background': 'url(/images/bg_selectedProductSpecific.gif) no-repeat top left' });
		},
		function() {
		    $(".productsSpecificContent div").css({ 'opacity': '1', 'filter': 'alpha(opacity=100)', 'background': 'none' });
		}
	);

    //tabs behavior
    $("#tab_2_content").css("display", "none"); // set tab2 to 'display:none' so that it shows up with js off

    $(".tabsHdrDiv div").click(function() {

        var id = $(this).attr("id").split('_')[1];

        //switch tabs headers
        $(".activeTab").children().attr("src", function() {
            this.src = this.src.replace("_on", "_off");
            this.src = this.src.replace("_hover", "_off");
        });

        $(".tabsHdrDiv div").removeClass();
        $(this).children().attr("src", function() {
            this.src = this.src.replace("_off", "_on");
            this.src = this.src.replace("_hover", "_on");
        });

        $(this).addClass("activeTab");


        //show/hide tabs content
        $(".tabsContentDiv > div").hide();
        $(".tabsContentDiv div#tab_" + id + "_content").show();

    });

    $(".tabsHdrDiv div img").hover(
		function() {
		    if (!$(this).parent().hasClass("activeTab")) {
		        this.src = this.src.replace("_off", "_hover");
		    }
		},
		function() {
		    if (!$(this).parent().hasClass("activeTab")) {
		        this.src = this.src.replace("_hover", "_off");
		    }
		}
	);

    //initial set to display none lists for items that have image "plus"
    var plusImg = $(".ingredientDetailDiv h2 img");
    if (plusImg.length) {

        $(plusImg).parent().parent().addClass("discDiv");
        $("div.discDiv ul").css("display", "none");

    };
    //collapse/expand behavior
    $(plusImg).click(function() {

        var par = $(this).parent().parent();
        var list = $(par).children("ul");

        if ($(list).css("display") == "none") {

            var ListStatus = $(list).css("display", "block");
            this.src = this.src.replace("_plus", "_minus");
            this.alt = this.alt.replace("Expand", "Collapse");

        }
        else {

            var ListStatus = $(list).css("display", "none");
            this.src = this.src.replace("_minus", "_plus");
            this.alt = this.alt.replace("Collapse", "Expand");
        }
    });


    //lightbox popups behavior
    $(".boxclose a").click(function() {

        $(".box").hide();
        $("#filter").hide();
        return false;

    });

    $(".lightBoxLink").click(function() {

        attr = $(this).attr("href");
        //calculation of lightbox popup position
        popupWidth = $(".box").width();
        winWidth = $(window).width();
        marLeft = (winWidth - popupWidth) / 2;
        $(".box").css("left", marLeft + "px");

        //animate scroll up to the top of the page
        jQuery.fn.extend({
            scrollTo: function(speed, easing) {
                return this.each(function() {
                    var targetOffset = $(this).offset().top;
                    $("html, body").animate({ scrollTop: targetOffset }, speed, easing);
                });
            }
        });

        $("#" + attr).show();
        $("#filter").show();

        $("body").scrollTo(400);

        pageTracker._trackPageview(attr);
        return false;

    });


    // popup links behavior
    $("a.popupLink").click(function() {

        var url = $(this).attr("href");
        if ($(this).attr("id") == "survey") {
            window.open(url, '', 'width=576,height=640,scrollbars=yes,resizable=yes,status=yes');
        }
        else {
            window.open(url, '', 'width=780,height=730,scrollbars=yes,resizable=yes,status=yes');
        }
        pageTracker._trackPageview(url);
        return false;

    });

    // print link behavior
    $("a.printLink").click(function() {

        window.print();
        return false;

    });

    //simple rollover for image button links
    $("img.imgBtn").hover(
		function() {
		    this.src = this.src.replace("_off", "_on");
		},
		function() {
		    this.src = this.src.replace("_on", "_off");
		}
	);

    // functions for Locations
    $(".locationsDiv li").hover(
		function() {
		    $(this).addClass("hover");
		},
		function() {
		    $(this).removeClass("hover");
		}
	);
    $(".locationsPageDiv li").hover(
		function() {
		    $(this).addClass("hover");
		},
		function() {
		    $(this).removeClass("hover");
		}
	);
    $(".changeLocBtn").hover(
		function() {
		    $(".locationsDiv").css("display", "block");
		},
		function() {
		    $(".locationsDiv").css("display", "none");
		}
	 );

    $(".rememberChk :checkbox").click(
	    function() {
	        if ($(this).attr("checked")) {
	            $(".rememberChk :checkbox").attr("checked", "checked");
	            pageTracker._trackEvent('Locale_Selector', 'Check Remember');
	        }
	        else {
	            $(".rememberChk :checkbox").removeAttr("checked");
	            pageTracker._trackEvent('Locale_Selector', 'Uncheck Remember');
	        }
	    });





    $(".flagDiv").hover(
		function() {
		    $(".flagDiv a span").css("display", "block");
		},
		function() {
		    $(".flagDiv a span").css("display", "none");
		}
	);
	
// removing border from every 4th item for formula list : Aaron

	$('ul.packaging li:nth-child(4n)').addClass('no-border');
	
// hover-fade over packaging li's

  $('.packaging li img').css('opacity', '.8');
  $('.packaging li img').mouseover(function() {
	$(this).stop().animate({opacity: 1}, 'fast')
	});
  $('.packaging li img').mouseout(function() {
	$(this).stop().animate({opacity: .8}, 'fast')
	});

// upgraded tool-tips for formula info as hovering over all of h4 : Aaron 
 	
	$('.toolTipArea h4.hasInfo').mouseover(function()  {
    var xCoor = $(this).find('img').position().left - 11;
	var yCoor = $(this).find('img').position().top;
    $(this).parent().find('.ingredientToolTipHover').addClass('reveal').css('left', xCoor, 'top', yCoor);
});
	$('.toolTipArea h4.hasInfo').mouseout(function() {
		$(this).next('.ingredientToolTipHover').removeClass('reveal');
	});
	
//	$('.formulaDrpDwn').selectbox();

});
