//##############################################################
jQuery.cookie = function (key, value, options) {
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);
		if (value === null || value === undefined) {
			options.expires = -1;
		}
		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		value = String(value);
		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
//##############################################################
function get_favorite()
{
	var get_ck = $.cookie('favorite_bid_rid');

	if(get_ck)
	{
		var bidrid_explode = get_ck.split('###');
		if(bidrid_explode)
		{
			return bidrid_explode;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
//##############################################################
function put_favorite(bid_rid,bname)
{
	if(bid_rid)
	{
		var bidrid_explode = get_favorite();
		var favorite_bid_rid = "";
		var bid_rid_arr = {};

		if(bidrid_explode.length > 0)
		{
			$.each(bidrid_explode, function(i, v)
			{
				if(v && v != "null")
				{
					bid_rid_arr[v] = v;
				}
			});
		}

		if((bid_rid.match(/###/i)))
		{
			var multi_bidrid_explode = bid_rid.split('###');

			if(multi_bidrid_explode.length > 0)
			{
				$.each(multi_bidrid_explode, function(i, v)
				{
					if(v && v != "null")
					{
						bid_rid_arr[v] = v;
					}
				});
				alert("お気に入りに登録しました");
			}
		}
		else
		{
			if(bid_rid_arr[bid_rid])
			{
				delete bid_rid_arr[bid_rid];
				alert("お気に入りから削除しました");
			}
			else
			{
				bid_rid_arr[bid_rid] = bid_rid;
				alert("お気に入りに登録しました");
			}
		}

		if(Object.keys(bid_rid_arr).length)
		{
			$.each(bid_rid_arr, function(i, v)
			{
				if(favorite_bid_rid){favorite_bid_rid += "###";}
				favorite_bid_rid += v;
			});
		}

		$.cookie('favorite_bid_rid',favorite_bid_rid,{path: '/', expires:30});
		change_favorite_mark();
	}
}
//##############################################################
function change_favorite_mark()
{
	$(".favor_mark img").attr("src","./source/img/list_ico_03.png");
	$(".modal_favor_btn").attr("src","./source/img/list_ico_03.png");

	var bidrid_explode = get_favorite();

	if(bidrid_explode.length > 0)
	{
		$.each(bidrid_explode, function(i, v)
		{
			if(v && v != "null" && !(v.match(/undefined/i)))
			{
				$("#favor_mark_" + v + " img").attr("src","./source/img/list_ico_03b.png");
				$("#modal_favor_btn_" + v).attr("src","./source/img/list_ico_03b.png");
			}
		});
	}
	$(".modal_favor_btn_list").attr("src","./source/img/list_ico_03b.png");
}
//##############################################################
