var dimension = 27;
var border = 10.0;

function movePosition(type, times) {
	pos = $.cookie('interest_pos');
	if (pos == null) {
		plist = null
	}
	else {
		ps = pos.split(':');
		plist = new Array();
		for (i = 0; i < ps.length; i++) {
			axis = Number(ps[i]);
			if (!isNaN(axis))
				plist.push(axis);
		}
		if (plist.length != dimension)
			plist = null;
	}
	if (plist == null) {
		plist = new Array();
		for (i = 0; i < dimension; i++)
			plist.push(0.0);
	}
	type = parseInt(type);
	temp = plist[type] / border / 2 - 0.5;
	temp *= times;
	plist[type] = (temp + 0.5) * border * 2;
	pos = '';
	for (i = 0; i < dimension - 1; i++)
		pos += plist[i].toString() + ':';
	pos += plist[dimension - 1].toString();
	$.cookie('interest_pos', pos, {expires: 365, path: '/'});
}

function parseXml(data) {
	if ($.browser.msie) {
		var xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.loadXML(data);
		return xml;
	}
	return data;
}

function generateItem(data, act) {
	var url = $(data).find('url').text();
	var title = $(data).find('title').text();
	var id = $(data).find('id').text();
	var type = $(data).find('type').text();
	var show = title;
	var width = 0;
	for (i = 0; i < show.length; i++)
		if (/\w/.test(show.charAt(i)))
			width += 1;
		else
			width += 3;
	if (width > 36) {
		width = 0;
		for (i = 0; i < show.length; i++) {
			if (/\w/.test(show.charAt(i)))
				width += 1;
			else
				width += 3;
			if (width > 30)
				break;
		}
		show = show.substr(0, i) + '...';
	}
	var ret = '<li><span>·</span><a href="' + url + '" title="' + title + '" class="link">' + show + '</a>';
	ret += '<img src="../img/' + act + '.gif" alt="" class="' + act + '" >';
	ret += '<input type="hidden" name="id" value="' + id + '" />';
	ret += '<input type="hidden" name="type" value="' + type + '" /></li>';
	return ret;
}

function removeFavorite(id) {
	fav = $.cookie('favorite_page');
	flist = fav.split(':');
	fav = '';
	for (i = 0; i < flist.length; i++)
		if (flist[i] != id.toString()) {
			fav += parseInt(flist[i]).toString();
			if (i < flist.length - 1)
				fav += ':';
		}
	$.cookie('favorite_page', fav, {expires: 365, path: '/'});

	var fav = $("#site_list_favorite");
	fav.find("input[name='id'][value='" + id.toString() + "']").parent().remove();
	if (fav.find('ul li').length == 0)
		fav.fadeOut('slow');
}

function addFavorite(id) {
	fav = $.cookie('favorite_page');
	if (fav == null) {
		fav = id.toString();
	}
	else {
		flist = fav.split(':');
		fav = '';
		for (i = 0; i < flist.length; i++)
			fav += parseInt(flist[i]).toString() + ':';
		fav += id.toString();
	}
	$.cookie('favorite_page', fav, {expires: 365, path: '/'});

	$.get(
		'/navigator/page?id=' + id.toString(),
		function(data) {
			data = parseXml(data);
			ul = $('#site_list_favorite ul');
			oldc = $('li', ul).length;
			$(generateItem(data, 'remove')).appendTo(ul).find('img.remove').click(function(e) {
				e.preventDefault();
				removeFavorite($(this).parent().find('input[name="id"]').val());
			});
			if (oldc == 0)
				ul.parent().fadeIn('slow');
			movePosition($(data).find('type').text(), 0.8);
		}
	)
}

function refreshNavigator() {
	$.get(
		'/navigator/',
		function(data) {
			data = parseXml(data);
			var list = '';
			$(data).find('page').each(function() {
				list += generateItem(this, 'add');
			});
			$('#site_list_hot ul').fadeOut('slow', function() {
				$(this).find('li:not(.button)').remove();
				$(this).prepend(list).find('li:odd').addClass('shadow');
				$(this).fadeIn('slow');
				$(this).find('a.link').click(function() {
					movePosition($(this).parent().find('input[name="type"]').val(), 0.95);
				});
				$(this).find('img.add').click(function(e) {
					e.preventDefault();
					addFavorite($(this).parent().find('input[name="id"]').val());
				});
			});
		}
	);
}

function fetchFavorite() {
	$.get(
		'/navigator/fav',
		function(data) {
			data = parseXml(data);
			var list = '';
			$(data).find('page').each(function() {
				list += generateItem(this, 'remove');
			});
			var fav = $('#site_list_favorite');
			fav.find('ul').html(list);
			if (list != '')
				fav.fadeIn('slow');
			fav.find('img.remove').click(function(e) {
				e.preventDefault();
				removeFavorite($(this).parent().find('input[name="id"]').val());
			});
		}
	);
}

$(function() {
	refreshNavigator();
	fetchFavorite();
	$('#site_list_hot input[type=button]').click(function() {
		refreshNavigator();
	});
});
