﻿$(document).ready(function() {
    InitializeGoalNavigation();
    InitializeVideos();
    InitializeTools();
    FormatRelatedArticleThumbnails();    
    InitializeSlideshows();
    AddVideoIcons();
    FixPNGs();
});

var iconsSelector = "div#goal-navigation-icons-container > ul#icons > li:not('.active')";
var defaultHeaderSelector = "div#goal-navigation-header-container > div.active";

function InitializeGoalNavigation() 
{
    $(iconsSelector).hover(
        function() 
        {
            SwapGoal($(this));        
        },
        function() 
        {
            SwapGoal($(this));
        }
    );
}

function SwapGoal(goal)
{
    var iconImg = $(goal).find("img").first();  
    SwapIcon(iconImg);
    
    var headerSelector = "div#goal-navigation-header-container > div#" + $(goal).attr("id") + "-header";    
    var goalHeader = $(headerSelector);
    SwapHeader(goalHeader);    
}

function SwapIcon(img)
{
    var iconImgSrc = $(img).attr("src").toString();
    
    if(iconImgSrc.indexOf("-active.png") >= 0)
    {
        iconImgSrc = iconImgSrc.replace("-active.png", ".png");
    }
    else
    {
        iconImgSrc = iconImgSrc.replace(".png", "-active.png");
    }
 
    $(img).attr("src", iconImgSrc);   
}

function SwapHeader(h)
{
    if($(h).is(":visible")) // hover out
    {
        $(h).hide();
        $(defaultHeaderSelector).show();
    }
    else
    {
        $(defaultHeaderSelector).hide();
        $(h).show();        
    }   
}

function InitializeTools()
{
    $("a#bmc-open").click(function() {
        ToolClicked_Floodlight('body_mass');
        OpenToolDialog($("div#tool-body-mass-calculator"), "Body Mass Index Calculator");             
    });
    
    $("a#bfc-open").click(function() {
        ToolClicked_Floodlight('body_fat');
        OpenToolDialog($("div#tool-body-fat-calculator"), "Body Fat Calculator");             
    });
    
    $("a#hrc-open").click(function() {
        ToolClicked_Floodlight('heart_rate');
        OpenToolDialog($("div#tool-heart-rate-calculator"), "Heart Rate Calculator");             
    });
    
    $("a#prsj-open").click(function() {
        OpenToolDialog($("div#tool-progress-reports"), "Progress Reports and Success Journal");             
    });    
    
    $("a.tagged_download").click(function() {
        var filePath = $(this).attr("href");
        DownloadClicked_Floodlight(filePath);
    });    
}

function OpenToolDialog(tool, caption)
{
    $(tool).dialog(
        {
            autoOpen : true,
            width : 500,
            draggable : false,
            modal : true,
            resizable : false,
            title : caption
        });
}

function ToolClicked_Floodlight(toolID) 
{
    var tagSource = '';
    
    switch(toolID)
    {
        case 'body_mass':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=bodym239;ord=';
            break;
        case 'body_fat':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=bodyf963;ord=';
            break;
        case 'heart_rate':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=heart516;ord=';
            break;
        default: 
            break;    
    }

    if(tagSource != '')
    {
        var axel = Math.random() + "";
        var a = axel * 10000000000000;
        var tag_url = new Image();
        tag_url.src = tagSource + a + '?';
    }
}

function DownloadClicked_Floodlight(fileID)
{
    var tagSource = '';
    
    switch(fileID) 
    {
        case '/downloads/bfl_charts.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=pdfda498;ord=';           
            break;
        case '/downloads/bflnutrition.xls':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=theea041;ord=';           
            break;
        case '/downloads/bfltraining.xls':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=thetr041;ord=';           
            break;
        case '/downloads/dream-list.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=pdfdr901;ord=';           
            break;
        case '/downloads/journey-to-success.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=pdfjo955;ord=';           
            break;
        case '/downloads/12-week-goals.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=pdf12436;ord=';           
            break;
        case '/downloads/day-1-pms.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=power688;ord=';           
            break;
        case '/downloads/day-1-efl.pdf':
            tagSource = 'http://fls.doubleclick.net/activityi;src=2644366;type=easfw329;cat=eatin968;ord=';           
            break;    
        default:
            break;
    }
    
    if(tagSource != '')
    {    
        var axel = Math.random() + "";
        var a = axel * 10000000000000;
        var tag_url = new Image();
        tag_url.src = tagSource + a + '?';
    }
}

