$(function(){		// DOM onload
	$('a').click(function(){ $(this).blur(); });
	if ($.browser.mozilla)
		$('a.thickbox').lightBox();
	$('#content-center-white h2:first, #content-center-white h1:first').css('padding-top', '5px');
	
	
	initMenuEvents();
	initUserEvents();
});



function initMenuEvents()
{
	//show menu items that are expanded
	$('#menu li.expanded ul').show();
	
	
	$('#menu li.expandable span').click(function(){
		$(this).parent().toggleClass('expanded').end().next().slideToggle();
	});
	
	$('#menu ul li').hover(
		function(){ $(this).addClass('hover'); },		// mouse over
		function(){ $(this).removeClass('hover'); }		// mouse out
	);
}


function initUserEvents()
{
	initRijSchoolAdmin();
	initMijnAccount();
	initStudents();
	
}





// url: /admin
function initRijSchoolAdmin()
{
	$('#activate-rij-button').click(function(ev){
		ev.preventDefault();
		$('#activeer-rij').val('true');
		$('#activation-rij-form').submit();
	});
	
	
	$('#deactivate-rij-button').click(function(ev){
		ev.preventDefault();
		$('#activeer-rij').val('false');
		$('#activation-rij-form').submit();
	});
	
	// show selected 
	$('#activate-rij-select, #deactivate-rij-select').change(function(){ aShowSelectedRijSchool(this); })
	
	
	// select all checkbox, class of checkbox'es to select is defines by rel
	$('.select-all').click(function(){
		var classname = $(this).attr('rel');
		if (this.checked)
			$('.' + classname + ':enabled').each(function(i){ this.checked = true; })
		else
			$('.' + classname + ':enabled').each(function(i){ this.checked = false; })
	});
	
	
	
	// feedback table			/admin/feedback
	$('.feedback td.clickable').click(function(){
		var id = $(this).parent().attr('rel');
		$('#feed-info').load('/admin/feedback/id-' + id, null, function(data){		// click on feedback request
			$('#feedback-respond').click(function(ev){		// RESPOND button
				ev.preventDefault();
				//load respond form
				$('#feedback-respond-form-holder').load('/admin/feedback/respond', null, function(){
					$('#r-subject').val('Re: ' + $('#feed-subject').html());
					$('#send-response').click(function(ev){													//click on SEND 
						var subj = $('#r-subject').val();
						var rbody = $('#r-body').val();
						alert(rbody);
						$.post('/admin/feedback/send', {'id': id, 'subject': subj, 'body': rbody}, function(data){
							if (data == 1)
								window.location.href = window.location.href;
							else			//error sending email
							{
								alert('Error sending email');
							}
						});
					});
				});
			});
		}).show();
	});
	
	
	
	
}
// url: /mijnaccount
function initMijnAccount()
{
	//ajaxify checkbox'es for Rijbewijzen
	$(':checkbox[rel=rijbewijzen-group]').click(function(){
		var rsid = $('#rsid').val();
		var rbid = $(this).val();
		var active =  (this.checked) ? 1 : 0;
			
		$.post('/ajaxcb', {'action': 'rijbewijzen','rsid': rsid, 'rbid':rbid, 'active':active}, function(data){
			if (data == 1)
			{
				if (!active)
					$('#submitPreview' + rbid + ', #submitEdit' + rbid).attr({'disabled': 'disabled'});
				else
					$('#submitPreview' + rbid + ', #submitEdit' + rbid).attr({'disabled': ''});
			}
		});
		
		
	});
	
	
	
	
	//send feedback form validation
	$('#feedback-form').submit(function(ev){
		
		$('#subject').removeClass('error-input');
		$('#body').removeClass('error-input');
		$('#message').html('').hide();
		
		ev.preventDefault();
		var title = $('#subject').val();
		var body = $('#body').val();
		

		$.post('/ajaxcb', {'action':'feedbackvalidation','subj':escape(title), 'body':escape(body)}, function(data){
			if (data == 1)
				$.post('/ajaxcb', {'action':'feedbacksubmit', 'subj':title, 'body':body}, function(data){
					$('#message').html('Thank You for Your feedback').show();
					setTimeout('location.href="' + $('#feedback-holder a').attr('href') + '";', 3000);
				});
			else
			{
				if (data == 0)
					$('#message').html('Wait for a while to submit another feedback').show();
				else
					$('#' + data).addClass('error-input');
			}
		})
	});
	
	
	// ajaxify reviews, show currently selected review
	$('.reviews td.clickable').click(function(ev){
		var id = $(this).parent().attr('rel');
		$('#review-info').load('/reviews/id/' + id, null, function(data){
			//show review info
			$(this).show();
		});
	
	});
}




function aShowSelectedRijSchool(obj)
{
	var id = obj.options[obj.selectedIndex].value;
	
	$('#selected-data').load('/templates/new/scripts/rijschoolgegevens.php', {'id': id});
}



function initStudents()
{
	
	$('#students td.clickable').click(function(){
		var id = $(this).parent().attr('rel');
		
		$('#student-info').load('/students/id/'+id, null, function(data){
			$(this).show();
			
			$('#status-select').change(function(ev){
				var value = this[this.selectedIndex].value;
				if (value ==2)		// set to finished
				{
					$('#greet-msg').show();
				}
				else
				{
					$('#greet-msg').hide();
				}
			});
			
			
			$('#bSubmitStudentStatus').click(function(ev){
				ev.preventDefault();
				var statusObj = document.getElementById('status-select');
				var message = $('#studMessage').val();
				var id = $('#studRelationID').val();
				
				$.post('/students', {'status':statusObj.options[statusObj.selectedIndex].value, 'message':message, 'id':id, 'status_submit':1}, function(data){
					window.location.href = window.location.href;
				});
			});
		});
	});
	
	
	$('a.popup').click(function(ev){
		
		ev.preventDefault();
		
		var url = $(this).attr('href');
		var params = $(this).attr('rel').replace('|',',');
		
		window.open(url, '', params);
		
	});
}




