/**
* JS knihovna pro pridani/odstraneni zbozi do/z oblibenych v e-shopu
* COPYRIGHT WDT s.r.o.
*/

function shop_webfavs_process (shopid, action, reload)
{
    var add = false;
    
    if (action) {
        if (shop_webfavs_is(shopid)) {
            shop_webfavs_del(shopid);
        } else {
            if (shop_webfavs_add(shopid)) {
              add = true;
            }
        }
    }
    
    var e_add = document.getElementById('webfavs_control_div_add_'+shopid);
    var e_del = document.getElementById('webfavs_control_div_del_'+shopid);
    
    if (e_add && e_del) {
        if (shop_webfavs_is(shopid)) {
            e_add.style.display = 'none';
            e_del.style.display = 'block';
        } else {
            // pokud pridavame a nebylo id nalezeno, pak neprobehlo pridani a zobrazime alert
            if (add) {
                alert('Tato funkce není pro Váš prohlížeč dostupná.');
                return false;
            }
            e_del.style.display = 'none';
            e_add.style.display = 'block';
        }
    }

    if (reload) {
        document.location.href = document.location.href;
        return true;
    }

}

function shop_webfavs_add (shopid)
{
    var cwf = readCookie(wf_cookie_name);
    var astr = '';
    if(cwf) {
        var carr = cwf.split(cookies_sep1);
        if(carr.length>=10) {
            alert('Maximální počet položek je 10.');
            return false; 
        } else {
            astr = cwf + cookies_sep1 + shopid;
        }
    } else {
        astr = shopid;
    }
    createCookie(wf_cookie_name, astr, 365);
    return true;
}

function shop_webfavs_del (id)
{
    var clist = readCookie(wf_cookie_name);
    var carr = 0;
    var res = '';
    if(clist) {
        carr = clist.split(cookies_sep1);
        if(carr.length>0) {
            for(var i=0;i<carr.length; i++) {
                if(carr[i] != id) {
                    if(res != '') {
                        res = res + cookies_sep1;
                    }
                    res = res + carr[i];
                }
            }
            createCookie(wf_cookie_name, res, 365);
            return true;
        }
    }
    return false;
}

function shop_webfavs_is (id)
{
    var clist = readCookie(wf_cookie_name);
    var carr = 0;
    if(clist) {
        carr = clist.split(cookies_sep1);
        if(carr.length>0) {
            for(var i=0;i<carr.length; i++) {
                if(carr[i] == id) {
                    return true;
                }
            }
        }
    }
    return false;
}

function shop_webfavs_count ()
{
    var clist = readCookie(wf_cookie_name);
    var r = 0;
    if(clist) {
        r = clist.split(cookies_sep1).length;
    }
    return r;
}

// nazev cookie
var wf_cookie_name = '_CH_wf';

