$(document).ready(function(){ $('#page_margins').addClass('jsEnabled'); });

var formSectionsActive    = false;
var formSectionSlideSpeed = 250;
var formSectionBodyFieldsCount = 1;
var formSectionBodyFieldsSum   = 0;
var formId = '';
 
function startFormSections(id, active) {   
    formSectionsActive = active;
    formId             = id + ' ';
    
    $(formId + '#FormShowAll a').click(function(event) {
        if (!formSectionsActive) {
         /*   formSectionsActive = true;
            initFormSections();*/
            return false;
        }
        formSectionsActive = false;
        
        $(this).hide();
        $(formId + '.FormSection .FormSectionBodyRatingFieldInfo').show(); 
        $(formId + '.FormSection').removeClass('FormSectionFolded'); 
        $(formId + '.FormSection .FormSectionProgress').removeClass("FormSectionProgressShow");
        $(formId + '.FormSection .FormSectionBody').slideDown(formSectionSlideSpeed);   
        return false;
    });

    $(formId + '.FormSection .FormSectionHead').click(function() {
        if (!formSectionsActive) {
            return false;
        }
        
        last = $(formId + '.FormSection .FormSectionBody:visible');
        next = $(this).parent().find('.FormSectionBody');
        
        lastSection = last.parent();
        nextSection = next.parent();
        
        if (lastSection.get(0).id != nextSection.get(0).id) {  
            last.parent().removeClass('FormSectionFilled');      
            last.find('.FormSectionBodyRatingFieldInfo').hide();
            
            lastSection.find('.FormSectionHead').css('cursor', 'pointer');
            nextSection.find('.FormSectionHead').css('cursor', 'auto');
            
            last.slideUp(formSectionSlideSpeed);            
            next.slideDown(formSectionSlideSpeed);
            

            lastSection.addClass('FormSectionFolded');
            nextSection.removeClass('FormSectionFolded');      
            next.find('.FormSectionBodyRatingFieldInfo').show();
            last.parent().addClass('FormSectionFilled');
        }
        
        return false;
    });

    $(formId + '.FormSection .FormSectionProgress a').click(function(event) {
        $(this).parent().parent().parent().next().find('.FormSectionHead').click();
        
        return false;
    });

    $(formId + '.FormSectionBody fieldset li').click(function() {
        checkbox = $(this).find('input[type=radio]').get(0);
        
        if (checkbox.disabled) {
            return false;
        }
        
        checkbox.checked = true;    
        $(this).parent().find('li').removeClass('active');
        $(this).addClass('active');
    });

    if (formSectionsActive) {
        $(formId + '#FormShowAll a').css('display', 'block');
       
        /*$(formId + '.FormSectionBodyField:has(fieldset)').each(function(index, field) {    
            height = $(field).height();
            
            $(field).find('li').height(height).height(function(index, height) {
                padding = parseInt($(this).css('padding-top').replace('px', '')) 
                        + parseInt($(this).css('padding-bottom').replace('px', ''))
                        + parseInt($(field).css('border-top-width').replace('px', ''))
                        + parseInt($(field).css('border-bottom-width').replace('px', ''));
                
                $(this).height(height - padding);
            });
        });*/
        
        $(formId + '.FormSectionHead').css('cursor', 'pointer');
        
        $(formId + '.FormSection .FormSectionBody').hide();
        $(formId + '.FormSection').addClass('FormSectionFolded');

        $(formId + '.FormSection:first .FormSectionBody').show();
        $(formId + '.FormSection:first').removeClass('FormSectionFolded');
        $(formId + '.FormSection:first .FormSectionHead').css('cursor', 'auto');

        formSectionBodyFieldsCount = $(formId + '.FormSectionBodyField').length;

        $(formId + '.FormSection').each(function(index, section) {            
            formSectionBodyFieldsSum = formSectionBodyFieldsSum + $(section).find('.FormSectionBodyField').length;  

            percent = Math.round((formSectionBodyFieldsSum / formSectionBodyFieldsCount) * 100);

            $(section).find('.FormSectionProgressBarDone').addClass("fspbp" + percent);
            span = $(section).find('.FormSectionProgressBar span')

            span.text(span.text().replace('#value#', percent));
        });


        $(formId + '.FormSection .FormSectionProgress').addClass("FormSectionProgressShow");
        $(formId + '.FormSection:last .FormSectionProgress a').hide();
               
        $(formId + '.FormSection:has(li.FormSectionBodyFieldWithErrors)').addClass('FormSectionWithErrors');
        //.first().find('.FormSectionHead').click();                    
    }
    
    $(formId + '.FormSectionBody fieldset li:has(input[type=radio]:checked)').addClass('active');         
    $(formId + '.FormSectionBody fieldset li').css('cursor', 'pointer');
}


function slideBox(base, button, content, noFold, isShow) {
    if ($(base +' '+ button).length == 0 || $(base +' '+ content).length == 0 ) {
        return false;
    }
    
    if (!isShow) {
        $(base +' '+ content).hide();   
        if (noFold) {
            $(base +' '+ content + noFold).show();             
        }
    }

    $(base +' '+ button).css('cursor', 'pointer').addClass('rtHoverable').click(function() {
        searchFields = $(this).parent().find(content);

        if (searchFields.is(content +':hidden')) {
            searchFields.slideDown();    
        } else {
            searchFields.slideUp();                
        }

        return false;
    });
}

$(document).ready(function(){ slideBox('.rtSearchForm', '.rtSearchFormGroupLegend', '.rtSearchFields', '.noFold', false); });
$(document).ready(function(){ slideBox('#rtFaqContent', '.rtFaqItemHeading', '.rtFaqItemBody', '', false); });


//$(document).ready(function(){ slideBox('.rtModule2', 'h3', '.rtModule2Body', '', true); });
