(function() {
	var $j = jQuery.noConflict();
	var log = new FRD.logging.Log('BTE');
	
	// Array Remove - By John Resig (MIT Licensed)
	Array.prototype.remove = function(from, to) {
		var rest = this.slice((to || from) + 1 || this.length);
		this.length = from < 0 ? this.length + from : from;
		return this.push.apply(this, rest);
	};

	$j(document).ready(function() {
	
		collapseNews();
	
		if (window.loadAdmin) loadAdmin();

		window.videoEnded = videoEnded;
		window.playList = false;
		window.activeVideo = false;
		
		$j('#content a.video').each(prepareVideoLink);
		$j('#content a.news').each(prepareNewsLink);
		
		$j('a#introButton').bind('click', showIntro);
		$j('a#newsButton').bind('click', showHeadlines);
		$j('a#playAllButton').bind('click', playAllVideos);
		
		$j('a.nav').bind('click', collapseNews);
		
		var fragmentStart = document.location.href.indexOf("#");
		if (fragmentStart > -1 && document.location.href.length > fragmentStart) {
			var fragment = document.location.href.substring(fragmentStart);
			$j("#content a.video[href='" + fragment + "']").click();
			if (fragment.length > 1) showNews(fragment.substring(1));
		}
		
		if (!window.activeVideo) playAllVideos();

	});
	
	
	var prepareVideoLink = function(i, it) {
	
		var link = $j(it);
		var mediaUrl = link.parent().find('a[name]').attr('name');
		log.debug("Preparing media URL: " + mediaUrl);

		link.bind('click', function() {
			if (playList && playList.length > 0) {
				if (playList[0] != this) cancelPlayAll();
				else playList.remove(0);
			}
			var previous = window.activeVideo;
			window.activeVideo = link;
			deHighlightVideo(previous);
			showVideo(mediaUrl, link.children('p').html());
			highlightThumbnail(link);
			highlightVideo(link);
			trackPageView();
		});
		link.find('div').css('opacity', 0).show();
		
		link.hover(
			function() {
				highlightVideo(link)
			},
			function() {
				deHighlightVideo(link)
			}
		);
		
		if (link.hasClass('on')) link.click();
	};
	
	
	var prepareNewsLink = function(i, it) {

		var link = $j(it);
		var itemId = link.attr('href').substring(1)
		link.bind('click', function() {
			showNews(itemId);
			trackPageView();
		});
		
	}
	

	var showVideo = function(url, caption) {
		
		log.debug("Playing video " + url);
		var intro = $j('#intro');
		var video = $j('#videoWrapper');
		var activeEl = (intro.css('display') == 'none') ? video : intro;
		
		activeEl.fadeOut(500, function() {
			video.children('p').html("");
			loadVideo(url);
			video.show();
			video.children('p').hide().html(caption).fadeIn(1000);
			$j('#introButton').show();
		});
				
	};
	
	
	var highlightThumbnail = function(link){
	
		$j('ul.thumbnails a img').css({'border-color': '#000'});
		if (link) {
			link.children('img').css({'border-color': '#f00'});
			highlightVideo(link)
		}
		else cancelPlayAll();
		
	};
	
	
	var highlightVideo = function(link) {
		link.find('img').fadeTo('fast', 0.5).end().find('div').fadeTo('fast', 1);
	};
	

	var deHighlightVideo = function(link) {
		if (link && link != window.activeVideo) link.find('img').fadeTo('fast', 1).end().find('div').fadeTo('fast', 0);
	};
	

	var loadVideo = function(url) {
	
		$j('#videoWrapper').hide();
		swfobject.embedSWF('/media/player/video-player-0.4.swf', "video", "490", "275", "9.0.0", '/media/player/expressInstall.swf', {url: url}, {bgcolor: '#000000', allowFullScreen: true});
		
	};
		

	var showNews = function(id) {
		var item = $j('#item'+id);
		var inner = $j('#item'+id+' div');
		$j('div.story').each(function(i, it) {
			var height = 1;
			var speed = 300;
			if (it == item.get(0) && item.height() == 1) {
				height = inner.height() + parseInt(inner.css('padding-top')) + parseInt(inner.css('padding-bottom'));
				speed = 300 + height / 8;
			}
			$j(it).animate({height: height}, speed);
		});
	}
	
	
	var collapseNews = function() {
		$j('div.story').animate({height: 1}, 300);
	}
	
	
	var showHeadlines = function(id) {
		$j('#story').fadeOut(500, function() {
			$j('#newsContainer').fadeIn(1000);
			$j('#container').append($j('#newsButton'));
		});
	}
	
	
	var showIntro = function() {
		
		highlightThumbnail(null);
		$j('#videoWrapper').fadeOut(500, function() {
			$j('#videoWrapper').html("<div id='video'></div>");
			$j('#intro').show();
			$j('#introButton').hide();
		});
		
	};
	
	
	var playAllVideos = function() {
	
		window.playList = $j.makeArray($j('#content a.video'));
		$j('a#playAllButton').hide();
		videoEnded();
	
	};
	
	var cancelPlayAll = function() {
	
		window.playList = false;
		$j('a#playAllButton').show();
		
	};
	
	
	var videoEnded = function() {
	
		if (playList && playList.length > 0) {
			$j(playList[0]).click();
		} else {
			window.activeVideo = false;
			highlightThumbnail(null);
			$j('#videoWrapper').hide();
		}
		
	}
	
	var trackPageView = function() {
//		log.debug("Tracking page view to " + document.location.href);
//		log.debug(window.pageTracker._trackPageView)
//		if (window.pageTracker) window.pageTracker._trackPageView(document.location.href);
	}
	
})();