﻿//var sPageName;
$(document).ready(function() {

    //sPageName = getPageName(false);
    Sys.Application.add_load(function() {
        $(".b2-theme").each(function() {
            attachtracking($(this));
        });
        $(".a2-theme").each(function() {
            attachtracking($(this));
        });
        $(".a1-theme").each(function() {
            attachtracking($(this));
        });
    });
    $(".ui-tabcontrol").each(function() {
        var tab = new ui.TabControl($(this), function(that, index) {
            if (that.contents.children().eq(index - 1).children("[id!='']:first-child").length > 0) {
                var id = that.contents.children().eq(index - 1).children(":first-child").attr("id");
                $("#" + id + " .inner-product-wrapper").removeAttr("style");
                ui.equaliseByRow($("#" + id), { container: 'product', rowItemClass: 'inner-product-wrapper', setQL: true });
            }
        });
    });

    //Home page and Department Ask Expert Button redirect
    $('#ask-expert-button').click(function() {
        tempAsk = $('#TextBoxAsk').val();
        $('#ask-expert-button').attr('href', '/ResearchCentre/AskAnExpertSearch.aspx?scrit=' + tempAsk);
    });

    
    //Department Enter handler
    $("#TextBoxAsk").keydown(function(e) {
        if (e.which == 13) {
            e.preventDefault();
        }
    }).keyup(function(e) {

        // trigger on enter
        if (e.which == 13) {
            e.preventDefault();
            tempAsk = $('#TextBoxAsk').val();
            window.location = "/ResearchCentre/AskAnExpertSearch.aspx?scrit=" + tempAsk;
        }
    });




    //Home page footer banner logic
    $('#ty_forum_button').mouseover(function() {
        $("#forum_div").show();
    });

    $('#ty_blog_button').mouseover(function() {
        $("#blog_div").show();
    });

    $('#ty_forum_button').mouseout(function() {
        $("#forum_div").hide();
    });
    $('#ty_blog_button').mouseout(function() {
        $("#blog_div").hide();
    });


    //Hide article if empty
    if ($('.tech-right .article-headline').text() == "") {
        $('.tech-right').hide();
    }

    //Home page articles resize
    $(this).find('.TechSpotLight-content').each(function() {

        var articleText = $(this).find('.article-body-text').text();
        var articleTitle = $(this).find('.article-headline a').text();
        //console.log(articleTitle);

        if (articleTitle.length < 54 && articleText.length > 85) {

            articleText = articleText.substring(0, 85);
            $(this).find('.article-body-text').text(articleText + '...');
        }
        else {
            articleText = articleText.substring(0, 60);
            $(this).find('.article-body-text').text(articleText + '...');
        }

        if (articleTitle.length > 62) {
            articleTitle = articleTitle.substring(0, 62);
            $(this).find('.article-headline a').text(articleTitle + '...');
        }

    });

    $(".home-page-article .TechSpotLight-content:first").css("width", "294px");
    $(".home-page-article .article-read-more:first").css("width", "294px");

    $(".tech-spot-divider:last").text("");

    $(this).find('.supporting-info').each(function() {
        var tmpImg = $(this).find('img:first').clone().removeAttr('align').removeAttr('width').removeAttr('height');
        $(this).find('img').remove();
        $(this).prepend(tmpImg);

        // remove empty paragraphs
        $(this).find('p').each(function() {
            if ($(this).html() == "" || $(this).html() == "&nbsp;") {
                $(this).remove();
            }
        });

    });


});

function attachtracking(obj) {
    $(obj).find("a[href*=product]").each(function() {

        // Get language
        var url = $(this).attr("href");
        var lang = url.match(/(en|fr)-CA/gi)[0].replace("-CA", "");

        // Get Sku
        var sku = url.substring(url.lastIndexOf('/') + 1);
        sku = sku.substring(0, sku.lastIndexOf('.'));

        // Get Title
        var titlearr = url.split("/");
        var title = (titlearr.length > 3) ? titlearr[3].replace(/\-/g, "") : "";

        //sPageName = (sPageName == "home" || sPageName == "accueil") ? "home" : sPageName;
        //$(this).attr("href", url + "&lid=fp-" + sku + "-" + title + "-" + lang);
        $(this).attr("href", url + "&lid=fp-" + sku + "-" + title.slice(0, 30) + "-" + lang);
        
        //$(this).click(function() {        
        //    trackEvent.event('event.link', { lid: "fp-" + sku + "-" + title.slice(0, 20) + "-" + lang });
        //});
    });
}

// Param: ext - Keep file extension or not
function getPageName(ext) {
    ext = typeof (ext) != 'undefined' ? ext : false; 
    
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    if (!ext) {
        sPage = sPage.substring(0, sPage.lastIndexOf('.'));
    }
    return sPage;
}




