// JavaScript Document
$(document).ready(function(){

	$(".multiple_input .actions a.add").click(function () { 
		var is_first = false;
		var is_friend = false;
		var parent = $(this).parent().parent().parent();
		if (parent.hasClass("first")) {
			parent.removeClass('first');
			is_first = true;
		}
		var clone = parent.clone(true).fadeIn('slow').insertAfter(parent);
		
		// normal text inputs
		clone.children().children().children('.no_clone').val('');
		clone.children().children().removeClass('error');
		
		// checkboxes in radio group
		clone.children().children().children().children().children('.no_clone_checkbox').removeAttr('checked');
		
		if (is_first) {
			parent.addClass('first');
		}
		
		return false;
	});

	$(".multiple_input .actions a.delete").click(function () { 
		var parent = $(this).parent().parent().parent(); 
		parent.fadeOut('normal',function() { $(this).remove() });
		
		return false;
	});
	$(".multiple_input .actions a.delete_no_confirm").click(function () { 
		var parent = $(this).parent().parent().parent(); 
		parent.fadeOut('normal',function() { $(this).remove() });
		
		return false;
	});
	
	$('a.delete').click(function(){
		if (confirm($(this).attr('title'))) {
			return true;
		} else {
			return false;	
		}			 
	});
	
});

function datepicker_update(source) {
	if ($(source).val() == ' ') {
		var parent = $(source).parent().parent();
		var el = parent.children().children('.date_start');
		$(source).val(el.val());
	}
}

function toggle_date_detail(id) {
	
	var destination = $('#date_detail_'+id);
	
	if( destination.children().size() > 0) {
		if(destination.css('display') == 'none') {
			destination.fadeIn('normal').show();
		} else {
			destination.fadeOut('normal');	
		}
	} else {
		update_html('#date_detail_'+id,'/lib/ajax/date_detail.php?id='+id);	
	}
		
}

function toggle(id) {

var destination = $(id);
	destination.toggle().fade();
	
	//update_html('#comment_holder_'+id,'/lib/ajax/confirmation_comment.php?id='+id);
}

function update_html(el,source) {

	console.log(source);
	
	$(el).html(get_ajax_loader());
	
	$.ajax({
		type: "GET",
		url: source,
		cache: true,
		dataType: "html",
		error: function() {
			$(el).html(get_ajax_error());
		},
		success: function(html) {
			$(el).html(html);	
			
		}
	});
	
}

function append_html(el,source,loader) {
	
	$(loader).html(get_ajax_loader());
	
	$.ajax({
		type: "GET",
		url: source,
		cache: true,
		dataType: "html",
		error: function() {
			$(el).append(get_ajax_error());
		},
		success: function(html) {
			$(el).append(html);	
			$(loader).remove();
		}
	});
	
}

function get_ajax_loader() {
	
	return '<div class="ajax_loader"><img src="'+ HTTP_ROOT +'css/img/loader.gif"></div>';
	
}
function get_ajax_error() {
	
	return '<div class="ajax_error">Er is iets fout gegaan bij het ophalen van de data. Probeer het opnieuw.</div>';
	
}

function load_more_activity(start_at) {
	var list = $('#activity_list');
	l
}

function load_confirmation_comment(id) {
	
}

function poke(id) {
	
	$('#friend_'+id+' .actions .resend_holder').html('<img src="'+ HTTP_ROOT +'css/img/loader_small.gif">');
	
	$.getJSON(HTTP_ROOT + 'lib/ajax/poke.php?id='+id, function(data) {
		if (data.response == '100') {
			css_class = 'notify';
			$('#friend_'+id+' .actions .resend_holder').html('<img src="'+ HTTP_ROOT +'css/img/icons/accept.png">');
		} else {
			css_class = 'error';	
			$('#friend_'+id+' .actions .resend_holder').html('<a href="javascript:poke('+id+')" class="resend">Resend invitation</a>');
		}
		$('#friend_'+id+' .invited').html('<div class="'+css_class+'">' + data.text + '</div>').fadeIn('slow');
	});

	
}

