﻿var shareMode = '';
var lightBoxShareSlug = '';
var linklocation = '';

$(window).ready(function () {
    $(".rtIn").attr("title", "");
});

//$(window).ready(function ()
//{
//    $("a").click(function (event)
//    {
//        if (this.target != "_blank")
//        {
//            event.preventDefault();
//            linklocation = this.href;
//            $("#washout").css({ "opacity": "1.0" });
//            $("#washout").fadeIn(300, RedirectToPage);
//        }
//    });

//    $("#washout").fadeOut(300, function ()
//    {
//        $("#washout").css({ "opacity": "0.9" });
//    });

//    function RedirectToPage()
//    {
//        window.location = linklocation;
//    }
//});

//$(window).bind("beforeunload", function ()
//{
//    $("#washout").fadeIn("fast");
//});

function SetRandomTagline()
{
    var randomnumber = Math.floor(Math.random() * 9);

    if (randomnumber > 4)
    {
        $("#MEYOU").addClass("Tagline1");
    }
    else
    {
        $("#MEYOU").addClass("Tagline2");
    }
}

function FixZindex()
{
    var zIndexNumber = 1000;
    $('div').each(function ()
    {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
}

function MoveFooter(ref)
{
    try
    {
        var contentHeight = $(ref).height() + $(ref).offset().top;
        var navHeight = $("#BelowNav").height() + $("#BelowNav").offset().top;
        var windowHeight = $(window).height();

        //Never come above the secondary Nav
        if (contentHeight < navHeight + 25)
            contentHeight = navHeight + 25;

        //if their window is longer than the content, push the footer down
        if (contentHeight + 350 < windowHeight)
            contentHeight = windowHeight - 350;

        $("#washout").css({ "height": contentHeight + 50 + 275 });

        //    if ($("#AnchoredAccentTiles").length > 0)
        //    {

        //        $("#AnchoredAccentTiles").offset({ top: contentHeight });
        //        $("#SocialWrapper").offset({ top: contentHeight + 226 });
        //        $("#FooterWrapper").offset({ top: contentHeight + 226 });
        //    }
        //    else
        //    {
        $("#SocialWrapper").offset({ top: contentHeight + 50 });
        $("#FooterWrapper").offset({ top: contentHeight + 50 });

        //    }

        $("#FooterWrapper").show();
        $("#SocialWrapper").show();
    }
    catch (err)
    {
    }
}

function ResizeLeftCol()
{
    var wid = ($(window).width() - 960) / 2 + 1;

    $("#left-side-outer").width(wid);
}

function ShowNextImage(id, index, effect, delay)
{
    var currentImage = document.getElementById(id + "Image" + index);

    index++;

    if (index > 5)
        index = 1;

    var nextImage = document.getElementById(id + "Image" + index);

    if (nextImage.src == "" && index == 2 || delay == 0)
    {
        //there's nothing to animated...let's bail.
        return;
    }

    if (nextImage.src == "")
    {
        index = 1;
        nextImage = document.getElementById(id + "Image" + index);
    }

    switch (effect)
    {
        case "fade":
            $(currentImage).fadeOut("slow");
            $(nextImage).fadeIn("slow");
            break;
        case "none":
            $(nextImage).show(0, function ()
            {
                $(currentImage).css({ "z-index": "1" });
                $(nextImage).css({ "z-index": "0" });
            });
            break;
        default:
            var options = {};
            $(currentImage).css({ "z-index": "1" });
            $(nextImage).css({ "z-index": "0" });
            $(nextImage).show();
            $(currentImage).effect(effect, options, 500, function ()
            {
            });
    }

    setTimeout("ShowNextImage('" + id + "'," + index + ",'" + effect + "'," + delay + ")", delay);
}

var videoSlugMap = new Array();

function RegisterVideoSlug(slug, videoID)
{
    if (videoSlugMap[slug] == null)
    {
        videoSlugMap[slug] = videoID;
    }
}

function ShowVideoBySlug(slug)
{
    if (videoSlugMap[slug] != null)
        ShowVideo(videoSlugMap[slug]);
}

function EmailVideo(slug)
{
    var url;

    if (location.hash != "")
        url = window.location.href.substr(0, window.location.href.indexOf('#'))
    else
        url = window.location;

    url = url + "%23" + slug;

    var mailWinLink = 'mailto:?subject=Shared Content from imc2heathandwellness.com: ' + FixQuotes(document.title) + '&body=I found some interesting information on imc2healthandwellness.com (the website for imc2, a strategic and creative agency that works across mass, digital and emerging media) and wanted to share the link below. You can also experience their site by visiting www.imc2healthandwellness.com.  \r\n\r\nEnjoy!  ' + url;

    var shareWin = window.open(mailWinLink, 'mailwinlink');

    if (shareWin && shareWin.open && !shareWin.closed)
        shareWin.close();
}

function FixQuotes(str)
{
    return str.replace("'", "\'").replace("&", "%26").replace("²", "2").replace("—", "-").replace("’", "\'");
}

function ShareVideo(id, slug)
{
    lightBoxShareSlug = slug;
    ShowShareBox($("#LightboxShare" + id), "below", "center", "LightBox");
}

function ShowVideo(videoID)
{
    document.getElementById("VideoPlayer" + videoID).style.top = $(window).scrollTop() + 12 + "px";
    $("#VideoPlayer" + videoID).css({ opacity: 0 });
    $("#VideoPlayer" + videoID).css({ left: "150px" });

    $("#washout").fadeIn("slow", function ()
    {
        //animation complete, start the video fadein
        $("#VideoPlayer" + videoID).animate({ opacity: 1 }, 500, function ()
        {
            //PlayVideo("myExperience" + videoID);
        });
    });

    $("#washout").click(function ()
    {
        HideVideo(videoID);
    });
}

function PlayVideo(experienceID)
{
    try
    {
        var player = brightcove.getExperience(experienceID);
        var videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);

        if (!videoPlayer.isPlaying())
        {
            videoPlayer.play();
        }
    }
    catch (err)
    {
        alert(err);
    }
}

function PauseVideo(experienceID)
{
    try
    {
        var player = brightcove.getExperience(experienceID);
        var videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);

        if (videoPlayer && videoPlayer.isPlaying())
        {
            videoPlayer.pause(true);
        }
        else
        {
            //Brightcove's API doesn't support mobile devices, so we redraw this div to reload (thus pausing) the video.
            var videoID = experienceID.replace("myExperience", "");
            var videoPlayer = document.getElementById("VideoPlayer" + videoID);

            var html = videoPlayer.innerHTML;

            videoPlayer.innerHTML = html;
        }
    }
    catch (err)
    {
        //Brightcove's API doesn't support mobile devices, so we redraw this div to reload (thus pausing) the video.
        var videoID = experienceID.replace("myExperience", "");
        var videoPlayer = document.getElementById("VideoPlayer" + videoID);

        var html = videoPlayer.innerHTML;

        videoPlayer.innerHTML = html;
    }
}

