// Cuts off textarea when it gets to maxlength

function impose_maxlength() {        
    // Get all textareas that have a "maxlength" property.
    $('textarea[maxlength]').each(function() {

        // Store the jQuery object to be more efficient...
        var $textarea = $(this);

        // Store the maxlength and value of the field.
        var maxlength = $textarea.attr('maxlength');
        var val = $textarea.val();

        // Trim the field if it has content over the maxlength.
        $textarea.val(val.slice(0, maxlength));

        // Bind the trimming behavior to the "keyup" event.
        $textarea.bind('keyup', function() {
            $(this).val($(this).val().slice(0, maxlength));
        });

    });
}

//Function to fade out action button when user does not have sufficient reputation or is not logged in.
function fade_not_allowed() {

    $(".not_allowed").click(function (){
        if ($(this).hasClass("busy")) {
            return false;
        }
        $(".not_allowed").addClass("busy");
    
        $(this).fadeOut(500, function(){
            
            action_id=$(this).attr('id');
         
            width= $(this).find("img").attr("width");
            
            if (action_id=="add_product_button") //Action 2
            {
                action="/images/action_buttons/add_product.png";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png"
                
            }
            
            else if (action_id=="edit_product_button") //Action 3
            {
                action="/images/action_buttons/edit_product.jpg";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png";
                
            }
            
            
            else if (action_id=="delete_product_button") //Action 4
            {
                action="/images/action_buttons/delete.jpg";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png";
                
            }
            
            else if (action_id=="edit_vendor_button") //Action 6
            {
                action="/images/action_buttons/edit_listing.png";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png";
            }
            
            else if (action_id=="delete_vendor_button") //Action 7
            {
                action="/images/action_buttons/delete_vendor.png";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png";
            }
            
            else if (action_id=="add_category_button") //Action 8
            {
                action="/images/action_buttons/add_category.png";
                warning="/images/action_warning_buttons/insufficient_reputation_general.png";
            }
            
            
            else if (action_id=="edit_category") //Action 9
            {
                action="/images/action_buttons/edit_category.png";
                warning="/images/action_warning_buttons/insufficient_reputation_edit_category.png";
            }
            
            else if (action_id=="delete_category") //Action 10
            {
                action="/images/action_buttons/delete_category.png";
                warning="/images/action_warning_buttons/insufficient_reputation_delete_category.png";
            }
            
        
            $(this).removeClass("not_allowed")
            $(this).find("img").attr("src", warning);
       
            $(this).find("img").attr("width", 140);
            
            $(this).fadeIn(1500,function(){
                $(this).fadeOut(1500, function(){
                    //  $(this).find("img").attr("width", width);
                    $(this).addClass("not_allowed")
                    $(this).find("img").attr("src", action);
                    $(this).fadeIn();
                    $("a.busy").removeClass("busy");
                    $(this).find("img").attr("width", width);

                });
            }); 
         
        });
        return false;
    });
    return;
}


//Function to fade out action button when user is not logged in.
function fade_not_logged_in() {

    $(".not_logged_in").click(function (){
        if ($(this).hasClass("busy")) {
            return false;
        }
        $(".not_logged_in").addClass("busy");
    
        $(this).fadeOut(500, function(){
            
            action_id=$(this).attr('id');
         
            width= $(this).find("img").attr("width");
            
            
            if (action_id=="add_product_button") //Action 2
            {
                action="/images/action_buttons/add_product.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
                
            }
            
            
            else if (action_id=="edit_product_button") //Action 3
            {
                action="/images/action_buttons/edit_product.jpg";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
                
            }
            
            else if (action_id=="delete_product_button") //Action 4
            {
                action="/images/action_buttons/delete.jpg";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
                
            }
            
          
            else if (action_id=="edit_vendor_button") //Action 6
            {
                action="/images/action_buttons/edit_listing.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
            }
            
            else if (action_id=="delete_vendor_button") //Action 7
            {
                action="/images/action_buttons/delete_vendor.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
            }
            
             else if (action_id=="add_category_button") //Action 8
            {
                action="/images/action_buttons/add_category.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
            }
            
            
            else if (action_id=="edit_category") //Action 9
            {
                action="/images/action_buttons/edit_category.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
            }
            
            
            else if (action_id=="delete_category") //Action 10
            {
                action="/images/action_buttons/delete_category.png";
                warning="/images/action_warning_buttons/must_login_perform_action.png"
            }
            
          
            
         
            
  
            $(this).removeClass("not_logged_in")
            $(this).find("img").attr("src", warning);
       
            $(this).find("img").attr("width", 140);
            
            $(this).fadeIn(1500,function(){
                $(this).fadeOut(1500, function(){
                    //  $(this).find("img").attr("width", width);
                    $(this).addClass("not_logged_in")
                    $(this).find("img").attr("src", action);
                    $(this).fadeIn();
                    $(".busy").removeClass("busy");
                    $(this).find("img").attr("width", width);

                });
            }); 
         
        });
        return false;
    });
    return;
}


function vote_exists() {
 
  
 
    $(".vote_exists").click(function (){
        if ($(this).hasClass("busy")) {
            return false;
        }
       
        $(".vote_exists").addClass("busy");

        $(this).fadeOut(500, function(){
            xx=  $(this).find("img").attr("src");
            $(this).find("img").attr("src", "/images/action_warning_buttons/already_voted.png");
            $(this).find("img").attr("width", 130);
            $(this).removeClass("vote_exists")
            $(this).fadeIn(1500,function(){
                $(this).fadeOut(1500, function(){
                    $(this).find("img").attr("width", 30);
                    $(this).addClass("vote_exists")
                    $(this).find("img").attr("src", xx);
                    $(this).fadeIn();
                    $(".vote_exists").removeClass("busy");
                //return false;
                });
            }); 
        
        });
            
        return false;
        
    });
        
    return;
}

function check_logged_in(){
    
    
    $(".logged_out").click(function (){
        if ($(this).hasClass("busy")) {
            return false;
        }
        $(".logged_out").addClass("busy");
        // return false;
        $(this).fadeOut(500, function(){
            yy=  $(this).find("img").attr("src");
            $(this).removeClass("logged_out")
            $(this).find("img").attr("src", "/images/login_to_vote.png");
            $(this).find("img").attr("width", 130);
            
            $(this).fadeIn(1500,function(){
                $(this).fadeOut(1500, function(){
                    $(this).find("img").attr("width", 30);
                    $(this).addClass("logged_out")
                    $(this).find("img").attr("src",yy);
                    $(this).fadeIn();
                    $(".logged_out").removeClass("busy");

                });
            }); 
        
        });
        return false;
    }); 
    return;
}

