// クッキーを使用したブックマーク管理クラス
function Bookmark()
{
    var COOKIENAME    = "Bookmark";
    this.urls        = new Array();
    this.titles        = new Array();
    this.prefs        = new Array();
    this.images        = new Array();
    this.infos          = new Array();
    this.times          = new Array();

    Bookmark.prototype.load            = _load;
    Bookmark.prototype.save            = _save;
    Bookmark.prototype.clearSave    = _clearSave;
    Bookmark.prototype.add            = _add;
    Bookmark.prototype.remove        = _remove;
    Bookmark.prototype.numOfItems    = _numOfItems;
    Bookmark.prototype.getUrl        = _getUrl;
    Bookmark.prototype.getTitle        = _getTitle;
    Bookmark.prototype.getPref        = _getPref;
    Bookmark.prototype.getImage        = _getImage;
    Bookmark.prototype.getInfo        = _getInfo;
    Bookmark.prototype.getTime        = _getTime;

    _loadTo(this);

    return this;

    //
    function _loadTo(obj)
    {
        // 再初期化
        obj.urls        = new Array();
        obj.titles        = new Array();
        obj.prefs        = new Array();
        obj.images        = new Array();
        obj.infos        = new Array();
        obj.times        = new Array();

        cookies    = getCookies();
        if ( cookies[COOKIENAME] != undefined )
        {
            list = cookies[COOKIENAME].split("\t");
            for (i = 0; i < list.length; i++)
            {
                pair = list[i].split("\\");
                obj.urls[i]    = pair[0];
                obj.titles[i]    = pair[1];
                obj.prefs[i]    = pair[2];
                obj.images[i]    = pair[3];
                obj.infos[i]    = pair[4];
                obj.times[i]    = pair[5];

            }
        }
    }

    //
    function _load()
    {
        _loadTo(this);
    }

    //
    function _save(expiredays)
    {
        if ( this.numOfItems() > 0 )
        {
            list = new Array();
            for (i = 0; i < this.numOfItems(); i++)
            {
                list[i] = this.urls[i] + "\\" + this.titles[i]+ "\\" + this.prefs[i]+ "\\" + this.images[i] + "\\" + this.infos[i] +"\\" + this.times[i];
            }
            value = list.join("\t");
            setCookie(COOKIENAME, value, expiredays);
        }
    }

    function _clearSave()
    {
        setCookie(COOKIENAME, "", -1);
        this.urls    = new Array();
        this.titles    = new Array();
        this.prefs    = new Array();
        this.images    = new Array();
        this.infos    = new Array();
        this.times    = new Array();
    }

    function _add( url, title, pref, image, info, time)
    {
        //check
        list_check = new Array();
        var cnt = 0;
        var str_cookie="";
        var str_cookie_new="";
        pair = new Array();
        _loadTo(this);
        str_cookie_new=url + "\\" + title+ "\\" + pref + "\\" + image;
        str_cookie_time_new=url + "\\" + title+ "\\" + pref;
        
        cookies    = getCookies();
        
        if ( cookies[COOKIENAME] != undefined )
        {
            list_check = cookies[COOKIENAME].split("\t");
            for (k = 0; k < list_check.length; k++)
            {
                str_cookie="";
                pair = list_check[k].split("\\");
                str_cookie = pair[0] + "\\" + pair[1]+ "\\" + pair[2]+ "\\" + pair[3];
                str_cookie_time = pair[0] + "\\" + pair[1]+ "\\" + pair[2];
                
                //if(str_cookie_new==str_cookie)
                if(str_cookie_time_new==str_cookie_time)
                {
                    cnt=cnt+1;
                }
            }
        }
        
        //check
        i = this.numOfItems();
        if(cnt==0){
            this.urls[i]    = url;
            this.titles[i]    = title;
            this.prefs[i]    = pref;
            this.images[i]    = image;
            this.infos[i]   = info;
            this.times[i]   = time;
        }
    }

    function _remove(index)
    {
        newUrls        = new Array();
        newTitles    = new Array();
        newPrefs    = new Array();
        newImages    = new Array();
        newInfos    = new Array();
        newTimes    = new Array();
        
        j = 0;
        for ( i=0; i<this.numOfItems(); i++)
        {
            if ( i!=index )
            {
                newUrls[j]    = this.urls[i];
                newTitles[j]    = this.titles[i];
                newPrefs[j]    = this.prefs[i];
                newImages[j]    = this.images[i];
                newInfos[j]    = this.infos[i];
                newTimes[j]    = this.times[i];
                j++;
            }
        }
        c=this.numOfItems();
        
        if(c==1)
        {
            this.urls    = new Array();
            this.titles    = new Array();
            this.prefs    = new Array();
            this.images    = new Array();
            this.infos    = new Array();
            this.times    = new Array();
            setCookie(COOKIENAME, "", -1);
        }
        else
        {
            this.urls    = new Array();
            this.titles    = new Array();
            this.prefs    = new Array();
            this.images    = new Array();
            this.infos    = new Array();
            this.times    = new Array();
            
            this.urls    = newUrls;
            this.titles    = newTitles;
            this.prefs    = newPrefs;
            this.images    = newImages;
            this.infos    = newInfos;
            this.times    = newTimes;
        }
    }

    //
    function _numOfItems()
    {
        return this.urls.length;
    }

    function _getUrl(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.urls[index] : "";
    }

    function _getTitle(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.titles[index] : "";
    }
    function _getPref(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.prefs[index] : "";
    }
    function _getImage(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.images[index] : "";
    }
    function _getInfo(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.infos[index] : "";
    }
    function _getTime(index)
    {
        return ( index >= 0 && index < this.numOfItems() ) ? this.times[index] : "";
    }

}
