function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = '';
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}
function deleteAllBookmarks()
{
	if (page == 'view_all')
		$('.content').hide();
	else if (page == 'view_map')
		map.removeAllOverlays()
	
	setCookie('bookmarks', getCookie('bookmarks'), 'Thu, 01-Jan-70 00:00:01 GMT', folder);
	$('.bookmarks_count').html('0');
	$('.b_show').hide();
	$('.no_bookmarks').show();
	$('.bookmarks').html('');
}
function deleteAllWatch()
{
	setCookie('watch', getCookie('watch'), 'Thu, 01-Jan-70 00:00:01 GMT', folder);
	$('.no_watch').show();
	$('.w_show').hide();
}
function deleteBookmark(e_id)
{
	if (page == 'view_all')
		$('.del_' + e_id).parent().parent().parent().hide();
	else if (page == 'view_map')
		map.removeOverlay(placemark_arr[e_id]);
	
	var date = new Date();
	var expires = new Date(date.getTime() + 604800*1000);
	var cookie = getCookie('bookmarks');
	var values_array = cookie.split(',');
	if (values_array.length == 1)
		deleteAllBookmarks();
	else 
	{
		var value = '';
		values = []; 
		for(var i=0; i <= (parseInt(values_array.length) - 1); i++)
		{
			if (values_array[i] != e_id)
			{
				values[values.length] = values_array[i]; 
			}
		}
		if (values.length != 1)
			value = values.toString();
		else
			value = values[0];
		setCookie('bookmarks', value, expires.toGMTString(), folder);
		$('.' + e_id).remove();
		var bookmarks_count = $('.bookmarks_count').html();
		$('.bookmarks_count').html(parseInt(bookmarks_count) - 1);
	}
}
function AddToBookmarks(e_id, action, e_name)
{
	var date = new Date();
	var expires = new Date(date.getTime() + 604800*1000);
	var cookie = getCookie(action);
	var value = e_id;
	if ((action == 'bookmarks') && (e_name.length > 0))
	{
		if (cookie.length > 0)
			value = value + ',' + cookie;
		var values_array = cookie.split(',');
		for(var i=0; i <= values_array.length - 1; i++)
		{
			if (values_array[i] == e_id)
			{
				alert('"' + e_name + '" уже добавлен в закладки!');
				return;
			}
		}
		
		$('.b_show').show();
		$('.no_bookmarks').hide();
		$('.bookmarks').html('<div class="' + e_id + '"><a class="ent" href="' + path + '2' + e_id + '.html">' + e_name + '</a>' +
								'</div>' + $('.bookmarks').html());	
		var bookmarks_count = $('.bookmarks_count').html()
		$('.bookmarks_count').html(parseInt(bookmarks_count) + 1);
		ShowControl();
	}
	if ((action == 'watch') && (cookie.length > 0))
	{
		var values_array_new = [];
		var values_array_old = cookie.split(',');
		for(var i=0; i <= values_array_old.length - 1; i++)
		{
			if (values_array_old[i] == e_id)
				continue;
			values_array_new[values_array_new.length] = values_array_old[i];	
		}
		if (values_array_new.length > 0)
		{
			values_array_new.splice(0, 0, e_id);
						
			if (values_array_new.length > 10)
				values_array_new.splice(10, values_array_new.length);
			if (values_array_new.length != 1) 
				value = values_array_new.toString();
			else
				value = values_array_new[0];	
		}
	}
	setCookie(action, value, expires.toGMTString(), folder);	
}

//Переменные для появляющейся кнопки
var on_el = '.ent'; //Идентификатор элемента, при наведении на который появляется контрол
var timer;
function ShowControl()
{
	$(on_el).mouseover(function() {
		var parent_id = $(this).parent().attr('class'); //ID контейнера
		$('.temp').remove();
		clearTimeout(timer);
		$('.' + parent_id).append(' <a class="temp chain rightpopup" title="удалить закладку" style="display: none; background: #f4f4f4;"' +
							'href="javascript: deleteBookmark(&quot;' +	parent_id + '&quot;)">удалить</a>'); //Добавление самого контрола
		$('.temp').show();
		$(".temp").mouseover(function() {
			$(".temp").show();
			clearTimeout(timer);
		}).mouseout(function() {
			Timer(); 
		});
	}).mouseout(function() {
		Timer(); 
	});
}
function Timer()
{
	clearTimeout(timer);
	timer = setTimeout("$('.temp').fadeOut(300)", 1000);
}
$(document).ready(function () {
	ShowControl();
});