var videoContainerFormat = "<div id=\"{VIDEO_ID}-video-container\" src=\"{VIDEO_ID}\"  class=\"video_container\" />";
var videoLoaderFormat = "<div src=\"/images/training/videos/536/{IMAGE_FILE}.jpg\" alt=\"{IMAGE_ALT}\" title=\"{IMAGE_ALT}\" class=\"video_screenshot_loader\" />";
var videoPlayIconFormat = "<img src=\"/images/global/video-play-icon-large.png\" alt=\"Play Video\" title=\"Play Video\" class=\"video_play_icon\" style=\"top: {TOP}px;\" />";
function InitializeVideos()
{
    if($.browser.msie && $.browser.version <= 6) 
    {
        videoPlayIconFormat = videoPlayIconFormat.replace(/.png/g, ".gif");     
    }

    $("span.video").each(function() {
        var currentVideoID = $(this).attr("id");                  
        var currentVideoCaption = $(this).text();

        var videoContainer = videoContainerFormat.replace(/{VIDEO_ID}/g, currentVideoID.replace(/_/g, '-'));
        var video = $(videoContainer).insertAfter($(this));   
                   
        $(this).remove();        
        
        if($.trim(currentVideoCaption).length > 0)
        {
            $(video).before("<h5 class=\"video_caption\">" + currentVideoCaption + "</h5>");        
        }
        
        var videoLoader = videoLoaderFormat.replace(/{IMAGE_FILE}/g, currentVideoID).replace(/{IMAGE_ALT}/g, currentVideoCaption);

        var loader = $(video).append(videoLoader);
      });
      
    var screenshotLoaderCssClass = "video_screenshot_loader";
  
    $("." + screenshotLoaderCssClass).each(function() {
        LoadVideoScreenshot($(this), screenshotLoaderCssClass);       
    });
    
    $("div.video_container").click(function() {
        var videoPlayerID = $(this).attr("id").toString(); 
        var videoFileID = $(this).attr("src").toString();
        videoFileID = videoFileID.replace(/\-/g, '_');         
        
        $(this).empty();
        
        RenderVideoPlayer(videoPlayerID, videoFileID, null);
        
        $(this).unbind('click');
    }); 
}

function LoadVideoScreenshot(loader, loadingCssClass)
{
    var iSrc = $(loader).attr("src");
    var iAlt = $(loader).attr("alt");
    
    var img = new Image();
    
    $(img)
        .load(function() {
            $(this).hide();
            
            var parent = $(loader).parent();
            
            $(loader).replaceWith(this);           

            var height = $(this).outerHeight();
            var width = $(this).outerWidth();
            
            $(parent).css({'height' : height + 'px', 'width' : width + 'px'});
            
            $(this).show();   
            
            var playIconOffset = ((height / 2) - 42);            
            var playIcon = videoPlayIconFormat.replace(/{TOP}/g, playIconOffset);
            $(parent).append(playIcon);
        })    
        .attr({
            'src' : iSrc,
            'alt': (iAlt != undefined) ? iAlt : '',
            'title' : (iAlt != undefined) ? iAlt : '',
            'class' : 'video_screenshot'
        }); 

}


function FormatRelatedArticleThumbnails()
{
    var thumbnailSelector = "div#related-articles > ul > li";

    $(thumbnailSelector + ":nth-child(4n)").css("padding-right", "0");
}

function InitializeSlideshows()
{
    $("table.article_slideshow td.images > ul > li:first").addClass("active_slide");
    
    $("table.article_slideshow td.previous > a").click(function() {
        var slideshow = $(this).parents("table.article_slideshow").find("td.images > ul");
        
        var itemToShow = $(slideshow).children("li.active_slide").prev();
        
        if(itemToShow.length > 0)
        {       
            $(".active_slide").removeClass("active_slide");
            $(itemToShow).addClass("active_slide");
        }
    });
    
    $("table.article_slideshow td.next > a").click(function() {
        var slideshow = $(this).parents("table.article_slideshow").find("td.images > ul");
        
        var itemToShow = $(slideshow).children("li.active_slide").next();
        
        if(itemToShow.length > 0)
        {       
            $(".active_slide").removeClass("active_slide");
            $(itemToShow).addClass("active_slide");
        }
    });
}

function FixPNGs()
{
    $("ul#related-products img").ifixpng();
}

function AddVideoIcons()
{
    var largeIcon = "<a class=\"video_icon\" href=\"{url}\"><img src=\"/images/global/video-play-icon-large.png\" alt=\"Video\" title=\"Video\" /></a>";
    var smallIcon = "<a class=\"video_icon\" href=\"{url}\"><img class=\"video_icon\" src=\"/images/global/video-play-icon-small.png\" alt=\"Video\" title=\"Video\" /></a>";

    if($.browser.msie && $.browser.version <= 6) 
    {
        largeIcon = largeIcon.replace(/.png/g, ".gif");   
        smallIcon = smallIcon.replace(/.png/g, ".gif");   
    }


    var featuredArticleSelector = "div#featured-article.video";

    var featuredIconOffetTop = (parseInt($(featuredArticleSelector).outerHeight()) / 2) - 42;    
    var featuredIconOffsetLeft = (parseInt($(featuredArticleSelector).outerWidth()) / 2) - 42;

    var featuredLinkUrl = $(featuredArticleSelector).find("div#featured-link > a").attr("href");
    
    var featuredIcon = $(largeIcon.replace(/{url}/g, featuredLinkUrl)).appendTo(featuredArticleSelector);
    
    $(featuredIcon).css("top", featuredIconOffetTop + "px");
    $(featuredIcon).css("left", featuredIconOffsetLeft + "px");
    
    $("div.article_thumbnail_image_container.video").each(function() {
        var currentItemUrl = $(this).parents("li").find("div.article_thumbnail_snippet_container > h3 > a").attr("href");
    
        $(this).append(smallIcon.replace(/{url}/g, currentItemUrl));
    });   

}
