$(document).ready(function(){
  $("#video-pane").mouseover(function(){
        $('div.video-button').addClass("video-button-hover");
    }).mouseout(function(){
        $('div.video-button').removeClass("video-button-hover");    
    });
    
    $('#video-pane, div.video-button').click(function(){
        $('#video-pane').remove();
        $('#video-box').html('<iframe width="736" height="445" src="http://www.youtube.com/embed/FQhg2_WavsU?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>');
    });



    /*$(".accordion h3:first").addClass("active");*/
    /*$(".accordion .accordion-panel:not(:first)").hide();
 
    $(".accordion h3").click(function(){
 
        $(this).next("div.accordion-panel").slideToggle("slow")
        .siblings("div.accordion-panel:visible").slideUp("slow");
        $(this).toggleClass("active");
        $(this).siblings("h3").removeClass("active");
     });*/
     
     
    if($('#dpw').length){
        updateIMT();
    }
     
    
       
    //контролы калькулятора

    
    $("#WeightSlider").slider({ 
        from: 30, 
        to: 210, 
        scale: ['30', '|', '90', '|', '150', '|', '210'],    	
        limits: false, 
        step: 1, 
        dimension: '&nbsp;кг', 
        skin: "blue", 
        callback: function( value ){
            updateIMT();
        } 
    });   


    $("#HeightSlider").slider({ 
        from: 50, 
        to: 250, 
        scale: ['50', '|', '100', '|', '150', '|', '200', '|', '250'], 
        limits: false, 
        step: 1, 
        dimension: '&nbsp;см', 
        skin: "blue", 
        callback: function( value ){
            updateIMT();
        } 
    });
	
    $("#WeekOfPregnancySlider").slider({ 
        from: 2, 
        to:40, 
        scale: ['2', '|', '40'], 
        limits: false, 
        step: 2, 
        dimension: '&nbsp;нед.', 
        skin: "blue", 
        callback: function( value ){
            updateIMT();
        } 
    });
        
    var now = new Date();
    $('#firstDayOfMenstruationBox').DatePicker({
        flat: true,
        format: 'd/m/Y',
        date: new Date(now),
        current: new Date(now),
        calendars: 1,
                            
        starts: 1,
        view: 'days',
        onChange: function(formated, dates){
            updateDayOfBirth(dates);                                    
        }
    });
                
                
    $('#LastDayOfTheCycleBox').DatePicker({
        flat: true,
        format: 'd/m/Y',
        date: new Date(now),
        current: new Date(now),
        calendars: 1,
                            
        starts: 1,
        view: 'days',
        onChange: function(formated, dates){
            updateDayOfConception(dates);                                    
        }
    });
                
                
    //Калькулятор 4
    
    $('.answerfield').val('-1');
    $('#que-ribbon input:radio').removeAttr('checked');
    $('#questionCurent').val('0');
                
    $("#que-ribbon input:radio, .pollstableborder-poll input").uniform();
                
    $('#c4-prev-button').click(function(){
        prevSlideQuestion()
        return false;
    });
                
    $('#c4-next-button').click(function(){
        nextSlideQuestion()
        return false;
    });

             
    $('#que-ribbon .option').click(function(){
        var nameQuestion ='#' + $(this).attr('name') + 'Answer';
        var optionValue = parseInt($(this).val());
        //var oldOptionValue = parseInt($(nameQuestion).val());
        //if()
        //var totalScore = parseInt($('#totalScore').val());
        $(nameQuestion).val(optionValue);
        
        var questionUnanswered = 0;
        var totalScore = 0;
        
        $('.answerfield').each(function(i, elem){
            var oldOptionValue = parseInt($(this).val());
            
            
            if(oldOptionValue != -1)
            {
                var numberQuestion = $(this).attr('data-qnumber');
                if (numberQuestion < 5){
                    totalScore = totalScore + oldOptionValue;
                }
            }else{
                questionUnanswered = questionUnanswered + 1;
            }       
        });
                    
        if(questionUnanswered == 0){
            var verdictId = getVerdict(totalScore);
            $('#que-ribbon, a.c4-button, #c4-quenumber-box, #questionUnanswered, ').hide();
            $('div.c4b-box').text('Вердикт');
            
            $(verdictId).animate({
                    opacity: 'show'
                }, 'slow');
            
        } else{
            $('#questionUnanswered').text('Осталось вопросов: ' + questionUnanswered);
            nextSlideQuestion();
        }         

    });
	
	
});


