//SSC
$(document).ready(function(){
  $('#overlay').jqm({ajax: '@href', trigger: 'a.overlay_trigger', target: '#overlay-content'}).jqDrag('.overlay_drag');
});

$(document).ready(function(){
  $('.transcript').click(function(){$f('video-container').pause();});
  	
});


function delete_note(id, level, level_id){
	
		$.ajax({
			url: '/ajax/delete_note',
			type: 'GET',
			data: "id=" + id,
			error: function(){
				alert('Error deleting note');
			},
			success: function(){
				refresh_notes(level, level_id);
			}
		});
	
}
//autocomplete search
$(function(){
	$(".search-input").autocomplete("/search/auto_complete",{selectFirst: false,matchContains: true});
  });

//search box
$(function() {
	$(".search-input").focus(
		function(){
			$(this)
			//Fade to the new color
			.animate({backgroundColor: '#dcecf7'}, 500)
		}	
	);
}); 

$(function() {
	$(".search-input").blur(
		function(){
			$(this)
			//Fade to the new color
			.animate({backgroundColor: '#fff'}, 200)
		}	
	);
}); 

$(function(){
	$(".search-but").hover(
			function(){$(this).css({"backgroundPosition" : " 0 -21px"});},
			function(){$(this).css({"backgroundPosition" : " 0 0"});}
	);
	
 });
//end search box
function show_message(message){
	if ( $("#msg").length > 0 ) {$('#msg').show();}
	else{	$('#content-container').prepend('<div id="msg"></div>');}
	$.ajax({
			url: '/ajax/get_message',
			type: 'GET',
			data: "msg=" + message ,
			error: function(ht){
				alert('Error reporting message');
			},
			success: function(ht){	
					$('#msg').html(ht);
			}
		});
}
function show_announcement(message){
	if ( $("#msg").length > 0 ) {$('#msg').show();}
	else{	$('#content-container').prepend('<div id="msg"></div>');}
	$.ajax({
			url: '/ajax/get_announcement',
			type: 'GET',
			data: "msg=" + message ,
			error: function(ht){
				alert('Error reporting message');
			},
			success: function(ht){	
					$('#msg').html(ht);
			}
		});
}
function show_overlay_message(message){
	
	if ( $("#msg").length > 0 ) {$('#msg').show();}
	else{	$('#overlay-content').append('<div id="msg"></div>');}
	$.ajax({
			url: '/ajax/get_message',
			type: 'GET',
			data: "msg=" + message ,
			error: function(ht){
				alert('Error reporting message');
			},
			success: function(ht){	
					$('#msg').html(ht);
			}
		});
}

function show_overlay(title, content, override){	

	$('#overlay').jqmShow();

	if(override==undefined ){
		
	$('#overlay-content').html('<h2 class="overlay-title">' + title + '</h2><div class="clear"></div><div class="overlay-content">' + content + '<br /></div>');
	
	//do the widths
	width = $('#overlay-container').width();

	//set the widths
	$('.shadow-top').width(width-108);
	$('.shadow-bottom').width(width-108);
	
	//align the overlay
	$('#overlay').css('margin-left',-(width/2));
	
	}
	else{
		$('#overlay-content').html(override);
		//do the widths
	width = $('#overlay-container').width();

	//set the widths
	$('.shadow-top').width(width-108);
	$('.shadow-bottom').width(width-108);
	
	//align the overlay
	$('#overlay').css('margin-left',-(width/2));
	}
	
}

//flow player functions
var currentPlaylistIndex = -1; 
function seekVideo(playlistIndex,seconds) { 
  var p = $f(); 
  // If the video is done playing, or not yet loaded, we have to  
  // give it a kickstart. 
  if (p.getState() == 1 || !p.isLoaded() || playlistIndex != currentPlaylistIndex) { 
    _startAndSeek(p, playlistIndex, seconds); 
  } else { 
    p.seek(seconds); 
    if (!p.isPlaying()) p.play(); 
  } 
} 
 