function HideVideo(videoID)
{
    HideSharePage();

    $("#VideoPlayer" + videoID).animate({ opacity: 0 }, 500, function ()
    {
        $("#VideoPlayer" + videoID).css({ left: "-9999px" });
        $("#washout").fadeOut("slow");
        PauseVideo("myExperience" + videoID);
    });        
}

function ShowImageLightbox(imgUrl, detailText)
{
    var img = document.getElementById("LightboxImage");

    document.getElementById("ImageLightbox").style.top = $(window).scrollTop() + 12 + "px";

    img.src = imgUrl;

    $("#washout").fadeIn("slow", function ()
    {
        //animation complete, start the lightbox fadein
        $("#ImageLightbox").fadeIn("fast");
    });

    $("#washout").click(function ()
    {
        CloseImageLightbox();
    });
}

function CloseImageLightbox()
{
    $("#ImageLightbox").fadeOut("fast", function ()
    {
        //animation complete, start the washout fadeout
        $("#washout").fadeOut("slow");
    });
}

function ShowContentLightbox(imgUrl, headerText, bodyText, readMoreLabel, readMoreUrl, readMoreTarget)
{
    //document.getElementById("LightboxImage").src = imgUrl;
    document.getElementById("LightboxTextHeader").innerHTML = headerText;
    document.getElementById("LightboxText").innerHTML = bodyText;

    document.getElementById("ContentLightbox").style.top = $(window).scrollTop() + 12 + "px";

//    $("#body").css('zIndex', 1000);
//    //$("#contentBlock").css('zIndex', 10);
//    $("#washout").css('zIndex', 20);
//    $("#ContentLightbox").css('zIndex', 400);
//    $("#LightboxUtilityBar").css('zIndex', 405);
//    $("#LightboxCloseSingle").css('zIndex', 410);

    $("#LightboxReadMore").attr("href", readMoreUrl);
    $("#LightboxReadMore").attr("target", readMoreTarget);
    $("#LightboxReadMore").html(readMoreLabel);

    $("#washout").fadeIn("slow", function ()
    {
        //animation complete, start the lightbox fadein
        $("#ContentLightbox").fadeIn("fast");
    });

    $("#washout").click(function ()
    {
        CloseContentLightbox();
    });
}