function updateIMT() {
    var weight = parseInt($('#WeightSlider').val());
    var height = parseInt($('#HeightSlider').val())/100;
	
    var IMT = weight/(height*height);
    var WeekOfPregnancy = parseInt($('#WeekOfPregnancySlider').val());
	
    var PregnancyData = new Object();
	
    PregnancyData['small'] = {
        '2': 0.5,
        '4': 0.9,
        '6': 1.4,
        '8': 1.6,
        '10': 1.8,
        '12': 2.0,
        '14': 2.7,
        '16': 3.2,
        '18': 4.5,
        '20': 5.4,
        '22': 6.8,
        '24': 7.7,
        '26': 8.6,
        '28': 9.8,
        '30': 10.2,
        '32': 11.3,
        '34': 12.5,
        '36': 13.6,
        '38': 14.5,
        '40': 15.2
    };
	
    PregnancyData['middle'] = {
        '2': 0.5,
        '4': 0.7,
        '6': 1.0,
        '8': 1.2,
        '10': 1.3,
        '12': 1.5,
        '14': 1.9,
        '16': 2.3,
        '18': 3.6,
        '20': 4.8,
        '22': 5.7,
        '24': 6.4,
        '26': 7.7,
        '28': 8.2,
        '30': 9.1,
        '32': 10.0,
        '34': 10.9,
        '36': 11.8,
        '38': 12.7,
        '40': 13.6 
    };
	
    PregnancyData['big'] = {
        '2': 0.5,
        '4': 0.5,
        '6': 0.6,
        '8': 0.7,
        '10': 0.8,
        '12': 0.9,
        '14': 1.0,
        '16': 1.4,
        '18': 2.3,
        '20': 2.9,
        '22': 3.4,
        '24': 3.9,
        '26': 5.0,
        '28': 5.4,
        '30': 5.9,
        '32': 6.4,
        '34': 7.3,
        '36': 7.9,
        '38': 8.6,
        '40': 9.1
    };
	
    //var pref = 'small';
    if (IMT<19.8) {
        var pref = 'small';
    }
        
    if (IMT>=19.8 && IMT<=26) {
        var pref = 'middle';
    }
        
    if (IMT>26) {
        var pref = 'big';
    }
	
    var DPW = PregnancyData [pref][WeekOfPregnancy];
    $('#dpw').text(DPW);
}


function updateDayOfBirth(dates){
    //вычисляем день родов 
    var resultDate = new Date();
    resultDate = dates; 
    resultDate.addWeeks(40);
    resultDate.addDays(-1);
   
    var textDate = resultDate.getDate() +'-е ' 
    + Date.MONTHNAMES[resultDate.getMonth()] 
    + ' ' + resultDate.getFullYear() + ' г.';
  
    $('#DayOfBirth').text(textDate);        
}

function updateDayOfConception(dates){
    //вычисляем  оптимальные дни для зачатия
    var lastDay = new Date();
    var firstDay = new Date();
   
    lastDay = dates;
    lastDay.addDays(-11);
   
    firstDay = lastDay.copy();
    firstDay.addDays(-8);
   
    var textDate = firstDay.getDate() +'-е ' 
    + Date.MONTHNAMES[firstDay.getMonth()] 
    + ' ' + firstDay.getFullYear() + ' г. - '
    + lastDay.getDate() +'-е ' 
    + Date.MONTHNAMES[lastDay.getMonth()] 
    + ' ' + lastDay.getFullYear() + ' г.'
   
    $('#DayOfConception').text(textDate); 
}


Date.prototype.addDays = function(d) {
    this.setDate( this.getDate() + d );
};

Date.prototype.addWeeks = function(w) {
    this.addDays(w * 7);
};

Date.prototype.addMonths= function(m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);

    if (this.getDate() < d)
        this.setDate(0);
};

Date.prototype.addYears = function(y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);

    if (m < this.getMonth()) {
        this.setDate(0);
    }
};

Date.prototype.copy = function () {
    return new Date(this.getTime());
};

Date.MONTHNAMES = ["Января", 
    "Февраля", 
    "Марта", 
    "Апреля", 
    "Мая", 
    "Июня", 
    "Июля", 
    "Августа", 
    "Сентября", 
    "Октября", 
    "Ноября", 
    "Декабря"];

function nextSlideQuestion(){
    var questionCurent = parseInt($('#questionCurent').val());
    var questionNumber = questionCurent + 1;
    slideQuestion(questionNumber);
}

function prevSlideQuestion(){
    var questionCurent = parseInt($('#questionCurent').val());
    var questionNumber = questionCurent - 1;
    slideQuestion(questionNumber);
}

function slideQuestion(questionNumber){
    var questionCount = parseInt($('#questionCount').val());
    
    if ((questionNumber + 1) > questionCount){
        questionNumber = 0;
    }
    
    if (questionNumber < 0 ){
        questionNumber = questionCount - 1;
    }
    
    
    var questionBoxWidth = parseInt($('.que-box:first').css('width'));
    var questionRibbonMarginVal = questionBoxWidth * questionNumber * -1;
    var questionRibbonMargin = questionRibbonMarginVal + 'px';
    // alert(questionRibbonMargin);
    $('#que-ribbon').animate({
        'margin-left': questionRibbonMargin
    }, 450);
           
    $('#c4-quenumber-box').text('Вопрос ' + (questionNumber + 1) + ' из ' + questionCount);
    $('#questionCurent').val(questionNumber);
    
}


function getVerdict(totalScore){
    
    var verdictId = '#v4';
    
    if (totalScore >= 5 && totalScore <= 10){
        verdictId = '#v4';
    }
    
    if (totalScore >= 11 && totalScore <= 15){
        verdictId = '#v3';
    }
    
    if (totalScore >= 16 && totalScore <= 20){
        verdictId = '#v2';
    }
    
    if (totalScore >= 21 && totalScore <= 25){
        verdictId = '#v1';
    }
    
    return verdictId;
    
}


