// JavaScript Document

$(document).ready(function() {
   var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
   if (badBrowser) {     
     $('.nav-main li').hover(function()
		{
			$(this).toggleClass('over');
		});	
	 $('.nav-main li:first').addClass('first');
   }   
   //var slideInervalId = setInterval( "slideSwitch(1)", 3000 );
});

function slideSwitch(direct) {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) {
        if(direct == 1) $active = $('#slideshow IMG:last');
        else $active = $('#slideshow IMG:first');
    }
    var $next = null;
    if(direct == 1){
        $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');
    } else {
        $next =  $active.prev().length ? $active.prev() : $('#slideshow IMG:last');
    }
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 700, function() {
            $active.removeClass('active last-active');
            $('#banner-more-btn').attr('href',$('#slideshow IMG.active').attr('alt'));
        });
}

function deleteProduct(item_id) {
    if (confirm('Вы действительно хотите удалить продукт из корзины?')) {
        $.ajax({
            url: '/shop/cart/delete/',
            type : 'post',
            dataType : 'json',
            data : {'item' : item_id},
            success: function(data) {
                $('#cart-size-info').text(data.total+ ' '+data.title);
                $('#amount').text(data.amount + ' ' + 'руб.');
                $('#row_'+data.item_id).remove();
            }
        });
    }
}

function changeAmount(obj, evt) {
    if((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode == 8)) {        
        setTimeout(function(){sendData(obj)}, 500);
    }
}

function sendData(obj) {
    var count = $(obj).val();
    var product = /\[([\d]+)\]/.exec($(obj).attr('name'));
    product = product[1];
    $.ajax({
        url: '/shop/cart/change-amount/',
        type : 'post',
        dataType : 'json',
        data : {'product' : product, 'count' : count},
        success: function(data) {
            $('#cart-size-info').text(data.total+ ' '+data.title);
            $('#amount').text(data.amount + ' ' + 'руб.');
            $('#amount_' + data.cproduct.id).text(data.cproduct.total_price + ' ' + 'р.');
        }
    });
}

function validateCart(evt){
    if((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode == 8)) {
       return true;
    } else
        return false;
}

