﻿$(document).ready(function () {
    var v = document.createElement("video");
    var timer;
    var buttonID;
    var currID;
    var loadVids;
    var currURL = document.URL;
	var countdown;

    // load initial video
    if ($.browser.msie) {
        $('.video-container').html('<div id="flashContent"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="960" height="576" id="about" align="middle"><param name="movie" value="network-flash/about.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="transparent" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="network-flash/about.swf" width="960" height="576"><param name="movie" value="network-flash/about.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="transparent" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--<![endif]--><a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object></div>');
        var flashObj = document.getElementById("about"); // id passed as a string
    }
    else {
        window.addEventListener("load", function () {
            loadVideos("vid1");
        }, false);
    }

    if (currURL.indexOf("c=true") != -1) {
        $(".video-container").css("top", "-800px");

        // determine where the user is coming from and what to show
        // load about from other interior pages
        if (!v.play || $.browser.msie /*|| $.browser.opera*/) {
            ///$("#flowplayer").flowplayer(0).pause();
        }
        else {
            var videoHasLoaded = setInterval(function () {
                var html = $("#demo-video").html();
                if (html != null) {
                    clearInterval(videoHasLoaded);
                    var vid = document.getElementById("demo-video");
                    vid.pause();
                }
            }, 100);
        }
    }
    else if (currURL.indexOf("v=true") != -1) {
        // load video from other interior pages
        $("#breadcrumbs").hide();
        $("#content-header-top").hide();
        $("#content-container").hide();
        $(".video-container").show().css("top", "-800px");
        $(".return").attr("src", "images/view-full-site-btn.png").removeClass("return").addClass("view-full-site");
        setTimeout(function () {
            $(".video-container").animate({ top: "0" }, 2000);
        }, 1000);
		$("#video-instructions").fadeIn("slow");
    }
    else {
        // intial load
        $(".video-container").show();
        $("#breadcrumbs").hide();
        $("#content-header-top").hide();
        $("#content-container").hide();
        $(".return").attr("src", "images/view-full-site-btn.png").removeClass("return").addClass("view-full-site");
		$("#video-instructions").fadeIn("slow");
    }

    function killTimer() {
        clearInterval(timer);
    }
	
    function loadVideos(containerID) {
        var poster = "videos/" + containerID + ".jpg";
        var mp4Vid = "videos/" + containerID + ".mp4";
        var webmVid = "videos/" + containerID + ".webm";
        var ogvVid = "videos/" + containerID + ".theroa.ogv";
        var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  

        if (!v.play || $.browser.msie /*|| $.browser.opera*/) 
    	{
        }
        //iPhone / iPad vids
        else if (mobile) {	
        	$("#video-instructions").hide();
	        var iPhoneVid = "videos/" + containerID + ".iphone.mp4";
        	$("#" + containerID).append("<video id='demo-video' poster='" + poster + "' height='540' width='960' controls='controls'>" +
                "<source src='" + iPhoneVid + "' type='video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;'></source></video>").fadeIn(1000);


            var vid = document.getElementById("demo-video");
            // stop video and show popup
            timer = setInterval(function () {
                if (Math.round(vid.currentTime) == Math.round(vid.duration)) {
                    $("video").remove();
                    $(".map").show();
                    $(".popup").fadeIn(1000, function() {
						for(var i = 0; i < 10; i++) {
						  $('.clock').attr('src', 'images/videos/clock.gif?' + i);
						}
					});

                    // close popup and load next video
                    loadVids = setTimeout(function () {
                        $(".popup").hide();
                        $(".map").delay(1000).hide();
                        $(".videos").delay(1000).hide();
						

                        if (containerID == "vid1") {
                            $("#network-story img").attr("src", "images/network-story-off.png");
                            $("#solutions img").attr("src", "images/solutions-on.png");
                            loadVideos("vid2");
							$("#demo-video").get(0).play();

                        }
                        else if (containerID == "vid2") {
                            $("#solutions img").attr("src", "images/solutions-off.png");
                            $("#meet-the-members img").attr("src", "images/meet-the-members-on.png");
                            loadVideos("vid3");
							$("#demo-video").get(0).play();

                        }
                        else if (containerID == "vid3") {
                            $("#meet-the-members img").attr("src", "images/meet-the-members-off.png");
                            $("#credentials img").attr("src", "images/rightchoice-on.png");
                            loadVideos("vid4");
							$("#demo-video").get(0).play();

                        }
                        else {
                            $("#credentials img").attr("src", "images/rightchoice-off.png");
                            $("#network-story img").attr("src", "images/network-story-on.png");
                            loadVideos("vid1");
                        }
                    }, 10000);

                    killTimer();
                }
            }, 1000);
        }
        else {

            $("#" + containerID).append("<video id='demo-video' poster='" + poster + "' height='540' width='960' controls='controls'>" +
                "<source src='" + mp4Vid + "' type='video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;'></source>" +
                "<source src='" + webmVid + "' type='video/webm; codecs=&quot;vp8, vorbis&quot;'></source>" +
                "<source src='" + ogvVid + "' type='video/ogg; codecs=&quot;theora, vorbis&quot;'></source>" +
                "</video>").fadeIn(1000);

            var vid = document.getElementById("demo-video");
            // stop video and show popup
            timer = setInterval(function () {
                if (Math.round(vid.currentTime) == Math.round(vid.duration)) {
                    $("video").remove();
                    $(".map").show();
                    $(".popup").fadeIn(1000, function() {
						for(var i = 0; i < 10; i++) {
						  $('.clock').attr('src', 'images/videos/clock.gif?' + i);
						}
					});

                    // close popup and load next video
                    loadVids = setTimeout(function () {
                        $(".popup").hide();
                        $(".map").delay(1000).hide();
                        $(".videos").delay(1000).hide();
						

                        if (containerID == "vid1") {
                            $("#network-story img").attr("src", "images/network-story-off.png");
                            $("#solutions img").attr("src", "images/solutions-on.png");
                            loadVideos("vid2");
							$("#demo-video").get(0).play();

                        }
                        else if (containerID == "vid2") {
                            $("#solutions img").attr("src", "images/solutions-off.png");
                            $("#meet-the-members img").attr("src", "images/meet-the-members-on.png");
                            loadVideos("vid3");
							$("#demo-video").get(0).play();

                        }
                        else if (containerID == "vid3") {
                            $("#meet-the-members img").attr("src", "images/meet-the-members-off.png");
                            $("#credentials img").attr("src", "images/rightchoice-on.png");
                            loadVideos("vid4");
							$("#demo-video").get(0).play();

                        }
                        else {
                            $("#credentials img").attr("src", "images/rightchoice-off.png");
                            $("#network-story img").attr("src", "images/network-story-on.png");
                            loadVideos("vid1");
                        }
                    }, 10000);

                    killTimer();
                }
            }, 1000);
        }
    }
    
    $(".resume-btn img").click(function () {
    
		if (!v.play || $.browser.msie /*|| $.browser.opera*/) 
		{
	    }
	    else 
	    {
		    currID = $(this).parent().parent().parent().parent().attr("id");
	        killTimer();
	        clearTimeout(loadVids);
	
	        $(".popup").hide();
	        $(".map").hide();
	        $(".videos").hide();
	
	        if (currID == "vid1") {
	            $("#network-story img").attr("src", "images/network-story-off.png");
	            $("#solutions img").attr("src", "images/solutions-on.png");
	            loadVideos("vid2");
	            $("#demo-video").get(0).play();
	        }
	        else if (currID == "vid2") {
	            $("#solutions img").attr("src", "images/solutions-off.png");
	            $("#meet-the-members img").attr("src", "images/meet-the-members-on.png");
	            loadVideos("vid3");
	            $("#demo-video").get(0).play();
	        }
	        else if (currID == "vid3") {
	            $("#meet-the-members img").attr("src", "images/meet-the-members-off.png");
	            $("#credentials img").attr("src", "images/rightchoice-on.png");
	            loadVideos("vid4");
	            $("#demo-video").get(0).play();
	        }
	        else {
				$(".video-container").animate({ top: "-800px" }, 2000);
				$("#wrapper").addClass("hidden"); //crm edit
	            setTimeout(function () {
	                $(".video-container").hide();
	                $("#content-header-top").show().css("top", "800px").animate({ top: "0" }, 2000);
	                $("#content-container").show().css("top", "800px").animate({ top: "0" }, 2000);
	                $(".view-full-site").attr("src", "images/top-bar-return.png").addClass("return").removeClass("view-full-site"); //.show() crm edit, moved to 2nd timeout function
	            }, 2000);
	            setTimeout(function () { 
					$("#breadcrumbs").fadeIn("slow");
					$("#wrapper").removeClass("hidden"); //crm edit 
					$(".view-full-site").attr("src", "images/top-bar-return.png").addClass("return").removeClass("view-full-site");
				}, 4000);
	        }
	    }
    });
    
    $(".button img").click(function () {
	    $('#video-instructions').hide();//NEW January 5 2012 CRM
        if ($(this).attr("src").indexOf("-on") == -1) {
			killTimer();
			clearTimeout(loadVids);
            buttonID = $(this).parent().parent().attr("id");
            $("#network-story img").attr("src", "images/network-story-off.png");
            $("#solutions img").attr("src", "images/solutions-off.png");
            $("#meet-the-members img").attr("src", "images/meet-the-members-off.png");
            $("#credentials img").attr("src", "images/rightchoice-off.png");
            $(this).attr("src", "images/" + buttonID + "-on.png");
            
            if (!v.play || $.browser.msie /*|| $.browser.opera*/) {
            }
            else {
	            $("video").remove();
	            $(".videos").hide();
	            
	            if (buttonID == "network-story") {
		            loadVideos("vid1");
					$("#demo-video")[0].play();
	            }
	            else if (buttonID == "solutions") {
	                loadVideos("vid2");
					$("#demo-video")[0].play();
	            }
	            else if (buttonID == "meet-the-members") {
	                loadVideos("vid3");
					$("#demo-video")[0].play();
	            }
	            else {
	                loadVideos("vid4");
					$("#demo-video")[0].play();
	            }
            }
        } 
    });
    
	  $("#top-bar a img").click(function () {
        if ($(this).hasClass("view-full-site")) {
            var vid = document.getElementById("demo-video");
            //alert(vid);
            if (!v.play || $.browser.msie /*|| $.browser.opera*/) {
	            flashObj.GotoFrame(0);
            }
            else if (vid != null) {
	             vid.pause();
             }
             else {
             	killTimer();
				clearTimeout(loadVids);
             }
            $(".video-container").animate({ top: "-800px" }, 2000);
			$("#wrapper").addClass("hidden"); //crm edit
            setTimeout(function () {
                $(".video-container").hide();
                $("#content-header-top").show().css("top", "800px").animate({ top: "0" }, 2000);
                $("#content-container").show().css("top", "800px").animate({ top: "0" }, 2000);
                $(".view-full-site").attr("src", "images/top-bar-return.png").addClass("return").removeClass("view-full-site"); //.show() crm edit, moved to 2nd timeout function
            }, 2000);
            setTimeout(function () { 
				$("#breadcrumbs").fadeIn("slow");
				$("#wrapper").removeClass("hidden"); //crm edit 
				$(".view-full-site").attr("src", "images/top-bar-return.png").addClass("return").removeClass("view-full-site");
			}, 4000);
        }
        else {
			//reset video to beginning 
			//var video = document.getElementsByTagName("video")[0];
			//video.load();
			$("#wrapper").addClass("hidden"); //crm edit
            $("#breadcrumbs").fadeOut("fast");
            $("#content-header-top").animate({ top: "800px" }, 2000);
            $("#content-container").animate({ top: "800px" }, 2000);
            setTimeout(function () {
                $("#content-header-top").hide();
                $("#content-container").hide();
                $(".video-container").show().animate({ top: "0" }, 2000);
                $(".return").attr("src", "images/view-full-site-btn.png").removeClass("return").addClass("view-full-site"); //.show() crm edit
				$("#wrapper").removeClass("hidden"); //crm edit 
            }, 2000);
        	var isPopup = $('.popup').css("display");
        	if (isPopup == 'none') {
        		$("#video-instructions").fadeIn("slow");
        	}
        	else {
        		$('.timer').hide();
        	}    
        }
    });
	
	$(".videos").click(function () {
		$("#video-instructions").hide();
	});
});
