$(document).ready( function () {
	
	$('[data-url]').on('click', function(e) {
    	window.location.href = $(this).data('url');
	});
	
	$('option').mousedown(function(e) {
	    e.preventDefault();
	    $(this).prop('selected', !$(this).prop('selected'));
	    return false;
	});
	
	var loc_uri = URI.parse(window.location.href);
	var loc_qry = URI.parseQuery(loc_uri.query)
	
	$('.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;
	});
	
	
	$(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();
		}
	});
	
	
});