// JavaScript Document
var NAV_FULLY_VISIBLE = "0px";
var NAV_HIDING = "-205px";
var NAV_PEEKING = "-180px";



$(document).ready( function() {
	
	
	
	$("#left-nav-dom").find('ul.breadcrumbs > li > ul > li.flyout').each(function()
	{
		var li = $(this);

		li.children('ul.flyoutsub').append('<span class="bottom"></span>');
			
			li.hover(
				function()
				{	
					li.css({background:'url(/images/bg-submenu-item.gif) 0px -20px no-repeat'});
					li.css({backgroundPosition:'0 -23px'}).children('ul').show();

					li.children('ul').find('> li > a').each(function()
					{
						
						($(this).height() > 23) ? $(this).css({lineHeight:'14px',paddingBottom:6,paddingTop:3}) : null;
					});

					li.children('ul').css({height:li.children('ul').height()});

					$('div.wrapper').css({overflow:'visible'});

					if($.browser.msie && $.browser.version < 7)
					{
						//$('select').css({visibility:'hidden'});
					}
					
					
				},
				function()
				{
					li.css({backgroundImage:'none'});
					li.css({backgroundPosition:'0 0'}).children('ul').hide();
					$('div.wrapper').css({overflow:'hidden'});
					
					if($.browser.msie && $.browser.version < 7)
					{
						$('select').css({visibility:'visible'});
					}

				}
			);
	});
	
	
	
	
	
	
	// onHide : fade the window out, remove overlay after fade.
	var customCloseReGet = function(hash) { hash.w.fadeOut('2000',function(){ 
							hash.o.remove(); 
							var loc = window.location;
    						window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
						}); 
					};
	var customCloseReload = function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); location.reload();}); };
	var customClose=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };
	var customOpen=function(hash) { hash.w.fadeIn('2000',function(){ }); };
	
	
	
	
	
	//give something the class of popup-link-r to reload the current page when the popup closes
	//give it a class of popup-link for a standard non-reloading modal
	//give it a class of popup-link-g to non-post-reload, as a GET request.
	
	$(".popup-link-r").live("click", function(){
    	scroll(0,0);
    	$('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    	  			$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customCloseReload});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	
	    	  			return false;
	    	  		});
	    
	      	return false;
    });
	
	
	$(".popup-link").live("click", function(){
		 scroll(0,0);
		 $('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    	  			$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customClose});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	    	  			
	
	    	  			return false;
	    	  		});
	    
	      	return false;
	});
	
	$(".popup-link-g").live("click", function(){
		scroll(0,0);
		$('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    		  		$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customCloseReGet});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	
	    	  			return false;
	    	  		});
	    
	      	return false;
		
	});
	
	
	
	/*$('.popup-link-r').click(function(){
	     
	      $('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    	  			$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customCloseReload});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	
	    	  			return false;
	    	  		});
	    
	      	return false;
	});
	
	$('.popup-link').click(function(){
	      $('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    	  			$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customClose});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	    	  			
	
	    	  			return false;
	    	  		});
	    
	      	return false;
	});
	
	$('.popup-link-g').click(function(){
	     
	      $('#modal-container').load($(this).attr('href'), 
	    		  function(){
	    		  		$('#modal-container').pngFix();
	    	  			$('#modal-container').jqm({onHide:customCloseReGet});
	    	  			$('#modal-container').jqm({onShow:customOpen});
	    	  			$('#modal-container').jqmShow();
	
	    	  			return false;
	    	  		});
	    
	      	return false;
	});*/
	
	
	
	$('#search-submit').click(function(e) {
		$.post('/ajax/getSearchLink.php', { q:$('#training-search-query').val() }, 
					function(data){
						location.href = data.link;
					}, "json"
				);
	});
	
	$('#training-search-query').click(function(){
		if( $('#training-search-query').val() == "Search" ) {
			$('#training-search-query').val("");
		}
		$('#training-search-query').focus();
	});
	
	$('.openquizzes').click(function(){
		$.get("ajax/quizhelper.php", {action: "open"}, function(data){
			//alert(data);
			location.reload();
		});
	});
	
	$('.closequizzes').click(function(){
		$.get("ajax/quizhelper.php", {action: "close"}, function(data){
			//alert(data);
			location.reload();
		});
	});
	
		
	$('.cancelBtn').click(function(e){
		$('#modal-container').jqmHide();
	});
	
	
	$('#training-search-query').keyup(function(e) {
		if( e.keyCode == 13 ) {
			$.post('/ajax/getSearchLink.php', { q:$('#training-search-query').val() }, 
						function(data){
							location.href = data.link;
						}, "json"
					);
		}
	});
	
	/*$('#training-search-query').quickselect({
		url:'/ajax/trainingSearch.php',
		match:'substring',
	    matchContains:true,
	    autoSelectFirst:false,
	    mustMatch:false,
	    autoFill:false,
	    maxItemsToShow:10,
	    width:137,
	   	delay:500
	});*/
	
	if(typeof sIFR == "function"){
    	sIFR();
	}
	
	$(document).pngFix();
});


