var _currentVideoFolder = ""; var tweetInterval = 5000; var int; var videoIsPlaying = false;
function isIE(){return isIE6()||isIE7()||isIE8();}
function isIE6(){return(navigator.appVersion.indexOf("MSIE 6.")>=0);}
function isIE7(){return(navigator.appVersion.indexOf("MSIE 7.")>=0);}
function isIE8(){return(navigator.appVersion.indexOf("MSIE 8.")>=0);}
function isSafari(){return(navigator.appVersion.indexOf("Safari")>=0)&&isChrome()!=true;}
function isChrome(){return(navigator.appVersion.indexOf("Chrome")>=0);}
function isFireFox(){return(navigator.userAgent.indexOf("Firefox")>=0);}
function isWebkit(){return(navigator.appVersion.indexOf("WebKit")>=0);}
function isMobile(){return isIpad()==true||isIphone()==true||isAndroid()==true;}
function isIos(){return isIpad()==true||isIphone()==true;}
function isIpad(){return(navigator.userAgent.indexOf('iPad')>=0);}
function isIphone(){return(navigator.userAgent.indexOf('iPhone')>=0);}
function isAndroid(){return(navigator.userAgent.indexOf('Android')>=0);}

$(document).ready(function () {
    $(".cell").hide(); $(".bubble").hide(); $("#poll-screen").hide(); $(".bubble").css({ "pointer-events": "none" }); $(".bubbleBox").css({ opacity: .9 }); var pathname = $.address.baseURL();
   // alert(pathname);
    if (pathname.match("explore")) {
        $(function () {
            var showControls = false; if ($('#slider1').children().length > 3) { showControls = true; }
            $('#slider1').bxSlider({ displaySlideQty: 3, moveSlideQty: 1, controls: showControls });
        });
    }
  
});

$(window).load(function () {
    var pathname = $.address.baseURL();
    var theVid = document.getElementById("vid");
    if (!isIE() && theVid != null) {
        VideoJS.DOMReady(function () { VideoJS.setup("vid"); });
    }
    var tweets = document.getElementById("tweets");
    if (tweets != null) {
        $(function () {
            $("#tweets").tweet({ avatar_size: 32, count: 100, query: "from:cognizant futureofwork", loading_text: "searching twitter...", refresh_interval: 100 });
        });
        int = self.setInterval("nextTweet()", tweetInterval);
    }

    $("#nav ul li").each(function () {
        if ($(this).hasClass("clickable") != undefined) {
            var url = $(this).find("a").attr("href");
            //alert("url" + url);
            //alert("pathname " + pathname);
            if (pathname.match(url)) {
                if ((url != "/Futureofwork/Pages/landingPage.aspx") && (url != "/Futureofwork") && (url != "landingPage")) {
                    $(this).addClass("selected");
                }
                if (url != undefined) {
                    var urlclass = url.split("/");
                    var cssclass = urlclass[urlclass.length - 1];
                                        
                    //$(".indicator").addClass(url.replace(".aspx", ""));
                    $(".indicator").addClass(cssclass.replace(".aspx", ""));
                }
            }
            $(this).click(function () {
                transitionOut(url);
            });
        };
    });

    $("#nav ul li a").click(function () {
        transitionOut($(this).attr("href"));
        return false;
    })
    $(".content-headline p a").click(function () {
        transitionOut($(this).attr("href"));
        return false;
    })
    transitionIn();
}); 




