BookmarkManager = Class.create();
BookmarkManager.prototype =
{
	initialize: function()
	{
		this.mg = new CookieManager();
		currentBk = this.mg.getCookie('bookmarks');
		if (currentBk && currentBk.length > 0) {
			this.currentBk = currentBk.gsub("%2C", ","); 
			this.currentBkArr = this.currentBk.split(',');
		}
		this.bkMAX = 15;
	},
	
	addBookmark: function(id)
	{
		var oldBk = this.mg.getCookie('bookmarks');
		if (!this.currentBk || this.currentBk.length < 1) {
			newBk = id;
		} else if (!this.isExistBookmark(id)) {
			if (this.currentBkArr.length >= this.bkMAX) {
				if (confirm('お気に入りが'+this.bkMAX+'件登録されています。\n新たに登録すると、一番古いお気に入りが消去されます。\nよろしいですか？')) {
					newBk = this.currentBkArr.slice(1) + ',' + id;
				} else {
					return false;
				}
			} else {
				newBk = id + ',' + this.currentBk;
			}
		}
		this.mg.setCookie('bookmarks', newBk);
		alert('お気に入りに追加しました');
		return true;
	},
	
	delBookmark: function(id)
	{
		deleted = this.isExistBookmark(id);
		if (deleted) {
			this.mg.setCookie('bookmarks', deleted.toString(','));
		}
	},
	
	isExistBookmark: function(searchId)
	{
		if (!this.currentBk || this.currentBk.length < 1) {
			return false;
		}
		for (i = 0; i <= this.currentBkArr.length; i++) {
			if (this.currentBkArr[i] == searchId) {
				return this.currentBkArr.without(searchId);
			}
		}
		return false;
	},
	
	countBookmark: function()
	{
		if (this.currentBk) {
			count = this.currentBkArr.length;
		} else {
			count = 0;
		}
		return count;
	}
	
}
function getThumbs()
{
	XOTree = new XML.ObjTree();
	XOTree.force_array = ["bookmark"];
	bTree = XOTree.parseHTTP('/bookmarks.php?listXml&' + Math.random());
	var obj = bTree.bookmarks;
	var thumbs = obj.bookmark.length;
	if (thumbs > 6) { thumbs = 6; }
	for (i = 0; i < thumbs; i++) {
		src = '<img src="';
		if (obj.bookmark[i].status == 2) {
			src += '/obj/images/common/soldout.gif';
		} else if(obj.bookmark[i].thumb) {
			src += obj.bookmark[i].dir+obj.bookmark[i].thumb;
		} else {
			src += '/obj/images/common/no_image.jpg';
		}
		src += '" title="' + obj.bookmark[i].name + '" alt="" width="80" height="60">';
		if (obj.bookmark[i].status != 2) {
			src = '<a href="'+obj.bookmark[i].dir+'">' + src + '</a>';
		}
		$('book' + i).innerHTML = src;
	}
}

function bookmarkInit()
{
	bk = new BookmarkManager();
	if ($('addBookmark')) {
		if (bk.isExistBookmark($('addBookmark').value)) {
			$('addBookmark').style.display = 'none';
			$('booked').style.display = '';
		}
		Event.observe('addBookmark', 'click', addBookmark);
	}
	count = bk.countBookmark();
	if (count > 0 && $('book0')) {
		$('bookmarkCount').innerHTML = count + '台';
		getThumbs();
	}
}

function addBookmark()
{
	bk = new BookmarkManager();
	if (bk.addBookmark($('addBookmark').value)) {
		$('addBookmark').style.display = 'none';
		$('booked').style.display = '';
	}
	return false;
}

function delBookmark(id)
{
	bk = new BookmarkManager();
	bk.delBookmark(id);
	$('ccc' + id).style.display = 'none';
	$('cc' + id).style.display = 'none';
	$('c' + id).style.display = 'none';
	$('carCount').innerHTML--;
}

function bookmarkEstimate()
{
	var elm = Form.getInputs($('estimate'),'checkbox');
	var checked = 0;
	elm.each(
		function(currentElm,idx) {if(currentElm.checked) {checked++;}}
	);
	if(checked==0) {
		alert('チェックされた車がありません。');
		return false;
	} else {
		$('estimate').target = '_blank';
		$('estimate').submit();
		$('estimate').target = '';
	}
}

Event.observe(window,'load', bookmarkInit);
