var slide_width = 480;
var slide_count = 5;
var slide_max = -(slide_width * (slide_count - 1));
var quiz_current_question = 1;
var quiz_correct_count = 0;

function move_slider(direction, mover_divid){
    var current_x = parseInt($(mover_divid).getStyle('left'));
    if(current_x % slide_width != 0)    //don't move if we are between slides
    {
        return false;
    }
    
    var new_x;
    if(direction == 'left'){
        new_x = parseInt(current_x) - parseInt(slide_width);
    }
    else if(direction == 'right') {
        new_x = parseInt(current_x) + parseInt(slide_width);
    }
    else {      //direction is a number so we go direction to the section
        
        new_x = (parseInt(direction) - 1) * -slide_width;
    }
    
    //do some error checking
    if(new_x > 0)   //we are dealing with negative positioning
    {
        new_x = 0;
    }
    if(new_x < slide_max)
    {
        new_x = slide_max;
    }
    
    var current_slide = Math.round(Math.abs(new_x / slide_width)) + 1;     //mostly negative
    
    
    //var current_slides = $('current_slides').value;
    
    
    new Effect.Morph(mover_divid, {
        style: {
            'left': new_x + 'px'
        },
        duration: 0.4,
        afterFinish: function(){
            
            //$('slideshow_nav_container').show();
            //do some navigation stuff
            
            $('slider_category_selection').select('.choose_slider').invoke('removeClassName','slider_category_selected');
            $('slider_category_selection').select('.choose_slider_'+current_slide).first().addClassName('slider_category_selected');    //should only have 1
            var left_visible = $('slider_left_arrow').visible();
            var right_visible = $('slider_right_arrow').visible();
            if(current_slide == 1)
            {
                if(left_visible)
                {
                    $('slider_left_arrow').fade({duration: 0.2});
                }
            }
            else
            {
                if(!left_visible)
                {
                    $('slider_left_arrow').appear({duration: 0.2});
                }
            }
            if(current_slide == 5)
            {
                if(right_visible)
                {
                    $('slider_right_arrow').fade({duration: 0.2});
                }
            }
            else
            {
                if(!right_visible)
                {
                    $('slider_right_arrow').appear({duration: 0.2});
                }
            }
            
            
        }
    });
    return false;       
}

function add_comment(entry_div_id){
    if(!$(entry_div_id).visible()){
        new Effect.SlideDown(entry_div_id, {
            duration: 0.2
        });
    }
    else{
        new Effect.SlideUp(entry_div_id, {
            duration: 0.2
        });
    }
    
}

function cancel_comment(entry_box_id, entry_div_id){
    $(entry_box_id).value = '';
    add_comment(entry_div_id);
}

function post_comment(entry_box_id, entry_div_id, category){
    var post_contents = $(entry_box_id).value;
    new Ajax.Request('XHR_post_comment', {
        method: 'post',
        asynchronous: true,
        parameters: {
            page_id: category,
            post_contents: post_contents
        },
        onSuccess:  function(){
            $(entry_box_id).value = '';
            add_comment(entry_div_id);
        }
    });
}

function reply_comment(comment_id){
    var reply_container_id = 'reply' + comment_id;
    var reply_link_id = 'reply_link' + comment_id;
    if($(reply_container_id).visible()){
        $(reply_link_id).removeClassName('active');
        new Effect.BlindUp(reply_container_id, {
           duration: 0.3 
        });
    }
    else{
        new Effect.BlindDown(reply_container_id, {
           duration: 0.3 
        });
        $(reply_link_id).addClassName('active');
    }
}

function submit_reply(page_id, reply_to_id){
    var reply_contents = $('reply_contents' + reply_to_id).value;
    $('reply_contents' + reply_to_id).value = '';
    new Ajax.Request('XHR_post_comment', {
        method: 'post',
        asynchronous: true,
        parameters: {
            post_contents: reply_contents,
            page_id: page_id,
            reply_to_id: reply_to_id
        },
        onSuccess:  function(){
            reply_comment(reply_to_id);
        }
    });
}

function begin_quiz(){
    $('quiz_question1').show();
    quiz_current_question = 1;
    quiz_correct_count = 0;
    $('quiz_nextbtn').show();
}

