function slideSwitch() {
    var $active = $('#demomenu IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#demomenu IMG:first');

    if($next.width() == 280)
        $next = $('#demomenu IMG:first');
        
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2500, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function() {
    try
    {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err){}
    
    setInterval( "slideSwitch()", 6000 );
    
    $("a.lightbox").lightBox('',baseUrl);
    refreshBasket('products');
    //refreshBasket('courses');
//    refreshBasket('products');
});

/* OBSLUHA KOSIKU  ---------------------------------------------------------------------------------------*/
function remove(cookiename)
{
    createCookie(cookiename, "", 1);
    refreshBasket(cookiename);
}

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(cookiename);
        //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();
        var price_dph = Math.round(parseInt(price) + (parseInt(price)/100)*parseInt(vat));
 
        //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)
        var price_total_dph = parseInt(ks) * parseInt(price_dph)
            
                 //alert(price_total);
        if(cookie != '')
            cookie = cookie +'-'+ id + ',' + ks + ',' + price_dph;
        else
            cookie = id + ',' + ks + ',' + price_dph;

        //$(this).find('.price_total_dph').text( numberFormat(price_total) );
        $(this).find('.price_total_dph').text( numberFormat(price_total_dph) ); 
        totalsum += price_total;
        totalsum_vat += price_total_dph;
    
    });

    $('#basket_'+cookiename+' .totalsum').text(numberFormat(totalsum));

    $('#basket_'+cookiename+' .totalsum_dph').text(numberFormat(totalsum_vat));
    createCookie(cookiename, cookie, 1);
    refreshBasket();
}

function parametersShow()
{
    $('#parameters').show();
    $('#additional').hide();
}

function additionalShow()
{
    $('#parameters').hide();
    $('#additional').show();
}

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);
    callAjax(id);
    refreshBasket(cookiename);
}

function handleResponse(transport)
{
    alert(transport.responseText);
}

//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 refreshBasket(cookiename)
{   

    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;
}