function CloseContentLightbox()
{
    $("#ContentLightbox").fadeOut("fast", function ()
    {
        //animation complete, start the washout fadeout
        $("#washout").fadeOut("slow");
    });
}

function ShowClientLandingLightbox(headerText, bodyText)
{
    document.getElementById("LightboxTextHeader").innerHTML = headerText;
    document.getElementById("LightboxText").innerHTML = bodyText;

    document.getElementById("ClientLightbox").style.top = $(window).scrollTop() + 150 + "px";

    $("#washout").fadeIn("slow", function ()
    {
        //animation complete, start the lightbox fadein
        $("#ClientLightbox").fadeIn("fast");
    });

    $("#washout").click(function ()
    {
        CloseClientLandingLightbox();
    });
}

function CloseClientLandingLightbox()
{
    $("#ClientLightbox").fadeOut("fast", function ()
    {
        //animation complete, start the washout fadeout
        $("#washout").fadeOut("slow");
    });
}

function PrintElement(element, secondaryCSS)
{
    var mywindow = window.open('', 'print_div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>' + document.title + '</title>');
    mywindow.document.write('<link rel="stylesheet" href="/Stylesheets/imc2.css" type="text/css" />');
    mywindow.document.write('<link rel="stylesheet" href="/Stylesheets/' + secondaryCSS + '.css" type="text/css" />');
    mywindow.document.write('</head><body>');
    mywindow.document.write(document.getElementById(element).innerHTML);
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    mywindow.focus();
    mywindow.print();
    mywindow.close();
    return true;
}

function EmailPage()
{
    var mailWinLink = 'mailto:?subject=Shared Content from imc2healthandwellness.com: ' + FixQuotes(document.title) + '&body=I found some interesting information on imc2healthandwellness.com (the website for imc2, a strategic and creative agency that works across mass, digital and emerging media) and wanted to share the link below. You can also experience their site by visiting www.imc2healthandwellness.com.  \r\n\r\nEnjoy!  ' + window.location;

    var shareWin = window.open(mailWinLink, 'mailwinlink');

    if (shareWin && shareWin.open && !shareWin.closed)
        shareWin.close();
}

function PrintPage()
{
    window.print();
}

