/*
	editfavorite.js お気に入り編集用
*/

var e2f_maxlinks = 20;
var e2f_PIfavorite_listinview = 0;
var e2f_PIfavorite_opened = false;

function AddFavorite(id, newt, newu) {
	var at = new Array();
	var au = new Array();
	var m = 0;
	for (var i = 0; i < 20; ++i) {
		var t = e2f_getCookie(id + 't' + i);
		var u = e2f_getCookie(id + 'u' + i);
		if (t != null && u != null) {
			if (newu == u && i <= 1)
				return;
			if (newu == u)
				continue;
			at[m] = t;
			au[m] = u;
			++m;
		}
	}
	var s = 0;
	if (m >= e2f_maxlinks)
		s = 1;
	var n = 0;
	e2f_setCookie(id + 't' + n, newt);
	e2f_setCookie(id + 'u' + n, newu);
	++n;
	for (var i = s; i < m; ++i) {
		e2f_setCookie(id + 't' + n, at[i]);
		e2f_setCookie(id + 'u' + n, au[i]);
		++n;
	}
	++n;
	while (n < e2f_maxlinks) {
		e2f_delCookie(id + 't' + n);
		e2f_delCookie(id + 'u' + n);
		++n;
	}
}

function e2f_getCookie(nm){
	if (document.cookie.length > 0) {
		var ofs = document.cookie.indexOf(nm + '=');
		if (ofs != -1) {
			ofs += nm.length + 1;
			var end = document.cookie.indexOf(';',ofs);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(ofs,end));
		}
	}
	return null;
}
function e2f_setCookie(name,value) {
	document.cookie = name + '=' + escape(value)
	 + '; expires=Thu, 31 Dec 2037 01:00:00 GMT; path=/;';
}
function e2f_delCookie(name) {
	document.cookie = name + '; expires=Sat, 01 Jan 2000 00:00:00 GMT; path=/;';
}

function e2f_PIfavorite_openclose(id) {
	e2f_PIfavorite_makelist(id, -1);
}
function e2f_PIfavorite_add1(id, url, wtitle, redraw) {
	AddFavorite(id, wtitle, url);
	if (redraw) {
		var n = 2;
		if (e2f_PIfavorite_opened)
			n = e2f_maxlinks;
		e2f_PIfavorite_makelist(id, n);
	}
}

function e2f_PIfavorite_makelist(id, n) {
	var dv = document.getElementById(id + 'List');
	if (dv != null) {
		var links = e2f_parsecookie(id);
		var s = '';
		var c = 0;
		if (n == -1) {
			if (e2f_PIfavorite_opened)
				n = 2;
			else
				n = e2f_maxlinks;
			e2f_PIfavorite_opened = !e2f_PIfavorite_opened;
		}
		for (i in links) {
			var s1 = '<li><a href="' + links[i][2]  + '">' + links[i][1] + '</a></li>';
			s = s + s1;
			if (++c >= n)
				break;
		}
		if (s != '')
			s = '<ul>' + s + '</ul>';
		if (n == 2 && links.length > 2)
			s = s + '<div class="viewall" onclick="e2f_PIfavorite_openclose(' + "'" +  id + "'" + ');" title="全部表示"><span>もっと見る...</span></div>';
		dv.innerHTML = s;
		e2f_PIfavorite_listinview = c;
	}
}

function e2f_parsecookie(id) {
	var links = new Array();
	var m = 0;
	for (var i = 0; i < e2f_maxlinks; ++i) {
		var t = e2f_getCookie(id + 't' + i);
		var u = e2f_getCookie(id + 'u' + i);
		if (t != null && u != null) {
			links[m] = new Array(1, t, u);
			++m;
		}
	}
	return links;
}

