﻿$(document).ready(function() { bluringAnchorsOnClick(); });

function search() {
    $("#cse-search-box").submit();
}

function textCounterQuestion(field) {
    var maxlimit = 140;
    if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
    else {
        jQuery("#remainingCharsIndicatorQuestion").html(maxlimit - field.value.length);
    }
}

function showError(msg) {
    window.scrollTo(0, 0);
    $(".top-message").html(msg);
    $('.top-message').fadeIn('slow', function() { setTimeout('fadeOutTopMessage();', 5000); });
    setTimeout('$("#recaptcha_response_field").unbind("focus");', 1000);
    
}

function fadeOutTopMessage() {
    $('.top-message').fadeOut('slow', function() { });
}

function showComments(answerId) {
    $("#comments_" + answerId).fadeIn('slow', function() { });
    $("#comments_" + answerId + " .new-comment").fadeIn('slow', function() { });
}

function expandTextArea(textarea, height) {
    $(textarea).css("height", height + "px");
}

function collapseTextArea(textarea, height) {
    $(textarea).css("height", height + "px");
}

function validateAnswerForm(validateAnonymous) {
    
    var result = true;

    $(".answerFrom #AnswerText").css("border-color", "");
    $(".answerFrom #Name").css("border-color", "");
    $(".answerFrom #Email").css("border-color", "");
    
    if ($(".answerFrom #AnswerText").val().length == 0) {
        $(".answerFrom #AnswerText").css("border-color", "#C98B31");
        showError('Neįvestas atsakymo tekstas');
        result = false;        
    }

    if (validateAnonymous) {
        if ($(".answerFrom #Name").val().length == 0) {
            $(".answerFrom #Name").css("border-color", "#C98B31");
            showError('Neįvestas vardas.');
            result = false;
        }
        if ($(".answerFrom #Email").val().length == 0) {
            $(".answerFrom #Email").css("border-color", "#C98B31");
            showError('Neįvestas el. pašto adresas');
            result = false;
        }
        else if (!emailValidate($(".answerFrom #Email").val())) {
            $(".answerFrom #Email").css("border-color", "#C98B31");
            showError('Neteisingai įvestas el. pašto adresas');
            result = false;
        }
    }
    return result;
}

function validateQuestionForm(validateAnonymous) {

    var result = true;

    $(".new-question #QuestionText").css("border-color", "");
    $(".new-question #Tags").css("border-color", "");
    $(".new-question #Name").css("border-color", "");
    $(".new-question #Email").css("border-color", "");

    if ($(".new-question #QuestionText").val().length == 0) {
        $(".new-question #QuestionText").css("border-color", "#C98B31");
        showError('Neįvestas klausimas');
        result = false;
    }

    if ($(".new-question #Tags").val().length == 0) {
        $(".new-question #Tags").css("border-color", "#C98B31");
        showError('Neįvestos klausimo žymės');
        result = false;
    }

    if (validateAnonymous) {
        if ($(".new-question #Name").val().length == 0) {
            $(".new-question #Name").css("border-color", "#C98B31");
            showError('Neįvestas vardas');
            result = false;
        }
        if ($(".new-question #Email").val().length == 0) {
            $(".new-question #Email").css("border-color", "#C98B31");
            showError('Neįvestas el. pašto adresas');
            result = false;
        }
        else if (!emailValidate($(".new-question #Email").val())) {
            $(".new-question #Email").css("border-color", "#C98B31");
            showError('Neteisingai įvestas el. pašto adresas');
            result = false;
        }
    }
    return result;
}


function validateLoginForm() {

    var result = true;

    $(".login #UserName").css("border-color", "");
    $(".login #Password").css("border-color", "");
    
    if ($(".login #UserName").val().length == 0) {
        $(".login #UserName").css("border-color", "#C98B31");
        showError('Neįvestas vartotojo vardas');
        result = false;
    }
    if ($(".login #Password").val().length == 0) {
        $(".login #Password").css("border-color", "#C98B31");
        showError('Neįvestas slaptažodis');
        result = false;
    }
    return result;
}

