$(document).ready(function(){


	$('#videoMenu a.video').click (function() {
		
		$('#videoJumpLinks').hide();
		
		$('#videoMenu').addClass('collapsed').jScrollPane({
			showArrows:false,
			scrollbarWidth:17
		});

		uniqueUrl = $(this).attr('rel');
		href = $(this).attr('href');
		
		$('#videoMenu a').removeClass('selected');
		$('#video-' + uniqueUrl).addClass('selected');

		if( $('#videoMenuWrapper:not(.sideMenu)').length > 0) {
			$('#videoMenu a').hide();
			$('#videoMenuWrapper').addClass('sideMenu');
			
			$('#videoMenu, .jScrollPaneContainer').css({height:'446px'}).animate({
				width:'332px'
			}, 200, function(){
				
				$('#videoMenu').jScrollPane({
					showArrows:false,
					scrollbarWidth:17
				});
			
				$('#videoMenu a').fadeIn(200);
				
				$.get(href, function(data) {
					$('#videoPlayerBox').removeClass('fullWidthVideo').addClass('compactVideo').html(data);
				}) ;
			});
		} else {
		
			$.get(href, function(data) {
				$('#videoPlayerBox').html(data);
			});
		
		};
		
		
		return false ;	
	}) ;
	

	//if ($('#technologyMenu a:first')) {
	//	$('#technologyMenu a:first').click() ;
	//}
	
	
	
		
		
	//set up advanced filtering dropdown menus
	$('.filterMenuBox').each(function(){
		var currentSort = $(this).find(':checked + label').text() + ' videos';
		var $sortForm = $('form', this);
		
		//Set up new structure and CSS for the menu
		$(this).css({
			position:'absolute',
			padding: '0',
			top:'0px',
			right:'12px',
			zIndex:'10',
			width:'180px',
			color:'#000',
			fontWeight:'normal'
		}).prepend(
			$('<div/>').attr({ className:'currentFilter' }).text(currentSort)
		).find('.formTitle').css({
			display:'block',
			position:'absolute',
			top:'2px',
			left:'-108px',
			width:'100px',
			textAlign:'right',
			color:'#FFF',
			fontSize:'12px',
			fontWeight:'bold'
		}).end().find('.filterOptionsBox').css({
			border:'none',
			padding:'0',
			backgroundColor:'#FFF',
			border:'1px solid #999999',
			borderTop:'0',
			padding:'0',
			display:'none'
		}).find('.formSubmit').css({
			display:'none'
		}).end().find('p').css({
			display:'none'
		}).end().find('.filterOption').css({
			float:'none',
			margin:'0',
			padding:'0'
		}).find('label').css({
			display:'block',
			padding:'3px 6px',
			cursor:'pointer'
		}).hover(
			function(){
				$(this).css({
					backgroundColor:'#AC0535',
					color:'#FFF',
					fontWeight:'bold'
				});
			},
			function(){
				$(this).css({
					backgroundColor:'#FFF',
					color:'#000',
					fontWeight:'normal'
				});
			}
		).click(function(e){
			$(this).parent().parent().find(':checked').attr('checked', '');
			$(this).prev(':radio').attr('checked', 'checked').change();
			currentSort = $(this).text() + ' videos';
			$('.currentFilter').text(currentSort);
			e.preventDefault();
			
			$sortForm.submit();
		
		}).end().find(':radio').change(function(){
			//insert handler code for changing the sort option
			
			
		}).css({ display: 'none' });
		
		//set up menu functionality
		//taken and modified from tuturial on jqueryfordesigners.com
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('.currentFilter', this);
		var menu = $('.filterOptionsBox', this);
		
		$([trigger.get(0)]).click(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				menu.slideUp(menuSpeed, function () {
					shown = false;
				});
			} else {
				// reset position of info box
				beingShown = true;
				
				menu.slideDown(menuSpeed, function () {
					beingShown = false;
					shown = true;
				});
			}
			return false;
		});
		
		$([trigger.get(0), menu.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				menu.slideUp(menuSpeed, function () {
					shown = false;
				});
	
			}, hideDelay);
	
			return false;
		});
		
	});
});

// Look for URL parameters and use them to auto-play videos
function autoPlayVideo() {
	// If a video param is supplied...
	if(jQuery.url.param("video") != null) {
		// Set video param to lowercase and store in var
		var video = jQuery.url.param("video").toLowerCase();
		// Look through the video links and find the one with href that matches video param
		jQuery('#videoMenu a').each(function(){
			if(jQuery(this).attr('href').toLowerCase() == "/videogallery/" + video + ".html") {
				// Once found, activate the appropriate video link
				jQuery(this).click();
				// Exit the loop
				return false;
			}
		})	
	} else {
		// Otherwise, look for a filter param...
		if(jQuery.url.param("filter") != null) {
			if(jQuery('#videoMenu a:first').length ) {
				// and activate the first video link
				jQuery('#videoMenu a:first').click();
			}
		}
	}
}

// Initialize functions on page load
jQuery(document).ready(function(){
	autoPlayVideo();
})