$(document).ready(function(){
    //table row expander
    $('.expander').each(function(){
        //show or hide rows when clicked
        $(this).click(function(){
            if($(this).hasClass("expanded")){
                collapse_event(this);
            }
            else{
                expand_event(this);
            }
        });
    });
    
    //user details form - validation for short bio maximum length
    $('form#user-form').submit(function() {
        var error_msg ='Short bio must not exceed 1000 characters in length';
        var short_bio = $('textarea[name=short_bio]').val();
        if(short_bio.length > 1000){
            //add the error banner to the top
            if(!$('#form_error').length){
                $('form#user-form').before('<div class="error" id="form_error">Some errors occurred in the form below. Please check the fields and re-submit the form.</div>');
            }
            
            if($('#error-short_bio .error').length){
                //change the error message if it already exists
                $('#error-short_bio .error').html(error_msg);
            }
            else{
                //create the error message
                $('#row-short_bio').after('<tr id="error_short-bio"><td class="error" colspan="2">' + error_msg + '</td></tr>');
            }
            
            $('textarea[name=short_bio]').focus();
            
            return false;
        }
    });
    
    // textarea counter
    $("form#comment-form textarea[name=comment]").counter({
        type: 'word', 
        goal: 200
    });
});

function collapse_event(elt){
    var id=$(elt).attr('id');
    $(elt).removeClass("expanded");
    $(elt).text("+");
    $(elt).attr('t', "Show events");
    $('.event_'+id).hide();
    $('#vtip').text("Show events");
    /*
    $('.event_'+id+' div').slideUp(200, function(){
        $('.event_'+id).hide();
        $('#vtip').text("Show events");
    });
    */
}
function expand_event(elt){
    var id=$(elt).attr('id');
    $(elt).addClass("expanded");
    $(elt).text("-");
    $(elt).attr('t', "Hide events");
    $('.event_'+id).show();
    $('#vtip').text("Hide events");
    /*
    $('.event_'+id+' div').slideDown(200, function(){
            $('#vtip').text("Hide events");
    });
    */
}

function collapse_all() {
    $('.collapse_all').css({'color' : '#000000', 'font-weight' : 'bold', 'text-decoration' : 'none'});
    $('.expand_all').css({'color' : '#333333', 'font-weight' : 'normal'});

    $(".expander").each(function() {
        if($(this).hasClass("expanded")){
            collapse_event(this);
        }
    });
    
    return false;
}

function expand_all() {
    $('.expand_all').css({'color' : '#000000', 'font-weight' : 'bold', 'text-decoration' : 'none'});
    $('.collapse_all').css({'color' : '#333333', 'font-weight' : 'normal'});
    
    $(".expander").each(function() {
        if(!$(this).hasClass("expanded")){
            expand_event(this);
        }
    });
    
    return false;
}


var updating = false;


function strstr (haystack, needle, bool) {
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    }
    else{
        if (bool){
            return haystack.substr( 0, pos );
        }
        else{
            return haystack.slice( pos );
        }
    }
}


function update_event(id, handler_url, rsvp, rsvp_first_timers){
    var checkbox = document.getElementById('checkbox-'+id);
    var proceed = true;
    
    if((checkbox.checked) && rsvp) {
        proceed = confirm("By adding this event to your calendar an official RSVP will be sent to APAM administration. \n\nDo you wish to proceed?");
    }
    else if((checkbox.checked) && rsvp_first_timers) {
        proceed = confirm("This event is open to all APAM first time attendees. By adding this event to your calendar an official RSVP will be sent to APAM administration. \n\nDo you wish to proceed?");
    }
    
    if(proceed){
        if(document.getElementById('checkbox-'+id).checked){
            var params = 'add[]='+id;
        }
        else {
            var params = 'remove[]='+id;
        }

        params = params+'submit=submit';
    
        submit_event(id, params, handler_url);
    }
    else {
        checkbox.checked = false;
    }
}


function submit_event(id, params, handler_url){
    updating = true;
    
    if(id.length > 0){
        $('#checkbox-'+id).disabled=true;
    }
    
    $.ajax({
        type: "POST",
        url: handler_url,
        data: params,
        success: function(msg){
            updating = false;
            if(id.length > 0){
                $('#checkbox-'+id).disabled=false;
            }
        }
    });
    
    return false;
}


window.onbeforeunload = confirmExit;
function confirmExit(){
    if(updating == true) {
        return "There are currently events being saved to your schedule. Please wait a moment for this process to complete to avoid losing data.";
    }
}