function ShowShareBox(element, vPos, hPos, mode)
{
    shareMode = mode;

    var eleHeight = element.height();
    var eleWidth = element.width();

    var shareHeight = $("#ShareShow").height();
    var shareWidth = $("#ShareShow").width();

    var eleTop = element.offset().top;
    var eleLeft = element.position().left;

    if (vPos == "above")
    {
        $("#ShareShow").css({ "top": eleTop - shareHeight });
        $("#ShareShowShim").css({ "top": eleTop - shareHeight });
    }
    else if (vPos == "below")
    {
        $("#ShareShow").css({ "top": eleTop + eleHeight });
        $("#ShareShowShim").css({ "top": eleTop + eleHeight });
    }
    else
    {
        return;
    }

    if (hPos == "right")
    {
        $("#ShareShow").css({ "left": eleLeft + (shareWidth - eleWidth) });
        $("#ShareShowShim").css({ "left": eleLeft + (shareWidth - eleWidth) });
    }
    else if (hPos == "left")
    {
        $("#ShareShow").css({ "left": eleLeft });
        $("#ShareShowShim").css({ "left": eleLeft });
    }
    else if (hPos == "center")
    {
        $("#ShareShow").css({ "left": eleLeft - ((shareWidth - eleWidth) / 2) });
        $("#ShareShowShim").css({ "left": eleLeft - ((shareWidth - eleWidth) / 2) });
    }
    else
    {
        return;
    }

    $("#ShareShow").fadeIn("fast");
    $("#ShareShowShim").show();
}

function HideSharePage()
{
    $("#ShareShowShim").hide();
    $("#ShareShow").fadeOut("fast");
}

function ShareLink(d)
{
    var urlfmt;
    var escapeUrl = true;
    var escapeTitle = true;

    switch (d)
    {
        case "FACEBOOK":
            urlfmt = "http://www.facebook.com/sharer.php?u=[URL]&t=[TITLE]";
            escapeTitle = false;
            break;
        case "TWITTER":
            urlfmt = "http://twitter.com/home?status=imc2healthandwellness.com - [TITLE] - [URL]";
            escapeTitle = false;
            escapeUrl = false;
            break;
        case "GOOGLE":
            urlfmt = "https://www.google.com/bookmarks/mark?op=edit&bkmk=[URL]&title=[TITLE]";
            break;
        case "DIGG":
            urlfmt = "http://digg.com/submit?phase=2&url=[URL]&title=[TITLE]";
            break;
        case "DELICIOUS":
            urlfmt = "http://www.delicious.com/save?url=[URL]&title=[TITLE]";
            break;
        case "STUMBLEUPON":
            urlfmt = "http://www.stumbleupon.com/submit?url=[URL]&title=[TITLE]";
            break;
        case "MYSPACE":
            urlfmt = "http://www.myspace.com/Modules/PostTo/Pages/?u=[URL]&title=[TITLE]";
            break;
        case "TUMBLR":
            urlfmt = "http://www.tumblr.com/share?v=3&u=[URL]&t=[TITLE]&s=";
            break;
        case "LINKEDIN":
            urlfmt = "http://www.linkedin.com/shareArticle?mini=true&url=[URL]&title=[TITLE]&source=imc2healthandwellness.com";
            break;
    }

    var shareURL;

    if (shareMode == "LightBox")
    {
        if (window.location.href.indexOf('#') > 0)
            shareURL = window.location.href.substr(0, window.location.href.indexOf('#')) + "#" + lightBoxShareSlug;
        else
            shareURL = window.location + "#" + lightBoxShareSlug;
    }
    else if (shareMode == "Page")
    {
        if (window.location.href.indexOf('#') > 0)
            shareURL = window.location.href.substr(0, window.location.href.indexOf('#'));
        else
            shareURL = window.location.href;
    }
    else
    {
        alert("Invaild ShareMode: " + shareMode);
        return;
    }

    if (escapeUrl)
        urlfmt = urlfmt.replace("[URL]", escape(shareURL));
    else
        urlfmt = urlfmt.replace("[URL]", shareURL);

    if (escapeTitle)
        urlfmt = urlfmt.replace("[TITLE]", escape(document.title));
    else
        urlfmt = urlfmt.replace("[TITLE]", document.title);

    window.open(urlfmt, 'share');

    HideSharePage();
}

function ToggleSection(sectionName)
{
    var expander = document.getElementById(sectionName + "PlusMinus");

    if (expander.innerHTML == "+")
        expander.innerHTML = "-";
    else
        expander.innerHTML = "+";

    $("#" + sectionName).slideToggle(500, function ()
    {
        MoveFooter($(".sfnewsSummary:last"));
    });
}
