var favoritesMaxDays = 1;
var favoritesTxtMax = 'Блокнот переполнен!';
var favoritesTxtAdd = 'Добавить в блокнот';
var favoritesTxtDel = 'Удалить из блокнота';

function setCookie(name, value, days) 
{
	function getExp(d) 
	{
		var exp = new Date();
		exp.setTime (exp.getTime() + d);
		return '; expires=' + exp.toGMTString();
	}
	if (value.length == 0)
		document.cookie = name + "=" + getExp(-1000) + ";path=/";
	else
		document.cookie = name + "=" + value + getExp(days*24*60*60*1000) + ";path=/";
}

function getCookie(name) 
{
    var dc = document.cookie;
    var prefix = name + '=';
    var begin = dc.indexOf('; ' + prefix);
    if (begin == -1) 
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) 
        	return null;
    }
    else 
    {
        begin += 2;
    }
    var end = dc.indexOf(';', begin);
    if (end == -1) 
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

function send_fav(id,kp,params)
{
var FavList = getCookie('favorites_uni');

if(!FavList)
	FavList = '';

$.get("backend.php", {key: 'fav_add' ,id: id, kp: kp, params: params, fav: FavList, time: new Date().getTime()},
	function(data){
		if(FavList=='' || FavList!=data) 
			{
			setCookie('favorites_uni', data, favoritesMaxDays);
			}
	});		
}
function del_fav(kp)
{
var FavList = getCookie('favorites_uni');

if(!FavList)
	FavList = '';
$.get("backend.php", {key: 'fav_del' ,kp: kp, fav: FavList, time: new Date().getTime()},function(data){});		
}

function get_fav()
{
var FavList = getCookie('favorites_uni');
//var id = $('input[name="form_name"]').val().replace(/form_/,'');
var id = $('.favourites_del').attr('id').replace(/del_/,'');;

if(id === undefined)
	id=0;

var o = new Object();
var params = new Array();
var hash = new Array();
var src = '';
var k = 0;	


if(FavList)
	{

	$.get("backend.php", {key: 'fav_get', fav: FavList, id: id, time: new Date().getTime()},
		function(data){
			hash = data.split(',');			
			for(var i=0; i<hash.length; i++)
				{
				if(hash[i]!="")
				{
				sel = $(".favorites[hash="+hash[i]+"]");
				src = sel.attr('src');
				k = k + 1;
				if(src!=undefined)
					{
					sel.attr('class','favoritesIn');
					sel.attr('src',src.replace(/_off/g, '_on'));
					sel.attr('title' , favoritesTxtDel);					
					}
				}
				}
			if(k!=0)
				$('.fav_count').html("("+k+")");
			else 		$('.fav_count').html("");
		});		
	}	

} 

$(document).ready(function(){

$(".favoritesIn").live('click',function () {	
	var o = new Object();
	var params = new Array();
	
	var src = $(this).attr('src');
	

	
	del_fav($(this).attr('hash'));
	
    $(this).attr('class','favorites');	
	$(this).attr('src',src.replace(/_on/g,'_off'));
	$(this).attr('title' , favoritesTxtAdd);
	
	var count = $('.fav_count').html().replace(/[\(\)]/g,'');
		count = count*1 - 1;
	if (count==0) 	
		$('.fav_count').html("");
	else
		$('.fav_count').html("("+count+")");
	
	});


$(".favorites").live('click',function () {

	var id = $(this).attr('id');
		id = id.replace(/favorites/,'');
	var o = new Object();
	var params = new Array();

	var src = $(this).attr('src');

	
	
	if(typeof search_fav == 'function') 
		{
		o = search_fav(id);
		for (var key in o) 
			{
			params.push(key+'='+o[key]); 
			}
		
		}
	

	
	send_fav(id,$(this).attr('hash'),params.join('&'));	
	
    $(this).attr('class','favoritesIn');
	$(this).attr('src',src.replace(/_off/g, '_on'));
	$(this).attr('title' , favoritesTxtDel);
	
	var count = $('.fav_count').html().replace(/[\(\)]/g,'');
		count = count*1 + 1;
	$('.fav_count').html("("+count+")");	
	
	});

$(".favourites_del").live('click',function () {
	var id = $(this).attr('id');
		id = id.replace(/del_/,'');		
	var FavList = getCookie('favorites_uni');

	if(!FavList)
		FavList = '';
	$.get("backend.php", {key: 'fav_del_all' ,id: id, fav: FavList, time: new Date().getTime()},
		function(data){
			var src = $(".favoritesIn").attr('src');	
			if(src!==undefined)
			{
			$(".favoritesIn").attr('src',src.replace(/_on/g,'_off'));
			$(".favoritesIn").attr('title', favoritesTxtAdd);	
			$(".favoritesIn").attr('class', 'favorites');
			}
			
			$('.fav_count').html("");
		});				
	});

	
	get_fav();
		

	
});	

