(function() {

    // Video handling

    var platformVideos = []; // needs to be populated with the actual platform videos


    var completeScreens = [
        'after-record-and-play',
        'after-save-and-search',
        'after-send-and-share',
        'after-apps'
    ];


    // ASYNCHRONOUS, because if you're lucky it will destroy
    // the flash that calls it, and then try to return into
    // the environment it just destroyed. So that's fun.
    //
    // So if you need to do something AFTER clearVideo,
    // then use the (optional) and_then function argument
    var clearVideo = function(and_then) {
        window.setTimeout(function() {
            
            if(whatIsItVideo.isInserted('video_playback_spot')) {
                whatIsItVideo.uninsertVideo('video_playback_spot');
            }

            $A(platformVideos).each(function(v) {
                if(v.isInserted('multi-play-area')) {
                    v.uninsertVideo('multi-play-area');
                }
            });

            $$('.hot').invoke('removeClassName', 'hot');

            if(and_then) {and_then();}

        }, 50);

        return void(0);
    }


    var popDown = function(evt) {
        clearVideo();

        $('home-video-container').hide();
        $('multi-video-popup').hide();
        
        if(evt) {Event.stop(evt);}

        return false;
    };



    var show_video_complete_screen = function(show_id) {        
        clearVideo(function() {
            $A(completeScreens).each(function(id) {$(id).hide();});
            $(show_id).show();
        });
    };


    // the "Main" video
    var whatIsItVideo = VideoManager({ 
        width:608, height:336, 
        flv: '/media/video/home/echo_homepage_vid_608x336.flv', 
        mp4: '/media/video/home/echo_homepage_vid_608x336_h264.mp4',
        oncomplete: popDown
    });


    // the Platform videos
    var recordAndPlayVideo = VideoManager({
        width: 597, height: 336,
        flv: '/media/video/home/record-and-play-back.flv',
        mp4: '/media/video/home/record-and-play-back.mp4',
        oncomplete: function() {
            show_video_complete_screen('after-record-and-play');
        }
    });

    var saveAndSearchVideo = VideoManager({
        width: 597, height: 336,
        flv: '/media/video/home/save-and-search.flv',
        mp4: '/media/video/home/save-and-search.mp4',
        oncomplete: function() {
            show_video_complete_screen('after-save-and-search');
        }
    });

    var sendAndShareVideo = VideoManager({
        width: 597, height: 336,
        flv: '/media/video/home/send-and-share.flv',
        mp4: '/media/video/home/send-and-share.mp4',
        oncomplete: function() {
            show_video_complete_screen('after-send-and-share');
        }
    });

    var appsVideo = VideoManager({
        width: 597, height: 336,
        flv: '/media/video/home/apps.flv',
        mp4: '/media/video/home/apps.mp4',
        oncomplete: function() {
            show_video_complete_screen('after-apps');
        }
    });

    platformVideos = [
        recordAndPlayVideo,
        saveAndSearchVideo,
        sendAndShareVideo,
        appsVideo
    ];

    $$('.close-video-button').each(function(el) {
        Event.observe(el, 'click', popDown);
    });
 
    Event.observe($('home-video-container'), 'click', function(evt) {
        if($('home-video-container') == Event.element(evt)) {
            popDown(evt);
        }
    });


    var play_multi_video = function(videoManager, button_class) {

        clearVideo(function() {

            $A(completeScreens).each(function(id) {
                $(id).hide();
            });


            $('multi-video-popup').show();    
            $$('.screenholder')[0].relativize();
            
            videoManager.insertVideo('multi-play-area');

            $$('.sprite-button.' + button_class).invoke('addClassName', 'hot');

            return false;        
        });
    };

    // GLOBAL PLAYBACK FUNCTIONS
    // These can be called by onclick handlers in the body of the page

    window.PlayRecordAndPlayBack = function() {
        play_multi_video(recordAndPlayVideo, 'multi-play-record-and-play-back'); 
    }

    window.PlaySaveAndSearch = function() {
        play_multi_video(saveAndSearchVideo, 'multi-play-save-and-search');
    }

    window.PlaySendAndShare = function() {
        play_multi_video(sendAndShareVideo, 'multi-play-send-and-share');
    }
    
    window.PlayApps = function() {
        play_multi_video(appsVideo, 'multi-play-apps');
    }

    /// END GLOBAL PLAYBACK FUNCTIONS

    $$('.multi-pop-button').each(function(el) {
        Event.observe(el, 'click', function() {
            ftGoalTag6284();
			//PlayRecordAndPlayBack();
			return false;
        });
    });

    $$('.multi-play-record-and-play-back').each(function(el) {
        Event.observe(el, 'click', PlayRecordAndPlayBack);
    });

    $$('.multi-play-save-and-search').each(function(el) {
        Event.observe(el, 'click', PlaySaveAndSearch);
    });
       
    $$('.multi-play-send-and-share').each(function(el) {
        Event.observe(el, 'click', PlaySendAndShare);
    });

    $$('.multi-play-apps').each(function(el) {
        Event.observe(el, 'click', PlayApps);
    });

    $$('.watch-button').each(function(el) {
	    
        Event.observe(el, 'click', function() {
			ftGoalTag6255();
            //$('home-video-container').show();
            //whatIsItVideo.insertVideo('video_playback_spot');
            return false;
        });
            
    });

    popDown();
    
    // Effects and fanciness

    $$('.feature-block a').each(function(ln) {

        var glow = $(ln).up('.feature-block').down('.poster-glow');
        Event.observe(ln, 'mouseover', function(evt) {
            glow.show();
        });

        Event.observe(ln, 'mouseout', function(evt) {
            glow.hide();
        });
    });

	/*flashtalking active tracking - Livescribe Home - Video (How does it work)*/
	function ftGoalTag6284(){
		if(document.cookie.match(/(^|;\s*)ldinfo=./)) {
			return; // GO NO FURTHER in LD
		}
		
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix6284 = new Image();
		ftGoalTagPix6284.src = "http://servedby.flashtalking.com/spot/1625;6284;686/?Livescribe Home - Video How does it work" + num;
		ftGoalTagPix6284.onload = ftLoaded6284;
	};
	
	function ftLoaded6284() {
		PlayRecordAndPlayBack();
		//top.location.href="http://www.UrlGoesHere.pdf";
	};
	
	function ftGoalTag6255(){
		if(document.cookie.match(/(^|;\s*)ldinfo=./)) {
			return; // GO NO FURTHER in LD
		}
		
		var ftRand = Math.random() + "";
		var num = ftRand * 1000000000000000000;
		var ftGoalTagPix6255 = new Image();
		ftGoalTagPix6255.src = "http://servedby.flashtalking.com/spot/1625;6255;686/?LH - Video What is a smartpen" + num;
		ftGoalTagPix6255.onload = ftLoaded6255;
	};
			
	function ftLoaded6255() {
		$('home-video-container').show();
        whatIsItVideo.insertVideo('video_playback_spot');
		//top.location.href="http://www.UrlGoesHere.pdf";
	};

})();
