function togglePhase1() {
    $("#phase1").show();
    $("#phase2").hide();
    $("#phase3").hide();
}
function togglePhase2() {
    $("#phase1").hide();
    $("#phase2").show();
    $("#phase3").hide();
}
function togglePhase3() {
    $("#phase1").hide();
    $("#phase2").hide();
    $("#phase3").show();
}
function hidePhases () {
    $(".phase").hide();
}
function disableSubmit (mode) {
    if (mode == 'undisable') {
        $("#tdomf_form8_send").removeClass('notAllowed');
        $("#tdomf_form8_send").attr("disabled", false);
        $("#tdomf_form8_send").fadeTo("fast", 1);
    } else if (mode == 'disable') {
        $("#tdomf_form8_send").addClass('notAllowed');
        $("#tdomf_form8_send").attr("disabled", true);
        $("#tdomf_form8_send").fadeTo("fast", 0.5);
    }
}
function changePostButton (mode) {
    if (mode == 'submit') {
        $("#tdomf_form8_send").removeClass('messageContinue');
        $("#tdomf_form8_send").addClass('messageSubmit');
        $("#tdomf_form8_send").unbind("click");
        $("#tdomf_form8_send").click(function(){
            return true;
        });
        disableSubmit('undisable');
    } else if (mode == 'continue') {
        $("#tdomf_form8_send").removeClass('messageSubmit');
        $("#tdomf_form8_send").addClass('messageContinue');
        $("#tdomf_form8_send").click(function(){
            window.frames[0].submitContinue();
            return false;
        });
    }
}
function toggleUpload() {
    $('#imageUpload').toggle();
    var isVisible = $('#imageUpload').is(':visible');
    if( isVisible ) {
        $('#toggleImageUpload').hide();
        //changePostButton('continue');
    } else {
        $('#toggleImageUpload').show();
        $('#toggleImageUpload').text("Haluan lisätä viestiin kuvia");
        //changePostButton('submit');
    }
    return true;
}

function sizeIFrame() {
    var helpFrame = $("#uploadfiles_inline");
    var innerDoc = (helpFrame.get(0).contentDocument) ? helpFrame.get(0).contentDocument : helpFrame.get(0).contentWindow.document;
    helpFrame.height(innerDoc.body.scrollHeight + 5);
}

$(document).ready(function() {

    $('a#searchBtn').click(function(){  
        document.searchFormMain.submit();
    }); 

    $("#uploadfiles_inline").load(sizeIFrame);
    $('#imageUpload').toggle();
    venytys();
    
    $(".toggleCommentForm").click(function(){
        var $this = $(this);
        var commentFormID = $this.attr('rel');
        $("#"+commentFormID).toggle();
        var isVisible = $("#"+commentFormID).is(':visible');
        if( isVisible ) {
            $this.text("Piilota lomake");
        } else {
            $this.text("Kommentoi viestiä");
            $("#"+commentFormID+" .required").hide();
        }
        return false;
    });
    
    $("#toggleImageUpload").click(function(){

        var $this = $(this);
        
        $('#imageUpload').toggle();
        
        var isVisible = $('#imageUpload').is(':visible');
        if( isVisible ) {
            $this.hide();
            togglePhase2();
            changePostButton('continue');
            disableSubmit('disable');
        } else {
            console.log("submit");
            $this.text("Haluan lisätä viestiin kuvia");
            togglePhase1();
            changePostButton('submit');
        }
        
        sizeIFrame();
        return false;
    });
    
    $(".postCommentForm").submit(function() {
        
        var ID = $(this).attr("id");
        var author = this.author.value;
        var email = this.email.value;
        var comment = this.comment.value;
        var error = 0;
        
        if (author == "") {
            error = 1;
        }

        if (comment == "") {
            error = 1;
        }
        
        var errorID = "#"+ID+" .required";

        if (error == 1) {
            $(errorID).show();
            return false;
        }
    });
});