jQuery(document).ready(function($) {

    var searchHandler = 'MoneySearch';
    var thiscounter = 0;

    function mousedownSearchTracking(e) {
        var $zone = $(this);

        if (typeof (e.data.action) == "undefined") {
            if (typeof (CreateOnClickEvent) != "undefined")
                CreateOnClickEvent('Search', 30, searchHandler + e.data.section)   // just section data  
        }
        else if (e.data.action == "link") {
            var linkText = $zone.text();
            if (typeof (CreateOnClickEvent) != "undefined")
                CreateOnClickEvent('Search', 30, searchHandler + e.data.section + ':' + linkText)   // with link data
        }
        else if (e.data.action == "position") {
            var position = $zone.attr('position');
            if (typeof (CreateOnClickEvent) != "undefined")
                CreateOnClickEvent('Search', 30, searchHandler + e.data.section + ':' + position)   // add position data
        }

        $zone.unbind('mousedown');
    }

    /// Handle the Search box submit on the Search page
    function submitSearchTracking(e) {
        var $zone = $(this);
        var inputValue = $('input[id=prod-search]').val();  // get the search value the user typed in

        if (typeof (CreateOnClickEvent) != "undefined")
            CreateOnClickEvent('Search', 30, searchHandler + e.data.section)   // just section data  

        $zone.unbind('submit');
    }


    // Click Events to bind    
    // -----------------------------------------------
    // re-search
    $('form[id=searchform]').bind('submit', { section: ":re-search" }, submitSearchTracking);

    // Did You Mean
    $('div.did-you-mean a').bind('mousedown', { section: ":DidYouMean" }, mousedownSearchTracking);

    // Category filters
    $("ul li a", $("#trackcatCategory")).not($("li.toggle")).each(function(counter) {
        if (counter < 10) {
            thiscounter = counter + 1;
            $(this).bind('mousedown', { section: ":Category" + thiscounter }, mousedownSearchTracking);
        }
    });

    // Brand filters
    $("ul li a", $("#trackcatBrand")).not($("li.toggle")).each(function(counter) {
        if (counter < 10) {
            thiscounter = counter + 1;
            $(this).bind('mousedown', { section: ":Brand" + thiscounter }, mousedownSearchTracking);
        } 
    });

    // Price range filters
    $("ul li a", $("#trackcatPriceRange")).each(function(counter) {
        $(this).bind('mousedown', { section: ":PriceRange" }, mousedownSearchTracking);
    });

    // The 'more' buttons to expand long filters
    //$("li[trackcat='Category'] li.toggle a").bind('mousedown', { section: ":CategoryMore" }, mousedownSearchTracking);
    //$("li[trackcat='Brand'] li.toggle a").bind('mousedown', { section: ":BrandMore" }, mousedownSearchTracking);

    // Learn tab content
    $("ul[trackcat='learntabcontent'] li a", $("#ArticleList")).each(function(counter) {
        thiscounter = counter + 1;
        $(this).bind('mousedown', { section: ":Article" + thiscounter }, mousedownSearchTracking);
    });

    // Support content
    $("ul[trackcat='supportcontent'] li a", $("#supportcontent")).each(function(counter) {
        thiscounter = counter + 1;
        $(this).bind('mousedown', { section: ":Support" + thiscounter }, mousedownSearchTracking);
    });

    // Community content
    $("ul[trackcat='communitycontent'] li a", $("#communitycontent")).each(function(counter) {
        thiscounter = counter + 1;
        $(this).bind('mousedown', { section: ":Community" + thiscounter }, mousedownSearchTracking);
    });

    // Sort strip links
    $("div.r-sort-strip-top a.showall").bind('mousedown', { section: ":SortStripShowAll" }, mousedownSearchTracking);
    $("div.r-sort-strip a.r-sort-prev").bind('mousedown', { section: ":SortStripPrevPage" }, mousedownSearchTracking);
    $("div.r-sort-strip a.r-sort-next").bind('mousedown', { section: ":SortStripNextPage" }, mousedownSearchTracking);
    $("div.r-sort-strip-top a[trackcat='switchtopaged']").bind('mousedown', { section: ":SortStripSwitchToPaged" }, mousedownSearchTracking);
    $("div.r-sort-strip a[trackcat='pagelink']").each(function() {
        var inputValue = $(this).text();
        $(this).bind('mousedown', { section: ":SortStripResults" + inputValue }, mousedownSearchTracking);
    });

    // product block links
    $("div.productList-block-container", $("#maincolumn")).each(function(counter) {
        thiscounter = counter + 1;
        // product link
        $(this).find("a.pg-img").bind('mousedown', { section: ":Product" + thiscounter }, mousedownSearchTracking);
        $(this).find("div.productList-desc a").bind('mousedown', { section: ":Product" + thiscounter }, mousedownSearchTracking);
        // add to cart
        $(this).find("div.productList-action a").bind('mousedown', { section: ":Product" + thiscounter + "AddToCart" }, mousedownSearchTracking);
        // reviews
        $(this).find("div.star-rating a").bind('mousedown', { section: ":Product" + thiscounter + "Reviews" }, mousedownSearchTracking);
        // outlet
        $(this).find("a[trackcat='Outlet']").bind('mousedown', { section: ":Product" + thiscounter + "Outlet" }, mousedownSearchTracking);
        // scratch and dent
        $(this).find("a[trackcat='Scratch & Dent']").bind('mousedown', { section: ":Product" + thiscounter + "ScratchAndDent" }, mousedownSearchTracking);
    });
});



