function video_caption(aCaptions, t)
{
    var i;
	var s = '&nbsp;';
	if (aCaptions && aCaptions.length) {
		for(i = aCaptions.length-1; i >= 0; i--) {
			if (t > aCaptions[i][0]) {
				s = aCaptions[i][1];
				break;
			}
		}
	}
	$('#vCaption').show().html(s);
}

function fix_resize() {
    var h = $(window).height();
    var w = $(window).width();
    var gw = (w - 142);
    var gh = (h - 142);

    $('.bg').height(h);
    $('#topGrad,#bottomGrad').width(gw);
    $('#leftGrad,#rightGrad').height(gh);
}
$().ready(function(){
			var $video;
            var $playing=false;

            $('#vPlayer,#vCaption').hide();

fix_resize();$(window).resize(fix_resize);
window.onorientationchange=fix_resize;
window.onrotationchange=fix_resize;
window.ontouchmove=function(ev){if(!ev)ev=event;ev.preventDefault();};//ipad

	// set where the logo is set
	var $outPos = '#center';

	//put everything off screen apart from video
	function outofsight() {
		$('#logo').stop(true,true).animate({marginTop: '-=650px'}, 1600, 'easeOutCubic', function() {} );
		$('.branch1').stop(true,true).animate({marginTop: '-=650px'}, 1600, 'easeOutCubic', function() {} );
	}
	
	function intosight() {
		$('#logo').stop(true,true).animate({marginTop: '+=650px'}, 1600, 'easeOutCubic');
		$('.branch1').stop(true,true).animate({marginTop: '+=650px'}, 1600, 'easeOutCubic');
	}

    var world = {
        'unique': [ -160,-240, -22,-22 ],
        'whatis': [ +250,+160, 22,22 ],
        'ourPosition': [ -260,+160, 44,-44 ]
    };

    function sign(a) { return a < 0 ? '-' : '+'; };
    function abs(a) { return a < 0 ? -a : a; }
	
	// branch clicks
    var x=0,y=0;
    function branch(me) {
        $('#check,.branch1,#logo,#'+me+',#'+me+'sub').stop(true,true);
        var longer =false;

        if($outPos != '#center') {
            // back to home
            $moveCentTop = sign(-y)+'='+abs(y)+'px';
            $moveCentLeft = sign(-x)+'='+abs(x)+'px';
            x=y=0;

            $('.branch1').animate({marginTop: $moveCentTop, marginLeft: $moveCentLeft});
            $('#logo').animate({marginTop: $moveCentTop, marginLeft: $moveCentLeft});
            $($outPos +'sub').stop(true,true).slideUp(400);

            $outPos = '#center';
            longer = true;

            $('.branch1,#logo').delay(200);
        }

        if(world.hasOwnProperty(me)) {
            var w=world[me];
            $moveBg = w[2]+'px '+w[3]+'px';

            y = w[0]; x = w[1];
            $moveOutTop = sign(y) + '=' + abs(y) + 'px';
            $moveOutLeft = sign(x) + '=' + abs(x) + 'px';

            $('.branch1').css('opacity',0.4);
            $('#'+me).css('opacity',1.0);

            $('.branch1').animate({marginTop: $moveOutTop, marginLeft: $moveOutLeft});
            $('#logo').animate({marginTop: $moveOutTop, marginLeft: $moveOutLeft});

            $('#'+me+'sub').delay(longer ? 800 : 200).slideDown(400);
        
            $outPos = '#' + me;
        } else {
            $('.branch1').css('opacity',1);
        }
    }
    $('#logo,.branch1,body').mousedown(function(ev) {
        ev.preventDefault();
        ev.stopPropagation();

        if($playing) {
            video_stop();
            return false;
        }
        var me=this.id;
        if(this==document.body) {
            if($outPos && $outPos !='#center') {
                // check the position of the click; if we're touching the body and we're nowhere near the menu
                // consider we just clicked the center... this seems to make it easier to do stuff
                var da = world[$outPos.substr(1,$outPos.length-1)];
                var dx = (document.body.clientWidth / 2);
                if(da[1] < 0 && ev.pageX > dx) return false;
                if(da[1] > 0 && ev.pageX < dx) return false;
            }
            me='center';
        }

        if($outPos == '#'+me)me='center';
        branch(me);
        return false;
    });

    //close and pause video
    function video_stop() {
        if(!$playing)return;
        video_caption();
        $playing=false;
        $('#vPlayer,#vCaption').fadeOut('slow',function(){
            $('#vPlayer').html('').hide();
            $('#vCaption').hide();
        }); // toss
        $('#vCaption').hide();
        intosight();
        $($outPos+'sub').delay(800).slideDown(400);
    }
	
	window.video_stop = video_stop;

    $('.sublev').mousedown(function() {
        outofsight();
        $(this.parentNode).stop(true,true).slideUp(400);
        var ah = 'play.html#'+this.id;
        if(this.id.indexOf('.html')>-1)ah=this.id;
        var captions = [];
        $(this).find(".captions span").each(function(){
            captions[captions.length]=[
                parseFloat($(this).attr("data")),
                $(this).text()
            ];
        });
	var s = 'scrolling="no"';
	if(this.className.indexOf('scroll') > -1)s='';
        $video = $('<iframe src="telemetry_website/'+ah+'" width="640" height="368" frameborder="0" class="vidpla" seamless="seamless" marginwidth="0" marginheight="0" '+s+' onunload="video_stop();video_caption();" /><a href="javascript:window.video_stop()" style="position:relative;top:-20px;left:630px;color:#aaa;font-family:Helvetica,Arial,Sans-Serif;font-fize:18px">X</a>');
        $video.stop=function(){video_stop();};
        $video.caption=function(t){video_caption(captions,t);}
        try{$video[0].stop=function(){video_stop();};}catch(e){};
        try{$video[0].caption=function(t){video_caption(captions,t);};}catch(e){};

        window.setTimeout(function(){
            $('#vPlayer').html($video).fadeIn('slow');
            $('#vCaption').html('').show();
            $playing=true;
        }, 10);
    });
});

