$(document).ready(function() {
    try
    {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err){}
    
    $("a.lightbox").lightBox('',$('#baseUrl').text());
    //refreshBasket('courses');
//    refreshBasket('products');
});

/* OBSLUHA KOSIKU  ---------------------------------------------------------------------------------------*/
function removeFromBasket(id, cookiename)
{
    var cookie = readCookie(cookiename);
    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(cookiename, new_cookie, 1);
        refreshBasket();
        //location.reload(true);

    }
}

function recount(cookiename)
{
    var cookie = '';
    var totalsum = 0;
    var totalsum_vat = 0;
    
    var tmp = $('#basket_'+cookiename+' tr.item').each(function(index)
    {
        var id = $(this).find('.id').text();
        var ks = $(this).find('.ks').attr('value');
        var price = $(this).find('.price').text();
        var vat = $(this).find('.vat').text();
        
        //alert($(this).find('.ks').val()); 
        //alert(id+'-'+ks+'-'+price);
        //return;
        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)
                 //alert(price_total);
        if(cookie != '')
            cookie = cookie +'-'+ id + ',' + ks + ',' + price;
        else
            cookie = id + ',' + ks + ',' + price;

        //$(this).find('.price_total_dph').text( numberFormat(price_total) );
        $(this).find('.price_total_dph').text( numberFormat(Math.ceil(price_total + (price_total/100)*vat)) ); 
        totalsum += price_total;
        totalsum_vat += parseInt(Math.ceil(price_total + (price_total/100)*vat));
    
    });

    $('#basket_'+cookiename+' .totalsum').text(numberFormat(totalsum));

    $('#basket_'+cookiename+' .totalsum_dph').text(numberFormat(totalsum_vat));
    createCookie(cookiename, cookie, 1);
    refreshBasket();
}

function addToBasket(id, ks, price, cookiename)
{
    
    if(ks==0)
    {
        var ks = document.getElementById(id).value;
        
        if(ks=='') 
        {   
            alert('Zadejte prosím počet požadovaných míst.');
            return;
        }          
    }

    if(price==0)
    {
        alert('Zboží není momentálně dostupné.');
        return;
    }
    
    //removeFromBasket(id,cookiename);
    addToCookie(id,parseInt(ks), parseInt(price), cookiename);
    refreshBasket();
}

function handleResponse(transport)
{
    alert(transport.responseText);
}

//creates
function callAjax()
{   
    if ($.browser.msie && $.browser.version.substr(0,1)<7)
    {     
        location.reload();
    }
    else
    { 
        try
        {
            $.ajax({
               type: "GET",
               url: baseUrl+'/ajaxbasket/',
               success: function(msg){
                 $("#basket_content").html(msg);
               }
             });
        }catch(ex)
        {}
    }

}

function refreshBasket()
{   
    callAjax();
    //var cookie = readCookie(cookiename);
//    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]));
//        }
//    }
//    
    //--------------------------------------------------------------------------
//    $('#'+cookiename+'_ks').text(ks_total);
//    $('#'+cookiename+'_price').text(numberFormat(price_total));
}

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, cookiename)
{
    //nacteni cookie
    var old_cookie = readCookie(cookiename);
    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(cookiename, 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;
}