function nextTweet(){if($(".tweet_list li").length<2){clearInterval(int);$(".tweet_list").html("<li>Twitter seems to be down right now. Please try again soon.</li>");$(".tweet_list li").fadeIn();}else{$(".tweet_list li:first").fadeOut(800,function(){$(".tweet_list li:first").appendTo(".tweet_list");$(".tweet_list li:first").fadeIn(800);});}}
function transitionIn(){$("#loadInfo").hide();var d=1000;$(".cell").show();$(".cell").each(function(index){$quote=$(this).find(".cell-headline");changeFontSize($quote,65,.3);var oPosX=$(this).position();if(oPosX.left<100){$(this).css("left",oPosX.left-1000);}else{$(this).css("left",oPosX.left+1000);}
if(oPosX.top<300){$(this).css("top",oPosX.top-1000);}else{$(this).css("top",oPosX.top+1000);}
var ran=Math.random()*500+index*100;d=ran;$(this).css("visibility","visible");$(this).delay(d).animate({left:oPosX.left,top:oPosX.top},1000,'easeOutBack',function(){});});$("#slider1 li div").each(function(index){$quote=$(this).find(".cell-headline");changeFontSize($quote,22,.2);});$(this).delay(1000).fadeIn(0,function(){$(".video a").click(function(){var url=$(this).attr("href");url=url.replace("#!/video-","");videoClick(url);return false;});$(".video").click(function(){var url=$(this).find("a").attr("href");url=url.replace("#!/video-","");videoClick(url);});$(".video").mousemove(function(e){var pathname=$.address.baseURL();if(pathname.match("explore")){$(".bubble").show();var description=$(this).find(".video-description").text();$(".bubble p").text(description);var bph=$(".bubble p").height();var pos=$(this).offset();var w=$(this).width();var bw=$(".bubble").width();var bd=bw/2
        var bh = $(".bubbleBox").height() + 50; var newX = pos.left + w / 2 - bw / 2 - 15; $(".bubble").delay(50).css({ left: parseInt(e.pageX - bd), top: e.pageY - bh });
    } 
}); $(".video").mouseleave(function (e) { var pos = $(this).offset(); var w = $(this).width(); var h = $(this).height(); if (e.pageX < pos.left || e.pageX > pos.left + w || e.pageY < pos.top || e.pageY > pos.top + h) { $(".bubble p").text(""); $(".bubble").hide(); } }); $(".bx-prev").mouseenter(function (e) { $(".bubble p").text(""); $(".bubble").hide(); }); $(".bx-next").mouseenter(function (e) { $(".bubble p").text(""); $(".bubble").hide(); });

$(".pdf a").click(function () {
    var url = $(this).attr("href");
    transitionOut(url);
    return false;
});

$(".pdf").click(function () {
    var url = $(this).find("a").attr("href");
    transitionOut(url);
});

//Added by Cognizant 
$(".learn-more").click(function () {
    var url = $(this).find("a").attr("href");
    transitionOut(url);
});

$(".poll a").click(function(){pollClick();return false;});$(".poll").click(function(){pollClick();});$.address.change(function(event){checkForOverlay();});checkForOverlay();if($.browser.msie){$(this).attr("filter","")}});}
function changeFontSize(obj,maxSize,perc){$h=$(obj).parent().height();$w=$(obj).parent().width();$sqft=$h*$w*(perc);$size=maxSize;obj.css("fontSize",$size);while(obj.height()*obj.width()>$sqft){obj.css("fontSize",$size);$size--;}
$leading=parseFloat(obj.css("fontSize"))*.975+"px";obj.css("line-height",$leading);}
function transitionOut(url) {
    var filename = location.pathname.substr(location.pathname.lastIndexOf("/") + 1, location.pathname.length);
    var pathname = $.address.baseURL();
    //alert(pathname);
    //alert(filename);
    if (url != filename) {
        $("#loadInfo").show();
        $(".bubble").hide();
        var count = 0;
        $(".cell").each(function (index) {
            var posX = 0;
            var posY = 0;
            var oPosX = $(this).position();
            if (oPosX.left < 100) { posX = oPosX.left - 1000; } else { posX = oPosX.left + 1000; }
            if (oPosX.top < 300) { posY = oPosX.top - 1000; } else { posY = oPosX.top + 1000; }
            var ran = Math.random() * 500 + index * 100;
            d = ran;
            $(this).delay(d).animate({ left: posX, top: posY }, 1000, 'easeInBack', function () {
                count++; if (count == $(".cell").length) {
                if (pathname.match("pages") || pathname.match("Pages")){
                        //alert("auth" + url);
                        window.location = url;
                    }
                    else {
                        //alert("preview");
                        window.location = url.replace(".aspx", "");
                    }                    
                    //alert(url);
                }
            });
        });
    }
    //alert(url);
    if ($(".cell").length < 1) {
        //alert("inside if");
        if (pathname.match("pages") || pathname.match("Pages")) {
            //alert("auth");
            window.location = url;
        }
        else {
            //alert("preview");
            window.location = url.replace(".aspx", "");
        } 
        //alert(window.location);
    }
}
function videoClick(value){$(".bubble p").text("");$(".bubble").hide();$.address.value("video-"+value);}
function closeLightbox(){if(isIE()&&videoIsPlaying){ie_stop_video();}else if(videoIsPlaying){stop_video();}
$("#video-endscreen").hide();$("#poll-screen").hide();$(".lightbox").fadeOut(1000,function(){$(".lightbox").css("top","-5000");$.address.value("");});}

