jQuery(function($) {
    $(".latestBlogs").vAlign();
    InitCartoons();
    InitLatestOpinions();
    //$('.pnlRotator, .pnlLatestNews>div').equalHeights();
    
    var $ = jQuery;
    
    // Triple-DL clicks
    if($("#rotator-tabs-wrapper").length > 0) {
        var tabs = $("#rotator-tabs-wrapper a");
        
        tabs.click(function(){
            var n = parseInt( tabs.index($(this)) ) + 1;
            trackHomePageEvent("Triple DL Click", "DL tab " + n);
        });
    }
    
    // Latest News clicks
    if($(".module-latest-news").length > 0) {
        $(".module-latest-news .latest-news-controls a").click(function(){
            trackHomePageEvent("Latest News Slider Click", $(this).text());
        });
    }
    
    // Thumbnail Slider clicks
    if($(".module-article-slider").length > 0) {
        $(".module-article-slider .controls a").live("click", function() {
            trackHomePageEvent("Thumbnail Slider Click", $(this).text());
        });
    }
    
    // Article List tab clicks
    if($(".article-stack").length > 0) {
        $(".article-stack .tabs a").click(function(){
            trackHomePageEvent("Article List Tab Click", $(this).text());
        });
    }
    
    // Tabbed Shell tab clicks
    if($(".module-tabbed-shell").length > 0) {
        $(".module-tabbed-shell .module-tabs a").click(function(){
            trackHomePageEvent("Tabbed Shell Tab Click", $(this).find("span").text());
        });
    }
    
    // Cartoon clicks
    if($(".module-cartoons").length > 0) {
        $(".module-cartoons .buttons a").click(function(){
            trackHomePageEvent("Cartoons Slider Click", $(this).text());
        });
    }
    
    // Slideshow clicks
    if($(".module-slideshow").length > 0) {
        $(".module-slideshow .controls a").live("click", function(){
            trackHomePageEvent("Slideshow Click", $(this).text());
        });
    }
    
    // Partner Poll clicks
    if($(".module-partner-poll").length > 0) {
        $(".module-partner-poll ul li .poll-intro > a").live("click", function(){
            var btn = ( $(this).hasClass("ppoll-next") ) ? "Next" : "Previous" ;
            trackHomePageEvent("Partner Poll Slider Click", btn);
        });
    }
    
    // Quote Of The Day clicks
    if($(".module-quote-of-the-day").length > 0) {
        $(".module-quote-of-the-day .buttons a").click(function(){
            trackHomePageEvent("Quote Of The Day Click", $(this).attr("href"));
        });
    }
    
    // 5 Questions clicks
    if($(".module-5-questions").length > 0) {
        $(".module-5-questions .paging a").live("click", function(){
            trackHomePageEvent("5 Questions For Click", $(this).text());
        });
    }
    
});


function trackHomePageEvent(module_label, clicked_item_label) {
    if(typeof trackEvent === "function") {
        trackEvent(document.title, module_label, clicked_item_label);
    }
}

function InitQuestionsPager(index) {
    
    $('a.qaPager').each(function(i) {
        var ele = $(this);
        var txtPagerIndex = $('#' + $$('hdnQuestionsPagerIndex').attr('id'));
        var currentIndex = txtPagerIndex.val();
        
        ele.text(parseInt(ele.attr('rel')) + 1);

        if (ele.attr('rel') == currentIndex)
            ele.wrapInner('<span>' + '</span>');

        ele.click(function() {
            var rotator = $find($(".rotQuestions").attr("id"));
            var clickedIndex = ele.attr('rel');
            currentIndex = txtPagerIndex.val();

            if (clickedIndex > currentIndex) {
                for (var i = currentIndex; i < clickedIndex; i++) {
                    rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
                }
                txtPagerIndex.val(i);
            }
            else {
                for (var i = currentIndex; i > clickedIndex; i--) {
                    rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Right);
                }
                txtPagerIndex.val(i);
            }

            $('a.qaPager').each(function(ii) {
                var item = $(this);
                item.text(parseInt(item.attr('rel')) + 1);
            });

            ele.wrapInner('<span>' + '</span>');
        });
    });

}
function InitCartoons() {
    $("#pager-cartoons-prev").click(function () {
        var rotator = $find($$('rotCartoons').attr('id'));
        rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Right);
    });
    $("#pager-cartoons-next").click(function() {
        var rotator = $find($$('rotCartoons').attr('id'));
        rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
    });
}
function InitLatestOpinions() {
    $("#pager-latest-op").click(function() {
        var rotator = $find($$('rotLatestOpinions').attr('id'));
        rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
    });
}
