$(document).ready(function() {
		
	$('.VideoContainer').cycle({
		fx:     'scrollHorz', 		 
		timeout: 0, 
		next:   '#next_videos', 
		prev:   '#prev_videos' 
	});
	

	$('.Facts_List').cycle({ timeout: 12000, cleartype: 1, speed: 1000, next: '#next_fact', prev: '#prev_fact' });	
	
	/* thumbnail hover */
	$('.Video_Thumbnails').hover(function(){
		$(".cover", this).stop().animate({top:'60px'},{queue:false,duration:160});
		$(".thumbPlayButton", this).addClass('thumbPlayButton_Over');
	}, function() {
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
		$(".thumbPlayButton", this).removeClass('thumbPlayButton_Over');
	});	
		
	/* slide up news items */
	var showText='Show';
	var hideText='Hide';
	
	$('h2').parent().next('#newscontent').slideUp('slow');
	
	$('h2').click(function() {	
		//alert('click news title');
		$(this).parent().next('#newscontent').toggle('slow');
		$(this).toggleClass('active');
	});	
	
	// Show / Hide All news items	
	$('#expand_news').click(function(event) {		
		event.preventDefault();
		$('h2').parent().next('#newscontent').slideDown('slow');
		$('h2').addClass('active');
	});	
		
	$('#collapse_news').click(function(event) {
		event.preventDefault();
		$('h2').parent().next('#newscontent').slideUp('slow');
		$('h2').removeClass('active');
	});
		
	$('#newscontent a').each(function() {	
		
				
		if (!($(this).attr("id"))) {	
						
			var title = $(this).text();
			var hyperlink = escape($(this).attr("href"));
			
			var title_uri_encode = encodeURIComponent(title);	
			var url_uri_encode = encodeURIComponent(hyperlink);
			
			var category = $('#hiddencat').attr("cat");
			var videoid = $('#hiddencat').attr("vid");		
			
			$(this).attr('target', '_blank');
			
			if(videoid != ""){
				$(this).attr("href", "external.php?title=" + title_uri_encode + "&exturl=" + url_uri_encode + "&cat=" + category + "&vid=" + videoid);
			} else {
				$(this).attr("href", "external.php?title=" + title_uri_encode + "&exturl=" + url_uri_encode + "&cat=" + category);
			}
		}
						
	});		
	
	$(".Video_Thumbnails").click( function(event) { 		
		
		var $videoid = $(this).attr("url");
		var $category = $('#hiddencat').attr("cat");
		$('#hiddencat').attr("vid", $videoid);
		
		var $vimeo_embed = "Scripts/loadvideo.php?videoid=" + $videoid;
		var $vimeo_meta_news = "Scripts/videometanews.php?videoid=" + $videoid + "&cat=" + $category;
				
		$(".Video_Vimeo").load($vimeo_embed);
		$(".Video_Specific_Content").slideUp('slow', function(){
															  
			//$(".Video_Vimeo").load($vimeo_embed);
			
			$(".Video_Specific_Content").load($vimeo_meta_news, function(){
				
				$(".Video_Specific_Content").slideDown('slow', function(){
				
					$('h2').parent().next('#newscontent').slideUp('slow');					
					
					$('#expand_news').click(function(event) {		
						event.preventDefault();
						$('h2').parent().next('#newscontent').slideDown('slow');
						$('h2').addClass('active');
					});	
						
					$('#collapse_news').click(function(event) {
						event.preventDefault();	
						$('h2').parent().next('#newscontent').slideUp('slow');
						$('h2').removeClass('active');	
					});					
					
					$('#expand_news_btm').click(function(event) {		
						event.preventDefault();
						$('h2').parent().next('#newscontent').slideDown('slow');
						$('h2').addClass('active');
					});	
						
					$('#collapse_news_btm').click(function(event) {
						event.preventDefault();	
						$('h2').parent().next('#newscontent').slideUp('slow');
						$('h2').removeClass('active');	
					});					
					
					$('h2').click(function() {										   
						$(this).parent().next('#newscontent').toggle('slow');
						$(this).toggleClass('active');						
					});					
					
					$('#newscontent a').each(function() {	
						
						if (!($(this).attr("id"))) {							
														
							// var title = escape($(this).text());
							
							var title = $(this).text();							
							var hyperlink = escape($(this).attr("href"));						
							
							var title_uri_encode = encodeURIComponent(title);	
							var url_uri_encode = encodeURIComponent(hyperlink);
							
							var category = $('#hiddencat').attr("cat");
							var videoid = $('#hiddencat').attr("vid");		

							$(this).attr('target', '_blank');

							if(videoid != ""){
								$(this).attr("href", "external.php?title=" + title_uri_encode + "&exturl=" + url_uri_encode + "&cat=" + category + "&vid=" + videoid);
							} else {
								$(this).attr("href", "external.php?title=" + title_uri_encode + "&exturl=" + url_uri_encode + "&cat=" + category);
							}
						}
					});				
					
				});
								
			});
			
		});		
				
		event.preventDefault();	
	});
	
});