function showEndScreen() {
    var w = $(".video-social").width();
    var h = $(".vjs-poster").height();
    var pos = $(".video-social").offset();
    

    $("#video-endscreen").css({ width: w, height: "400px" })
    $(".video-js-box").hide();
    $.ajax({ type: "GET", url: (_currentVideoFolder + "/endscreen.xml"), dataType: "xml", success: function (xml) {
        $(".endscreen-headline").text($(xml).find('title').text());
        $(".endscreen-headline").css("height", "auto");
        var bullets = "";
        $(xml).find('bullets').each(function () {
            $(this).find("bullet").each(function () {
                bullets += '<li><p>' + $(this).text() + '</p></li>';
            })
        });
        $(".video-bullets").html(bullets);
        $(".video-bullets").css("height", "auto");
        var thumbnails = "";
        $(xml).find('thumbnails thumb').each(function () {
            var path = $(this).attr("imgPath");
            var url = $(this).attr("url");

            var pathname = $.address.baseURL();
            var urlarray = pathname.split("/");
            //alert(urlarray.length);
            var finalurl;
            if (urlarray.length == 4) {
                finalurl = urlarray[0] + "/" + urlarray[1] + "/" + urlarray[2] + "/" + urlarray[3] + "/Pages/landingPage.aspx";
                //alert(finalurl);
            }
            else if (urlarray.length == 5) {
                if (urlarray[4] == "") {
                    finalurl = urlarray[0] + "/" + urlarray[1] + "/" + urlarray[2] + "/" + urlarray[3] + "/Pages/landingPage.aspx";
                    //alert(finalurl);
                }
                else {
                    finalurl = urlarray[0] + "/" + urlarray[1] + "/" + urlarray[2] + "/" + urlarray[3] + "/Pages/" + urlarray[4] + ".aspx";
                    //alert(finalurl);
                }
            }
            else if (urlarray.length == 6) {
                if (urlarray[2].indexOf("auth") == -1) {
                    finalurl = urlarray[0] + "/" + urlarray[1] + "/" + urlarray[2] + "/" + urlarray[3] + "/" + urlarray[5].replace(".aspx", "");
                }
                else {
                    finalurl = pathname;
                }
                //alert(finalurl);
            }

            url = finalurl + url;
            //            if ($.address.baseURL().indexOf("explore") != -1) {
            //                //alert("explore");
            //                url = url.replace("explore", "landingPage");
            //            }
            //            else if ($.address.baseURL().indexOf("landingPage") != -1) {
            //                //alert("else");
            //                url = url.replace("landingPage", "explore");
            //            }
            //            else {
            //                url = url.replace("reinvent", "explore");
            //            }

            ////            alert("url" + url);
            ////            alert("pathname" + $.address.baseURL());
            //            url = $.address.baseURL() + url;
            //            //alert("concatenated url " + url);
            //            if ($.address.baseURL().indexOf("explore") != -1) {
            //                //alert("explore");
            //                url = url.replace("explore", "landingPage");
            //            }
            //            else {
            //                //alert("else");
            //                url = url.replace("landingPage", "explore");
            //            }

            thumbnails += '<li><a href="' + url + '" onclick="RaiseVideoRelatedContentGAEvent(\'' + 'content-clicked:' + url + '&from:' + document.location.href + '\');"><img src="' + path + '" /></a></li>';
        });
        $(".video-images li a").click(function () {
            $("#video-endscreen").hide();
            var url = $(this).attr("href");
            window.location = url;
            window.location.hash = $(this).attr('rel');
            window.location.reload();
            if ($(this).attr("href").match($.address.value())) {
                $("#video-endscreen").hide();
            };
            return false;
        });
        $(".video-images").html(thumbnails);
        $(".video-images").css("height", "auto");
        var morelinks = "<p>";
        $(xml).find('morelinks link').each(function () {
            var copy = $(this).attr("copy");
            var url = $(this).attr("url");

            morelinks += '<a href="' + url + '" onclick="RaiseVideoRelatedContentGAEvent(\'' + 'content-clicked:' + url + '&from:' + document.location.href + '\');">' + copy + '</a><br/>';
        });
        morelinks += "</p>";
        $(".more-links").html(morelinks);
        var related = '<a href="' + $(xml).find('relatedContent').attr("url") + '">See All Related Content</a>'
        $(".video-see-all").html(related);
        $(".more-links").css("height", "auto");
        $(".video-info").css("height", "auto");
    }
    });
    $("#video-endscreen").show();
}
function RaiseVideoRelatedContentGAEvent(docName) {
    _gaq.push(['_trackEvent', 'FoW2', 'related-video', docName]);    

}

