showQuickPost// default functions

function closePanel() {

    var p = document.getElementById("close_panel");
    var b = document.getElementById("close_panel_btns");

    p.style.display = "none";
    b.style.display = "none";
}

function showQuickPost() {

    var q = document.getElementById("add_file_quick");
    var f = document.getElementById("add_file_full");
    var n = document.getElementById("post_nav");

    $("#add_file_quick").removeClass("form_no_display");
    $("#add_ex_quick_intro_text1").removeClass("form_no_display");
    $("#add_ex_quick_intro_text2").removeClass("form_no_display");
    
    $("#add_file_full").addClass("form_no_display");
    $("#add_ex_full_intro_text").addClass("form_no_display");

    $(n).removeClass("full").addClass("quick");
    window.form_type_selected = "quick";

}

function showFullPost() {

    var q = document.getElementById("add_file_quick");
    var f = document.getElementById("add_file_full");
    var n = document.getElementById("post_nav");

    $("#add_file_quick").addClass("form_no_display");
    $("#add_ex_quick_intro_text1").addClass("form_no_display");
    $("#add_ex_quick_intro_text2").addClass("form_no_display");
    
    $("#add_file_full").removeClass("form_no_display");
    $("#add_ex_full_intro_text").removeClass("form_no_display");

    $(n).addClass("full").removeClass("quick");
    window.form_type_selected = "full";
}

$(document).ready(function(){
    $('.ebox_meta img.up').click(function(){
        //alert("a)");
        });
});

function comment_vote_pos(comment_id) {
    comment_vote(comment_id, "positive");
}

function comment_vote_neg(comment_id) {
    comment_vote(comment_id, "negative");
}

function comment_vote(comment_id, outcome) {
    /*var img = ''; if (outcome == "positive") img = "up"; else if (outcome == "negative") img = "down";
    $("#comment_id_" + comment_id + " ." + img).attr('onclick', '').toggleClass("vote_cursor");*/
    $("#comment_id_" + comment_id + " .up").attr('onclick', '').toggleClass("vote_cursor");
    $("#comment_id_" + comment_id + " .down").attr('onclick', '').toggleClass("vote_cursor");
    $.post(window.COMMENT_VOTE_URL, {
        comment_id: comment_id,
        outcome: outcome
    }, function(retrieved_data){
        if (retrieved_data.length > 0) $("#comment_id_" + comment_id + " ." + outcome + "_votes_count").text("(" + retrieved_data + ")");
    });
}

function profile_vote_pos(page, profile_id, boy_girl) {
    profile_vote(page, profile_id, "positive", boy_girl);
}

function profile_vote_neg(page, profile_id, boy_girl) {
    profile_vote(page, profile_id, "negative", boy_girl);
}

function get_counter_outcome(outcome) {
    if (outcome == "positive") return "negative";
    else return "positive";
}

function profile_vote(page, profile_id, outcome, boy_girl) {
    var outcome_str = ''; if (outcome == "positive") outcome_str = "ewww. your ex sucks!"; else if (outcome == "negative") outcome_str = "you deserved it, loser.";
    switch (page) {
        case "short":
            $("#profile_id_" + profile_id + " ." + outcome + "_vote").attr('onclick', '').toggleClass("vote_cursor");
            $("#profile_id_" + profile_id + " ." + get_counter_outcome(outcome) + "_vote").attr('onclick', '').toggleClass("vote_cursor");
            break;
        case "index":
            $("#" + boy_girl + "_of_day_ul a." + outcome + "_vote").attr('onclick', '').toggleClass("vote_cursor").toggleClass("vote_underline");
            $("#" + boy_girl + "_of_day_ul a." + get_counter_outcome(outcome) + "_vote").attr('onclick', '').toggleClass("vote_cursor").toggleClass("vote_underline");
            break;
        case "show":
            $("#good_bad_box a." + outcome + "_vote").attr('onclick', '').toggleClass("vote_cursor");
            $("#good_bad_box a." + get_counter_outcome(outcome) + "_vote").attr('onclick', '').toggleClass("vote_cursor");
            break;
    }
    $.post(window.PROFILE_VOTE_URL, {
        profile_id: profile_id,
        outcome: outcome
    }, function(retrieved_data){
        //alert(retrieved_data);
        if (retrieved_data.length > 0)
            switch (page) {
                case "short":
                    $("#profile_id_" + profile_id + " ." + outcome + "_votes_count").text(outcome_str + " (" + retrieved_data + ")");
                    break;
                case "index":
                    $("#" + boy_girl + "_of_day_ul ." + outcome + "_votes_count").text("(" + retrieved_data + ")");
                    break;
                case "show":
                    $("#good_bad_box ." + outcome + "_votes_count").text(outcome_str + " (" + retrieved_data + ")");
                    break;
            }
    });
}

