window.onload = function(e) {

    try
    {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err){}

};

$(function() {
        $('a.gallery').lightBox();
        refreshBasket();
    });

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) );
        totalsum += price_total;
    });

    $('#totalsum').text(numberFormat(totalsum));
    $('#totalsum_dph').text(numberFormat(Math.ceil(totalsum + (totalsum/100)*19)));
    createCookie('basket', cookie, 1);
    refreshBasket();
}

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 addToBasket(id, tmp_price, tmp_ks)
{

    if(parseInt($('#product_price').text()) <= 0)
    {
        alert('Produkt zatím není možné zakoupit.');
        return;
    }


    //NASTAVENI POCTU KUSU
    if(tmp_ks>0)
    {
        if(tmp_price <= 0)
        {
            alert('Produkt zatím není možné zakoupit.');
            return;
        }

        ks = tmp_ks
    }
    else
    {
        var ks = parseInt($('#product_ks').attr("value"));

        if(ks != $('#product_ks').attr("value"))
        {
            alert('Musíte zadat číslo.');
            return;
        }
    }

    if(ks < 0)
    {
        alert('Počet kusů nemí být záporný.');
        return;
    }

    if(tmp_price > 0)
    {
        addToCookie(parseInt(id),ks, parseInt(tmp_price));
    }
    else
    {
        addToCookie(parseInt(id),ks, parseInt($('#product_price').text()));
    }
    
    callAjax(id);
    refreshBasket();
}

//creates
function callAjax(guid)
{   
    try
    { 
        $.ajax({
           type: "GET",
           url: baseUrl+'/basketadd/',
           contentType: "application/json; charset=utf-8",
           data: {'id': guid},
           success: function(msg){ 
             $("#basket_add").html(msg);
           }
         });
    }catch(ex)
    {}

}

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;
}

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(price_total));
}

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 switchLang(value)
{
    createCookie('lang', value, 6000);
}

function changeForm(item)
{
    $('#obehove').hide("fast");
    $('#vodarna').hide("fast");
    $('#kalove').hide("fast");
    $('#vrt').hide("fast");
    $('#priruby').hide("fast");

    $('#'+item).show("fast");
}

function newRow()
{
    id = $('#priruby_count').attr("value");
    id = parseInt(id) + 1;

$('#priruba').children(':last-child').append(
'<tr class="priruby">'+
'<td>'+id+'</td>'+
'<td><input class="text mid" type="text" name="priruby_kod_'+id+'" value="" /></td>'+
'<td><input class="text mid" type="text" name="priruby_norma_'+id+'" value="" /></td>'+
'<td><input class="text short" type="text" name="priruby_dn_'+id+'" value="" /></td>'+
'<td><input class="text short" type="text" name="priruby_pn_'+id+'" value="" /></td>'+
'<td><input class="text mid" type="text" name="priruby_material_'+id+'" value="" /></td>'+
'<td><input class="text short" type="text" name="priruby_kusu_'+id+'" value="" /></td>'+
//'<td><input class="text mid" type="text" name="priruby_cena_'+id+'" value="" /></td>'+
'</tr>');

 $('#priruby_count').attr("value", id);

}

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;
}