/* ------------	::::: SORTING OF TAGS  ::::::----------------------------------------------
	
	I have 2 arrays categories_sorted_alpha & categories_sorted_pop, 
	these arrays are populated when the page loads. With the tag RegExp i match the name of 
	the tags and the popularity count and crate anonymous objects which i push to each array 	

	the categories_sorted_pop: 	is simply the categories as they are listed when the page is 
								first loaded; sorted by popularity by sql. 
	categories_sorted_alpha: 	is the categories sorted by tag name, this is done by the 
								compare_tags function which is passed to the sort method of
								the array.

	When the user click the 'Alfabetisk' or 'Popularitet' links i simple remove each of the
	previous listed tags and create a new list for each of the items in the array.

------------------------------------------------------------------------------------------*/
$(document).ready(function() {

	// globals 
	var categories_sorted_alpha 		= [];
	var categories_sorted_pop 			= [];
	var categories_sorted_user_count	= json_tags_rep;
	var tag = /(.*)(\(.*\))/;
	
	// add each of the categories to the categories arrays
	$("ul.kategori_liste li").each(function(){
		var matchset = $(this).text().match(tag);
		categories_sorted_pop.push({ 'name' : matchset[1], 'count' : matchset[2] });
		categories_sorted_alpha.push({ 'name' : matchset[1], 'count' : matchset[2] });
	});

	// Compare functions
	function compare_tags(tag1, tag2) {
		var first_tag = tag1.name.toLowerCase( );
		var second_tag = tag2.name.toLowerCase( );
		if (first_tag < second_tag) {return -1;}
		if (first_tag > second_tag) {return 1;}
		return 0;
	}
	function compare_tags_count(tag1, tag2) {
		var first_tag = tag1.count;
		var second_tag = tag2.count;
		if (first_tag > second_tag) {return -1;}
		if (first_tag < second_tag) {return 1;}
		return 0;
	}
	// now sort with the compare_tags function
	categories_sorted_alpha.sort(compare_tags);
	categories_sorted_user_count.sort(compare_tags_count);
	// Add the pseudo-class '.selected' when clicked
	$('#kategori_header ul a').bind('click',function(){
		$('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		return false;
	});

	// Whenever the user clicks the 'popularitet' link
	$('#kategori_header ul a').eq(0).bind('click', function(){
		$('ul.kategori_liste li').remove();
		$.each(categories_sorted_pop, function(i,n){
			$('<li><a href="/kategorier/index.php?tag=' + 
				categories_sorted_pop[i].name +'" target="_self">' + 
				categories_sorted_pop[i].name + '<span>' + categories_sorted_pop[i].count + '</span></a></li>')
			.appendTo('.kategori_liste');
		});
	});
	// Whenever the user clicks the 'alfabetisk' link 
	$('#kategori_header ul a').eq(2).bind('click', function(){
		$('ul.kategori_liste li').remove();
		$.each(categories_sorted_alpha, function(i,n){
			$('<li><a href="/kategorier/index.php?tag=' + 
				categories_sorted_alpha[i].name +'" target="_self">' + 
				categories_sorted_alpha[i].name + '<span>' + categories_sorted_alpha[i].count + '</span></a></li>')
			.appendTo('.kategori_liste');
		});
	});
	
	$('#kategori_header ul a').eq(1).bind('click', function(){
		$('ul.kategori_liste li').remove();
		$.each(categories_sorted_user_count, function(i,n){
			$('<li><a href="/kategorier/index.php?tag=' + 
				categories_sorted_user_count[i].name +'" target="_self">' + 
				categories_sorted_user_count[i].name + '<span>(' + categories_sorted_user_count[i].count + ')</span></a></li>')
			.appendTo('.kategori_liste');
		});
	});
});