$(document).ready(function() {
    jmagic.checkbox($('input[type=checkbox]'));
	
    $(".hover").fadeTo(1.0, 0);

    $(".hover").hover(function(){
        $(this).fadeTo(300, 1.0);
    },function(){
        $(this).fadeTo(100, 0);
    });

    $('.question').hover(function(){
        $(this).find('img').animate({
            opacity: 0.0
        },200);
    }, function(){
        $(this).find('img').animate({
            opacity: 1.0
        },200);
    });

    $('.button, .button2, .login').hover(function(){
        $(this).find('img').animate({
            opacity: 0.0
        },100);
    }, function(){
        $(this).find('img').animate({
            opacity: 1.0
        },100);
    });

    $('.simg, span.searchin').click(function(){
        $('.search-form2').slideToggle(150);
		
        var src = $(this).find('img').attr('src');
        if (src.indexOf('-up') != -1) {
            $(this).find('img').attr('src', src.replace('-up', ''));
        }
        else {
            $(this).find('img').attr('src', src.replace('ok', 'ok-up'));
        }
    });

    $('a[href*=#catalog]').live('click', function() {
        var o = $(this).parent();
        var action = $(this).attr('href').split('#')[1];
        var attrs = action.split('/');

        if(attrs[1] == 'add_cart') {
            var count = o.find('input').val();
            $.getJSON('/'+action+'/', {
                count: count ? count : 12
            }, function(data) {
                if(data.error) {
                    if(data.error == 'overflow') {
                        alert('Товара нет в наличии!');
                    } else {
                        alert('Невозможно добавить в корзину!');
                    }
                } else {
                    $('div.bear').html(data.cart);
                    $('span.p', o).html(data.n == 0 ? 'Положить в корзину' : 'Уже в корзине '+data.n+' пар'+data.end);
                    $('span.p', $('#jm-tooltip')).html(data.n == 0 ? 'Положить в корзину' : 'Уже в корзине '+data.n+' пар'+data.end);
                    $('div.presence span.p', o.parent()).html(data.n == 0 ? 'Положить в корзину' : 'Уже в корзине '+data.n+' пар'+data.end);
                }
            });
        }
        if(attrs[1] == 'view') {
            $('#blackbody').fadeIn('fast');
            $('#popup').html('<p class="loading"><img src="/img/bullets/indicator.gif" alt="" /> Загрузка данных...</p>').fadeIn();
            $.get('/'+action+'/', function(data) {
                $('#popup').html(data);
                $(function() {
                    tooltips.init('#popup');
                    $('div.big-tovar-p').hover(
                        function() {$('img.zoom_img').fadeIn('slow');}, 
                        function() {$('img.zoom_img').fadeOut(200);}
                    );
                });
            });
        }

        return false;
    });

    $('img.close, #blackbody').live('click', function() {
        $('#blackbody').fadeOut('fast');
        $('#popup').fadeOut();
        $('#feedback_div').hide(800);
        $('#question').animate({
            'right': -15
        }, 800, 'swing');
    });
    
    $('.count-view').attr('readonly', true);
	
    $('.counter a').show();
	
    $('a[href^=#plus]').live('click', function() {
        var o = $(this);
        var p = o.parent();
        var attrs = $(this).attr('href').split('/');

        var val = parseInt(attrs[2]);

        if(attrs[1] == 'pair') {
            var ov = parseInt($('input[name=counter]', p).val());
            $('input[name=counter]', p).val(ov+val);
        //$('.count-view', p).html(ov+val);
        }
        if(attrs[1] == 'box') {
            var ov = parseInt($('input[name^=box_qty]', p).val());
            //$('.count-view', p).html((ov/val)+1);
            //p.parent().next().html(ov+val);

            $.getJSON('/order/xedit_cart/'+attrs[3]+'/', {
                box_qty: ov+val
            }, function(res) {
                if(res.error) {
                    if(res.error == 'overflow') {
                        alert('Товара нет в наличии!');
                    } else {
                        alert('Невозможно добавить в корзину!');
                    }
                } else {
                    $('input[name^=box_qty]', p).val(ov+val);
                    p.parent().parent().find('td.sum').html(res.item_sum).end().find('td.pair_qty').html(res.item_count);
                    $('#full_sum').html(res.cart_sum);
                }
            });
        }
        return false;
    });
	
    $('input[name^=reload]').hide();
	
    $('a[href^=#minus]').live('click', function() {
        var o = $(this);
        var p = o.parent();
        var attrs = $(this).attr('href').split('/');

        var val = parseInt(attrs[2]);

        if(attrs[1] == 'pair') {
            var ov = parseInt($('input[name=counter]', p).val());
            if ((ov - val) <= 0) return false;
			
            $('input[name=counter]', p).val(ov-val);
        //$('.count-view', p).html(ov+val);
        }
        if(attrs[1] == 'box') {
            var ov = parseInt($('input[name^=box_qty]', p).val());
            if ((ov - val) <= 0) return false;
			
            //$('.count-view', p).html((ov/val)+1);
            //p.parent().next().html(ov+val);

            $.getJSON('/order/xedit_cart/'+attrs[3]+'/', {
                box_qty: ov-val
            }, function(res) {
                if(res.error) {
                    if(res.error == 'overflow') {
                        alert('Товара нет в наличии!');
                    } else {
                        alert('Невозможно добавить в корзину!');
                    }
                } else {
                    $('input[name^=box_qty]', p).val(ov-val);
                    p.parent().parent().find('td.sum').html(res.item_sum).end().find('td.pair_qty').html(res.item_count);
                    $('#full_sum').html(res.cart_sum);
                }
            });
        }
        return false;
    });

    $('a[href*=#expandable]').live('click', function() {
        var attrs = $(this).attr('href').split('#')[1].split('/');
        var f = $('#'+attrs[1]+'_container');
        var b = $('#'+attrs[1]+'_button');
        
        if(attrs[2] == 'show') {
            b.animate({
                top: -75
            }, 200, 'swing', function() {
                f.slideDown(300);
            });
        } else if(attrs[2] == 'hide') {
            f.slideUp(300, function() {
                b.animate({
                    top: 0
                }, 200, 'swing');
            });
        }
        
        return false;
    });

    // tooltips
    tooltips.init();
    thover();
});

