
function __() {
        try {
                console.log(__.caller, arguments);
        } catch (e) {}
}

$(window).ready(function(){

        if ($('a.lightbox').length>0)
                $('.lightbox').lightBox()
        $('.rounded').append('<i class="nw"></i><i class="sw"></i><i class="ne"></i><i class="se"></i>')

        //отправка поиска
        $('#search a.find').click(function(){
                $(this).closest('form').submit();
                return false;
        })
        
        //отступы у текста около картинок
        $('#content p img').parent().css('padding','0')

        //вторая рамка у картинок
        $('#content img').not('.get_out').each(function(){
                classes=$(this).attr('class');
                $(this).attr('class','').wrap('<div class="img-wrap '+classes+'">')
        })

        //зебра
        $('#content table.table tr:nth-child(odd) td').css('background-color','#1a1a1a')
        $('#content table.table tr:first').after('<tr class="support"><td></td><td></td></tr>')

        //нумированные списки
        $('#content ol li').each(function(){
                text=$(this).html()
                $(this).html('<span>'+text+'</span>')
        })


        // правое меню ховеры
        $('#submenu ul li').hover(function(){
                $(this)
                .find('a').css('background','none').end()
                .next('li').find('a').css('background','none')
        }, function(){
                $('#submenu ul li a').css('background',"url('/images/decor/news-sep.png') no-repeat 0 0")
        })

        //правое меню переключение
        var time=400;
        $('#submenu div h4').toggle(function(){

                $(this).parent().addClass('act').addClass('rounded').find('ul').css('height','auto').slideDown(time)

        },function(){
                $(this).parent().find('ul').animate({
                        height: 0
                }, time, function() { 
                        $(this).parent().removeClass('rounded').removeClass('act').find('ul').hide()

                });
                
             
        })



        $('#tabs li a').click(function(){
                $('#tabs li.act').removeClass('act')
                $(this).parent().addClass('act')
                
                jQuery.get($(this).attr('href'), {
                        action: 'index',
                        id: $(this).attr('id'),
                        system_name: $(this).attr('system_name')
                },
                function(data){
                        $('#catalog').html(data)
                        $('#catalog.rounded').append('<i class="nw"></i><i class="sw"></i><i class="ne"></i><i class="se"></i>')
                       
                })
                return false;
                
        })

        //раздвигаем сабменю
        sub_val=$('#opened').val();
        if (sub_val){
                $('#'+sub_val+' h4').click()
        }

        //form
        $('#divFeed input').focus(function(){
                if ($(this).attr('reserv')==$(this).attr('value'))
                        $(this).attr('value','')
        })

        $('#divFeed input').blur(function(){
                if ( $(this).attr('value')==''){
                        reserv=$(this).attr('reserv')
                        $(this).attr('value',reserv)
                }
        })

        $('#divFeed textarea').focus(function(){
                if ($(this).attr('reserv')==$(this).html(''))
                        $(this).html('')
        })
        $('#divFeed textarea').blur(function(){
                if ( $(this).html()==''){
                        reserv=$(this).attr('reserv')
                        $(this).html(reserv)
                }
        })

       

        
})

$(window).load(function(){

        //выравнивание вертикального позиционирования текста в ряду картинок разных размеров
        sameHeight()

})


function Feedback()
{
        if ($('#order_name').val() == $('#order_name').attr('reserv'))
                $('#order_name').val('')

        if ($('#order_email').val() == $('#order_email').attr('reserv'))
                $('#order_email').val('')

        if ($('#order_phone').val() == $('#order_phone').attr('reserv'))
                $('#order_phone').val('')

        if ($('#order_msg').html() == $('#order_msg').attr('reserv'))
                $('#order_msg').html('')



        // ajaxin
        var $form = $('form');
        $.ajax({
                type: 'post',
                url: "/ajax/?action=feedback",
                data: $form.serialize(),
                dataType: 'json',
                success: function(data){
                        if (data.state =='error'){
                                $('#done').show().html(data.message.join('<br/>'));
                                if ($('#order_name').val() =='')
                                        $('#order_name').val($('#order_name').attr('reserv'))

                                if ($('#order_email').val() =='')
                                        $('#order_email').val($('#order_email').attr('reserv'))

                                if ($('#order_phone').val() == '')
                                        $('#order_phone').val($('#order_phone').attr('reserv'))
                                if ($('#order_msg').val() == '')
                                        $('#order_msg').html($('#order_msg').attr('reserv'))
                        }
                        else{
                                $('#divFeed').html('Ваше сообщение отправлено.');
                                $('#done').hide();
                        }
                }
        });

}


function sameHeight(){
        el='imgPlace.list'
        count = parseInt($('.'+el).length / 3)  +1
        i=0;
        while(i<count){
                left=$('.'+el+':eq('+(i*3)+')').css('height')? parseInt($('.'+el+':eq('+(i*3)+')').css('height')):0
                mid=$('.'+el+':eq('+(1+i*3)+')').css('height')? parseInt($('.'+el+':eq('+(1+i*3)+')').css('height')):0
                right=$('.'+el+':eq('+(2+i*3)+')').css('height')? parseInt($('.'+el+':eq('+(2+i*3)+')').css('height')):0
                max=Math.max(left,right,mid)
                $('.'+el+':eq('+(i*3)+')').parent().css('height',max+'px')
                $('.'+el+':eq('+(1+i*3)+')').parent().css('height',max+'px')
                $('.'+el+':eq('+(2+i*3)+')').parent().css('height',max+'px')
                i++;
        }
}
