var notebook_regExp         = /notebook_[a-z]{1,}_[a-z]{1,}_[0-9]{1,}/;
var notebook_all            = '';
var notebook_all_array      = new Array();

var img_obj;
var img_del = '/img/basket_del.gif';
var img_add = '/img/basket_add.gif';

$(document).ready(function(){
    $(".notebook").css({ cursor: "pointer" });
    $(".notebook_mini_delete").css({ cursor: "pointer" });
    $("#notebook_count").html(getNotebookCount());
    setNotebookImg();
});

$(function(){
    $(".notebook").click(function(){
        img_obj = $(this).children();
        
        if ($.cookie(img_obj.attr('id'))) {
            img_obj.attr({ src: img_add, title: "Записать в блокнот" });
            $.cookie(img_obj.attr('id'), '', {expires: -1, path: '/', domain: '.auto.ru'});
            
            $("#link_"+img_obj.attr('id')).html("<a href='#'>Записать в блокнот</a>");
        } else {
            img_obj.attr({ src: img_del, title: "Стереть из блокнота" });
            $.cookie(img_obj.attr('id'), img_obj.attr('name'), {expires: 7, path: '/', domain: '.auto.ru'});
            
            $("#link_"+img_obj.attr('id')).html("<a href='#'>Стереть из блокнота</a>");
        }
        
        $("#notebook_count").html(getNotebookCount());
        return false;
    })
})

function setNotebookMiniDel(id)
{
    setNotebookDel(id)
    setNotebookMiniContent();
}

function setNotebookMiniContent()
{
    count = getNotebookCount();
    
    if (!count || count == '0') {
        $("#notebook_mini").hide();
        return;
    }
    
    $("#notebook_mini").show();
    
    content                 = '';
    notebook_arr        = document.cookie.split(';');
    
    for (i = 0; i < notebook_arr.length; i++) {
        regExp = notebook_regExp;
        if (regExp.test(notebook_arr[i])) {
            n_one_arr   = notebook_arr[i].split('=');
            n_id_arr    = n_one_arr[0].split('_');
            c_one       = n_one_arr[1].replace(/%20/g, ' ');
            
            content += '<li><a href="/'+n_id_arr[1]+'/'+n_id_arr[2]+'/sale/'+n_id_arr[3]+'.html">'+c_one+'</a> <a href="javascript: setNotebookMiniDel(\''+n_one_arr[0]+'\')" title="Стереть из блокнота" style="text-decoration:none;"><sup class="x_delete">x</sup></a></li>';
        }
    }
    
    content += '<li><br><input type=button value=Сравнить style="font-size:9px;" onclick="window.location.href = \'/notebook/\'"></li>';
    $("#notebook_mini_content").html(content);
}

function getNotebookCount()
{
    cookie_arr = document.cookie.split(';');
    count = 0;
    
    for (i = 0; i < cookie_arr.length; i++) {
        var regExp = notebook_regExp;
        if (regExp.test(cookie_arr[i])) {
            count++;
        }
    }
    
    if (!count)
        return '0';
    
    return count;
}

function setNotebookImg()
{
    var img_one_id;
    var notebook_one_arr    = new Array();
    var notebook_arr        = document.cookie.split(';');
    
    for (i = 0; i < notebook_arr.length; i++) {
        var regExp = notebook_regExp;
        if (regExp.test(notebook_arr[i])) {
            notebook_one_arr = notebook_arr[i].split('=');
            img_one_id = notebook_one_arr[0].replace(' ', '');
            $("#"+img_one_id).attr({ src: img_del, title: "Стереть из блокнота" });
            
            $("#link_"+img_one_id).html("<a href='#'>Стереть из блокнота</a>");
        }
    }
}

function setNotebookDel(id)
{
    $.cookie(id, '', {expires: -1, path: '/', domain: '.auto.ru'})
}

function setNotebookDelAndHref(id)
{
    setNotebookDel(id);
    window.location.href = '/notebook/';
}
