jQuery(function() {
    jQuery.each(jQuery('.kininaruList a'), function(index, obj) {
        jpartObserve2(this, 'add');
    });
    jQuery.each(jQuery('.kininaruListDel a'), function(index, obj) {
        jpartObserve2(this, 'remove');
    });
});

function jpartObserve2(favoriteBtn, actionType)
{
    Element.observe(favoriteBtn, 'click', function() {
        setJpartCookie2(actionType, favoriteBtn.parentNode.id);
        showFavoriteMsg2(actionType);
        jQuery.each(jQuery('.' + favoriteBtn.parentNode.className + ' a'), function(index, similarBtn) {            
            if (similarBtn.parentNode.id == favoriteBtn.parentNode.id) {
                similarBtn.parentNode.className = (actionType == 'add' ? 'kininaruListDel' : 'kininaruList');
                Event.stopObserving(similarBtn, 'click');
                setTimeout(function() {jpartObserve2(similarBtn, actionType == 'add' ? 'remove' : 'add');}, 1000);
            }
        });
        
        favoriteBtn.parentNode.className = (actionType == 'add' ? 'kininaruListDel' : 'kininaruList');
    });
}

function showFavoriteMsg2(actionType)
{
    var msgHtml = "（＋）気になるリストに追加しました";
    if (actionType == 'remove') {
        msgHtml = "（ー）気になるリストから削除しました";
    }
    jQuery.notifyBar({
         html: msgHtml,
         delay: 1000,
         animationSpeed: "normal"
    });
}

function setJpartCookie2(actionType, offerId)
{
	offerId = String(offerId).replace(/^0*/, '');
    var EXPIRE_LIMIT = 1000 * 60 * 60 * 24 * 7 * 2;
    var dat = new Date();
    var limitTime = (actionType == 'add') ? (dat.getTime() + EXPIRE_LIMIT) : (dat.getTime() - EXPIRE_LIMIT);
    dat.setTime(limitTime);
    expire = dat.toGMTString();
    document.cookie = 'jpartOfferFavorite[' + offerId + ']=' + Math.floor((new Date().getTime() / 1000)) + ';path=/;expires='+expire;
}