function profile_moderate_pos(profile_id) {
    profile_moderate(profile_id, "positive");
}

function profile_moderate_neg(profile_id) {
    profile_moderate(profile_id, "negative");
}

function profile_moderate(profile_id, outcome) {
    $.post(window.PROFILE_MODERATE_VOTE_URL, {
        profile_id: profile_id,
        outcome: outcome
    }, function(){
        window.location.reload();
    });
}

function promote_to_home_page(profile_id, page) {
    $.post(window.PROMOTE_TO_HOME_PAGE, {
        profile_id: profile_id
    }, function(){
        window.location.reload();
    });
}

function approve_exfile(profile_id) {
    $.post(window.APPROVE_EXFILE, {
        profile_id: profile_id
    }, function(){
        window.location.reload();
    });
}

function promote_to_ex_of_the_day(profile_id) {
    $.post(window.PROMOTE_TO_EX_OF_THE_DAY, {
        profile_id: profile_id
    }, function(){
        window.location.reload();
    //hide_admin_control(profile_id, "promote_to_ex_of_the_day");
    });
}

function delete_exfile(profile_id) {
    if (confirm("Are you sure that you want to permanently delete the exfile and all it's associated comments and abuse reports?")) {
        $.post(window.DELETE_EXFILE, {
            profile_id: profile_id
        }, function(a){
            window.location.reload();
        });
    }
}

/*function hide_admin_control(profile_id, control_name) {
    //alert("#profile_id_" + profile_id + " ." + control_name);
    $("#profile_id_" + profile_id + " ." + control_name).attr('onclick', '').hide();
}*/

function state_selected() {
    var state_code = $("#" + window.form_type_selected + "_state_input").attr("value");
    var other = '';
    if (window.form_type_selected == "quick") other = "full"; else other = "quick";
    $("#" + other + "_state_input").attr("value", state_code);
    $.post(window.PROFILE_GET_CITY_COMBO_BOX, {
        state_code: state_code
    }, function(retrieved_data){
        $(".city_select").html(retrieved_data);
    });
}

function country_selected() {
    var country_code = $("#" + window.form_type_selected + "_country_input").attr("value");
    var other = '';
    if (window.form_type_selected == "quick") other = "full"; else other = "quick";
    $("#" + other + "_country_input").attr("value", country_code);
    if (country_code != "us") {
        $(".us_field").hide();
        $("._state_input").html ('<option value="country_' + country_code + '"></option>');
        $(".us_field.zip_code_input").attr("value","0");
        $.post(window.PROFILE_GET_CITY_COMBO_BOX, {
            state_code: "country_" + country_code
        }, function(retrieved_data){
            $(".state_select").html('<option value="us_ak" selected="selected">Alaska</option>');
            $(".city_select").html(retrieved_data);
        });
    } else {
        $.post(window.PROFILE_GET_STATE_COMBO_BOX, {
            country_code: "us_%"
        }, function(retrieved_data){
            $(".state_select").html(retrieved_data);
            $(".us_field").show();
        });
        $.post(window.PROFILE_GET_CITY_COMBO_BOX, {
            state_code: "no-valid-state" //This will return just the Choose City legend
        }, function(retrieved_data){
            $(".city_select").html(retrieved_data);
        });
        $(".us_field.zip_code_input").attr("value","Postal Code - if known");
        window.zip_code_cleared = 0;
    }
}

