var ajaxURL = '/ajax/XMLResponser.php'; //url for ajax script response
var ajaxInProgress = false; //trigger, using to avoid multi ajax requesting

$(document).ready(function(){
    //выравниваем высоту левой колнки повысоте родителя
    if ($("#container .cw .column").height() < $("#container .cw #detail-gallery").height() || $("#container .cw #detail-gallery").height() < $("#container .cw").height())
        $("#container .cw .column").height(($("#container .cw #detail-gallery").height() + 20));
    
    $(".parts .part").click(function (){
        if (ajaxInProgress) return; //don't do anything until previous ajax request not done

        $(this).addClass('active'); //mark this
        $(this).siblings('.active').removeClass('active');

        var pid = $(this).attr('pid'); //part id

        $('#slPartitions').attr('value', pid);
        $('#slCategories').attr('value', 0); //reset whis value
        $('#slAdTypes').attr('value', 0); //reset whis value

        $('#next-button').hide(); //hide next button

        $('.tree-container .cats').stop(); //stop animating for cats panel
        $('.tree-container .cats').css('display', 'none');
        $('.tree-container .cats').css('width', '0');

        $('.tree-container .types').stop(); //stop animating for cats panel
        $('.tree-container .types').css('display', 'none');
        $('.tree-container .types').css('width', '0');

        $.ajax({
            url: ajaxURL,
            type: 'POST',
            data: 'cats=get&parent=' + pid,
            dataType: 'xml',
            success: function(data){
                var catsHtml = '<ul>';
                $(data).find('response').find('cats').find('element').each(function(){
                                                                                        catsHtml += '<li class="part" cid="' + $(this).find('id').text() + '">' + $(this).find('name').text() + '</li>';
                                                                                      });
                catsHtml += '</ul>';

                $('.tree-container .cats').animate({
                                                        opacity: 'show',
                                                        width: '220'
                                                    }, 'fast')
                                                    .html(catsHtml);
            }
        });
    });

    $(".cats .part").live('click', function (){ //loaded via ajax cats list
        if (ajaxInProgress) return; //don't do anything until previous ajax request not done

        $(this).addClass('active'); //mark this
        $(this).siblings('.active').removeClass('active');

        var pid = $(".parts .active").attr('pid');
        var cid = $(this).attr('cid');

        $('#slPartitions').attr('value', pid);
        $('#slCategories').attr('value', cid);
        $('#slAdTypes').attr('value', 0); //reset this value
        $('#next-button').hide(); //hide next button

        $('.tree-container .types').stop() //stop any animations
                                   .css('display', 'none') //hide pane
                                   .css('width', '0');
        $.ajax({
            url: ajaxURL,
            type: 'POST',
            data: 'adtypes=get&pid=' + pid + '&cid=' + cid,
            dataType: 'xml',
            success: function(xmlData){
                var adtypesHTML = '<ul>';
                $(xmlData).find('response').find('adtypes').find('element').each(function(){
                    adtypesHTML += '<li class="part" tid="' + $(this).find('id').text() + '">' + $(this).find('name').text() + '</li>';
                });
                adtypesHTML += '</ul>';

                $('.tree-container .types').html(adtypesHTML)
                                           .animate({
                                                       opacity: 'show',
                                                       width: '200'
                                                   },
                                                   'fast'
                                            );
            }
        });
    });

    $('.types .part').live('click', function(){//loaded via ajax types list
        if (ajaxInProgress) return; //don't do anything until previous ajax request not done

        $(this).addClass('active');
        $(this).siblings('.active').removeClass('active');
        $('#slAdTypes').attr('value', $(this).attr('tid'));
        $('#next-button').show(); //show next button
    })

    $('.loading').bind({ //show/hide animation than ajax activity/inactivity
        ajaxStart: function(){
            ajaxInProgress = true;
            $('.tree-container').css('opacity', '0.3');
            $(this).show();
        },
        ajaxStop: function(){
            $('.tree-container').css('opacity', '1');
            $(this).hide();

            ajaxInProgress = false;
        }
    });

    $('.actions').click(function(){//actions button in adv page
        $('.actions-list').slideToggle('slow');
        $(this).toggleClass('actions-active');
    });
    
    $('.actions_dorabotka').click(function(){//actions button in adv page
        $('.actions-list_dorabotka').slideToggle('slow');
        $('.actions_dorabotka').toggleClass('actions-active');
    });

    $('#notrequired').click(function(){ //adv master, step four
        if ($('#notrequired-fields').css('display') == 'none'){
            $('#notrequired-fields').animate({opacity: 'show'}, 'slow');
        }else{
            $('#notrequired-fields').animate({opacity: 'hide'}, 'slow');
        }
        $(this).toggleClass('active');
    })
});