function _startAndSeek(p, playlistIndex, seconds) { 
  var initialLoad = true; 
  // Add a callback to do the actual seeking after the player 
  // loads and we've got enough of the video buffered to know 
  // where the seekpoints are. 
  p.onStart(function () { 
    if (!initialLoad) return; // Make sure we don't do this for future onStarts. 
    initialLoad = false; 
    p.seek(seconds);  
    if (!p.isPlaying()) p.play(); 
  }); 
  if (p.isLoaded()) { 
    p.play(playlistIndex); 
    currentPlaylistIndex = playlistIndex; 
  } else { 
    p.load(function () { p.play(playlistIndex); currentPlaylistIndex = playlistIndex }); 
  } 
} 

function update_section_title(title, sub_title){
	if(sub_title==''){ sub_title='&nbsp;'}
	$('.content-title').fadeOut(150, function(){
											  $('#section-title').html(title);
											  $('#subsection-title').html(sub_title);
											  $('.content-title').fadeIn(150);
																		 });
	
	
}

function select_video(id){	
	 
	if(id>0){
		
	$('#section-nav .selected').removeClass('selected');
	$('#item' + id).addClass('selected');
	
	if($('#transcript-content').is(":visible")==true){
		$('#transcript-content').fadeOut();
		$('#transcript-content').load('/multimedia/get_transcript/' + id, function(){$('#transcript-content').fadeIn();});
	}else{
		$('#transcript-content').html('');
	}
	var clip = $f().getClip();
	
	
	if(clip.url=='/multimedia/get_file/' + id){
		//is the clip paused already?
		state = $f().getState();
		if(state==4){
			$f().play();
		}
		else{
			$f().pause();	
		}		
	}
	else{
		//play the new clip
		//update the video chapters		
		$('#video-chapters').fadeOut(250, function(){$('#video-chapters').load('/multimedia/get_video_chapters/' + id, function(){ $('#video-chapters').fadeIn(250)})});				
		//update the transcript link
		$('#transcript-link').attr('href', '/multimedia/get_transcript/' + id);	
		$('#video_id').attr('value', id);


		
		//add learning link
		$.ajax({
			url: "/my_learning/check_content",
			type: "GET",
			data: "area=video&id=" + id,
			error: function(){
				alert("Error adding to my learning");
			},
			success: function(msg){				
				if(msg==0){
					toggle_add_link(1,id, 'video');
					}				
				
				else if(msg==1){
					toggle_add_link(0,id, 'video');
				}
			}
				
		   });
		
		
		
				
		//get the next id
		var aws_url = '';
		$.ajax({
			url: '/multimedia/get_url/' + id,
			type: "GET",
			data: "id=" + id,
			error: function(){
				alert("Error getting AWS");
			},
			success: function(msg){		
				aws_url = msg;	
				$('#video-container').fadeIn(250, function(){$f().play(aws_url)});
			}
		});
		
		
		
	}
	}
}

function toggle_menu(id){
	if($('#ss_' + id).hasClass('ss_on')==false){	
	$('.ss_on').slideUp(150, function(){										
										$('.ss_menu').removeClass('ss_on');
										$('#ss_' + id).slideDown();
										$('#ss_' + id).addClass('ss_on');
										});	
		$('#ss_' + id).slideDown();
		$('#ss_' + id).addClass('ss_on');
	}

}


function toggle_transcript(){
	if($('#transcript-content').html()==''){
		$('#transcript-content').load('/multimedia/get_transcript/' + $('#video_id').attr('value'), function(){
																											 $('#transcript-content').slideToggle('fast');
																											 });
	} 
	else{ 
		$('#transcript-content').slideToggle('fast');
	} 
}


function select_question(area, section_id, case_num, question_num){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/mcq/select_question?area=' + area + '&section_id=' + section_id + '&case_num=' + case_num + '&question_num=' + question_num, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
											  });
}


function select_assessment_question(question_id){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/assessment/select_question/' + question_id, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
											  });
}


function select_assessment_results(){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/assessment/select_results', 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
											  });
}


function select_formative(area, section_id, case_num, question_num){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/formative/select_question?area=' + area + '&section_id=' + section_id + '&case_num=' + case_num + '&question_num=' + question_num, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
											  });
}

//mcq reset
function reset_formative(area, section_id, case_num, q_num){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/formative/reset_test?area=' + area + '&section_id=' + section_id +'&case_num=' + case_num +'&q_num=' + q_num, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });
}


//assessment reset
function reset_assessment(){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/assessment_mcq/reset_test', 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });
}

