function vote(post_id, val, m) {
    $.get('ajax/post.vote.php?post_id=' + post_id + '&m='+ m + '&vote=' + val, function(text) { $('#vote-status-' + post_id).html(text); } );
}

var hilite_row = 0;

function comment_hilite_parent(id) {
    $('#comment-'+hilite_row).removeClass('row-new');
    hilite_row = id; 
    $('#comment-'+id).addClass('row-new');

}

function comment_set_parent(id, message) {
    $('#parent_id').attr('value', id);
    $('#comment-button').attr('value', 'Post Reply');
    $('#comment-reply').html('<div class="info">'+message+' <a href="javascript:comment_unset_parent()">(undo)</a></div>');
}

function comment_unset_parent() {
    $('#parent_id').attr('value', 0);
    $('#comment-button').attr('value', 'Post Comment');
    $('#comment-reply').html('');
    
}

function imgwidth(classname, maxW) {
  $('.'+classname).each( function() {

    var w = $(this).attr('width');
    var h = $(this).attr('height');
    
    if (w > maxW) {
      var f = 1 - ((w - maxW) / w);
      $(this).attr('width', w * f);
      $(this).attr('height', h * f);
    }

  } 
  );
}

function donothing() {
    return false;
}

function toggleSlide(id) {

    if ($('#'+id).is(':hidden'))
        $('#'+id).slideDown(); 
    else
        $('#'+id).slideUp(); 
}

function selectAll(group) {
     $('.'+group).each( function() { 
    
        if ($(this).attr('id') == "all")
            selectall = this;
        else {
        
            $(this).attr('checked', $(selectall).attr('checked'));
        }    
    
        
    } ); 
}

function selectOne(group) {
    var selectall;
    var total = 0;
    var selected = 0;
    
    $('.'+group).each( function() { 
    
        if ($(this).attr('id') == "all")
            selectall = this;
        else {
        
            ++total;
            if ($(this).attr('checked') == 1)
                ++selected;            
        }    
    
        
    } ); 
    
    if (selected == total)
        $(selectall).attr('checked', 1);
    else
        $(selectall).attr('checked', 0);
}

function toggleDisplay(id) {
    var panel=document.getElementById(id);
    
    if (panel.style.display=='block')
        panel.style.display = 'none';
    else
        panel.style.display = 'block';
}

function toggleVisibility(val, id) {
    var panel=document.getElementById(id);
    
    if (val == '')
        panel.style.visibility = 'hidden';
    else
        panel.style.visibility = 'visible';
}

function toggleEnabled(val, id) {
    var panel=document.getElementById(id);
    
    if (val == '')
        panel.disabled = true;
    else
        panel.disabled = false;
}

function insert_around_text(id, before, after) {
    var textbox = document.getElementById(id);
    
	if (document.selection != null)
	{
		textbox.focus();    
		sel = document.selection.createRange();    
		sel.text = before + sel.text + after; 
	} 
	else if (textbox.selectionStart != null) {
		var startPos = textbox.selectionStart;
		var endPos = textbox.selectionEnd;
    
		textbox.value = textbox.value.substring(0, startPos) + before + textbox.value.substring(startPos, endPos) + after + textbox.value.substring(endPos, textbox.value.length);
	}
}

function insert_image(id) {
    var url = prompt('Please enter the address of the image.'); 
    
	if (url != null)
		insert_around_text('message', '[img]' + url + '[/img]', '')
}

function insert_youtube(id) {
    var url = prompt('Please enter the YouTube video address.'); 
    
	if (url != null) {
	    var pos = url.search(/v=/);
	    var amp = url.search(/&/);
	    
	    if (amp != -1)
            url = url.substring(pos+2, amp);
        else
            url = url.substring(pos+2);
    
		insert_around_text('message', '[youtube]' + url + '[/youtube]', '')
    }
}

function insert_url(id) {
    var url = prompt('Please enter the address.', '');

    if (url != null) {
		
		var textbox = document.getElementById(id);

		if (document.selection != null)
		{
			textbox.focus();    
			sel = document.selection.createRange();    
			var length = sel.text.length;
		
		} 
		else if (textbox.selectionStart)
		{
			var length = textbox.selectionEnd - textbox.selectionStart;
		}

		if (length > 0) 
			insert_around_text(id, '[url=' + url + ']', '[/url]');
		else
			insert_around_text(id, '[url]' + url + '[/url]', '');   
	}
}