// NON-PROTOTYPE FUNCTIONs
function doInfoBoxToggle(id) {
	eval(id+".toggle()");
}

function doDelayedInfoBoxHide(id, time) {
	setTimeout(id+".hide()", time);
}




// CLASS DEFINITION
function infoBox(id) {

	// set the properties
	this.id = id;
	this.timeoutid = null;		// used to start or cancel a delayed hide

	// build some real content
	$("body").append("<div id=\"" + id + "\" class=\"infoBox\"></div>");

	// add this guy to the array
	if (aInfoBox) aInfoBox[aInfoBox.length] = this;

	// center it initially
	this.center();
}


// CLASS METHODS
function infoBoxAlertId() {
	alert(this.id);
}

function infoBoxClearTimeout() {
	clearTimeout(this.timeoutid);
}

function infoBoxAddClass(c) {
	$("#"+this.id).addClass(c);
}

function infoBoxRemoveClass(c) {
	$("#"+this.id).removeClass(c);
}

function infoBoxContent(s) {
	$("#"+this.id).html(s);
}

function infoBoxToggle() {
	this.cleartimeout();
	$("#"+this.id).is(":hidden") ? $("#"+this.id).show() : $("#"+this.id).hide();
	this.center();
}

function infoBoxHide() {
	this.cleartimeout();
	$("#"+this.id).hide();
}

function infoBoxShow() {
	this.cleartimeout();
	$("#"+this.id).show();
	this.center();
}

function infoBoxTempShow(delay) {
	this.cleartimeout();
	$("#"+this.id).show();
	this.center();
	var self = this;		// need another copy of "this", since by the time the timeout runs, "this" will means omething else
	this.timeoutid = setTimeout(function(){self.hide();}, delay);
}

function infoBoxDoAll(content, remove, add, delay) {
	if (remove!="") this.removeclass(remove);
	if (add!="") this.addclass(add);
	this.content(content);
	this.tempshow(delay);
}

function infoBoxCenter() {
	var page_width = (window.innerWidth ? window.innerWidth : (document.body.clientWidth ? document.body.clientWidth : $(document).width()));
	var page_height = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : (document.body.clientHeight ? document.body.clientHeight : $(document).height())));
	
	var scrollpos = document.body.scrollTop;
	if (scrollpos == 0) {
		if (window.pageYOffset) {
			scrollpos = window.pageYOffset;
		} else {
			scrollpos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
	
	var newleft = (page_width/2) - ($("#"+this.id).width()/2);
	var newtop = (page_height/2) - ($("#"+this.id).height()/2) + scrollpos;
	
	$("#"+this.id).css("left", newleft);
	$("#"+this.id).css("top", newtop);
}





/*************************************
Here is content specifically for the content-flagging stuff
*************************************/
function showFlagContent(objtypeid, objid, propername) {
	$("#flag_propernamediv").html("Flag " + propername + " as inappropriate:");
	$("#flag_objtypeid").val(objtypeid);
	$("#flag_objid").val(objid);
	refreshcaptcha("_flag", false);
	oBoxFlag.show();
	$("#flag_comments").focus();
}


function flagcontent() {
	var captcha = $("#captcha_flag").val();
	if (captcha.length!=6) {
		alert("Please enter the complete security code.\n");
	} else {
		if (!bSavingFlag) {
			bSavingFlag = true;
			$("#btnSubmitFlag").attr("disabled", "disabled");
			$("#btnCancelFlag").attr("disabled", "disabled");
			iFlag = setTimeout("flagcontenttimedout()", SAVE_TIMEOUT*1000);
			$.post(ajaxpath, {
					method:				"flagContent"
					, objecttypeid:		$("#flag_objtypeid").val()
					, objectid:			$("#flag_objid").val()
					, flagactiontypeid:	$("#flag_actiontype").val()
					, comments:			$("#flag_comments").val()
					, seed:				$("#seed_flag").val()
					, captcha:			$("#captcha_flag").val()
					, _ct:				$("#_ct_flag").val()
				}, function(response) {
					try {
						clearTimeout(iFlag);
						var json = eval("("+response+")");
						if (json.RESULTS_ARRAY.length) {
							alert(json.RESULTS_ARRAY.join("\n"));
							refreshcaptcha("_flag", true);		// do this so they can't keep trying at the same captcha
						} else {
							$("#flag_comments").val("");
							oBoxFlag.hide();
							// hide the link so they can't report again (when they refresh, the link will already be gone)
							$("#flagContentImageLink_"+json.OBJECTTYPEID+"_"+json.OBJECTID).hide();
						}
					}
					catch (e) {
						// if we can't get comments, fail
					}
					bSavingFlag = false;
					$("#btnSubmitFlag").removeAttr("disabled");
					$("#btnCancelFlag").removeAttr("disabled");
				});
		}
	}
}


function flagcontenttimedout() {
	clearTimeout(iFlag);
	alert(SAVE_TIMEOUT + " seconds expired without a response from the server.  Please try again.");
	bSavingFlag = false;
	$("#btnSubmitFlag").removeAttr("disabled");
	$("#btnCancelFlag").removeAttr("disabled");
}






var iFlag = 0;
var bSavingFlag = false;
$(document).ready(function(){

	// set an array for all infoboxes
	aInfoBox = new Array();

	// bind all the methods for the class
	infoBox.prototype.alertid = infoBoxAlertId;
	infoBox.prototype.cleartimeout = infoBoxClearTimeout;
	infoBox.prototype.addclass = infoBoxAddClass;
	infoBox.prototype.removeclass = infoBoxRemoveClass;
	infoBox.prototype.content = infoBoxContent;
	infoBox.prototype.toggle = infoBoxToggle;
	infoBox.prototype.hide = infoBoxHide;
	infoBox.prototype.show = infoBoxShow;
	infoBox.prototype.tempshow = infoBoxTempShow;
	infoBox.prototype.center = infoBoxCenter;
	infoBox.prototype.doall = infoBoxDoAll;


	// make it so when the window scrolls or resizes, this thing centers all info boxes
	$(window).resize(function() {
		for (var i=0; i<aInfoBox.length; i++) {
			aInfoBox[i].center();
		}
	}).scroll(function() {
		for (var i=0; i<aInfoBox.length; i++) {
			aInfoBox[i].center();
		}
	});

});
