$(document).ready(function() {
    var track = undefined;
    $("a[href^='http://']:not([href$=.pdf]):not([id$='lnkDataSheet'])").click(function() {
        if ($(this).attr('target') == "_blank") {
            track = '/outgoing/' + jQuery.url.segment(0) + '/' + $(this).attr("href").replace("http://", "");
            if (track != undefined) {
                trackLinks(track);
            }
        }
    });
    $("a[href^='http://'][href$=.pdf]:not([id$='lnkDataSheet'])").click(function() {
        if ($(this).attr('target') == "_blank") {
            track = '/outgoing/pdf/' + $(this).attr("href").replace("http://", "").replace('../', '');
            if (track != undefined) {
                trackLinks(track);
            }
        }
    });
    $("a[href$=.pdf]:not([href^='http://']):not([id$='lnkDataSheet'])").click(function() {
        if ($(this).attr('target') == "_blank") {
            track = '/download/pdf/' + $(this).attr("href").replace("http://", "").replace('../', '');
            if (track != undefined) {
                trackLinks(track);
            }
        }
    });

    $("a[id$='lnkDataSheet']").click(function() {
        if ($(this).attr('target') == "_blank") {
            track = '/outgoing/datasheets/' + $(this).parents('tr').find("a[id$='lnkMouserPartNumber']").html();
        }
        else {
            track = '/download/datasheets/' + $(this).parents('tr').find("a[id$='lnkMouserPartNumber']").html();
        }
        if (track != undefined) {
            trackLinks(track);
        }
    });

    $("div[id^='popup'] a").click(function() {
        track = '/popup/' + $(this).parents('tr').find("a[id$='lnkMouserPartNumber']").html();
        if (track != undefined) {
            trackLinks(track);
        }
    });

    //find similar	
    if ($("div[id*='FindSimilarPnl']").length > 0) {
        findsimilar("init");
        if ($("input[id*='btnfindsimilar']").length > 0) {
            if ($("input[id*='btnfindsimilar']").val() == "Find Similar") {
                $("input[id*='btnfindsimilar']").attr("value", "Show Similar");
            }
        }
    }
    $("td[class^='find-similar'] input").click(findsimilar);

});

function trackLinks(path) {
    try {
        pageTracker._trackPageview(path);
    } catch (err) { }
}

function findsimilar(status) {
    var ids = "";

    // collect all the checked box id
    var checkedcount = 0;
    $("td[class^='find-similar'] input").each(function() {


        if ($(this).attr('checked')) {
            var tid = $(this).attr('id').split('_');
            ids = ids + tid[tid.length - 1] + "+";
            checkedcount++;

        }
        else {
            //only check the first attribute selection which is the third checkbox
            if (status == "init") {
                if (checkedcount == 2) {
                    $(this).attr('checked', true);
                    var tid = $(this).attr('id').split('_');
                    ids = ids + tid[tid.length - 1] + "+";
                }
                status = "ready";
            }
        }
    });

    if (ids == "") {
        $('#lblfindsimilarcount').remove();
        $("input[id*='btnfindsimilar']").attr("disabled", true).css("opacity", 0.3);
        return;
    }
    //uvinet and stocked id
    ids = ids + "1323038";

    //add help icon
    if ($('#divfindsimilarhelp').length == 0) {
        $("input[id*='btnfindsimilar']").after("<div id='divfindsimilarhelp'  style='text-align:left'><div style='float:right;text-align:right;padding-top:5px;'>Per above selection(s)</div><img border='0' id = 'imghelp' src='http://www.mouser.com/Images/help-icon.png' ></img>&nbsp;Show product(s), check and uncheck the boxes above.</div>");
    }

    //add loading icon
    $("input[id*='btnfindsimilar']").before("<img border='0' style='vertical-align:middle' id = 'imgspinner' src='http://www.mouser.com/Images/spinner.gif' />");

    $.ajax({
        url: "/ajax/parametricoutput.ashx",
        data: "N=" + ids,
        success: function(msg) {

            $('#lblfindsimilarcount').remove();
            msg = msg.split(' ')[0];

            if (msg != "0") {
                msg = msg - 1;
            }

            //remove manufacture if count is 0, then remove the first checked attribute if count is 0 again
            if (msg == "0" && status == "ready") {
                //uncheck manufacturer
                if ($("td[class^='find-similar'] input:first").attr('checked')) {
                    $("td[class^='find-similar'] input:first").attr('checked', false);
                    findsimilar("ready");
                }
                //uncheck the first selected attribute		
                else if ($("td[class^='find-similar'] input:eq(2)").attr('checked')) {
                    $("td[class^='find-similar'] input:eq(2)").attr('checked', false);
                    findsimilar("ready");
                }
            }

            if (msg == "0") {
                $("input[id*='btnfindsimilar']").attr("disabled", true).css("opacity", 0.3);
            }
            else {
                $("input[id*='btnfindsimilar']").attr("disabled", false).css("opacity", 1);
            }

            //msg = 	"There are "+msg+" additional stocked parts available based on the above selection(s)";
            $("input[id*='btnfindsimilar']").after("<div id='lblfindsimilarcount' style='text-align:left'><div style='float:right;text-align:right;padding-top:0px;'><span class='red' style='text-align:right'>Found: " + msg + "</span></div><div class='red' style=''>Show Products With Similar Attributes.</div></div>");

            //remove the loading image
            $('#imgspinner').remove();
        },
        error: function(err) { $('#imgspinner').remove(); }

    });
}