function strrevert(s){
    s2 = '';
    for (x = s.length; x >= 0; x--){
        s2 += s.substr(x,1);
    }
    return s2;
}

function intcomma(x){
    y = '' + Math.round(x);
    y = strrevert(y);
    r = y.replace(/\d{3}/gi, '$& ');
    return strrevert(r);
}

$(function(){
        $.ajax({
                type: 'POST',
                url: '/basket/add/',
                data: 'id=0',
                dataType: 'json',
                success: function(obj){
                    if (obj.quant > 0) $('#basketimg').attr('src', '/media/basket_full.png');
                    $('#basket #quantity').text(obj.quant);
                    $('#basket #amount').text(intcomma(obj.sum));
                    }
        });
    $('#enter').click(function(){
        $('#login').toggle('fast');
    });
    $('a.to_basket').click(function(){
        basketinfo = $(this).parent().children('.basketinfo');
        thing_id = basketinfo.attr('id').replace('id_', '');
        $.ajax({
                type: 'POST',
                url: '/basket/add/',
                data: 'id='+thing_id,
                dataType: 'json',
                success: function(obj){
                    if (obj.quant > 0) $('#basketimg').attr('src', '/media/basket_full.png');
                    basketinfo.text('добавлено...');
                    basketinfo.delay(1500).fadeOut('slow');
                    $('#basket #quantity').text(obj.quant);
                    $('#basket #amount').text(intcomma(obj.sum));
                    }
        });
        basketinfo.text('добавляется...');
        basketinfo.fadeIn('slow');
    });
    $('.choose').click(function(){
        $(this).next('ul').slideToggle('fast');
        $(this).text( /\+.+/.test($(this).text()) ? $(this).text().replace('+ ', ''):$(this).text().replace('', '+ '));
        });
    $('#sort_price').click(function(){
            if (location.search.search('sort') == -1) {
                location.search += '&sort=price&arrow=down';
                return;
                }
            if (location.search.search('sort=price') == -1) {
                location.search = location.search.replace(/(sort=)\w+/g, '$1price');
                return;
                }
            if (location.search.search('down') == -1) {
                location.search = location.search.replace('up', 'down');
                return;
                }
            if (location.search.search('up') == -1) {
                location.search = location.search.replace('down', 'up');
                return;
                }
            if (location.search.search('arrow') == -1) {
                location.search += '&arrow=down';
                return;
                }
        });
    $('#sort_name').click(function(){
            if (location.search.search('sort') == -1) {
                location.search += '&sort=name&arrow=down';
                return;
                }
            if (location.search.search('sort=name') == -1) {
                location.search = location.search.replace(/(sort=)\w+/g, '$1name');
                return;
                }
            if (location.search.search('down') == -1) {
                location.search = location.search.replace('up', 'down');
                return;
                }
            if (location.search.search('up') == -1) {
                location.search = location.search.replace('down', 'up');
                return;
                }
            if (location.search.search('arrow') == -1) {
                location.search += '&arrow=down';
                return;
                }
        })
    $('#sort_stars').click(function(){
            if (location.search.search('sort') == -1) {
                location.search += '&sort=stars&arrow=down';
                return;
                }
            if (location.search.search('sort=stars') == -1) {
                location.search = location.search.replace(/(sort=)\w+/g, '$1stars');
                return;
                }
            if (location.search.search('down') == -1) {
                location.search = location.search.replace('up', 'down');
                return;
                }
            if (location.search.search('up') == -1) {
                location.search = location.search.replace('down', 'up');
                return;
                }
            if (location.search.search('arrow') == -1) {
                location.search += '&arrow=down';
                return;
                }
        })
    $('#view_9').click(function(){
            if (location.search.search('view') == -1) {
                location.search += '&view=9';
                return;
                }
            str = location.search.replace(/view=\w+/g, 'view=9');
            location.search = str;
        })
    $('#view_18').click(function(){
            if (location.search.search('view') == -1) {
                location.search += '&view=18';
                return;
                }
            str = location.search.replace(/view=\w+/g, 'view=18');
            location.search = str;
        })
    $('#view_36').click(function(){
            if (location.search.search('view') == -1) {
                location.search += '&view=36';
                return;
                }
            str = location.search.replace(/view=\w+/g, 'view=36');
            location.search = str;
        })
    $('#view_all').click(function(){
            if (location.search.search('view') == -1) {
                location.search += '&view=all';
                return;
                }
            str = location.search.replace(/view=\w+/g, 'view=all');
            location.search = str;
        })
    $('a#basketbox').fancybox({
//            'autoDimensions': false,
//            'width': 740,
//            'height': 430,
            'titleShow': false,
            'padding': 20,
            'centerOnScroll': true
            });
    $('a.from_basket').click(function(){
        div = $(this).parent().parent().parent();
        thing_id = div.attr('id').replace('id_', '');
        div.next('br').remove();
        div.prev('br').remove();
        div.fadeOut();
        $.ajax({
                type: 'POST',
                url: '/basket/del/',
                data: 'id='+thing_id,
                dataType: 'json',
                success: function(obj){
                    if (obj.quant == 0){
                     $('#basketimg').attr('src', '/media/basket.png');
                     $.fancybox.close();
                     }
                    $('#basket #quantity').text(obj.quant);
                    $('#basket #amount').text(intcomma(obj.sum));
                    }
            });
        });
    $('a.fbgal').fancybox({
            'hideOnContentClick': true,
            'titleShow': false
            });

    $('#textcomment').keyup(function(){
        var converter = new Showdown.converter();
        data = $(this).attr('value');
        html = converter.makeHtml(data);
        $('#preview').html(html);});
    $('#postcomment').click(function(){
        $('#sendlink').hide();
        $('#choosewatch').hide();
        $('#textcomment').focus();
        $.scrollTo('input[type="submit"]', 500);
    });
    $('#sharelink').mouseenter(function(){
        $('#choosewatch').slideUp('fast');
        $('#sendlink').slideDown('fast');
        $(document).stopTime('up1');
        $(document).stopTime('up2');
    });
    $('#sharelink').mouseleave(function(){
        $(document).oneTime(2000, 'up1', function(){$('#sendlink').slideUp('fast')});
    });
    $('#sendlink').mouseenter(function(){
        $(document).stopTime('up1');
    });
    $('#sendlink').mouseleave(function(){
        $(this).slideUp('fast');
    });
    $('#watchthing').mouseenter(function(){
        $('#sendlink').slideUp('fast');
        $('#choosewatch').slideDown('fast');
        $(document).stopTime('up1');
        $(document).stopTime('up2');
    });
    $('#watchthing').mouseleave(function(){
        $(document).oneTime(2000, 'up2', function(){$('#choosewatch').slideUp('fast')});
    });
    $('#choosewatch').mouseleave(function(){
        $(document).stopTime('up2');
    });
    $('#choosewatch').mouseleave(function(){
        $(this).slideUp('fast');
    });

/*
        $.ajax({
                type: 'POST',
                url: '/preview/',
                data: 'text='+data,
                dataType: 'html',
                success: function(obj){
                    $('#preview').html(obj);
                    }
            });
        });
*/
})


