$(function() {
	$('#comment-text').keyup(function() {
		// Displays and formats user comments on-screen
		var msg = $('#comment-text').val();
		msg = cleanComment(msg);
		msg = "<b>Comment Preview</b><br />" + msg;
		$('#comment-preview').html(msg);

		if(msg != "") {
			$('#comment-preview').show();
			}
		else {
			$('#comment-preview').hide();
			}
		});
	});


function mtReplyCommentOnClick(parent_id, author) {
    $('#comment-form-reply').show();

    var checkbox = document.getElementById('comment-reply');
    var label = document.getElementById('comment-reply-label');
    var text = document.getElementById('comment-text');

    // Populate label with new values
    var reply_text = 'Replying to \<a href=\"#comment-__PARENT__\" onclick=\"location.href=this.href; return false\"\>comment from __AUTHOR__\<\/a\>';
    reply_text = reply_text.replace(/__PARENT__/, parent_id);
    reply_text = reply_text.replace(/__AUTHOR__/, author);
    label.innerHTML = reply_text;

    checkbox.value = parent_id; 
    checkbox.checked = true;
    try {
        // text field may be hidden
        text.focus();
    } catch(e) {
    }

    mtSetCommentParentID();
}


	
function mtSetCommentParentID() {
    var checkbox = document.getElementById('comment-reply');
    var parent_id_field = document.getElementById('comment-parent-id');
    if (!checkbox || !parent_id_field) return;

    var pid = 0;
    if (checkbox.checked == true)
        pid = checkbox.value;
    parent_id_field.value = pid;
	}
	
	
function cleanComment(text) {
	text = text.replace(/<a.*javascript:.*?>/ig, ''); 	
	text = text.replace(/<a.*class=.*?>/ig, ''); 	
	text = text.replace(/<a.*style=.*?>/ig, ''); 	
	text = text.replace(/<a.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<i.*class=.*?>/ig, ''); 	
	text = text.replace(/<i.*style=.*?>/ig, ''); 	
	text = text.replace(/<i.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<b.*class=.*?>/ig, ''); 	
	text = text.replace(/<b.*style=.*?>/ig, ''); 	
	text = text.replace(/<b.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<img.*?>/g, ''); 	
	text = text.replace(/<(?!a.*?|\/a|b|\/b|i|\/i).*?>/ig, ''); 

	//text = autoLink(text);

	var pars = text.split(/\r?\n\r?\n/);
	var newtext = ''; 
	for(i = 0; i < pars.length; i++) {
		pars[i] = pars[i].replace(/\r?\n/g, '<br />\n'); 	
		newtext += '<p>\n' + pars[i] + '</p>\n';
	}
	newtext = cleanQuotes(newtext);
	return newtext;
}

function autoLink(text) {
    text = text.replace( /(^|\s+)(http:\/\/\S+)/igm, '$1<a href="$2">$2</a>');
    var amatches = text.match(/>(.*?)<\/a>/g);
    if(amatches) {
        for(i = 0; i < amatches.length; i++) {
            var href = amatches[i];
            if(href.length > 60) {
                var nhref = href.substr(0, 59);
                var index = text.indexOf(href);
                var stext = text.substr(0, index);
                var etext = text.substr(index + href.length, text.length);
                text = stext + nhref + "...</a>" + etext;  
            }
        }
    }
    return text;
}

function cleanQuotes(text) {
    // clean smart chars and such...
    // 8216, 8217, 8220, 8221, 8212, 8211
    var chars = text.split("");
    var newtext = '';
    for(i = 0; i < chars.length; i++) {
        var chr = chars[i].charCodeAt(0);
        switch( chr ) {
            case 8216: newtext += "'"; break;
            case 8217: newtext += "'"; break;
            case 8220: newtext += '"'; break;
            case 8221: newtext += '"'; break;
            case 8211: newtext += "-"; break;
            case 8212: newtext += "-"; break;
            default: newtext += chars[i];
        }
    }
    return newtext;
}