tooltips = {
    init:function(container) {
        if(!container || container === undefined) container = 'body';

        var cc = $(container);

        if($('#jm-tooltip').length == 0) {
            $('<div/>').attr('id', 'jm-tooltip').addClass('tooltip').appendTo('body');
        }

        $('a[rel^=tooltip]', cc).mouseover(function(e) {
            var o = $(this);
            var c = o.attr('rel').split(':')[1];
            $('#jm-tooltip').
            addClass(c).
            html(
                $('.tt-data', o.parent()).
                html()
                ).
            show();
        }).mouseout(function(e) {
            var o = $(this);
            var c = o.attr('rel').split(':')[1];
            $('#jm-tooltip').html('').removeClass(c).hide();
        }
        ).mousemove(function(e) {
            $('#jm-tooltip').css({
                top: e.pageY-43,
                left: e.pageX+5
            });
        });
    }
}

function thover() {
    $('table.goods tr').hover(
        function() {
            var o = $(this);
            if(!o.hasClass('no-hover')) o.addClass('tr-hover')
        },
        function() {
            $(this).removeClass('tr-hover')
        }
        )
}


/*---------------------------feedback-----------------------------*/
feedback_popup = {
    show: function() {
        $('#blackbody').show();
        $('#feedback_div').fadeIn(800);
        $('#question').animate({
            'right': -80
        }, 800, 'swing');
    },
	
    hide: function() {
        $('#blackbody').hide();
        $('#feedback_div').hide(800);
        $('#feedback_form').clearForm();
        $('#question').animate({
            'right': -15
        }, 800, 'swing');
    }
}
/*---------------------------feedback-----------------------------*/

/*-----------------------order_good_show--------------------------*/
$('a[href*=#order]').live('click', function() {
    var o = $(this).parent();
    var action = $(this).attr('href').split('#')[1];
    var attrs = action.split('/');

    if(attrs[1] == 'view') {
        $('#blackbody').fadeIn('fast');
        $('#popup').html('<p class="loading"><img src="/img/bullets/indicator.gif" alt="" /> Загрузка данных...</p>').fadeIn();
        $.get('/'+action+'/', function(data) {
            $('#popup').html(data);
            $(function() {
                tooltips.init('#popup');
            });
        });
    }

    return false;
});
/*-----------------------order_good_show--------------------------*/

function antispam(tail, middle, head) {
    document.write(head+"@"+middle+"@"+tail);
} 
