var nav_start_top;
var navLinksInViewport = [];

$(document).ready(function() {
        if(!Modernizr.video.h264 || $('html').hasClass('ie9') ) {
            $('.article .video').each( function(i, elem) {
                var video = $(elem).children();
                var videoUrl = $(video).attr('src');
                var videoWidth = $(video).attr('width');
                var videoHeight = $(video).attr('height');
                var splashImg = $(video).attr('poster');

                $(elem).css('width', videoWidth+'px');
                $(elem).css('height', videoHeight+'px');
                $(elem).html('<img src="' + splashImg + '" width="' + videoWidth + '" height="' + videoHeight + '" alt="" />');

                 var player = $f($(elem).attr('id'), "lib/js/flowplayer/flowplayer-3.1.5.swf", {
                                                        clip: {
                                                            autoPlay:  true,
                                                            width:  videoWidth+'px',
                                                            height: videoHeight+'px', 
                                                            onStart:  function(e) {
                                                                    pageTracker._trackEvent('ISSUE 2010 04 Videos', 'Click', 'Video #'+ (i+1));
                                                                }
                                                            },
                                                        playlist: [videoUrl]
                                                        });
            });
        }else {
            /*$('.article .video').click( function(e) {
                try {
                    var video = this.getElementsByTagName('video')[0];
                    video.play();
                }catch(e){alert(e.message);}
            });*/
        }
	
	nav_start_top = $('#nav').position().top;


	//append "back to top links" to each article
	var last_article_id = "";
	$(".article").each(function() {
		$(this).append($('#article_footer').html());	
		last_article_id = $(this).attr("id");
	});
	$('#'+last_article_id).addClass('last');
	
	//attach scrollTo to each #link
	$("a[href^=#]").each(function() { 
		$(this).click(function() { 
			$(window).unbind('scroll');
			$.scrollTo($(this).attr('href'), 200, {offset:-40,onAfter:bindwindowscroll}); 
			$('#nav > li').each(function(){ $(this).removeClass('active'); });
			$('#nav li a[href='+$(this).attr('href')+']').parents("li").addClass('active');
			return false; 
			}
		);
	});

	//attach lightbox to all rel=lightbox links
	var boxen = [];
	//find all links w/ rel=lightbox[...
	$('a[rel*=lightbox]').each(function() {
		//push only unique lightbox[gallery_name] into boxen array
		if ($.inArray($(this).attr('rel'),boxen)) boxen.unshift($(this).attr('rel'));
	});
	$(boxen).each(function(i,val) { $('a[rel='+val+']').lightBox({overlayBgColor: '#060910',overlayOpacity:'0.95',imageBtnClose:'lib/images/web/lightbox-close-img.png',imageBtnPrev:'lib/images/web/lightbox-btn-prev.png',fixedNavigation: true,imageBtnNext:'lib/images/web/lightbox-btn-next.png',imageLoading:'lib/images/web/ajax-loader.gif'}); });
	
	//fetch navlinks for auto-nav highlighting
	$('#nav li a').each(function() { navLinksInViewport = navLinksInViewport + $(this).attr('href') + ":in-viewport,"; } );
	navLinksInViewport = navLinksInViewport.substring(0,navLinksInViewport.length-1);
	
	//set nav selected state when coming from a direct link to an anchor,
	//like foo.com/#article3
	if(window.location.hash) {
		setNav(window.location.hash);
	}	

	bindwindowscroll();
	
});

var setAutoNav = function() {
		var winHalf = $(window).height()/2;
		$(navLinksInViewport).each(function() {
				var fromTop = -($(window).scrollTop()-$(this).position().top);
				//console.log(fromTop);
				var articleID = $(this).attr("id");
				if (fromTop < winHalf) {
					setNav('#' + articleID);
				}
			});
	};
	
var bindwindowscroll = function() {
		ie6nav();
		$(window).bind("scroll", function(event) {
			if ($.browser.msie && $.browser.version == "6.0") {
				$(window).scroll(ie6nav);
			}
			if (!($.browser.msie && $.browser.version <= 7)) {
				setAutoNav();
			} 
		});
	};

var ie6nav = function() {
	if ($.browser.msie && $.browser.version == "6.0") {
		$('#nav').animate({top: (nav_start_top+$(window).scrollTop())+'px'},0);
	}
};

var openLightbox = function(lightbox) {
	$('a[rel*='+lightbox+'] img').eq(0).parent().trigger("click");
};

var setNav = function (hash) {
	$('#nav li.active').removeClass('active');
	$('#nav a[href='+hash+']').parent().addClass('active');
};

