

jQuery.noConflict();

RegExp.escape = function(str) {
    var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g");
    return str.replace(specials, "\\$&");
}

jQuery(document).ready(function($) {
    $('div#rightCol div.productCarousel div.carouselMask ul.newProductListing').adidoCarousel({ buttonNext: 'div.btnCarouselNext a', buttonPrev: 'div.btnCarouselPrev a' });
    $('div#featured1 div.carouselMask > ul').adidoCarousel({ buttonNext: 'div#featured1 a.btnCarouselNext', buttonPrev: 'div#featured1 a.btnCarouselPrev', startDelay: 0 });
    $('div#featured2 div.carouselMask > ul').adidoCarousel({ buttonNext: 'div#featured2 a.btnCarouselNext', buttonPrev: 'div#featured2 a.btnCarouselPrev', startDelay: 1000 });
    $('div#featured3 div.carouselMask > ul').adidoCarousel({ buttonNext: 'div#featured3 a.btnCarouselNext', buttonPrev: 'div#featured3 a.btnCarouselPrev', startDelay: 2000 });
    $('div.mainCarousel ul').adidoCarousel({ animDelay: 250, animSpeed: 500, buttonNext: 'div.mainCarousel a.btnCarouselNext', buttonPrev: 'div.mainCarousel a.btnCarouselPrev', mode: 'slideFade', speed: 5499 });

    $.fn.adidoCarousel.btnNext = function(e) {
        btn = e.parents('div.productCarousel').find('a.btnCarouselNext');
        return btn;
    }

    $.fn.adidoCarousel.btnPrev = function(e) {
        btn = e.parents('div.productCarousel').find('a.btnCarouselPrev');
        return btn;
    }
    $('div#content div.productCarousel div.carouselMask div.carouselPosition > ul').adidoCarousel({ buttonNext: 'custom', buttonPrev: 'custom', startPosition: 3 });

    var h = 200;

    $('div.categoryGrid div.categoryBox a.prodLink').bigTarget({ clickZone: 'div:eq(0)' });
    $('div.customerSupport h3 a').bigTarget({ clickZone: 'div:eq(1)' });

    $('div.categoryGrid div.categoryBox h3 span').each(function() {
        var ph = $(this).parent().height();
        var th = $(this).height();
        var diff = ph - th;
        var padd = diff / 2;
        $(this).css('padding-top', padd + 'px');
    });
    $('ul#nav-prod li li.navdrop > a').click(function(e) {
        e.preventDefault();
        $(this).next().slideToggle();
        $(this).parent().toggleClass('open');
    });

    $('ul#nav-prod li a').each(function() {
        var catID = $(this).attr("href").match(/(?!.*-)([0-9]+)/)[1];
        $(this).attr('rel', catID);
    });

    $('ul#nav-prod ul').hide();

    var categoryID = "";

    var catRegex = /(?!.*-)([0-9]+)/;
    var prodCatRegex = /([0-9]+)(?=\/)/;

    var catResult = window.location.href.match(catRegex);
    var prodCatResult = window.location.href.match(prodCatRegex);

    if (prodCatResult != null)
        categoryID = prodCatResult[1];
    else if (catResult != null)
        categoryID = catResult[1];
    else
        categoryID = "";

    if (categoryID != "") {
        $('ul#nav-prod li a[rel=' + categoryID + ']').parents('ul').show()
        .end().parents('li').addClass('catActive').children('ul').show()
        .end().next().show();
        var rootTxt = $('ul#nav-prod > li.catActive > a').text();
    }

    $('div.productDetails').corners();
    $('div.productTabsContent').corners();
    $('div#content div.productCarousel').corners();
    $('div.homeBanner').corners();

    $('div.productTabs ul.tabs li:first').addClass('activeTab');
    $('div.productTabs div.productTabsContent div.tabContent').hide().first().show();

    $('div.productTabs ul.tabs li a').click(function(e) {
        e.preventDefault();
        var i = $(this).parent().index();
        $('div.productTabs div.productTabsContent div.tabContent').hide().eq(i).show();
        $(this).parent().addClass('activeTab').siblings().removeClass('activeTab');
    });

    $('div.productGrid div.colUpdateProgress').css({ opacity: 0.3 })

    $('.basketLinks a.lnkHelp').tipsy({ gravity: $.fn.tipsy.autoNS }).click(function(e) { e.preventDefault(); }); ;

    // 18/02/2011 - Enquiry form stuff - JWM
    $('#rightCol div.enquiryForm label').each(function() {
        var assocControl = $('#' + $(this).attr('for'));
        defaultText(assocControl, $(this).text());
    });

    $('#rightCol div.enquiryForm .textbox').blur(function() {
        defaultText($(this), $('label[for="' + $(this).attr('id') + '"]').text());
    });

    function defaultText(control, text) {
        if (control.val() == '') {
            control.val(text);
        }
    }
});
