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 && 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) {
        document.write(this.linkHTML(id));
    }

    // return HTML of toggle link, marked as active if it is presented in the list
    this.linkHTML = function(id) {
        var THIS = this;
        var html = "<a href=\"#\" onclick=\"Favorites.linkClick(this); return false;\" id=\"fav_" + id + "\""+ ($.inArray(id, this.list) != -1 ? " class=\"selected-i\" title=\"убрать из избранного\"" : " class=\"select-i\" title=\"добавить в избранное\"") + ">&nbsp;&nbsp;</a>";
        
        return html;
    }    
    
    // listener - observes for link CLICK events
    this.linkClick = function(link) {
        var id = 1 * link.id.substring(4);

        if(!$(link).hasClass("selected-i")) {
            if($.inArray(id, this.list) == -1) {
                this.addEntry(id);
                $(link).removeClass("select-i");
                $(link).addClass("selected-i");
            }
        } else {
            if($.inArray(id, this.list) != -1) {
                this.removeEntry(id);
                $(link).removeClass("selected-i");
                $(link).addClass("select-i");
            }
        }
    }

    // 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");
                }
            }
        );

        $("#total_selected").text(this.list.length);
        $("#word-a").text(this.generateLinkWord(this.list.length));

        if(this.list.length > 0) {
            $("#informer *").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");
            }
        );

        $("#total_selected").text(this.list.length);
        $("#word-a").text(this.generateLinkWord(this.list.length));

        if(this.list.length <= 0) {
            $("#informer *").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) {

        document.write("<a id=\"total_link_" + this.totalLinks.length + "\" href=\"" + href + "\" style=\"color: #c69;\">" + this.generateLinkText(this.list.length) + "</a>");
        var link = $("#total_link_" + this.totalLinks.length);
        this.totalLinks.push(link);

        if(this.list.length < 1) {
            link.css("visibility", "hidden");
        }
    }

    // displays informer with number of selected entries. Used in top of right column
    this.totalInformer = function(href) {

        document.write("<div id=\"informer\"><span id=\"total_selected\" class=\"sel-A\">" + this.list.length + "</span> <span id=\"total_span\" class=\"selected-a\"><a href=\"" + href + "\" id=\"word-a\">" + this.generateLinkWord(this.list.length) + "</a> в избранном</span></div>");
        var link = $("#total_selected");
        var span = $("#total_span");

        if(this.list.length < 1) {
            link.css("visibility", "hidden");
            span.css("visibility", "hidden");
        }
    }
	
    // display span with number of selected entries
    this.totalSpan = function(prefix, postfix) {

        document.write(prefix + "<span id=\"total_link_" + this.totalLinks.length + "\">" + this.generateLinkText(this.list.length) + "</span>" + postfix);
        var link = $("#total_link_" + this.totalLinks.length);
        this.totalLinks.push(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;
    }

    // 
    this.generateLinkWord = function(total) {
        var mod10 = total % 10;
        var mod100 = total % 100;
        if(mod10 == 1 && mod100 != 11) {
            suff = 'е';
        } else if(mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) {
            suff = 'я';
        } else {
            suff = 'й';
        }

        return "объявлени" + suff;
    }
}
