



/**
 * Confirm
 */
$(document).ready(function() {	
	bindActions();
	
});


$(document).ready(function() {
	
	$("#loading-process").ajaxStart(function(){ 
	 // $(this).show(); 
	});
	$("#loading-process").ajaxStop(function(){ 
	  //$(this).hide(); 
	});
	
	
});
/**
 * 
 * @param {Object} caller
 * @param {Function} callback
 */
function simpleAjax(params) {
	var url = $(params.caller).attr('href');
	if (url.indexOf(URL) == -1) {
		url = URL + url;
	}
	
	$.loaderPage(true);
	$.ajax({
		type: "GET",
		url: url,
		success: function(data) {
			params.callback.call(params.caller);
			$.loaderPage(false);
		}
	});
}
function bindActions() {
	/**
	 * Question::new
	 * 
	 * @param {Object} e
	 */
	$('#save_project').click(function(e){
		var url = $(this).attr('rel');
		var question = $('#project').val();
		$.submitForm('#projectForm');
		
		e.preventDefault();
		
	});
	$('#add_openquestion').click(function(e){
		var url = $(this).attr('rel');
		
		var question = $('#question').val();
		var correct_answer = $('#correct_answer').val();
		if (question == '' || correct_answer=='') {
			return false;
		}
		$('#question').val('');
		$('#correct_answer').val('');
		$.ajax({
			type: "POST",
			data: 'question=' + question + '&correct_answer=' + correct_answer,
			url: url,
			success: function(data) {
				if (data != 0) {
					$('#sortQuestions')
					    .append(data);
						
					var id = $('#sortQuestions').find('div.groupItem:last').attr('id');
					
					$('#sortQuestions').SortableAddItem(document.getElementById(id));
					bindActions();
					$('#question').focus();
				}
			}
		});
		e.preventDefault();
	});
	$('#correct_answer').keydown(function(e){
		if (e.keyCode == 13) {
			$('#add_openquestion').click();
		}
	});
	
	$('#save_question').click(function(e){
		
		var url = $(this).attr('rel');
		
		var question = $('#question').val();
		if (question == '') {
			return false;
		}
		$.loaderPage(true);
		$.ajax({
			type: "POST",
			data: 'question=' + question,
			url: url,
			success: function(data) {
				$.loaderPage(false);
			}
		});
		e.preventDefault();
	});
	$('#add_question').click(function(e){
		var url = $(this).attr('rel');
	
		var question = $('#question').val();
		if (question == '') {
			return false;
		}
		$('#question').val('');
		$.ajax({
			type: "POST",
			data: 'question=' + question,
			url: url,
			success: function(data) {
				if (data != 0) {
					$('#sortQuestions')
					    .append(data);
						
					var id = $('#sortQuestions').find('div.groupItem:last').attr('id');
					
					$('#sortQuestions').SortableAddItem(document.getElementById(id));
					bindActions();
				}
			}
		});
		e.preventDefault();
	});
	$('#question').keydown(function(e){
		if (e.keyCode == 13) {
			$('#add_question').click();
		}
	});
	/**
	 * Anservariants::new
	 * 
	 * @param {Object} e
	 */
	$('#add_answer_variant').click(function(e){
		var url = $(this).attr('rel');
		
		var description = $('#description').val();
		if (description == '') {
			return false;
		}
		$('#description').val('');
		$.ajax({
			type: "POST",
			data: 'description=' + description,
			url: url,
			success: function(data) {
				if (data != 0) {
					$('#sortAnswervariants')
					    .append(data);
						
					var id = $('#sortAnswervariants').find('div.groupItem:last').attr('id');
					
					$('#sortAnswervariants').SortableAddItem(document.getElementById(id));
					// force to checked first variant
					if ($('input[name=correct_answer]').length == 1) {
						$('input[name=correct_answer]:first').attr('checked','checked');
					}
					
					WdkInit.links();
				
					bindActions();
				}
			}
		});
		e.preventDefault();
	});
	$('#description').keydown(function(e){
		if (e.keyCode == 13) {
			$('#add_answer_variant').click();
		}
	});
	
	$('.correctAnswer').click(function(){
		var correct_answer = $('.correctAnswer[@type=checkbox]').serialize(); // poll
		if (correct_answer == '') {
			correct_answer = 'correct_answer_id=' + $(this).val(); 
		}
		$.ajax({
			type: "POST",
			data: correct_answer,
			url: URL + '/elearning/answervariants/correctanswer/question_id/'+REQUEST.question_id,
			success: function(data) {
				if (data != 0) {
					
				}
			}
		});
	});
	
	
	/**
	 * delete
	 * 
	 * @param 
	 */
	$('.deleteAjax').click(function(e){
		var $this = $(this);
		var answr = $this.attr('title') || 'Czy na pewno usunąć?';
			
		if (confirm(answr)) {
			simpleAjax({
				caller: this,
				callback: function(){
					$(this).parent().parent().fadeOut();
				}
			})
		}	
		
		
		
		e.preventDefault();
	});
	/**
	 * 
	 * move up
	 * @param 
	 */
	$('.moveupAjax').click(function(e){
		simpleAjax({
			caller: this,
			callback: function(){
				var current = $(this).parent().parent('tr');
				var prev = $(current).prev();
				$(current).insertBefore(prev);
			}
		})
		
		e.preventDefault();
	});
	/**
	 * 
	 * move down
	 * @param 
	 */
	$('.movedownAjax').click(function(e){
		simpleAjax({
			caller: this,
			callback: function(){
				var current = $(this).parent().parent('tr');
				var next = $(current).next();
				$(current).insertAfter(next);
			}
		})
		
		e.preventDefault();
	});
	
	
	$.logger('bind el actions');
	$('.popupThis').unbind('click');
	$('.popupThis').click(function(){
		
		var width = $(this).attr('width') || 900;
		windowNew($(this).attr('href'), width,650);
		return false;
	});
}




// open new popup window
// @todo Refactor this! Use thickbox ...
function windowNew( sAdres, iWidth, iHeight, sTitle, iReturn ){
  if ( !sTitle )
    sTitle = '';
  if( !iReturn )
    iReturn = false;
	
	if( +iHeight > 530 )
		iHeight = 530
	else
		iHeight = +iHeight + 40;

	var iX = ( screen.availWidth - iWidth ) / 2;
	var iY = ( screen.availHeight - iHeight ) / 2;

  var refOpen = window.open( sAdres, sTitle, "height="+iHeight+",width="+iWidth+",top="+iY+",left="+iX+",resizable=yes,scrollbars=yes,status=0;" );
  
  
  	return refOpen
}