function validateForumTopicForm(isAnonymous) {

    var result = true;

    $(".forums #threadName").css("border-color", "");
    $(".forums #threadText").css("border-color", "");

    if (isAnonymous) {
        result = false;
        showError('Forume rašyti gali tik registruoti sistemos vartotojai. Prašome prisiregistruoti arba prisijungti.');
    }       
    if ($(".forums #threadName").val().length == 0) {
        $(".forums #threadName").css("border-color", "#C98B31");
        showError('Neįvestas temos pavadinimas');
        result = false;
    }
    if ($(".forums #threadText").val().length == 0) {
        $(".forums #threadText").css("border-color", "#C98B31");
        showError('Neįvesta žinutė');
        result = false;
    }
    return result;
}

function validateForumMessageForm(isAnonymous) {

    var result = true;

    $(".forums #threadText").css("border-color", "");

    if (isAnonymous) {
        result = false;
        showError('Forume rašyti gali tik registruoti sistemos vartotojai. Prašome prisiregistruoti arba prisijungti.');
    }
    if ($(".forums #threadText").val().length == 0) {
        $(".forums #threadText").css("border-color", "#C98B31");
        showError('Neįvestas temos pavadinimas');
        result = false;
    }
    
    return result;
}

function validateBlogPostCommentForm(isAnonymous) {

    var result = true;

    $(".blog #threadText").css("border-color", "");

    if (isAnonymous) {
        result = false;
        showError('Komentuoti gali tik registruoti sistemos vartotojai. Prašome prisiregistruoti arba prisijungti.');
    }
    if ($(".blog #commentText").val().length == 0) {
        $(".blog #commentText").css("border-color", "#C98B31");
        showError('Neįvestas komentaras');
        result = false;
    }

    return result;
}

function validateCommentForm(answerId) {

    var result = true;

    $("#comments_" + answerId + " #commentText").css("border-color", "");

    if ($("#comments_" + answerId + " #commentText").val().length == 0) {
        $("#comments_" + answerId + " #commentText").css("border-color", "#C98B31");
        showError('Neįvestas komentaras');
        result = false;
    }

    return result;
}