function questionSubmit(type){
	
	if(type == 'edit'){
		$('#editform').submit(function() {
		$.post('admintemplate.php', { editQuestion: $('#editQuestion').val(), question: $('#question').val(),
									  answer1: $('#answer1').val(), answer2: $('#answer2').val(), 
									  answer3: $('#answer3').val(), answer4: $('#answer4').val(), 
									  correct: $("input[name='correct']:checked").val() }, 
								function(data){
									$('#editquizlink').click();
								}, "html");
		
		return false;
	});
	}else if(type =='add'){
		$('#addform').submit(function() {
			
			alert("addQuestion:" + $('#addQuestion').val() +", question: "+$('#question').val()+",answer1: "+$('#answer1').val()+", answer2: "+$('#answer2').val()+",answer3: "+$('#answer3').val()+", answer4: "+$('#answer4').val()+",correct: "+$("input[name='correct']:checked").val());

			$.post('admintemplate.php', { addQuestion: $('#addQuestion').val(), question: $('#question').val(),
										  answer1: $('#answer1').val(), answer2: $('#answer2').val(), 
										  answer3: $('#answer3').val(), answer4: $('#answer4').val(), 
										  correct: $("input[name='correct']:checked").val() }, 
									function(data){
										$('#editquizlink').click();
									}, "html");
			
			return false;
		});
	}
	
	
}

function loginSubmit(){
	
	$.post("/ajax/authHelper.php", { action:"authenticate", username: $('#username').val(), password: $('#password').val()},
		function(data){
	    	$('#modal-content').append(data); 
	  		}, "html");
	
	
}

function addGlossaryAction(){
	
	$('.itemCancel').click(function(e){
		$(e.target).parent().parent().children('.def').html($(e.target).parent().children('.definput').val());
		$(e.target).parent().parent().children('.term').html($(e.target).parent().children('.terminput').val());
	});
	
	
	$('.itemUpdate').click(function(e){
		var type;
		var data;
		
		if($(e.target).parent().parent().children('.def').size()>0){
			type='def';
			data = $(e.target).parent().children('.definput').val();
			//alert("clicked save def.");
		}else{
			type='term';
			
			//alert("clicked save term: "+ $(e.target).parent().children('.terminput').val());
			//alert("clicked save term: "+ $(e.target).parent().parent().children('.terminput').val());
			data = $(e.target).parent().children('.terminput').val();
		}
	
		$.post('/ajax/glossaryUpdater.php', { gid: $(e.target).parent().parent().children('#glossary_id').val(), data: data, type: type},
		  function(data){
		    $(e.target).parent().parent().children('.def').html($(e.target).parent().children('.definput').val());
		  	$(e.target).parent().parent().children('.term').html($(e.target).parent().children('.terminput').val());
		  });
	});
	
	
	$('.itemDelete').click(function(e){
		$.post('/ajax/glossaryUpdater.php', { delete_id: $(e.target).parent().parent().children('#glossary_id').val() });
		var loc = window.location;
    	window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
	});
	
}



function getNavState() {
	return 	$("#left-nav-dom").css("left");
}

function showLeftNav() {
	scrollNavTo(NAV_FULLY_VISIBLE, function() {
		$("#left-shutter-bar").css({backgroundPosition: '0px'});											
	});
	
}

function hideLeftNav() {
	scrollNavTo(NAV_HIDING, function() {
	 	$("#left-shutter-bar").css({backgroundPosition: '-10px'});
	});
	
}

function peekLeftNav() {
	scrollNavTo(NAV_PEEKING, function() {
	 	$("#left-shutter-bar").css({backgroundPosition: '-10px'});
	});
}

function openTest( quiz_id ) {
	
	href = "/ajax/launchQuiz.php?open=" + quiz_id;
	
	//alert( "href=" + href );
	
	$('#modal-container').load( href, 
		function(){
			$('#modal-container').jqm({onHide:customCloseReload});
			$('#modal-container').jqm({onShow:customOpen});
			$('#modal-container').jqmShow();
			$('#modal-container').pngFix();
		
			return false;
		});
		
	var customCloseReload = function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); location.reload();}); };
	var customClose=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };
	
	var customOpen=function(hash) { hash.w.fadeIn('2000',function(){ }); };
}
   
function openLogin() {
	
	href = "/ajax/authHelper.php";
	
	$('#modal-container').load( href, 
		function(){
			$('#modal-container').pngFix();
			$('#modal-container').jqm({onHide:customCloseReload});
			$('#modal-container').jqm({onShow:customOpen});
			$('#modal-container').jqmShow();
		
			return false;
		});
		
	var customCloseReload = function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); location.reload();}); };
	var customClose=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };
	
	var customOpen=function(hash) { hash.w.fadeIn('2000',function(){ }); };
}      

function scrollNavTo(offset, callback) {
	
	$("#left-nav-dom").animate( {left: offset}, callback);
	
	$("#left-shutter").animate
	({
		left: offset
	}
	);	
}