function load_abuse_report_modal(id) {
    createPopUp('modal_hidden_div', 525, 320);
    $.post(window.PROFILE_LOAD_REPORT_ABUSE_MODAL, {
        id: id
    }, function(retrieved_data){
        $("#NSPopUP").html(retrieved_data);
    });
}

function load_edit_filed_under_modal() {
    createPopUp('modal_hidden_div', 680, 400);
    $.post(window.PROFILE_LOAD_EDIT_FILED_UNDER_MODAL, {
        }, function(retrieved_data){
            $("#NSPopUP").html(retrieved_data);
        });
}

function unload_modal() {
    closePopUp();
}

function refresh_abuse_report_abuse_type_question() {
    $(".abuse_question_p").hide();
    $("#abuse_question_p_" + get_abuse_report_type_id()).show();
}

function get_abuse_report_type_id() {
    var abuse_form = document.getElementById("abuse_form");
    if (abuse_form.ex_files_abuse_report_type[1].checked) return abuse_form.ex_files_abuse_report_type[1].value;
    else if (abuse_form.ex_files_abuse_report_type[2].checked) return abuse_form.ex_files_abuse_report_type[2].value;
    else return abuse_form.ex_files_abuse_report_type[0].value;
}

function save_abuse_report_form(id) {
    var abuse_form = document.getElementById('abuse_form');
    var csrf_token = $('#ex_files_abuse_report__csrf_token').attr('value');
    $.post(window.PROFILE_SAVE_ABUSE_REPORT_FORM, {
        ex_files_abuse_report_type: get_abuse_report_type_id(),
        details: abuse_form.details.value,
        exfile_id: id,
        _csrf_token: csrf_token
    }, function(retrieved_data){
        if (retrieved_data != "success") alert (retrieved_data);
        else {
            //alert ("Abuse reported succesfully. Thanks for helping us!");
            unload_modal();
        }
    });
}

function load_email_a_friend_modal(id) {
    hide_video();
    createPopUp('modal_hidden_div', 525, 250);
    $.post(window.PROFILE_LOAD_EMAIL_A_FRIEND_MODAL, {
        id: id
    }, function(retrieved_data){
        $("#NSPopUP").html(retrieved_data).height("auto");
    });
}

function process_email_a_friend_form (id) {
    var email_a_friend_form = document.getElementById("email_a_friend_form");
    if (emailcheck(email_a_friend_form.from.value) && emailcheck(email_a_friend_form.to.value)) {
        $("#messages_div span").text("Sending email...");
        $("#messages_div").show();
        var speed = 1500;
        $.post(window.PROFILE_PROCESS_EMAIL_A_FRIEND_FORM, {
            from: email_a_friend_form.from.value,
            to: email_a_friend_form.to.value,
            comments: email_a_friend_form.comments.value,
            id: id
        }, function(retrieved_data){
            if(retrieved_data == "success") {
                $("#messages_div span").text("Email sent!");
            } else {
                //alert (retrieved_data);
                $("#messages_div span").text("Email sending failed. Sorry for the inconvenience.");
                speed = 3000;
            }
            $("#messages_div span").animate({
                opacity: 1
            }, speed, function(){
                unload_modal();
            });
        });
    } else {
        $("#messages_div span").text("Please enter 2 valid emails");
        $("#messages_div").show();
    }
}

function emailcheck(str) {

    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;

    //no @, @ at the beggining or @ at the end
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }
    //no ., . at the beggining or . at the end
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }
    //just one @
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    //. just before @ or ????
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    //dot after the @
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }
    //no spaces
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}

function characters(txtarea) {
    var txtarea_parent = $(txtarea).parent()
    if ($(txtarea_parent).hasClass('full_long')) var max_size = 2000; else max_size = 300;
    if ($(txtarea).attr('value').length > max_size) $(txtarea).attr('value', $(txtarea).attr('value').substring(0, max_size));
    $(txtarea_parent).find('strong').text($(txtarea).attr('value').length + '/' + max_size + ' characters');
}

//Facebook share
function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}




