var Favorites = new function() {
	// days to store cookie
	this.days = 30;
    this.currentActiveDiv = null;

	// cookie functions - taken from quirksmode.org/...
	this.createCookie = function(name, value, days) {
		if(days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}

		document.cookie = name+"="+value+expires+"; path=/";
	}

	this.readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');

		for(var i = 0, ic = ca.length; i < ic; ++ i) {
			var c = ca[i];

			while(c.charAt(0) == ' ')
				c = c.substring(1, c.length);

			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
		}

		return '';
	}

	this.eraseCookie = function(name) {
		this.createCookie(name,"",-1);
	}

	// initializing script - take cookie string and splits it into objects list
	this.list = [];

	var list = decodeURIComponent(this.readCookie('fav_list'));
	if(list) {
		var THIS = this;
		$.each(list.split(','), function(key, value) {
			if(value === null || isNaN(1 * value)) {
				return;
			}
			THIS.list.push(1 * value); 
		});
	}

	// inserts toggle link, marked as active if it is presented in the list
	this.link = function(id) {
		var THIS = this;
		return $("<a href=\"#\" id=\"fav_" + id + "\""+ ($.inArray(id, this.list) != -1 ? " class=\"active\"" : "") + ">&nbsp;</a>").bind("click", function() {THIS.linkClick(this); return false;});
	}

	// listener - observes for link CLICK events 
	this.linkClick = function(link) {

		var id = 1 * link.id.substring(4);

		if(!$(link).hasClass("active")) {
			if($.inArray(id, this.list) == -1) {
				this.addEntry(id);
				$(link).addClass("active");
			}
		} else {
			if($.inArray(id, this.list) != -1) {
				this.removeEntry(id);
				$(link).removeClass("active");
			}
		}
	}

	// adds entry to the list and cookie
	this.addEntry = function(id) {
		this.list.push(id);
		
		this.updateCookie();
		
		var THIS = this;
		$.each(
			this.totalLinks,
			function() {
				this.html(THIS.generateLinkText(THIS.list.length));
				if(THIS.list.length > 0 && this.is("a")) {
					this.css("visibility", "visible");;
				}
			}
		);
	}

	// removes entry from the list and cookie
	this.removeEntry = function(id) {

		if((pos = $.inArray(id, this.list)) == -1) {
			return;
		}

		this.list.splice(pos, 1);

		this.updateCookie();

		var THIS = this;
		$.each(
			this.totalLinks,
			function() {
				this.html(THIS.generateLinkText(THIS.list.length));
				if(THIS.list.length < 1 && this.is("a"))
					this.css("visibility", "hidden");
			}
		);
	}

	// serializes the list values and writes it to cookie
	this.updateCookie = function() {
		this.createCookie("fav_list", this.list.join(','), this.days);
	}

	// displays link with number of selected entries
	this.totalLinks = new Array();
	this.totalLink = function(href) {
		
		var link = $("<a id=\"total_link_" + this.totalLinks.length + "\" href=\"" + href + "\" class=\"fav_total\">" + this.generateLinkText(this.list.length) + "</a>");
		this.totalLinks.push(link);

		if(this.list.length < 1) {
			link.css("visibility", "hidden");;
		}

                return link;
	}
	
	// display span with number of selected entries
	this.totalSpan = function(prefix, postfix) {
		
		var link = $(prefix + "<span id=\"total_link_" + this.totalLinks.length + "\">" + this.generateLinkText(this.list.length) + "</span>" + postfix);
		this.totalLinks.push(link);
                return link;
	}

	// generates text "You've got XX selected ads"
	this.generateLinkText = function(total) {
		var mod10 = total % 10;
		var mod100 = total % 100;
		if(mod10 == 1 && mod100 != 11) {
			suff1 = 'ое';
			suff2 = 'е';
		} else if(mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) {
			suff1 = 'ых';
			suff2 = 'я';
		} else {
			suff1 = 'ых';
			suff2 = 'й';
		}

		return (total ? total : "Нет") + " выбранн" + suff1 + " объявлени" + suff2;
	}
}
