// DiplomR Lib

$(document).ready(function(){

$(".resetFont").click(function(){
               setFontSize(originalFontSize);
               $("#changeFont").fadeToggle("fast");
               return false;
       });

       $(".increaseFont").click(function(){
               var fontSize = getFontSize();
               var newFontSize = fontSize + 1;
               setFontSize(newFontSize);
               return false;
       });

       $(".decreaseFont").click(function(){
               var fontSize = getFontSize();
               var newFontSize = fontSize - 1;
               setFontSize(newFontSize);			   
               return false;
       });

function getFontSize() {
       var currentSize = $("#content-container").css("font-size");
       var currentSizeNumber = parseFloat(currentSize, 12);
       if(currentSizeNumber > 18) {
               currentSizeNumber = 18;
       }
	    if(currentSizeNumber <10) {
               currentSizeNumber = 10;
       }
       return currentSizeNumber;
}

function setFontSize(size) {
       $("#content-container").css("font-size", size);
       $(".actualSize").html(size);
	   //make it persist
	   	$.ajax({
			url: '/ajax/set_font_size',
			type: 'GET',
			data: "size=" + size,
			error: function(){
				alert('Error setting font size');
			},
			success: function(){
				
			}
		});
	   
}

var originalFontSize = getFontSize();
$(".originalSize").html(originalFontSize);
$(".actualSize").html(originalFontSize);

});


//media expander
$(document).ready(function(){
						   
		$(".media-expand").click(function(event){
										   
										  $(this).parent().parent(".inline-media-container").children(".media-container-top").slideToggle(300);
										  if($(this).html()=='+ expand'){$(this).html('- collapse');}
										  else{$(this).html('+ expand');}
										  return false;
										  });
});



function mcq_reset_test(id){
	$.ajax({
			url: '/ajax/mcq_reset_test/',
			type: 'GET',
			data: "id=" + id,
			error: function(){
				alert('Error changing order');
				
			},
			success: function(){
				window.location.reload();
			
			}
	});
}


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

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);
			}
		});
	
}


// notes
function refresh_notes(level, id){	
		
		window.location.reload();
		//$('#notes').load('/ajax/get_notes/?id=' + id + '&level=' + level);	
}

//menu

(function($){
 	// Define the function
	$.fn.singleDropMenu = function(options){
		// Function Vars
		var timer; // ID of timeout set
		var menu; // ID of menu open
		// Default Settings
		var settings = $.extend({
			timer: 500,
			parentMO: null,
			childMO: null
		},options||{});

		// Run Menu
		this.each(function(){
			$('li', this).mouseover(function(){
				// Clear any open menus
				closemenu();
				// Open nested list
				$('a#module-select', this).addClass(settings.parentMO).siblings('ul').show();
			}).mouseout(function(){
				// Prevent auto close
				menu = this;
				timer = setTimeout(function(){closemenu()}, settings.timer);
			});
			// Dropped Menu Highlighting
			$('li ul li', this).mouseover(function(){
				$('a', this).addClass(settings.childMO);
			}).mouseout(function(){
				$('a', this).removeClass(settings.childMO);
			});
		});

		// Closes any open menus when mouse click occurs anywhere else on the page
		$(document).click(function(){closemenu();});

		// Function to close set menu
		var closemenu = function(){
			if (menu && timer){
				$('a', menu).removeClass(settings.parentMO).siblings('ul').hide();
				clearTimeout(timer);
				menu = '';
			}
		}
	}
})(jQuery);



$(document).ready(function(){
	$('#module-dropdown').singleDropMenu({timer: 500, parentMO: 'module-dropdown-hover', childMO: 'ddchildhover'});
});



//save interactions score
function save_interaction_score(interaction_id, score){
	$.ajax({
			url: '/ajax/save_interaction_score',
			type: 'GET',
			data: "interaction_id=" + interaction_id + "&score=" + score,
			error: function(){
				alert('Error saving score');
			},
			success: function(){				
			}
		});
}


//autocomplete search
$(function(){
	$(".search-input").autocomplete("/search/auto_complete",{selectFirst: false});
  });

//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('<h3 class="overlay-title">' + title + '</h3><div class="overlay-close"><a href="#" onclick="$(\'#overlay\').jqmHide();return false;"><span>[close]</span></a></div><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));
	}
	
}

//delete interactions comment
function interaction_delete_comment(comment_id){
	$.ajax({
			url: '/interaction/delete_comment',
			type: 'GET',
			data: "comment_id=" + comment_id,
			error: function(){
				alert('Error saving score');
			},
			success: function(){
					window.location.reload();
			}
		});
}