function validateLostPasswordChangeForm() {

    var result = true;

    $(".lost-password #Password").css("border-color", "");
    $(".lost-password #PasswordConfirm").css("border-color", "");
    
    if ($(".lost-password #Password").val().length == 0) {
        $(".lost-password #Password").css("border-color", "#C98B31");
        showError('Neįvestas slaptažodis');
        result = false;
    }
    if ($(".lost-password #PasswordConfirm").val().length == 0) {
        $(".lost-password #PasswordConfirm").css("border-color", "#C98B31");
        showError('Nepatvirtintas slaptažodis');
        result = false;
    }
    return result;
}
function validateLostPasswordForm() {

    var result = true;

    $(".lost-password #Email").css("border-color", "");
    
    if ($(".lost-password #Email").val().length == 0) {
        $(".lost-password #Email").css("border-color", "#C98B31");
        showError('Neįvestas el. pašto adresas');
        result = false;
    }
    else if (!emailValidate($(".lost-password #Email").val())) {
        $(".lost-password #Email").css("border-color", "#C98B31");
        showError('Neteisingai įvestas el. pašto adresas');
        result = false;
    }
    return result;
}
function validateProfileData() {
    
    var result = true;

    $(".user-profile #Website").css("border-color", "");
    
    if ($(".user-profile #Website").val().length > 0 && !urlValidate($(".user-profile #Website").val())) {
        $(".user-profile #Website").css("border-color", "#C98B31");
        showError('Neteisingai įvestas adresas');
        result = false;
    }
    return result;
}
function emailValidate(str) {
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1)
        return false;
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
        return false;
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
        return false;
    if (str.indexOf(at, (lat + 1)) != -1)
        return false;
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot)
        return false;
    if (str.indexOf(dot, (lat + 2)) == -1)
        return false;
    if (str.indexOf(" ") != -1)
        return false;
    return true;
}
function urlValidate(input) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(input)) {
        return false;
    }
    return true;
} 
function bluringAnchorsOnClick() {
    $('a').live('mousedown', function(e) {
        e.target.blur();
        e.target.hideFocus = true;
        e.target.style.outline = 'none'
    }).live('mouseout', function(e) {
        e.target.blur();
        e.target.hideFocus = false;
        //e.target.style.outline = null;
    });
}
function filterDrop(el) {
    if ($(el).hasClass('down')) {
        $(el).removeClass('down');
        $('#filter-content').hide();
    } else {
        $(el).addClass('down');
        $('#filter-content').show();
    }
}
function questionsFilterSelect(el) {
    $('#filter').html($(el).html());
    $('#filter').removeClass('down');
    $('#filter-content').hide();
    $('.tabs #loader').show();

    $.get($(el).attr('href'), function(response) {
        $('.questions-list-placeholder .questions').html($('.questions', response).html());
        $('.questions-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        $('.tabs #loader').hide();
    });
}
function userActivityFilterSelect(el) {
    $('#filter').html($(el).html());
    $('#filter').removeClass('down');
    $('#filter-content').hide();
    $('.tabs #loader').show();
    $.get($(el).attr('href'), function(response) {
        $('.user-activity-list-placeholder .activities').html($('.activities', response).html());
        $('.user-activity-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        $('.tabs #loader').hide();
    });
}
function answersFilterSelect(el) {
    $('#filter').html($(el).html());
    $('#filter').removeClass('down');
    $('#filter-content').hide();
    $('.tabs #loader').show();
    $.get($(el).attr('href'), function(response) {
    $('.answers-placeholder .answers-result').html($('.answers-result', response).html());
        $('.tabs #loader').hide();
    });
}
function usersFilterSelect(el) {
    $('#filter').html($(el).html());
    $('#filter').removeClass('down');
    $('#filter-content').hide();
    $('.tabs #loader').show();

    $.get($(el).attr('href'), function(response) {
        $('.users-list-placeholder .users').html($('.users', response).html());
        $('.users-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        $('.tabs #loader').hide();
    });
}
function addMoreLinkBehaviour() {
    $('.questions-list-placeholder #more-link').live('click', function() {
        $(this).addClass('loading');
        $.get($(this).attr('href'), function(response) {
            $('.questions-list-placeholder .questions').append($('.questions', response).html());
            $('.questions-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        });
        return false;
    });
}
function addMoreUserActivitiesLinkBehaviour() {
    $('.user-activity-list-placeholder #more-link').live('click', function() {
        $(this).addClass('loading');
        $.get($(this).attr('href'), function(response) {
            $('.user-activity-list-placeholder .activities').append($('.activities', response).html());
            $('.user-activity-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        });
        return false;
    });
}
function addMoreUsersLinkBehaviour() {
    $('.users-list-placeholder #more-link').live('click', function() {
        $(this).addClass('loading');
        $.get($(this).attr('href'), function(response) {
            $('.users-list-placeholder .users').append($('.users', response).html());
            $('.users-list-placeholder #more-link-placeholder').replaceWith($('#more-link-placeholder', response));
        });
        return false;
    });
}
function addCommentsPosting(answerId) {
    $("#comments_" + answerId + " form").submit(function() {
        if (validateCommentForm(answerId)) {
            var form = $("#comments_" + answerId + " form");
            var action = form.attr("action");
            var serializedForm = form.serialize();

            $("#btnCommentSubmit_" + answerId).attr("disabled", "true");

            $.post(action,
                    serializedForm,
                    function(result) {
                        if ($('.error', result).html() != null)
                            showError($('.error', result).html());
                        else {
                            $("#comments_" + answerId + " .comments-placeholder").html($('.comments', result).html());
                            $('.comments form textarea').val("");
                            $("#comments_" + answerId + " .comments-placeholder p:last").highlightFade({ color: 'rgb(138,170,0)', complete: function() { /*$(this).remove()*/ }, iterator: 'sinusoidal' });
                        }
                        
                        $("#btnCommentSubmit_" + answerId).attr("disabled", "");
                    });
            collapseTextArea($("#comments_" + answerId + " form textarea"), 50);
        }
        return false;
    });
}
function showReportPanel(id, sender) {
    $(".report-popup").hide();
    $("#report-popup-" + id).show();
    $("#report-popup-" + id).css("left", $(sender).position().left);
    $("#report-popup-" + id).css("top", $(sender).position().top - 30 - parseInt($("#report-popup-" + id).css("height")));
}
function closeReportPopups() {
    $(".report-popup").hide();
}
function reportQuestion(questionId) {
    
}

function reportAnswer(answerId) {

}
