﻿// 	depends 
//		<%= script("jquery.sortelements.js") %>
// 		<%= script("jquery.cookie.js") %>


var website = {};

website.swith = function () {
	$(".view_switch a").click(function () {
		$("header").toggleClass("grid").toggleClass("list");
		$(".switch").toggleClass("msa"); // fix webkit bug with "~"
		return false;
	});
};

website.sort_save = function(){
	var selected_tags = $("#left_column .tags .tag.selected").ids();
	var selected_cats = $("#left_column .cats .tag.selected").ids();
	
	var cookie_option = { path: '/' };
	$.cookie('tags', selected_tags, cookie_option);
	$.cookie('cats', selected_cats, cookie_option);
};
	
website.sort_restory = function(){
	var selected_tags = $.cookie('tags');
	selected_tags = selected_tags == null ? [] : selected_tags.split(",");

	var selected_cats = $.cookie('cats');
	selected_cats = selected_cats == null ? [] : selected_cats.split(",");
	
	$(selected_tags).each(function(){
		var tag_id = "" + this;
		$("#left_column .tags .tag[data-id='" + tag_id + "']").addClass("selected");
	});

	$(selected_cats).each(function(){
		var cat_id = "" + this;
		$("#left_column .cats .tag[data-id='" + cat_id + "']").addClass("selected");
	});
};

website.sort = function (thresholds) {
	// default thresholds	
	if (!thresholds)
		thresholds = {
			"80": "Excellent match",
			"60": "Good match",
			"40": "Some matches",
			"0": "No matches"
		};
	
	// work around Chome and Firefox object property sort order
	var thresholds_a = []
	// make array from threshold object, for sorting
	$.each(thresholds, function(threshold_val, threshold_name){
		thresholds_a.push({ val: threshold_val, name: threshold_name });
	});
	// sort array by threshold.val
	thresholds_a.sort(function(a,b){ return b.val - a.val; });

	function sort_by_seleted_tags_and_cats() {
		var selected_tags = $("#left_column .tags .tag.selected").ids();
		var selected_cats = $("#left_column .cats .tag.selected").ids();
		var selected_size = selected_tags.length + selected_cats.length;

		function dispaly_match_value(article, relativ_count) {
			var precent = 100 * relativ_count / selected_size;

			var name = "";

			for (var i = 0; i < thresholds_a.length; i++ )
			{
				var threshold = thresholds_a[i];
				if (precent >= threshold.val){
					name = threshold.name;
					break;
				}
			}

			$(".relative", article).text(name);
		}

		function relativ(article) {
			var article_cats,
			article_tags,
			relativ_count;

			article_tags = $(".tags .tag", article).ids();
			article_cats = $(".cats .tag", article).ids();
			relativ_count = 0;

			$(article_tags).each(function () {
				if ($.inArray("" + this, selected_tags) !== -1) {
					return relativ_count++;
				}
			});
			$(article_cats).each(function () {
				if ($.inArray("" + this, selected_cats) !== -1) {
					return relativ_count++;
				}
			});

			dispaly_match_value(article, relativ_count);

			return relativ_count;
		}

		$(".article").fadeTo("normal", 0.6);

		$("#content section").each(function(){
			var section = $(this);
			
			$(".article", section).sortElements(function (a, b) {
				var rel_a,
				rel_b;
				rel_a = relativ(a);
				rel_b = relativ(b);
				return rel_b - rel_a;
			});
		});

		$(".article").fadeTo("normal", 1);
	};

	$("#left_column .tag").click(function () {
		var tag = $(this);

		tag.toggleClass("selected");

		website.sort_save();

		if ($("article").hasClass("single") == false)
		{
			sort_by_seleted_tags_and_cats();
			return false;
		}
	});
	
	website.sort_restory();
	
	if ($("article").hasClass("single") == false)
		sort_by_seleted_tags_and_cats();
};

$(function () {
	website.swith();
});
