$(document).ready(function(){
        /*$('a.admin.del').click(function(){
            return confirm($(this).attr('rel'));
        });*/
        
        if($('#specoffers .soffer').length>0){
            var roundHeight = false;
            $('#specoffers .soffer').each(function(){
                if(false == roundHeight){
                    roundHeight = $(this).height();    
                }else{
                    if($(this).height()>roundHeight){
                        roundHeight = $(this).height();    
                    }
                }
            });
            if(roundHeight){
                $('#specoffers .soffer').height(roundHeight);    
            }    
        }
        
        
        $('.ramka').each(function(){
            var    
                html = $(this).html(),
                corners = ['lt','lb','rt','rb'],
                edges = ['t','b','l','r'],
                div = $('<div></div>'),
                corner,
                edge,
                parent = false,
                block = div.clone().addClass('block');
                
            $(this).html('');
            for (c in corners){
                corner = div.clone().addClass('corner '+corners[c]);
                $(this).append(corner);
            }
            for(e in edges){
                edge = div.clone().addClass(edges[e]);
                if(false === parent){
                    $(this).append(edge);
                }else{
                    parent.append(edge);
                }
                parent = edge;        
            }
            parent.append(block.append(html));
        });
        
        
        $('a.prod_gall_link:not(.done)').click(function(){
            var link = $(this);
            var url = $(this).attr('href');
            var target = $(this).parents('.card').find('div.hidden');
            $.ajax({ 
                url: url, 
                success: function(data){
                    link.addClass("done");
                    target.html(data);
                    target.find('.photos_list a.vlightbox:first').trigger('click');
                }
            });
            return false;
        });
        
        $('a.prod_gall_link.done').click(function(){
            var target = $(this).parent().find('div.hidden');
            target.find('a:first').trigger('click');    
        });
        
        
        
        $('a.basket.add').click(function(){
            var qty = $(this).parents('div.quantity').find('input[name=qty]').val();
            var url = $(this).attr('href')+'&qty='+qty;
            //alert(url);
            
            $.ajax({ 
                url: url, 
                success: function(data){
                    alert('Товар успешно добавлен!');
                    $('#pcount').text(data);
                    if (parseInt(data) % 10 == 1) {
                        $('#basket-count-title').text('товар');
                    } else if (parseInt(data) % 10 == 2 || parseInt(data) % 10 == 3 || parseInt(data) % 10 == 4) {
                        $('#basket-count-title').text('товара');
                    } else {
                        $('#basket-count-title').text('товаров');
                    }
                }
            });
            return false;
        });
        
        $('#basket input[name=qty]').live('change',function(){
            var qty = $(this).val();
            var url = $(this).parents('.qty').find('a.url').attr('href') + '&qty=' + qty; 
            var input = $(this);
            $.ajax({ 
                url: url, 
                success: function(data){
                    alert('Информация успешно обновлена!');
                    old_p = parseFloat(input.parents('td').find('.price span').text());
                    new_p = parseFloat(data);
                    input.parents('td').find('.price span').text(data);
                    if(new_p > old_p){
                        p = new_p - old_p + parseFloat($('#all_price').text());    
                    }else{
                        p = parseFloat($('#all_price').text()) - (old_p - new_p);   
                    }
                    
                    $('#all_price').text(p+' руб.');
                    count = 0;
                    $('#basket input[name=qty]').each(function(){
                        count = count + parseInt($(this).val());
                    });
                    $('#all_count').html(count);
                    if (parseInt(count) % 10 == 1) {
                        $('#all_count_text').text('единица');
                    } else if (parseInt(count) % 10 == 2 || parseInt(count) % 10 == 3 || parseInt(count) % 10 == 4) {
                        $('#all_count_text').text('единицы');
                    } else {
                        $('#all_count_text').text('единиц');
                    }
                    
                }
            });
        });
        
        $('#basket a.basket_del').click(function(){
            return confirm($(this).attr('rel'));
        });
        
        
        $('#schedule .close a').click(function(){
            $('#schedule').fadeOut();
            return false;
        });
        
        $('#schedule_closed .btn a').click(function(){
            $('#schedule').fadeIn();
            return false;
        });
        
        $('input').click(function(){
            if( $(this).attr('value') == $(this).attr('title') ) {
                $(this).attr('value', '');
            }
        });
        
        $(document).click(function(){
            $('input:text').each(function(){
                $(this).bind ( 'blur', function (){
                    if($(this).val() == '') {
                        $(this).attr('value', $(this).attr('title'));
                    }
                });
            });
        })
         
        
        $('#node_products span.up').click(function(){
            input = $(this).parents('.quantity').find('input.text');
            input.val(parseInt(input.val()) + 1);
        })
        $('#node_products span.down').click(function(){
            input = $(this).parents('.quantity').find('input.text');
            if (parseInt(input.val()) > 1) {
                input.val(parseInt(input.val()) - 1);
            }
        })
        
        $('#basket span.up').click(function(){
            input = $(this).parents('.qty').find('input.text');
            input.val(parseInt(input.val()) + 1);
            input.change();
        })
        $('#basket span.down').click(function(){
            input = $(this).parents('.qty').find('input.text');
            if (parseInt(input.val()) > 1) {
                input.val(parseInt(input.val()) - 1);
            }
            input.change();
        });
        
        
});



