$(document).ready(function () {
    $('.product').hover(function () { $(this).find('.productcategory').stop(true, true).fadeIn(); }, function () { $(this).find('.productcategory').stop(true, true).fadeOut(); });

    var MoveTimer = 0;

    var ImageToLoad = '';

    function LoadZoom(img) {
        $('#imgzoom_sel').attr('id', '');

        OldImageToLoad = ImageToLoad;
        ImageToLoad = img.replace('t.jpg', 'z.jpg');
        if (ImageToLoad != OldImageToLoad) {
            $('.imgzoom_right img').each(function () {
                if ($(this).attr('src').indexOf(ImageToLoad.replace('z.jpg', '')) >= 0)
                    $(this).attr('id', 'imgzoom_sel');
            });

            $('.imgzoom_img').fadeOut('fast', function () {
                $('.imgzoom_img').attr('src', ImageToLoad);
            });
        }
    }

    $('.imgzoom input').click(function () {
        $('.overlay').hide();
        $('.imgzoom').hide();
    });

    var mx = 0;
    var my = 0;
    var lx = 0;
    var ly = 0;

    $('.imgzoom_left').mousemove(function (event) {
        mx = event.clientX;
        my = event.clientY;
    });

    function MoveLoop() {
        if (lx != mx || ly != my) {
            imgwidth = $('.imgzoom_img').width();
            imgheight = $('.imgzoom_img').height();
            winwidth = $('.imgzoom_left').width();
            winheight = $('.imgzoom_left').height();
            diffwidth = imgwidth - winwidth;
            diffheight = imgheight - winheight;
            l = mx - $('.imgzoom_left').offset().left;
            t = my - $('.imgzoom_left').offset().top;

            var newleft = 0;
            var newtop = 0;

            if (diffwidth > 0)
                newleft = Math.round(0 - (l / winwidth) * diffwidth);
            else
                newleft = Math.round((winwidth / 2) - (imgwidth / 2));

            if (diffheight > 0)
                newtop = Math.round(0 - (t / winheight) * diffheight);
            else
                newtop = Math.round((winheight / 2) - (imgheight / 2));

            $('.imgzoom_img').css({ 'top': newtop + 'px', 'left': newleft + 'px' });

            lx = mx;
            ly = my;
        }
        MoveTimer = setTimeout(function () { MoveLoop(); }, 30);
    }

    $('.imgzoom_right img').click(function () {
        LoadZoom($(this).attr('src'));
    });

    $('.imgzoom_img').load(function () {
        $('.imgzoom_img').hide();
        imgwidth = $('.imgzoom_img').width();
        winwidth = $('.imgzoom_left').width();
        imgheight = $('.imgzoom_img').height();
        winheight = $('.imgzoom_left').height();
        $('.imgzoom_img').css({ 'top': ((winheight / 2) - (imgwidth / 2)) + 'px', 'left': ((winwidth / 2) - (imgwidth / 2)) + 'px' });
        $('.imgzoom_img').fadeIn();
    });

    $('.zoom, .zoomsmall').click(function (e) {
        $('.overlay').show();
        $('.overlay').click(function () { $('.overlay').hide(); $('.imgzoom').hide(); });
        $('.imgzoom_img').hide();
        $('.imgzoom').show();
        ImageToLoad = '';
        if (MoveTimer == 0)
            MoveTimer = setTimeout(function () { MoveLoop(); }, 30);
        LoadZoom($(this).attr('href'));
        e.preventDefault();
    });

    $('.searchbox').attr('autocomplete', 'off');
    $('.searchbox').keyup(function () {
        $('#searchac').load('/SearchAutocomplete.aspx?q=' + escape($('.searchbox').attr('value')), function () { $('#searchac').fadeIn(); });
    });
    $('#tabsearch').append('<div style="position:absolute; z-index:9999; display:none; font-size:11px; top:24px; left:2px; width:240px; border:1px solid #ccc; border-top:none; padding:4px; background:#fff;" id="searchac">Search</div>');
    $('body').click(function () { $('#searchac').fadeOut(); });

    $('.clickable').click(function () {
        document.location = $(this).find('a').attr('href');
    });

    $('.clickable').hover(function () {
        $(this).find('.desc').slideDown();
    }, function () {
        $(this).find('.desc').slideUp();
    });

    $('.searchbox').focus(function () { if ($(this).attr('value') == 'Tool Search') $(this).attr('value', ''); });
    $('.searchbox').blur(function () { if ($(this).attr('value') == '') $(this).attr('value', 'Tool Search'); });

    $('.tabs li').hover(function () {
        $(this).addClass('ontop');
        $(this).find('.tabs_drop').addClass('ontop');
        $(this).find('.tabs_drop').slideDown('fast');
    }, function () {
        $(this).removeClass('ontop');
        $(this).find('.tabs_drop').removeClass('ontop');
        $(this).find('.tabs_drop').stop(true, true).hide();
    });
});

function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
         if (re.test(elm.name))
            elm.checked = false;
   }
   current.checked = true;
}