$(document).ready(function () {


    $.preload(['s0', 's1', 's2', 's3', 's4', 's5', 's6'], {
        base: root + 'style/images/',
        ext: '.png'
    });

    $("#container").supersleight();
    m.i();

    $("#nav > li").each(function (index) {
        var id = "s" + eval(index);
        if (!$(this).hasClass(id)) {
            $(this).hover(
		    function () {
		        $(this).parent().addClass(id);
		    },
		    function () {
		        $(this).parent().removeClass(id);
		    }
	    );
        }
    });


    $(".logincontrol").hover(
        function () {
            $(".loginpanel", this).show();
            var s = $(".login").attr("src");
            $(".login", this).attr("src", s.replace('_s1.png', '_s2.png'));

        },
        function () {
            $(".loginpanel", this).hide();
            var s = $(".login").attr("src");
            $(".login", this).attr("src", s.replace('_s2.png', '_s1.png'));
        }
    );

    $(".search").watermark("Search", { className: 'gray' });


    $("#aboutus,#products,#industriesserved").hover(
		function () {
		    $("ul", this).show();
		},
		function () {
		    $("ul", this).hide();
		}
	);

    if ($("#cycle").exists()) {
        $('#cycle').cycle();
    }




    $(".crumbs li a:last").addClass("background-none");



    try {
        $(".shadow").each(function () {
            var d = $(this);

            d.wrap('<div class="shadowcontainer" />');

            var t = $("<div></div>").attr("class", "shadowtop");
            t.insertBefore(d);

            var b = $("<div></div>").attr("class", "shadowbottom");
            b.insertAfter(d);
        });
    } catch (e) {

    }



    $('input.search').autocomplete({
        serviceUrl: root + "search/default.aspx",
        minChars: 2,
        maxHeight: 400,
        width: 300,
        zIndex: 9999,
        deferRequestBy: 0, //miliseconds
        onSelect: function (value, data) {
            window.location = root + "products/by-cas-number/" + data + "/";
            //            alert('You selected: ' + value + ', ' + data);
        }

    });


    $(".form").each(function (e) {
        $(this).keypress(function (e) {
            if (e.which == 13 && e.target.type != 'textarea') {
                eval($(this).find('.submit').attr('href'));
            }
        });
    });


    $("span.linklist").each(function () {
        var l = $(this).find("a").length;
        $("a", this).map(function (index) {
            if (index < l - 1) {
                $(this).after(", ");
            }
        });
    });

    $("#searcha").click(function () {
        window.location = root + "search/?q=" + $(".search").val();
    });

    $(".search").keypress(function (e) {
        if (e.which == 13) {
            window.location = root + "search/?q=" + $(".search").val();
        }
    });

    if ($("#productcategory").exists()) {
        if ($("#productcategory .item").length < 2) {
            $("#productcategory").addClass("nb");
        }
    }
     
});
 
$.fn.hoverClass = function (c) {
    return this.each(function () {
        $(this).hover(
			function () { $(this).addClass(c); },
			function () { $(this).removeClass(c); }
		);
    });
};
jQuery.fn.exists = function () { return jQuery(this).length > 0; }
m = {
    i: function () {
        $(".hover img:not(.exclude)").hover(function () {
            m.o(this);
        }, function () {
            m.ou(this);
        });
    },
    o: function (e) {
        if (!$(e).hasClass('on')) {
            m.si(e, $(e).attr('src').replace('_s1.png', '_s2.png'));
        }
    },
    ou: function (e) {
        if (!$(e).hasClass('on')) {
            m.si(e, $(e).attr('src').replace('_s2.png', '_s1.png'));
        }
    },
    si: function (e, s) {
        if ($.browser.msie && $.browser.version < 7 && $(e).hasClass("png")) {
        } else {
            $(e).attr('src', s);
        }
    }
}
function GotoValidationSummary() {
    if (typeof (ValidatorOnSubmit) == "function") {
        var isValidated = ValidatorOnSubmit();
        if (!isValidated) {
            var pos = $("#validationsummary").show().position();
            window.scrollTo(pos.left, pos.top);
            return false;
        }
        return true;
    }
}
function GetImage(id, alt) {
    var img$ = $("#" + id + " img").filter(function (index) {
        return $(this).attr("alt") == alt;
    })
    return img$;
}
function GetByRel(id, rel) {
    var img$ = $("#" + id + " img").filter(function (index) {
        return $(this).attr("rel") == rel;
    })
    return img$;
}
