$(document).ready( function () {
	
	$('[data-url]').on('click', function(e) {
    	window.location.href = $(this).data('url');
	});
	
    $( ".datepicker" ).datepicker({
    });
	
	$('select[multiple] > option').mousedown(function(e) {
		if(e.which==1) {
		    e.preventDefault();
		    $(this).prop('selected', !$(this).prop('selected'));
		    return false;
		}
	});
	
	$('.filters-bar').children('select[data-filter]').each(function () {
		var filter_name = $(this).data('filter');
		if(filter_name in loc_qry) {
			$(this).val(loc_qry[filter_name]);
		}
	});
	
	$('#stories').on('change', 'select[data-filter]', function() {
		
		var filters = [];
		var filtersbar = $('#stories > .filters-bar');
		
		filtersbar.children('select').each(function () {
			if (this.value.length > 0) {
				filters.push($(this).data('filter') + "=" + $(this).val());
			}
		});
		
		var url = document.URL;
		var target = url.split('?')[0];
		if (filters.length > 0) {
			target = target + '?' + filters.join('&');
		}
		window.location.href = target;
	});

	$('#stories').on('click', 'a[data-page]', function() {
		
		var filters = [];
		var filtersbar = $('#stories > .filters-bar');
		
		filtersbar.children('select').each(function () {
			if (this.value.length > 0) {
				filters.push($(this).data('filter') + "=" + $(this).val());
			}
		});
		
		var url = document.URL;
		var target = url.split('?')[0];
		if (filters.length > 0) {
			target = target + '?' + filters.join('&') + '&page=' + $(this).data('page');
		} else {
			target = target + '?page=' + $(this).data('page');
		}

		window.location.href = target;
	});
	
	$('#new-story').on('click', function(event) {
		
		event.preventDefault();
		
		var filters = [];
		var filtersbar = $('#stories > .filters-bar');
		
		filtersbar.children('select').each(function () {
			if (this.value.length > 0) {
				filters.push($(this).data('filter') + "=" + $(this).val());
			}
		});
		
		var url = $('#new-story').attr('href');
		var target = url.split('?')[0];
		if (filters.length > 0) {
			target = target + '?' + filters.join('&');
		}
		window.location.href = target;
	});
	
	var epic = getUrlParameter('epic');
	var name = getUrlParameter('name');
	var weight = getUrlParameter('weight');
	var assignee = getUrlParameter('assignee');
	var sprint = getUrlParameter('sprint');
	
	if (epic) { $('#id_epic').val(epic); }
	if (name) { $('#id_name').val(name); }
	if (weight) { $('#id_weight').val(weight); }
	if (assignee) { $('#id_assignees').val([assignee]); }
	if (sprint) { $('#id_sprints').val([sprint]); }
	
	$(document).on('click', function(event) {
	    if(!$(event.target).closest('#user-panel').length) {
	        if($('#user-dropdown').is(":visible")) {
	            $('#user-dropdown').hide();
	        }
	    }
	    if(!$(event.target).closest('#notif-panel').length) {
	    	if($('#notif-dropdown').is(":visible")) {
	    		$('#notif-dropdown').hide();
	    	}
	    }
	});
	
	$(document).on('click', '#user-show-btn', function(event) {
		event.preventDefault();
		if($('#user-dropdown').is(":visible")) {
			$('#user-dropdown').hide();
		}
		else {
			$('#user-dropdown').show();
		}
	});
	
	$(document).on('click', '#notif-show-btn', function(event) {
		event.preventDefault();
		if($('#notif-dropdown').is(":visible")) {
			$('#notif-dropdown').hide();
		}
		else {
			$('#notif-dropdown').show();
		}
	});
	
	$(".notif-seen").click(function(event) {
		event.preventDefault();
		var notif = $(this).closest('.notif');
		var notif_id = notif.data('id');
		
        $.ajax({
            type: "POST",
            url: "/notif/seen/" + notif_id + "/",
            data: '{notif_id:' + notif_id + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            },
            success: function (response) {
            	notif.remove();
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
	});
	
	$(".notif-all-seen").click(function(event) {
		event.preventDefault();
		var notif_list = $('#notif-dropdown').find('.notif-list');
		if (confirm('Êtes vous sûr de vouloir faire disparaitre toutes les notifications ?')) {
			$.ajax({
				type: "POST",
				url: "/notif/allseen/",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				beforeSend: function(xhr, settings) {
					if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
						xhr.setRequestHeader("X-CSRFToken", csrftoken);
					}
				},
				success: function (response) {
					notif_list.html('');
					$('.notif-footer').html('<i>Aucune notification à afficher</i>');
					$('.notif-count').html('0');
					$('#notif-dropdown').hide();
				},
				failure: function (response) {
					alert(response.responseText);
				},
				error: function (response) {
					alert(response.responseText);
				}
			});
		}
	});
	
});