function quiz_answer(question_id,answer_id,answercounter,question_number){
    var answer_div_id = 'qa_' + question_id + answer_id;
    var icon_div_id = 'qaicon_' + question_id + answer_id;
    var answered_link = $(answer_div_id).down();
    if(!(answered_link.hasClassName('answered'))){
        new Ajax.Request('XHR_quiz_answer', {
            method: 'post',
            asynchronous: true,
            parameters: {
                question_id: question_id,
                answer_id: answer_id
            },
            onSuccess:  function(ret){
                var res = ret.responseText;
                
                var str_selector = '#quiz_question' + question_number + ' a.quiz_answer';
                $$(str_selector).each(function(item){
                    item.addClassName('answered');
                });
                
                str_selector = '#quiz_question' + question_number + ' td.quiz_answer_icon';
                $$(str_selector).each(function(item){
                    item.setStyle({
                       'opacity': '0.5' 
                    });
                });
                
                if(res == '1'){
                    $(answer_div_id).addClassName('correct');
                    $(icon_div_id).removeClassName('quiz_question' + answercounter);
                    $(icon_div_id).addClassName('quiz_correct' + answercounter);
                    $(icon_div_id).setStyle({
                       'opacity': '1' 
                    });
                    quiz_correct_count++;
                }
                else{
                    $(answer_div_id).addClassName('incorrect');
                    $(icon_div_id).removeClassName('quiz_question' + answercounter);
                    $(icon_div_id).addClassName('quiz_wrong' + answercounter);
                    $(icon_div_id).setStyle({
                       'opacity': '1' 
                    });
                }
                
                quiz_current_question = question_number;
                
                $('quiz_nextbtn').show();
                
                var answered_num = $('answered_quiz_questions').value;
                answered_num = parseInt(answered_num) + 1;
                $('answered_quiz_questions').value = answered_num;
                
                var total_q = $('num_quiz_questions').value;
            }
        });
    }
}

function quiz_nextquestion(){
    $$('.quiz_question').each(function(item){
        item.hide();
    });
    
    var answered_num = $('answered_quiz_questions').value;
    var total_q = $('num_quiz_questions').value;
    
    if(answered_num == total_q){
        $('quiz_nextbtn').hide();
        finish_quiz();
    }
    else{
        var nextquestion = parseInt(quiz_current_question) + 1;
        
        $('quiz_question' + nextquestion).show();
    }
}

function finish_quiz(){
    $$('.quiz_question').each(function(item){
        item.hide();
    });
    var total_q = $('num_quiz_questions').value;
    var content_grp_value = $('content_grp').value;
    var strUserType = $('txtUserType').value;
    var strResultDiv = "3-4";
    
    //Figure out which result div to display.
    if (content_grp_value == "1")
    {
        switch (quiz_correct_count)
        {
            case 5:
                strResultDiv = "5";
                break;
            case 0:
            case 1:
            case 2:
                strResultDiv = "0-2";
                break;
        }
    }
    
    //Save score to database.
    new Ajax.Request('XHR_quiz_score', {
        method: 'post',
        asynchronous: true,
        parameters: {
            num_correct: quiz_correct_count,
            total_questions: total_q,
            content_grp: content_grp_value,
            user_type: strUserType
        },
        onSuccess:  function(ret){
            quiz_correct_count = 0;
        }
    });
    
    var percentage = Math.round((parseInt(quiz_correct_count)/parseInt(total_q)) * 100);
    var str_score = quiz_correct_count + '/' + total_q;
    $('quiz_score').innerHTML = str_score;
    $('quiz_score_' + strResultDiv).style.display = "";
    $('quiz_score_container').show();
}

function print_checklist(pdf_element_id){
    var thepdf = document.getElementById(pdf_element_id);
    thepdf.print();
}

function validate_contactform(form_id, formtype){
    $$('.validation_message').each(
        function(item){
            item.hide();
        }
    );
    
    $('contact_comments').removeClassName('validation_fail');
    $('contact_name').removeClassName('validation_fail');
    $('contact_email').removeClassName('validation_fail');
    
    
    var stremail = $('contact_email').value;
    var strname = $('contact_name').value;
    var strquestion = $('contact_comments').value;
    
    var subit = true;
    if(formtype == 'askexpert'){
        if(strquestion == 'What\'s your question?'){
            subit = false;
            $('validate_contact_comments').show();
            $('contact_comments').addClassName('validation_fail');
        }
    }
    else{
        if(strquestion == 'What would you like to tell us?'){
            subit = false;
            $('validate_contact_comments').show();
            $('contact_comments').addClassName('validation_fail');
        }
    }
    if(strname == 'Your name'){
        subit = false;
        $('validate_contact_name').show();
        $('contact_name').addClassName('validation_fail');
    }
    if(stremail == 'Your email address'){
        subit = false;
        $('validate_contact_email').show();
        $('contact_email').addClassName('validation_fail');
    }
    else{
        if(!isEmail(stremail)){
            subit = false;
            $('valid_contact_email').show();
            $('contact_email').addClassName('validation_fail');
        }
        
    }
    if(subit){
        subform(form_id);
    }
}

function subform(form_id){
    $(form_id).submit();

}

function isEmpty( str ){
    var strRE = /^[\s ]*$/gi;
    return strRE.test( str );
}
function isEmail(str){
    var strRE = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
    return strRE.test(str);
}

function open_demo(strName)
{
    window.open("demo?name=" + strName, "", "width=835, height=668");
}