window.onload = function(e) {

    try
    {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err){}

};

$(function() {
        $('.photo .big a').lightBox('',$('#baseUrl').text());
        
    });

/* OBSLUHA KOSIKU  ---------------------------------------------------------------------------------------*/
function removeFromBasket(id)
{
    var cookie = readCookie('basket');
    var new_cookie = '';

    if(cookie != null && cookie != '')
    {
        products =  cookie.split('-');

        for(var i=0;i < products.length;i++)
        {
            attr = products[i].split(',');

            if(id != attr[0])
            {
                if(new_cookie != '')
                    new_cookie = new_cookie +'-'+products[i];
                else
                    new_cookie = products[i];
            }
        }

        createCookie('basket', new_cookie, 1);
        location.reload(true);

    }
}

function recount()
{
    var cookie = '';
    var totalsum = 0;

    var tmp = $('#basket_detail tr.item').each(function(index)
    {
        var id = $(this).find('.id').text();
        var ks = $(this).find('.ks').attr('value');
        var price = $(this).find('.price').text();

        if(parseInt(ks) != ks)
        {
            alert('Musíte zadat číslo.');
            $(this).find('.ks').attr('value', 0);
            ks = 0;
        }

        if(ks<0)
        {
            alert('Počet kusů nemí být záporný.');
            $(this).find('.ks').attr('value', 0);
            ks= 0;
        }

        var price_total = parseInt(ks) * parseInt(price)

        if(cookie != '')
            cookie = cookie +'-'+ id + ',' + ks + ',' + price;
        else
            cookie = id + ',' + ks + ',' + price;

        $(this).find('.price_total').text( numberFormat(price_total) );
        $(this).find('.price_total_dph').text( numberFormat(Math.ceil(price_total + (price_total/100)*19)) ); 
        totalsum += price_total;
    });

    $('#totalsum').text(numberFormat(totalsum));
    $('#totalsum_dph').text(numberFormat(Math.ceil(totalsum + (totalsum/100)*19)));
    createCookie('basket', cookie, 1);
    refreshBasket();
}

function addToBasket(id, ks, price)
{

    if(id==0)
    {
        var idIndex = document.getElementById('product_variant').selectedIndex;
        var id = document.getElementById('product_variant')[idIndex].value;

    }

    if(ks==0)
    {
        var ksIndex = document.getElementById('product_items').selectedIndex;
        var ks = document.getElementById('product_items')[ksIndex].value;
    }


    if(price==0)
    {
        alert('Zboží není momentálně dostupné.')
    }


    addToCookie(parseInt(id),parseInt(ks), parseInt(price));
    refreshBasket();
}

function refreshBasket()
{
    var cookie = readCookie('basket');
    var ks_total = 0;
    var price_total = 0;

    if(cookie != null && cookie != '')
    {
        products =  cookie.split('-');

        for(var i=0;i < products.length;i++)
        {
            attr = products[i].split(',');

            ks_total = ks_total + parseInt(attr[1]);
            price_total = price_total + (parseInt(attr[1]) * parseInt(attr[2]));
        }
    }

    $('#basket_ks').text(ks_total);
    $('#basket_price').text(numberFormat(Math.ceil(price_total + (price_total/100)*19)));
}

function numberFormat(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  return x1 + x2;
}

/* COOKIES -------------------------------------------------------------------------------*/
function addToCookie(id, ks, price)
{
    //nacteni cookie
    var old_cookie = readCookie('basket');
    var new_cookie = '';
    var changed = false;

    if(old_cookie != null && old_cookie != '')
    {
        products =  old_cookie.split('-');

        //pridani produktu do kosiku
        for(var i=0;i < products.length;i++)
        {
            attr = products[i].split(',');

            if(id == attr[0])
            {
                attr[1] = parseInt(attr[1]) + ks;
                changed = true;
            }

            if(new_cookie != '')
                new_cookie = new_cookie + '-' + attr[0] + ',' + attr[1] + ',' + attr[2];
            else
                new_cookie = attr[0] + ',' + attr[1] + ',' + attr[2];
        }
    }

    if(!changed)
    {
        if(old_cookie != null && old_cookie != '')
        {
            new_cookie = new_cookie + '-' + id + ',' + ks + ',' + price;
        }
        else
        {
            new_cookie =  id + ',' + ks + ',' + price;
        }
    }

    //vlozeni kosiku do cookie
    createCookie('basket', new_cookie, 1);
}

function createCookie(name,value,hours) {
  if (hours)
  {
    var date = new Date();
    date.setTime(date.getTime()+(hours*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";

  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

