jQuery(document).ready(function(){
    
    var absolute_path = jQuery('#absolute_path').text();
    var i=1;
    jQuery('img').each(function(){
        im_src = jQuery(this).attr('src');
        if (im_src.search(/^http:\/\/.*/gi)<0){
            im_src = absolute_path + im_src;
            jQuery(this).attr('src', im_src);
        }
    })

    image_path = jQuery('#template_image_folder').text()+'/images/';

    jQuery('#koravto_main-menu li.parent a').each(function(){
        if (jQuery(this).parent().hasClass('parent')){
            jQuery('<img src="'+image_path+'/template/triangle.gif" width="9" height="5" alt="triangle"/>').appendTo(this);
        }
    });

    jQuery('#koravto_main-menu li').each(function(){
        li_parent = jQuery(this).parent().parent();
        if (jQuery(li_parent).hasClass('parent')){ //Если меню вложенное то ничего к нему не добавляем
            return;
        }
        if (jQuery(this).hasClass('active')){
            jQuery(this).children('a').addClass('active');
            jQuery('<div class="menu-right active"></div>').appendTo(this);
            jQuery(this).children('div.menu-right').css('background-position', '0px -39px');
        }
        else{
            jQuery('<div class="menu-right"></div>').appendTo(this);
        }
    });

    jQuery('#koravto_main-menu li.parent a').click(function(){
        if (jQuery(this).parent().hasClass('parent')){ //Если самый верхний уровень
            li_position = jQuery(this).parent().position();
            li_height = jQuery(this).height();
            jQuery(this).parent().children('ul').css({
                'left': 10+li_position.left+'px',
                'top': 41+li_position.top+'px'
            });
            jQuery(this).parent().children('ul').slideToggle(300);
            return false;
        }
    });
   
    jQuery('#koravto_main-menu .moduletable li a').mouseover(function(){
        jQuery(this).parent().toggleClass('active-menu');
        jQuery(this).parent().children().toggleClass('active-menu');
    });
    jQuery('#koravto_main-menu .moduletable li a').mouseout(function(){
        jQuery(this).parent().toggleClass('active-menu');
        jQuery(this).parent().children().toggleClass('active-menu');
    });
    
    jQuery('#koravto_main-menu li a').each(function(){
        jQuery(this).attr('title', jQuery(this).text());
    });

    menu_left = (jQuery(window).width()-jQuery('#koravto_main-menu').innerWidth())/2;
    if (menu_left>0){
        menu_left += 'px';
        jQuery('#koravto_main-menu').css('left', menu_left);
    }else{
        jQuery('#koravto_main-menu').css('left', '10px');
    }

    if(jQuery.browser.msie){
        if (jQuery.browser.version=='6.0'){
            jQuery('#koravto_main-menu li').css('background-image', 'url(images/template/main-menu/but-left.gif)');
            jQuery('#koravto_main-menu li a').css('background-image', 'url(images/template/main-menu/but-body.gif)');
            jQuery('.menu-right').css('background-image', 'url(images/template/main-menu/but-right.gif)');
        }
    }
    
    //Footer position
    footer_left = (jQuery(window).width()-jQuery('#koravto_footer').innerWidth())/2;
    if (footer_left>0){
        footer_left += 'px';
        jQuery('#koravto_footer').css('margin-left', footer_left);
        jQuery('#koravto_footer').css('visibility', 'visible');
    }else{
        jQuery('#koravto_footer').css('margin-left', '0px');
    }

    //Katalog position
    katalog_left = jQuery('#koravto_main-menu li:eq(1)').attr('offsetLeft')+jQuery('#koravto_main-menu').attr('offsetLeft')-20;
    katalog_left += 'px';
    jQuery('#koravto_katalog_link').css('left', katalog_left);
    jQuery('#koravto_katalog_link').css('visibility', 'visible');
    
    jQuery('#auto_order').each(function(){
        jQuery(this).validate({
            ignore: ":hidden"
        });

        root_id_obj= new Object();
        jQuery(this).find('option').each(function(){
            i = jQuery(this).attr('root_id');
            val = jQuery(this).text();
            root_id_obj[val]=i;
        });

        jQuery(this).find('#auto_type').change(function(){
            jQuery('#auto_root_id').val(root_id_obj[jQuery(this).val()]);
        });

        root_path = jQuery('#root_path').val();
        req_path = jQuery('#req_path').val();
        template_path = jQuery('#template_path').val();
        modul_path = jQuery('#modul_path').val()+'/';

        jQuery('.auto_order_field input.jselect').mouseover(function(){
            jQuery(this).css('background-color', '#ccc');
        });

        jQuery('.auto_order_field input.jselect').mouseout(function(){
            jQuery(this).css('background-color', '#fff');
        });

        jQuery('#producer_root_id').change(function(){
            jQuery('#auto_model').val('');
        });
        
        //Выбор производителя
        jQuery('input#auto_producer').click(function(){
            if (jQuery(this).attr('stopped')=='yes'){
                return false;
            }
            jQuery(this).attr('stopped','yes');
            id = jQuery('input#auto_root_id').val();
            if (id==''){
                return false;
            }
            auto = jQuery('#producer_data');
            search_type = 'producer';
//            console.log(modul_path);
            jQuery(auto).empty();
            jQuery.ajax({
                url: modul_path+"get_data.php",
                type: "GET",
                dataType: 'json',
                data: "cat_id="+id+"&jbase="+req_path+"&search="+search_type,
                success: function(data){
//                    console.log(data);
                    marks = data['json_array'];
                    jQuery('<ul></ul>').appendTo(auto);
                    auto_ul = jQuery(auto).find('ul');
                    if (search_type=='producer'){
                        jQuery(marks).each(function(idx){
                            jQuery('<li p_id="'+marks[idx]["id"]+'">'+marks[idx]["name"]+'</li>').appendTo(auto_ul);
                        });
                        jQuery(auto).slideDown(300);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert(textStatus);
                }
            }); //end of ajax request

            jQuery('#producer_data li').live('click', function(){
                //                        ul      div      input
                id_field = jQuery('input#producer_root_id');
                jQuery(id_field).val(jQuery(this).attr('p_id'));
                val_fieid = jQuery('input#auto_producer');
                jQuery(val_fieid).val(jQuery(this).text());
                jQuery('input#auto_model').val('');
                jQuery(auto).slideUp(300, function(){
                    jQuery('input#auto_producer').attr('stopped','no');
                });
                
            });
            jQuery('#producer_data li').live('mouseover', function(){
                jQuery(this).css('background-color', '#333');
                jQuery(this).css('color', '#fff');
            });
            jQuery('#producer_data li').live('mouseout', function(){
                jQuery(this).css('background-color', '#fff');
                jQuery(this).css('color', '#06c');
            });
            
        });

        jQuery('.auto_data').click(function(){
            jQuery('.jselect').attr('stopped', 'no');
            jQuery(this).slideUp(300);
        });
        
        //Если название модели вводили вручную
        jQuery('input#auto_model').blur(function(){
            jQuery('input#auto_model').attr('readonly','readonly');
        });
        
        
        //Выбор модели
        jQuery('input#auto_model').click(function(){
            if (jQuery(this).attr('stopped')=='yes'){
                return false;
            }
            jQuery(this).attr('stopped','yes');
            id = jQuery('input#producer_root_id').val();
            if (id==''){
                return false;
            }
            auto = jQuery('#model_data');
            search_type = 'model';
            jQuery(auto).empty();
            jQuery.ajax({
                url: modul_path+"get_data.php",
                type: "GET",
                dataType: 'json',
                data: "cat_id="+id+"&jbase="+req_path+"&search="+search_type,
                success: function(data){
                    marks = data['json_array'];
                    if (marks.length==0){
                        jQuery('<p style="margin:3px 6px; color:#666;">В нашей базе пока нет автомобилей этой марки. Если вы хотите оставить заказ именно на автомобиль этой марки, то впишите название вручную, выбрав «Ввести название вручную», ниже</p>').appendTo(auto);
                    }
                    jQuery('<ul></ul>').appendTo(auto);
                    auto_ul = jQuery(auto).find('ul');
                    jQuery(marks).each(function(idx){
                        jQuery('<li p_id="'+marks[idx]["id"]+'">'+marks[idx]["name"]+'</li>').appendTo(auto_ul);
                    });
                    jQuery('<li style="color:#bb1133; white-space: nowrap;" class="manual">Ввести название вручную</li>').appendTo(auto_ul);
                    jQuery(auto).slideDown(300);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert(textStatus);
                }
            }); //end of ajax request

            jQuery('#model_data li').live('click', function(){
                val_fieid = jQuery('input#auto_model');
                if (jQuery(this).text()=='Ввести название вручную'){
                    jQuery(val_fieid).val('');
                    jQuery('input#auto_model').removeAttr('readonly');
                    jQuery('input#auto_model').focus();
                }else{
                    jQuery(val_fieid).val(jQuery(this).text());
                }
                jQuery('#model_data').slideUp(300, function(){
                    jQuery('input#auto_model').attr('stopped','no');
                    jQuery('input#auto_model').focus();
                });

            });
            jQuery('#model_data li').live('mouseover', function(){
                jQuery(this).css('background-color', '#333');
                jQuery(this).css('color', '#fff');
            });
            jQuery('#model_data li').live('mouseout', function(){
                jQuery(this).css('background-color', '#fff');
                jQuery(this).css('color', '#06c');
            });
            jQuery('#model_data li.manual').live('mouseout', function(){
                jQuery(this).css('background-color', '#fff');
                jQuery(this).css('color', '#bb1133');
            });

        }); //Выбор модели конец

        jQuery('.auto_order_buttons .reset').mouseover(function(){
            jQuery(this).css('color', '#000');
        });
        jQuery('.auto_order_buttons .reset').mouseout(function(){
            jQuery(this).css('color', '#666');
        });
        jQuery('.auto_order_buttons .send').mouseover(function(){
            jQuery(this).css('color', '#fff');
        });
        jQuery('.auto_order_buttons .send').mouseout(function(){
            jQuery(this).css('color', '#e1e1e1');
        });
        jQuery('.auto_order_buttons .send').mousedown(function(){
            jQuery(this).css('color', '#ff008a');
        });
        jQuery('.auto_order_buttons .send').mouseup(function(){
            jQuery(this).css('color', '#fff');
        });

        jQuery('#auto_order').submit(function(){
            jQuery('#auto_order input.send').attr('disabled', 'disabled');

            if (jQuery('#auto_order').valid()){
                auto_producer = jQuery('input#auto_producer').val();
                mail_addres = jQuery('input#mail_sendto').val();
                model = jQuery('input#auto_model').val();
                y_s = jQuery('select#year_start').val();
                y_en = jQuery('select#year_end').val();
                engine = jQuery('#engine_type').val();
                info = jQuery('#add_info').val();
                name = jQuery('#person_name').val();
                phone = jQuery('#person_phone').val();
                mail = jQuery('#person_mail').val();
                from = jQuery('#from_where').val();
                ip = jQuery('#ip_address').val();

                jQuery.ajax({
                    url: modul_path+"send_form.php",
                    type: "GET",
                    dataType: 'json',
                    data: {
                        jbase:req_path,
                        producer:auto_producer,
                        mail_sendto:mail_addres,
                        auto_model:model,
                        year_start:y_s,
                        year_end:y_en,
                        engine_type:engine,
                        add_info:info,
                        person_name:name,
                        person_phone:phone,
                        person_mail:mail,
                        from_where:from,
                        ip_address:ip
                    },
                    success: function(data){
                        jQuery('#auto_producer').val('');
                        jQuery('#auto_model').val('');
                        jQuery('#engine_type').val('');
                        jQuery('#add_info').val('');
                        jQuery('#add_info').val('');
                        jQuery('<div id="mail_report"></div>').appendTo('body');
                        m_height = jQuery(document).height();
                        s_height = jQuery(document).scrollTop();
                        m_height += 'px';
                        jQuery('#mail_report').css({
                            'position':'absolute',
                            'width':'100%',
                            'height':m_height,
                            'top':'0px',
                            'left':'0px',
                            'z-index':'999'
                        });
                        jQuery('<div id="mail_report_over"></div>').appendTo('#mail_report');
                        jQuery('#mail_report_over').css({
                            'position':'absolute',
                            'width':'100%',
                            'height':'100%',
                            'top':'0px',
                            'left':'0px',
                            'background-color':'#000'
                        });
                        jQuery('#mail_report_over').fadeTo(20, 0.8);
                        jQuery('<div id="mail_report_res"></div>').appendTo('#mail_report');
                        jQuery('<p style="font-size:18px;">Сообщение успешно отправлено.</p>').appendTo('#mail_report_res');
                        jQuery('<a href="#" style="font-size: 18px;" title="Закрыть">Закрыть</a>').appendTo('#mail_report_res');
                        s_height = ((jQuery(window).height())/2)+s_height -125;
                        s_height += 'px'
                        jQuery('#mail_report_res').css({
                            'position':'absolute',
                            'width':'300px',
                            'height':'100px',
                            'top':'0px',
                            'left':'50%',
                            'text-align':'center',
                            'padding':'20px',
                            'background-color':'#fff',
                            'border':'5px solid #c1c1c1',
                            'color':'#666',
                            'margin-left':'-175px'
                        });
                        jQuery('#mail_report_res').css('margin-top', s_height);
                        jQuery('#mail_report_res a').live('click',function(){
                            jQuery('#mail_report').remove();
                            return false;
                        });
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        jQuery('<div id="mail_report"></div>').appendTo('body');
                        m_height = jQuery(document).height();
                        s_height = jQuery(document).scrollTop();
                        m_height += 'px';
                        jQuery('#mail_report').css({
                            'position':'absolute',
                            'width':'100%',
                            'height':m_height,
                            'top':'0px',
                            'left':'0px',
                            'z-index':'999'
                        });
                        jQuery('<div id="mail_report_over"></div>').appendTo('#mail_report');
                        jQuery('#mail_report_over').css({
                            'position':'absolute',
                            'width':'100%',
                            'height':'100%',
                            'top':'0px',
                            'left':'0px',
                            'background-color':'#000'
                        });
                        jQuery('#mail_report_over').fadeTo(20, 0.8);
                        jQuery('<div id="mail_report_res"></div>').appendTo('#mail_report');
                        jQuery('<p style="font-size:18px;">Сообщение отправить не удалось. Попробуйте еще раз или позвоните нам.</p>').appendTo('#mail_report_res');
                        jQuery('<a style="font-size:18px;" href="close_message" title="Закрыть">Закрыть</a>').appendTo('#mail_report_res');
                        s_height = ((jQuery(window).height())/2)+s_height -125;
                        s_height += 'px'
                        jQuery('#mail_report_res').css({
                            'position':'absolute',
                            'width':'300px',
                            'height':'100px',
                            'top':'0px',
                            'left':'50%',
                            'text-align':'center',
                            'padding':'20px',
                            'background-color':'#fff',
                            'border':'5px solid #c1c1c1',
                            'color':'#666',
                            'margin-left':'-175px'
                        });
                        jQuery('#mail_report_res').css('margin-top', s_height);
                        jQuery('#mail_report_res a').live('click',function(){
                            jQuery('#mail_report').remove();
                            return false;
                        });
                    }
                });
            }

            jQuery('#auto_order input.send').removeAttr('disabled');
            return false;
        });
        
    }); //Конец обработки формы заказа авто

    //Форма заказа запчастей
    jQuery('#accessor_order').each(function(){
        jQuery(this).validate({
            ignore: ":hidden"
        });
        jQuery('.accessor_order_buttons .reset').mouseover(function(){
            jQuery(this).css('color', '#000');
        });
        jQuery('.accessor_order_buttons .reset').mouseout(function(){
            jQuery(this).css('color', '#666');
        });
        jQuery('.accessor_order_buttons .send').mouseover(function(){
            jQuery(this).css('color', '#fff');
        });
        jQuery('.accessor_order_buttons .send').mouseout(function(){
            jQuery(this).css('color', '#e1e1e1');
        });
        jQuery('.accessor_order_buttons .send').mousedown(function(){
            jQuery(this).css('color', '#ff008a');
        });
        jQuery('.accessor_order_buttons .send').mouseup(function(){
            jQuery(this).css('color', '#fff');
        });

        jQuery('#accessor_order').submit(function(){
            jQuery('#accessor_order input.send').attr('disabled', 'disabled');
            modul_path = jQuery('#modul_path').val()+'/';

            if (jQuery('#accessor_order').valid()){
                mail_addres = jQuery('input#mail_sendto').val();
                accessor_type = jQuery('input#accessor_type').val();
                car_brend = jQuery('input#accessor_car_brend').val();
                car_model = jQuery('input#accessor_car_model').val();
                y_s = jQuery('#year_start').val();
                y_en = jQuery('#year_end').val();
                person_name = jQuery('input#person_name').val();
                person_phone = jQuery('input#person_phone').val();
                person_email = jQuery('input#person_email').val();
                spares = jQuery('#spares').val();
                accessories = jQuery('#accessories').val();
                add_info = jQuery('#add_info').val();
                know_from = jQuery('input#know_from').val();
                ip_addr = jQuery('input#ip_address').val();
            }
            jQuery.ajax({
                url: modul_path+"send_form.php",
                type: "GET",
                dataType: 'json',
                data: {
                    mail_addres:mail_addres,
                    accessor_type:accessor_type,
                    car_brend:car_brend,
                    car_model:car_model,
                    y_s:y_s,
                    y_en:y_en,
                    person_name:person_name,
                    person_phone:person_phone,
                    person_email:person_email,
                    spares:spares,
                    accessories:accessories,
                    add_info:add_info,
                    know_from:know_from,
                    ip_addr:ip_addr
                },
                success: function(data){
                    jQuery('#accessor_type').val('');
                    jQuery('#accessor_car_brend').val('');
                    jQuery('#accessor_car_model').val('');
                    jQuery('#spares').val('');
                    jQuery('#accessories').val('');
                    jQuery('#add_info').val('');
                    jQuery('<div id="mail_report"></div>').appendTo('body');
                    m_height = jQuery(document).height();
                    s_height = jQuery(document).scrollTop();
                    m_height += 'px';
                    jQuery('#mail_report').css({
                        'position':'absolute',
                        'width':'100%',
                        'height':m_height,
                        'top':'0px',
                        'left':'0px',
                        'z-index':'999'
                    });
                    jQuery('<div id="mail_report_over"></div>').appendTo('#mail_report');
                    jQuery('#mail_report_over').css({
                        'position':'absolute',
                        'width':'100%',
                        'height':'100%',
                        'top':'0px',
                        'left':'0px',
                        'background-color':'#000'
                    });
                    jQuery('#mail_report_over').fadeTo(20, 0.8);
                    jQuery('<div id="mail_report_res"></div>').appendTo('#mail_report');
                    jQuery('<p style="font-size:18px;">Сообщение успешно отправлено.</p>').appendTo('#mail_report_res');
                    jQuery('<a href="#" style="font-size: 18px;" title="Закрыть">Закрыть</a>').appendTo('#mail_report_res');
                    s_height = ((jQuery(window).height())/2)+s_height -125;
                    s_height += 'px'
                    jQuery('#mail_report_res').css({
                        'position':'absolute',
                        'width':'300px',
                        'height':'100px',
                        'top':'0px',
                        'left':'50%',
                        'text-align':'center',
                        'padding':'20px',
                        'background-color':'#fff',
                        'border':'5px solid #c1c1c1',
                        'color':'#666',
                        'margin-left':'-175px'
                    });
                    jQuery('#mail_report_res').css('margin-top', s_height);
                    jQuery('#mail_report_res a').live('click',function(){
                        jQuery('#mail_report').remove();
                        return false;
                    });
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    jQuery('<div id="mail_report"></div>').appendTo('body');
                    m_height = jQuery(document).height();
                    s_height = jQuery(document).scrollTop();
                    m_height += 'px';
                    jQuery('#mail_report').css({
                        'position':'absolute',
                        'width':'100%',
                        'height':m_height,
                        'top':'0px',
                        'left':'0px',
                        'z-index':'999'
                    });
                    jQuery('<div id="mail_report_over"></div>').appendTo('#mail_report');
                    jQuery('#mail_report_over').css({
                        'position':'absolute',
                        'width':'100%',
                        'height':'100%',
                        'top':'0px',
                        'left':'0px',
                        'background-color':'#000'
                    });
                    jQuery('#mail_report_over').fadeTo(20, 0.8);
                    jQuery('<div id="mail_report_res"></div>').appendTo('#mail_report');
                    jQuery('<p style="font-size:18px;">Сообщение отправить не удалось. Попробуйте еще раз или позвоните нам.</p>').appendTo('#mail_report_res');
                    jQuery('<a style="font-size:18px;" href="close_message" title="Закрыть">Закрыть</a>').appendTo('#mail_report_res');
                    s_height = ((jQuery(window).height())/2)+s_height -125;
                    s_height += 'px'
                    jQuery('#mail_report_res').css({
                        'position':'absolute',
                        'width':'300px',
                        'height':'100px',
                        'top':'0px',
                        'left':'50%',
                        'text-align':'center',
                        'padding':'20px',
                        'background-color':'#fff',
                        'border':'5px solid #c1c1c1',
                        'color':'#666',
                        'margin-left':'-175px'
                    });
                    jQuery('#mail_report_res').css('margin-top', s_height);
                    jQuery('#mail_report_res a').live('click',function(){
                        jQuery('#mail_report').remove();
                        return false;
                    });
                }
            });
            jQuery('#accessor_order input.send').removeAttr('disabled');
            return false;
        });
    });
    //
    //Если на странице есть кнопка «Заказать» — список
    jQuery('.itemOrderLink a').click(function(){
        order_box = jQuery(this).parent();
        ord_idx = jQuery('.itemOrderLink').index(order_box);
        if ((ord_idx>0) &&(jQuery('.itemBody').size()>0)){
            jQuery('.itemOrderLink:first a').click();
            jQuery('html, body').animate({
                scrollTop:0
            }, 'slow');
            //            jQuery(document).animate({scrollTop: 0}, 1000);
            //            console.log(jQuery(document).scrollTop());
            return false;
        }
        jQuery('#OrderThisItemForm').remove();
        order_coord = jQuery(this).parent().offset();
        item_box = jQuery(this).parent().parent().parent();
        if (jQuery(item_box).hasClass('itemView')){
            item_lot = jQuery(item_box).find('.itemExtraFieldsValue:first').text();
            item_marka = jQuery(item_box).find('.itemExtraFieldsValue:eq(1)').text();
            item_model = jQuery(item_box).find('.itemExtraFieldsValue:eq(2)').text();
        } else{
            item_box = jQuery(this).parent().parent();
            item_lot = jQuery(item_box).find('.catItemExtraFieldsValue:first').text();
            item_marka = jQuery(item_box).find('.catItemExtraFieldsValue:eq(1)').text();
            item_model = jQuery(item_box).find('.catItemExtraFieldsValue:eq(2)').text();
        }
        delta_x = jQuery(this).parent().innerWidth();
        delta_y = jQuery(this).parent().innerHeight();
        jQuery('<div id="OrderThisItemForm"></div>').appendTo('body');
        jQuery('#OrderThisItemForm').css({
            'left':order_coord.left-400+78+'px',
            'top':order_coord.top+delta_y+3+'px'
        });
        jQuery('#OrderThisItemForm').show();
        jQuery('<div id="OrderThisArrow"></div>').appendTo('#OrderThisItemForm');
        jQuery('<div class="OrderThisLink"><a href="close" title="Закрыть">Закрыть окно</a></div>').appendTo('#OrderThisItemForm');
        jQuery('<div id="OrderThisContent"></div>').appendTo('#OrderThisItemForm');
        jQuery('#OrderThisContent').append('<h1 class="this_order_head">Заказ на лот №'+item_lot+', '+item_marka+' '+item_model+'</h1>');
        jQuery('#OrderThisContent').append('<p class="intro">Для того, чтобы сделать заказ введите в форме ниже ваше имя, контактный телефон и e-mail</p>');
        jQuery('#OrderThisContent').append('<form name="OrderThisItemSendForm" id="OrderThisItemSendForm" action="order_item" method="POST" enctype="multipart/form-data"></form>');
        jQuery('#OrderThisItemSendForm').append('<label for="order_this_name">Ваше имя:</label>');
        jQuery('#OrderThisItemSendForm').append('<input type="text" name="order_this_name" id="order_this_name" value="" class="required" size="60"/>');
        jQuery('#OrderThisItemSendForm').append('<label for="order_this_phone">Контактный телефон:</label>');
        jQuery('#OrderThisItemSendForm').append('<input type="text" name="order_this_phone" id="order_this_phone" value="" class="required" size="60"/>');
        jQuery('#OrderThisItemSendForm').append('<label for="order_this_mail">E-mail:</label>');
        jQuery('#OrderThisItemSendForm').append('<input type="text" name="order_this_mail" id="order_this_mail" value="" class="required email" size="60"/>');
        jQuery('#OrderThisItemSendForm').append('<input type="submit" name="SendingOrder" value="Отправить заказ" id="SendingOrder"/>');
        jQuery('#OrderThisItemForm').slideDown(300);
        jQuery('#OrderThisItemSendForm').validate({
            ignore: ":hidden"
        });
        jQuery('#OrderThisItemSendForm').submit(function(){
            jQuery('#OrderThisContent p.order-errors').remove();
            jQuery('#SendingOrder').attr('disabled', 'disabled');
            var j_path_base = jQuery('p#j_path_base').text();
            var modul_path = jQuery('p#modul_path').text()+'/';
            client_name = jQuery('#order_this_name').val();
            client_phone = jQuery('#order_this_phone').val();
            client_mail = jQuery('#order_this_mail').val();
            if (jQuery('#OrderThisItemSendForm').valid()){
                jQuery.ajax({
                    url: modul_path+"order_this_lot.php",
                    type: "GET",
                    dataType: 'json',
                    data: {
                        j_path:j_path_base,
                        name:client_name,
                        phone:client_phone,
                        mail:client_mail,
                        lot:item_lot
                    },
                    success: function(data){
                        jQuery('#OrderThisContent p.intro').remove();
                        jQuery('#OrderThisItemSendForm').remove();
                        jQuery('#OrderThisContent h1.this_order_head').after('<h1 class="this_order_head" style="margin: 12px 0; color: #0082ff;">Ваш заказ отправлен</h1>');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        jQuery('#OrderThisContent h1.this_order_head').after('<p class="order-errors" style="color: red; padding: 4px 0;">Произошла ошибка и сообщение не было отправлено. Попробуйте еще раз, или позвоните нам.</p>');
                    }
                });
            }
            jQuery('#SendingOrder').removeAttr('disabled');
            return false;
        });
        //        if (jQuery('#auto_order').valid()){
        jQuery('.OrderThisLink a').live('click', function(){
            jQuery('#OrderThisItemForm').remove();
            return false;
        });
        return false;
    });
    
    //Если на странице есть кнопка «Заказать» — список /конец

    //Фильтр лотов по типу

    jQuery('#itemFilterTypeSelect').change(function(){
        redirect = window.location.href.split('?');
//        request_uri = document.getElementById('request_uri').innerHTML;
        var request_uri = jQuery('#request_uri').val();

        s_limitstart  = /(.+)(&limitstart=[0-9]+)(.?)/gi;
        s_limit  = /(.+)(&limit=[0-9]+)(.?)/gi;
        s_filter = /(.+)(&filter=extra[0-9]+)(.?)/gi;
        s_filter2 = /(.+)(&filter=extra)([0-9]+)(.?)/gi;

        new_uri = request_uri.replace(s_limitstart, '$1$3'); //Удаляем limitstart
        new_uri = new_uri.replace(s_limit, '$1$3'); //Удаляем limit
        
        if (jQuery(this).val()==0){ //Удаляем фильтр, если выбраны все предложения
            new_uri = new_uri.replace(s_filter, '$1$3');
        } else{ //Заменяем фильтр, если выбран другой фильтр
            if ((new_uri.search(s_filter2))<0){ //Если фильтра еще не было
                new_uri = new_uri+"&filter=extra"+jQuery(this).val();
            }else{ //Если фильтр уже был
                new_uri = new_uri.replace(s_filter2, '$1$2'+jQuery(this).val()+'$4');
            }
        }

        window.location = new_uri;

    });

    //Фильтр лотов по типу конец

    //Выделяем красным цену и статус
    jQuery('span.catItemExtraFieldsLabel').each(function(){ //Краткая карточка
        if (jQuery(this).text()=='Цена'){
            jQuery(this).next().css('color', 'red');
        }
        if (jQuery(this).text()=='Статус'){
            jQuery(this).next().css('color', 'red');
        }
    });
    jQuery('span.latestItemExtraFieldsLabel').each(function(){ //Список спецпредложений
        if (jQuery(this).text()=='Цена'){
            jQuery(this).next().css('color', 'red');
        }
        if (jQuery(this).text()=='Статус'){
            jQuery(this).next().css('color', 'red');
        }
    });
    jQuery('span.itemExtraFieldsLabel').each(function(){ //Подробная карточка
        if (jQuery(this).text()=='Цена:'){
            jQuery(this).next().css('color', 'red');
        }
        if (jQuery(this).text()=='Статус:'){
            jQuery(this).next().css('color', 'red');
        }
    });

//Выделяем красным цену и статус конец

});

jQuery(window).resize(function(){
    menu_left = (jQuery(window).width()-jQuery('#koravto_main-menu').innerWidth())/2;
    if (menu_left>0){
        menu_left += 'px';
        jQuery('#koravto_main-menu').css('left', menu_left);
    }else{
        jQuery('#koravto_main-menu').css('left', '10px');
    }
    footer_left = (jQuery(window).width()-jQuery('#koravto_footer').innerWidth())/2;
    if (footer_left>0){
        footer_left += 'px';
        jQuery('#koravto_footer').css('margin-left', footer_left);
    }else{
        jQuery('#koravto_footer').css('margin-left', '0px');
    }
    katalog_left = jQuery('#koravto_main-menu li:eq(1)').attr('offsetLeft')+jQuery('#koravto_main-menu').attr('offsetLeft')-20;
    katalog_left += 'px';
    jQuery('#koravto_katalog_link').css('left', katalog_left);
    jQuery('#koravto_katalog_link').css('visibility', 'visible');
});
