/* --------------------------------------------------------------------
	Player - Video
-------------------------------------------------------------------- */

var flashvars = {};
var skinAddress = "swf/simple.swf";
var playerAddress = "swf/player.swf";

$(document).ready(function() {
		
	// Add Flash player
	flashvars.controlbar = "bottom";
//	flashvars.skin = skinAddress;
	flashvars.frontcolor = "ffffff";
	flashvars.backcolor = "000000";
	flashvars.screencolor = "000000";
	flashvars.dock = "false";
	flashvars.type = "video";
	flashvars.plugins = ["captions-1", "audiodescription-2"];

	var params = {
		allowfullscreen: "true",
		allowscriptaccess: "always",
		allownetworking: "all"
	};
	
	var attributes = {
		id: "video"
	};



	if ($('#video')) 
	{	
		//Display video or a "You need flash" div.
		$('#video').css('display', 'block');
	}

	if (flashvars.file) {
		swfobject.embedSWF(playerAddress, "video", "384", "216", "9.0.0", false, flashvars, params);
	}

	// goto external link page
	$('#content a.video, #relatedNav>.video .main ul a, #content a[rel*=gotoVideo]')
		.one('click', function(event) {
			$(this).attr('href', recode($(this).attr('href'))); 
		})
		.fancybox({
			'centerOnScroll': false,
			'frameWidth': 562,
			'hideOnContentClick': false,
			'overlayColor': '#47145C',
			'overlayOpacity': .7,
			'padding': 0,
			'callbackOnStart': function() {
				// remove some fancybox features
				$('#fancy_title, #fancy_left, #fancy_right').remove();
					
				// hide (for resize purposes)
				$('#fancy_outer').css('opacity', 0);
				
				// set alternative shading
				$('#fancy_bg').addClass('video');
				if ($.browser.msie) {
					$(".fancy_bg").attr('style', '').fixPNG();
				}
			},
			'callbackOnShow': function() {
				var videoH = 288;
				
				// Add Flash player
				if (flashvars.file) {
					swfobject.embedSWF(playerAddress, "video", "512", videoH, "9.0.0", false, flashvars, params, attributes);
				}
				
				// resize the fancybox
				var boxH = $('#fancy_ajax').height() + videoH;
				var minT = $('#fancy_overlay').position().top;
				var boxT = Math.max(0,( $(window).height() - boxH ) / 2);
				
				// check for height changes
				function resizing() {
					if (boxH !== $('#fancy_ajax').height()) {
						boxH = $('#fancy_ajax').height();
						
						$("#fancy_outer").css({
								"height" : boxH,
								"top" : minT + Math.max(0,( $(window).height() - boxH ) / 2)
						});

					};
				}
				resizingInterval  = setInterval(resizing, 50);
				
				addSlideBehaviours($('#video-downloads .section'), resizingInterval, resizing);
				
				// show
				$('#fancy_outer')
				.animate({height: boxH, top: minT + boxT}, 0)
				.animate(
					{opacity: "1"},
					250
				);
			
				// add behavior to the .back button
				$('#fancy_ajax a.back').click(function() {
					$.fn.fancybox.close();
					return false;
				});
			},
			'callbackOnClose': function() {
				clearInterval(resizingInterval);
				$('#fancy_bg').removeClass('video');
			}
		});
		
	/** 
	 *	This function contains instructions to initialize the slide behaviours associated with the "Download videos" tab
	 */
	function addSlideBehaviours(root, resizer, resizing) {
		
		var wrapper = $('.wrapper', root);
		
		if ( !$(root).hasClass('active') ) {
			$(wrapper).hide();
		} 
		
		$(root)
		.hover(function() {
			$(this).toggleClass('hover');
		}, function() {
			$(this).toggleClass('hover');
		})
		.find('h3')
		.click(function() {
			
			if ( $(root).hasClass('active') ) {
				$(root).removeClass('active');
			} else {
				$(root).addClass('active');
			};
			
			$(wrapper).slideToggle(500);
			/* Disable jump to video-downloads
			$(wrapper).slideToggle(500, function() {
				setTimeout(function() {
					window.location.hash="video-downloads";
				}, 20);
			});*/
			
			

		})
		.end()
		.find('.header, .footer')
		.click(function() {
			if ( !$(root).hasClass('active') || $(this).hasClass('header') ) {
				$('h3', root).trigger('click');
			};
		});		
	}		
		
	/**
	 *	This method adjusts all URLs to contain a proper ajax parameter
	 *  so the server will return a different view
	 */
	function recode(url) {
		var base, params;

		if (url.indexOf("?") != -1) {
			base = url.substring(0, url.indexOf("?"));
			params = url.substring(url.indexOf("?"));
			
			return base + params + "&view=ajax";
		}
		else {
			return url + "?view=ajax";
		}
	}	
});
