jQuery(document).ready(function(){
	
	jQuery("#os_user_story").keyup(function() {
	var box= jQuery(this).val();
	var main = box.length *100;
	var value= (main / 250);
	var count= 250 - box.length;
	
	if(box.length <= 250) {
		jQuery('#count').html(count);
	} else {
		var trimmed = jQuery(this).val().substring(0,250);
		jQuery(this).val(trimmed); //trim it!
	}
	return false;
	});
	
    jQuery('#os_form').submit(function(e) {
        e.preventDefault();
        jQuery.post(
            'wp-content/plugins/community-achievements/community-achievements.php',
            {
                _wpnonce: jQuery('#_wpnonce').val(),
                _wp_http_referer: jQuery("input[name='_wp_http_referer']").val(),
                os_user_name: jQuery("#os_user_name").val(),
                os_user_email: jQuery("#os_user_email").val(),
                os_user_title: jQuery("#os_user_title").val(),
                os_user_story: jQuery("#os_user_story").val(),
                os_captcha_code: jQuery("#os_captcha_code").val()
            },
            function(e){
                if(e.length != 0) {
                    jQuery('#os_user_submission').before(e);
                } else {
                    jQuery('#os_form').html('<p>Thank your for your post - Once approved by the admins it will appear across the site! Click <a href="'+window.location.href+'" id="os_reload_form">here</a> to submit another story</p>');
                    jQuery('#os_msg').show();
                }
            }
        );
    });
    
    jQuery('#os_form').ajaxError(function(e, xhr, settings, exception){
        alert(xhr);
    });

    jQuery('a#os_reload_form').click(function(event){
        event.preventDefault();
        alert(';stiuf');
        jQuery('#os_form').text('[os_news_form /]')
    });

    /*jQuery('#os_admin a').click(function(event){
        if(this.className == 'publish' || this.className == 'delete'){
            event.preventDefault();

            jQuery.post(
                '../wp-content/plugins/community-achievements/community-achievements.php',
                {
                    action: this.className,
                    id: this.id
                },
                function(e){
                    jQuery('#os_msg').html(e);
                    jQuery('#os_msg').show();
                    var row = jQuery(event.target).parents('tr');
                    row.each(function(){
                        jQuery(this).fadeOut('slow', function(e){
                            jQuery(this).remove();
                        });
                    });
                    jQuery(row).fadeOut('slow', function(e){
                        jQuery(row).remove();
                    });
                    setTimeout(function(e){
                        jQuery('#os_msg').fadeOut('slow');
                    }, 5000);
                }
            );
        }
    });*/
});
