/**
 * Create a member's tooltip
 */
function tooltip(element,content) {	
	/* CONFIG */		
		xOffset = 20;
		yOffset = 150;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(element).hover(function(e){
		$('body').prepend('<p class="tooltip" id="tooltip">' + content + '</p>');
		$('#tooltip')
			.css('top',(e.pageY - yOffset) + 'px')
			.css('left',(e.pageX + xOffset) + 'px')
			.corner()
			.fadeIn('fast');
		$('body').css('cursor', 'default');
    },
	function(){	
		$('body').css('cursor', 'auto');
		$('#tooltip').remove();
    });
	
	$(element).mousemove(function(e){
		$('#tooltip')
			.css('top',(e.pageY - yOffset) + 'px')
			.css('left',(e.pageX + xOffset) + 'px');
	});			
};

/**
 * Set flystatus of a member
 */
function set_flystatus(by, status, who) {
	window.location = base_url + 'admin/flystatus/modify/' + by + '/' + status + '/' + who;
}

/**
 * Get roles for a member in a group
 */
function get_roles(group_id, member_id) {
	$('#role_id').html('<option>Rollen worden geladen..</option>');
	
	$.ajax({
		url: base_url + 'club/groups/ajax_roles/' + group_id + '/' + member_id,
		type: 'POST',
		cache: false,
		success: function(data) {
			$('#role_id').html(data);
		}
	});
}

/**
 * Select group/role combination form to url for "Rechten per groep/rol combinatie"
 */
function grouprole_go() {
	var grouprole = $('#grouprole').val();
	window.location = base_url + 'admin/authorization/groupright/' + grouprole;
}

/**
 * Filter "overzicht groepen" either alpha or by category
 */
function filter_groups_go(sort) {
	if(sort != 'fout') {
		window.location = base_url + 'club/groups/index/' + sort;
	}
}
 
/**
 * Confirm before proceeding to a link popup
 */
function popup_confirm(msg,link) {
	var answer = confirm(msg);
	
	if(answer) {
		window.location = link;
	}
}

/**
 * Show general rights in document system
 */
function show_general_rights(setting) {
	if(typeof setting == 'undefined') {
		setting = false;
	}
	if(setting == 1) {
		$('#general_rights').css('display', 'none');
	} else {		
		$('#general_rights').css('display', 'table');
	}
}

/**
 * Javascript link
 */
function link(url) {
	window.location = url;
}

/**
 * Jump to flights page with specific date
 */
function go_starts_by_date(unix) {
	window.location = base_url + 'flight/start/specific/' + unix;
}

/**
 * (un)Check all working member types in memberlist selection screen
 */
function check_all_working() {
	var all_working = new Array('1', '2', '3', '4', '5', '6', '7');
	
	for(i = 0; i <= all_working.length; i++) {
		if($('#' + i).attr('checked')) {
			$('#' + i).attr('checked', false);
		} else {
			$('#' + i).attr('checked', true);
		}
	}
}

/**
 * Send the member selection type form
 */
function member_select_submit() {
	var url_part = '';
	
	$('input:checked').each(function(index,element) {
		if(element.id != 'all_working') {
			url_part += element.id + '-';
		}
	});
	
	url_part = url_part.substr(0, url_part.length-1);
	
	window.location = base_url + 'club/members/mlist/' + url_part;
}

/**
 * When changing from member type show the parent member field
 */
function check_membership_type(membershiptype) {
	if(membershiptype == 'Huisgenoot-lid') {
		$('#parent_member_tr').css('display', 'table-row');
	} else {
		$('#parent_member_tr').css('display', 'none');
	}
}

/**
 * Add bijlage veld to news item
 */
function news_add_attachment() {
	//determine the element we will put the tr after
	var element_put_after = $('#bijlages');
	for(i = 1; $('#bijlage_tr_' + i).length > 0; i++) {
		if($('#bijlage_tr_' + i).length > 0) {
			var element_put_after = $('#bijlage_tr_' + i);
		}
	}

	//create another bijlage field
	element_put_after.after('<tr id="bijlage_tr_' + i + '"><td class="noborder">&rarr; Bijlage ' + i + '</td><td class="noborder"><input type="file" name="bijlage_' + i + '" /> (<a onclick="news_delete_attachment(false,' + i + ')" href="#">verwijderen</a>)</td></tr>');
}

/**
 * Add bijlage veld to news item
 */
function news_delete_attachment(file_id,row_id) {
	//remove tablerow
	$('#bijlage_tr_' + row_id).remove();
	
	//perform ajax request to actually delete the attachment
	if(file_id != false) {
		$.ajax({
			url: base_url + 'admin/news/ajax_delete_attachment/' + file_id + '/'
		});
	}
}
