var xmlHttp
var postID
var origVal
var action
var settingName

function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

function getVideo(vpostID,vaction) {
	postID = vpostID
	action = vaction
	xmlHttpVideo=GetXmlHttpObject();
	if (xmlHttpVideo==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/video.php?post_id="+postID+'&action='+action;
	xmlHttpVideo.onreadystatechange=stateChangedShowVideo
	xmlHttpVideo.open("POST",url,true);
	xmlHttpVideo.send(null);
}
function stateChangedShowVideo() { 
	if (xmlHttpVideo.readyState==4 || xmlHttpVideo.readyState=="complete") {
		if (action == 'show') {
			document.getElementById("hide_video_"+postID).innerHTML = '· <a href="javascript:getVideo(\''+postID+'\',\'hide\');">close video</a>';
		} else {
			document.getElementById("hide_video_"+postID).innerHTML = '';
		}
		document.getElementById("video_"+postID).innerHTML=xmlHttpVideo.responseText;
	}
}

function likePost(postID, attachmentID) {
	document.getElementById("like_"+postID).innerHTML = '· liked';
	xmlHttpLike=GetXmlHttpObject();
	if (xmlHttpLike==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/like.php?post_id="+postID+'&attachment_id='+attachmentID;
	xmlHttpLike.open("POST",url,true);
	xmlHttpLike.send(null);
}

function showCommentForm(vpostID, vpicID) {
	postID = vpostID;
	picID = vpicID;
	document.getElementById("comment_summary_"+postID).innerHTML = '· <a href="javascript:hideCommentForm(\''+postID+'\',\''+picID+'\');">comment</a>';
	
	xmlHttpCommentForm=GetXmlHttpObject();
	if (xmlHttpCommentForm==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	//document.getElementById('new_comment_post_'+postID).innerHTML = '<td valign="middle"><input type="text" value="'+comment+'" readonly><input type="submit" value="Add" disabled>';
	var url="http://bluurt.com/show_comment_form.php?post_id="+postID+'&attachment_id='+picID;
	xmlHttpCommentForm.onreadystatechange=stateChangedCommentForm
	xmlHttpCommentForm.open("POST",url,true);
	xmlHttpCommentForm.send(null);
	
}
function stateChangedCommentForm() { 
	if (xmlHttpCommentForm.readyState==4 || xmlHttpCommentForm.readyState=="complete") {
		//document.getElementById("comment_summary_"+postID).innerHTML = '· comment';
		document.getElementById("new_comment_post_"+postID).innerHTML = xmlHttpCommentForm.responseText;
	}
}

function showCommentFormShowComment(vpostID, vpicID) {
	postID = vpostID;
	picID = vpicID;
	document.getElementById("comment_summary_"+picID).innerHTML = '· <a href="javascript:hideCommentFormShowComment(\''+postID+'\',\''+picID+'\');">comment</a>';
	document.getElementById("new_comment_post_"+picID).innerHTML = '';
	
	xmlHttpCommentFormShowComment=GetXmlHttpObject();
	if (xmlHttpCommentFormShowComment==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	//document.getElementById('new_comment_post_'+postID).innerHTML = '<td valign="middle"><input type="text" value="'+comment+'" readonly><input type="submit" value="Add" disabled>';
	var url="http://bluurt.com/show_comment_form.php?post_id="+postID+'&attachment_id='+picID;
	xmlHttpCommentFormShowComment.onreadystatechange=stateChangedCommentFormShowComment
	xmlHttpCommentFormShowComment.open("POST",url,true);
	xmlHttpCommentFormShowComment.send(null);
}
function stateChangedCommentFormShowComment() { 
	if (xmlHttpCommentFormShowComment.readyState==4 || xmlHttpCommentFormShowComment.readyState=="complete") {
		document.getElementById("new_comment_post_"+picID).innerHTML = xmlHttpCommentFormShowComment.responseText;
	}
}

function hideCommentFormShowComment(vpostID, vpicID) {
	postID = vpostID;
	picID = vpicID;
	document.getElementById("comment_summary_"+vpicID).innerHTML = '· <a href="javascript:showCommentFormShowComment(\''+postID+'\',\''+picID+'\');">comment</a>';
	document.getElementById("new_comment_post_"+vpicID).innerHTML = '';
}

function autosize(elem_id) {
	elem = document.getElementById(elem_id);
	elem.style.height = (elem.scrollHeight+4) + "px";
}



function hideCommentForm(vpostID, vpicID) {
	postID = vpostID;
	picID = vpicID;
	document.getElementById("comment_summary_"+postID).innerHTML = '· <a href="javascript:showCommentForm(\''+postID+'\',\''+picID+'\');">comment</a>';
	document.getElementById("new_comment_post_"+postID).innerHTML = '';
}

function submitComment(comment,vpostID,vpicID) {
	if (comment.length > 0) {
		postID = vpostID;
		picID = vpicID;
		comment = nl2br(comment);
		xmlHttpComment=GetXmlHttpObject();
		if (xmlHttpComment==null) {
			alert ("Browser does not support HTTP Request");
			return;
		}
		//document.getElementById('new_comment_post_'+postID).innerHTML = '<td valign="middle" height="40px"><table width="100%"><tr><td><textarea class="comment_textarea" id="comment_'+postID+'");" disabled>'+comment+'</textarea></td></tr><tr><td align="right" valign="bottom"></td></tr></table></td>';
		document.getElementById('comment_'+postID).disabled = true;
		var url="http://bluurt.com/post_comment.php?comment="+comment+"&post_id="+postID+'&attachment_id='+picID;
		xmlHttpComment.onreadystatechange=stateChangedComment
		xmlHttpComment.open("POST",url,true);
		xmlHttpComment.send(null);
	}
}

function nl2br(text) {
	text = escape(text);
	var re_nlchar;
	if (text.indexOf('%0D%0A') > -1) {
		re_nlchar = /%0D%0A/g ;
	} else if (text.indexOf('%0A') > -1) {
		re_nlchar = /%0A/g ;
	} else if (text.indexOf('%0D') > -1) {
		re_nlchar = /%0D/g ;
	}
	 return unescape( text.replace(re_nlchar,'<br />') );
}

function stateChangedComment() { 
	if (xmlHttpComment.readyState==4 || xmlHttpComment.readyState=="complete") {
			document.getElementById("comment_summary_"+postID).innerHTML = '· comment';
			//document.getElementById("new_comment_post_"+postID).innerHTML = xmlHttpComment.responseText;
			document.getElementById("comment_textarea_"+postID).innerHTML = xmlHttpComment.responseText;
			document.getElementById("comment_textarea_submit_"+postID).style.display = "none";
	}
}

function showAllComments(vpostID) {
	postID = vpostID;
	xmlHttpAllComment=GetXmlHttpObject();
	if (xmlHttpAllComment==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/show_comment.php?post_id="+postID;
	xmlHttpAllComment.onreadystatechange=stateChangedAllComment
	xmlHttpAllComment.open("POST",url,true);
	xmlHttpAllComment.send(null);
}

function stateChangedAllComment() { 
	if (xmlHttpAllComment.readyState==4 || xmlHttpAllComment.readyState=="complete") {
		//if (xmlHttpComment.responseText !== '') {
			document.getElementById('comments_area_'+postID).innerHTML = xmlHttpAllComment.responseText;
		//}
	}
}

function deletePost(vpostID) {
	postID = vpostID
	document.getElementById("delete_"+postID).innerHTML = 'deleted';
	xmlHttpDelete=GetXmlHttpObject();
	if (xmlHttpDelete==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/delete.php?post_id="+postID;
	xmlHttpDelete.open("POST",url,true);
	xmlHttpDelete.send(null);
}

function rebluurt(vpostID) {
	postID = vpostID
	document.getElementById("rebluurt_"+postID).innerHTML = 'rebluurted';
	xmlHttpRebluurt=GetXmlHttpObject();
	if (xmlHttpRebluurt==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/rebluurt.php?post_id="+postID;
	xmlHttpRebluurt.open("POST",url,true);
	xmlHttpRebluurt.send(null);
}

function report(vpostID) {
	postID = vpostID
	document.getElementById("report_"+postID).innerHTML = 'repoted to admin';
	xmlHttpRebluurt=GetXmlHttpObject();
	if (xmlHttpRebluurt==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/report.php?post_id="+postID;
	xmlHttpRebluurt.open("POST",url,true);
	xmlHttpRebluurt.send(null);
}

function followUser(username,action,vpostID) {
	postID = vpostID
	if (action == 'follow') {
		document.getElementById("follow_"+postID).innerHTML = '<span class="profile_clicked">following '+username+'</span>';
	} else {
		document.getElementById("follow_"+postID).innerHTML = '<span class="profile_clicked">not following '+username+'</span>';
	}
	xmlHttpFollow=GetXmlHttpObject();
	if (xmlHttpFollow==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/follow.php?username="+username+'&action='+action+'&post_id='+postID;
	xmlHttpFollow.onreadystatechange=stateChangedAllComment
	xmlHttpFollow.open("POST",url,true);
	xmlHttpFollow.send(null);
}

function showOptions (vpostID) {
	postID = vpostID
	xmlHttpOptions=GetXmlHttpObject();
	if (xmlHttpOptions==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	document.getElementById('show_options_'+postID).innerHTML = '· <a href="javascript:hideOptions(\''+vpostID+'\');">hide options</a>';
	var url="http://bluurt.com/more_post_options.php?post_id="+postID;
	xmlHttpOptions.onreadystatechange=stateChangedOptions
	xmlHttpOptions.open("POST",url,true);
	xmlHttpOptions.send(null);
}
function stateChangedOptions() { 
	if (xmlHttpOptions.readyState==4 || xmlHttpOptions.readyState=="complete") {
		document.getElementById('options_'+postID).innerHTML = xmlHttpOptions.responseText;
	}
}

function hideOptions (vpostID) {
	document.getElementById('show_options_'+postID).innerHTML = '· <a href="javascript:showOptions(\''+vpostID+'\');">show options</a>';
	document.getElementById('options_'+postID).innerHTML = '';
}


function setPostType(postType) {
	xmlHttpPostType=GetXmlHttpObject();
	if (xmlHttpPostType==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/new_bluurt/post_type.php?post_type="+postType;
	xmlHttpPostType.onreadystatechange=stateChangedPostType
	xmlHttpPostType.open("POST",url,true);
	xmlHttpPostType.send(null);
}

function stateChangedPostType() { 
	if (xmlHttpPostType.readyState==4 || xmlHttpPostType.readyState=="complete") {
		document.getElementById('new_post_area').innerHTML = xmlHttpPostType.responseText;
	}
}


function editSettings(vsettingName,action) {
	settingName = vsettingName
	xmlHttpSettings=GetXmlHttpObject();
	if (xmlHttpSettings==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	if (action == 'change') {
		document.getElementById('settings_option_'+settingName).innerHTML = '<a href="javascript:editSettings(\''+settingName+'\',\'hide\')">hide</a>';
	} else {
		document.getElementById('settings_option_'+settingName).innerHTML = '<a href="javascript:editSettings(\''+settingName+'\',\'change\')">change</a>';
	}
	var url="http://bluurt.com/settings/settings_form.php?setting="+settingName+'&action='+action;
	xmlHttpSettings.onreadystatechange=stateChangedSettings
	xmlHttpSettings.open("POST",url,true);
	xmlHttpSettings.send(null);
}
function stateChangedSettings() { 
	if (xmlHttpSettings.readyState==4 || xmlHttpSettings.readyState=="complete") {
		document.getElementById('settings_'+settingName).innerHTML = xmlHttpSettings.responseText;
	}
}

function hideSettings(vsettingName) {
	settingName = vsettingName
	document.getElementById('settings_option_'+settingName).innerHTML = '<a href="javascript:editSettings(\''+settingName+'\')">change</a>';
}

function showFullImage (picName,nextPic) {
	if (nextPic !== '') {
		document.getElementById('pic_display').innerHTML = '<a href="'+nextPic+'"><img src="http://bluurt.com/attachments/'+picName+'"></a>';
	} else {
		document.getElementById('pic_display').innerHTML = '<img src="http://bluurt.com/attachments/'+picName+'">';
	}
	document.getElementById('magnify').innerHTML = '';
}

function getLink (url) {
	xmlHttpLink=GetXmlHttpObject();
	if (xmlHttpLink==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/new_bluurt/curl.php?url="+url;
	xmlHttpLink.onreadystatechange=stateChangedLink
	xmlHttpLink.open("POST",url,true);
	xmlHttpLink.send(null);
}
function stateChangedLink() { 
	if (xmlHttpLink.readyState==4 || xmlHttpLink.readyState=="complete") {
		document.getElementById('link_results').innerHTML = xmlHttpLink.responseText;
	}
}


function updateCaption(attachmentID,caption) {
	xmlHttpUpdateCaption=GetXmlHttpObject();
	if (xmlHttpUpdateCaption==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	caption = nl2br(caption);
	var url="http://bluurt.com/view/view_pic/edit_caption.php?attachment_id="+attachmentID+'&caption='+caption+'&action=update';
	xmlHttpUpdateCaption.onreadystatechange=stateChangedUpdateCaption
	xmlHttpUpdateCaption.open("POST",url,true);
	xmlHttpUpdateCaption.send(null);
}
function stateChangedUpdateCaption() { 
	if (xmlHttpUpdateCaption.readyState==4 || xmlHttpUpdateCaption.readyState=="complete") {
		document.getElementById("caption").innerHTML=xmlHttpUpdateCaption.responseText;
	}
}

function editCaption(attachmentID) {
	xmlHttpEditCaption=GetXmlHttpObject();
	if (xmlHttpEditCaption==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://bluurt.com/view/view_pic/edit_caption.php?attachment_id="+attachmentID+'&action=edit';
	xmlHttpEditCaption.onreadystatechange=stateChangedEditCaption
	xmlHttpEditCaption.open("POST",url,true);
	xmlHttpEditCaption.send(null);
}
function stateChangedEditCaption() { 
	if (xmlHttpEditCaption.readyState==4 || xmlHttpEditCaption.readyState=="complete") {
		document.getElementById("caption").innerHTML=xmlHttpEditCaption.responseText;
	}
}