function checkForOverlay() {
    var path = $.address.value();
    if (path.match("videos")) {
        path = path.replace("/video-", "");
        path = "/FOW" + path;        
        doPlayVideo(path);
    }
    else if (path.match("poll")) {
        doShowPoll(path);
    }
    else if (path.match("overlay")) {
        doShowOverlay(); 
    }
}

function doPlayVideo(str) {
    videoIsPlaying = true; $(".video-js-box").show();
    $(".lightbox").css("top", 0);
    $(".lightbox").fadeOut(0);
    $(".lightbox").fadeIn(1000);
    $(".addthis_toolbox").show();
    $("#video-endscreen").hide();

    _currentVideoFolder = str;
    if (isIE()) {
        ie_switch_video(str + "/");
    }
    else {
        var theVid = document.getElementById("vid");
        theVid.player.onEnded(function () {
            showEndScreen();
        });        
        switch_video(str + "/");
    }
}

function doShowOverlay() {
    videoIsPlaying = false;
    $("#video-endscreen").hide();
    $(".video-js-box").hide();
    $(".video-social").hide();
    $(".lightbox").css("top", 0);
    $(".lightbox").fadeOut(0);
    $(".lightbox").fadeIn(1000);
    $("#poll-screen").hide();
    $("#overlay-screen").show();
}

function doShowPoll(url){videoIsPlaying=false;$("#video-endscreen").hide();$(".video-js-box").hide();$(".addthis_toolbox").hide();$(".lightbox").css("top",0);$(".lightbox").fadeOut(0);$(".lightbox").fadeIn(1000);$("#poll-screen").show();var colorArray=["#8b211e","#703ae3","#a8a8a8","#be8100","#387a72"]
$(".poll-results li").each(function(index){var perc=$(this).find(".poll-perc").text().replace("%","");perc=parseFloat(perc)+"%";$(this).find(".poll-bar-result").css({backgroundColor:colorArray[index],width:perc});});}
function pollClick(){$.address.value("poll");}