function reset_assessment_process(){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	/*$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/assessment_mcq/reset_test_process', 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });*/
	
	document.location.href='/assessment_mcq/reset_test_process';
}



function reset_formative_process(area, section_id, case_num){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/formative/reset_test_process?area=' + area + '&section_id=' + section_id + '&case_num=' + case_num,
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });
}

function select_results(area, section_id){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
								   
		$('#mcq-content').load('/mcq/select_results?area=' + area + '&section_id=' + section_id);
											 
}


function reset_test(area, section_id){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/mcq/reset_test?area=' + area + '&section_id=' + section_id, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });
}

function reset_test_process(area, section_id){
	original_height = $('#mcq-container').height();	
	$('#mcq-container').height(original_height);	
	$('#mcq-content').fadeOut(150,function(){										   
		$('#mcq-content').load('/mcq/reset_test_process?area=' + area + '&section_id=' + section_id, 
							   function(){
								   $('#mcq-content').fadeIn(150);
								   $('#mcq-container').height('auto');
							   
							   })	
	 });
}


function manage_my_learning(area, id, toggle){
	$.ajax({
			url: "/my_learning/" + toggle,
			type: "GET",
			data: "area=" + area + "&id=" + id,
			error: function(){
				alert("Error adding to my learning");
			},
			success: function(msg){
				
				if(area=='video'){
					if(toggle=='add'){
						$("#add-learning-link").effect('transfer',{to:'#my-leraning-nav', className: 'ui-effects-transfer'},500, function(){toggle_add_link(0,id,area);});
						}				
					
					else if(toggle=='remove'){
						$("#my-leraning-nav").effect('transfer',{to:'#add-learning-link', className: 'ui-effects-transfer'},500, function(){toggle_add_link(1,id,area);});
						}
				}
				
				if(area=='resource'){
					if(toggle=='add'){
						$("#add-learning-link-" + id).effect('transfer',{to:'#my-leraning-nav', className: 'ui-effects-transfer'},500, function(){toggle_add_link(0,id,area);});
						}				
					
					else if(toggle=='remove'){
						$("#my-leraning-nav").effect('transfer',{to:'#add-learning-link-' + id, className: 'ui-effects-transfer'},500, function(){toggle_add_link(1,id,area);});
						}
				}
				
				
				
			}
				
		   });
}

function toggle_add_link(on, id,area){
	if(area=='video'){
		if(on==0){
			//make it a remove link	
			$("#add-learning-link").html('Remove from My Learning');
			$('#my_learning_toggle').attr('value', 'remove');
		}
		else{
			$("#add-learning-link").html('Add to My Learning');
			$('#my_learning_toggle').attr('value', 'add');
		}	
	}
	if(area=='resource'){		
		if(on==0){
			//make it a remove link	
			$("#add-learning-link-" + id).html('Remove from My Learning');
			$('#my_learning_toggle-' + id).attr('value', 'remove');
		}
		else{
			$("#add-learning-link-" + id).html('Add to My Learning');
			$('#my_learning_toggle-' + id).attr('value', 'add');
		}	
	}
	
}

//add track
function add_track(area, id, status){
	$.ajax({
			url: "/multimedia/add_track",
			type: "GET",
			data: "area=" + area + "&id=" + id + "&status=" + status,
			error: function(){
				alert("Error adding tracking");
			},
			success: function(){
				
			}
		   });
}

//add track
function set_video_quality(quality, clicked){
	
	$.ajax({
			url: "/multimedia/set_quality",
			type: "GET",
			data: "quality=" + quality,
			error: function(){
				alert("Error adding tracking");
			},
			success: function(){
				$('#video-quality-links a').removeClass('quality-selected');	
				$(clicked).addClass('quality-selected');
				
				//change the video
				/*var clip = $f().getClip();	
				//is it the current clip?
				//clip_time = 
				$f().stop();
				$f().play();*/
				$('.vide-menu .selected').click();
				
			}
		   });
}


//mcqPopups
/*
$(function(){
	$('.model-answer-area a.video-link').click(function(){
		
		var the_href = $(this).attr('href');
		the_href = the_href + '&overlay=1';
		
		//load an overlay
		$('#overlay-content').load(the_href);
		$('#overlay').jqmShow();
		
	
		return false;	
	});
});
*/
