$(document).ready(function() {

    var urifilters = parseUri(window.location).queryKey.filter

	$('.source-tab').click(function() {
		name = $(this).find('a').attr('id');
		uri = parseUri(window.location);
		on = $(this).find('a').hasClass('on');
		if(uri.queryKey.filter != undefined)
		{
			if(on)
			{
				filters = uri.queryKey.filter.replace('+'+name, '');
				filters = filters.replace(name, '');
			}
			else {
				filters = uri.queryKey.filter + '+';
			}
		}
		else
		{
			filters = '';
		}
		if(filters.indexOf(name) >= 0)
		{
			return false;
		}
		if(filters[0] == '+')
		{
			filters = filters.substring(1);
		}
		if(on) name = '';
		newuri = 'http://' + uri.host + '/?filter=' + filters + name;
		console.log(newuri);return false;
		window.location = newuri;
		return false;
	});
	
    $('.more-arrow').bind("click", registerMores);

	var sindex = 2;
	$('#more').click(function() {
		content = $.get("/", { ajax: "true", s: (sindex-1)*10, filter: urifilters},
			function(data) {
				if(data == "\n")
				{
					$('#more').remove();
				}
				else 
				{
					$('#items').append(data);
                    $('#items').append('<h3><a href="#top">Back to top</a></h3>');
				}
                $('.more-arrow').bind("click", registerMores);
			}
		);
		sindex++;
		return false;
	});
});

function registerMores() {
    if($(this).html() == 'less')
    {
	    $(this).parent().parent().find('.stream-item-description').slideUp('fast');
	    $(this).html('more');
    }
    else
    {
	    $(this).parent().parent().find('.stream-item-description').slideDown('fast');
	    $(this).addClass('less-arrow');
	    $(this).html('less');
    }				
    return false;
}

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


