$(document).ready(function(){
    /* добавление в корзину */
    $('.lbox span a.tobasket').click(function(){
        $.ajax({
           type: "POST",
           url: "/shopcart/add/"+$(this).attr('id'),
           data: {count: 1},
           success: function(msg){
               if(msg=='ok') {
                   $('.lbox').append($('<div style="z-index: 1000;" id="flying_item"></div>')
                        .append($('.lbox img').clone()));
                   $('#flying_item').animate({
                                        top:'-200px',left:'600px',width: '50px',height:'50px', opacity: .1
                                     }, 500, "linear", function(){$(this).remove();} );
                   refreshShopCart();
               }
           }
         });
    })
    /* начальное обновление корзины */
    refreshShopCart();
    $('.tbasket').each(function(){
        refreshBasket();
        setEventsToBasket();
    });
    
    $('.fgalbox a').lightbox();
});

function setEventsToBasket(){
    /* управление корзиной */
    $('.tbasket .cross').mouseover(function(){$(this).css('cursor','pointer')});
    $('.tbasket .delall').click(function(){
        if(confirm('Вы уверены, что хотите очистить корзину?')){
            $.ajax({
               type: "POST",
               url: "/shopcart/clear/",
               cache: false,
               data: {basket: 1},
               success: function(msg){
                   if(msg=='ok') { refreshShopCart(); refreshBasket(); }
               }
             });
        }
    });
    $('.tbasket .delitem').click(function(){
        $.ajax({
           type: "POST",
           url: "/shopcart/add/"+$(this).attr('id'),
           data: {count: -1},
           success: function(msg){
               if(msg=='ok') { refreshShopCart(); refreshBasket(); }
           }
         });
    });
}

function refreshBasket(){
    $('#container').append($('<div id="globalwaiting"></div>'));
    $.ajax({type: "POST",
            url: "/shopcart/table",
            cache: false,
            data: {basket: 1},
            success: function(msg){
                $('.tbasket').replaceWith(msg);
                setEventsToBasket();
            },
            error: function(){
                $('.tbasket').replaceWith($('<div class="tbasket">Ошибка!<br />Не могу получить данные.</div>'));
            },
            complete: function(){
                $('#globalwaiting').remove();
            }
    });
}

function refreshShopCart(){
    $('.basket p.countbasket').addClass('waiting');
    $.ajax({type: "POST",
            url: "/shopcart/block",
            async: true,
            cache: false,
            data: {basket: 1},
            success: function(msg){
                $('div.basket').replaceWith(msg);
            },
            error: function(){
                $('.basket p.countbasket').html('Ошибка!<br />Не могу получить данные.');
            },
            complete: function(){
                $('.basket p.countbasket').removeClass('waiting');
            }
    });
}

function gHomePage(objSrc,homepage)
{
    if (objSrc.style.behavior)    
    {
        // MSIE
        objSrc.setHomePage(homepage);    
    }
    else if (netscape
                && netscape.security 
                && netscape.security.PrivilegeManager 
                && navigator.preference)    
    {
        // Mozilla/FireFox
        netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");        
        navigator.preference("browser.startup.homepage", homepage);    
    }
}
