jQuery(document).ready(function($){

// CHECKBOX toggle; <input class="chxtoggle" id="chx_formid" type="checkbox" name="dummy" value="">; toggles form with id formid
$("input.chxtoggle").click(function(event){
//event.preventDefault();
var form_id = $(this).attr("id");
form_id = form_id.replace("chx_","");
if ($(this).attr("checked") != false){
$("#" + form_id + " input:checkbox").attr("checked", true);
}
else {
$("#" + form_id + " input:checkbox").attr("checked", false);
}
	});

// BASIC DIV TOGGLE: toggle div with any clickable element (<a> or other) with id = "sometext1" ; The int creates unique ids;  toggles all divs with id containing "sometext"; the alternate clickable elements may be contained within <div>'s with id like "sometext_x"
$(".div_basictoggle").click(function(event){
event.preventDefault();
var div_id_text = $(this).attr("id");
div_id_text = div_id_text.replace(/[0-9]/gi,"");
// match links if within <div> containing "sometext"
$("div[id*=" + div_id_text + "]").has(".div_basictoggle").toggle();
//match div
$("div[id=" + div_id_text + "]").slideToggle();
	});

// CONTROL DIV POPUPS for HOVER; works with any clickable element (<a> or other)
$(".divcontroller").click(function(event){
event.preventDefault();
}) .hover(function () {
var div_id = $(this).attr("id");
// find div id with same value as <a>, only with "d_" prefix
div_id = div_id.replace("a_","d_");
// START if position not yet set to "absolute", figure out positioning
var cssposit = $("#" + div_id).css("position");
if (cssposit != "absolute")
	{
// if an element contains corresponding id "iframe_..." then add the iframe and style it
var isrc = $("#" + div_id + " #iframe_" + div_id).attr("title");
// <td> element that will contain iframe
$("#" + div_id + " #iframe_" + div_id).html("<iframe frameborder='0' scrolling='auto' src='"+ isrc +"'></iframe>");
$("#" + div_id + " iframe").css({ height: $("#" + div_id + " #iframe_" + div_id).css("height"), width: $("#" + div_id + " #iframe_" + div_id).css("width") });
$("#" + div_id + " #iframe_" + div_id).removeAttr("style");
// if an element contains corresponding id "ajaxd_..." then get the ajax content and add it
var jsrc = $("#" + div_id + " #ajaxd_" + div_id).attr("title");
// <td> element that will contain content
$("#" + div_id + " #ajaxd_" + div_id).load(jsrc);

// get css left value, if set
var cssleft = $("#" + div_id).css("left");
cssleft = parseInt(cssleft);
// if css left not set, use the div width to set position
if (isNaN(cssleft))
	{
	var divw = $("#" + div_id).css("width");
	if (typeof divw != "undefined")
		{
		divw = parseInt(divw);
		cssleft = (divw - 50) * -1;
		}
		else
		{
		cssleft = -300;
		}
	}
// get css top value, if set
var csstop = $("#" + div_id).css("top");
csstop = parseInt(csstop);
if (isNaN(csstop))
	{
	csstop = 15;
	}
//START position the div
var posleft = cssleft;
var postop = csstop;
var obj = this;
while (obj.offsetParent) 
	{
	posleft += obj.offsetLeft;
	postop += obj.offsetTop;
	obj = obj.offsetParent;
	}
// replace 0 with an integer to offset the vertical position 
postop += this.offsetHeight + 0;
if (document.body.currentStyle && document.body.currentStyle['marginTop']) 
	{
	postop += parseInt(document.body.currentStyle['marginTop']);
	}
postop = postop + "px";
posleft = posleft + "px";
$("#" + div_id).css({ top: postop, left: posleft, position: "absolute" });
//END position the div
	}
// END if position not yet set to "absolute", figure out positioning

if ($("#" + div_id).hasClass("slide"))
	{
	$("#" + div_id).slideDown();
	}
// slideDown doesn't always work
var disp = $("#" + div_id).css("display");
if (disp == "none")
	{
	$("#" + div_id).show();
	}
// 2nd part of hover
    }, function () {
var div_id = $(this).attr("id");
div_id = div_id.replace("a_","d_");
$("#" + div_id).hide();
	});

// CONTROL DIV POPUPS for CLICK; works with any clickable element (<a> or other)
$(".divcontroller_click").click(function(event){
event.preventDefault();
}) .toggle(function () {
var div_id = $(this).attr("id");
// find div id with same value as <a>, only with "d_" prefix
div_id = div_id.replace("a_","d_");
// START if position not yet set to "absolute", figure out positioning
var cssposit = $("#" + div_id).css("position");
if (cssposit != "absolute")
	{
// if an element contains corresponding id "iframe_..." then add the iframe and style it
var isrc = $("#" + div_id + " #iframe_" + div_id).attr("title");
// <td> element that will contain iframe
$("#" + div_id + " #iframe_" + div_id).html("<iframe frameborder='0' scrolling='auto' src='"+ isrc +"'></iframe>");
$("#" + div_id + " iframe").css({ height: $("#" + div_id + " #iframe_" + div_id).css("height"), width: $("#" + div_id + " #iframe_" + div_id).css("width") });
$("#" + div_id + " #iframe_" + div_id).removeAttr("style");
// if an element contains corresponding id "ajaxd_..." then get the ajax content and add it
var jsrc = $("#" + div_id + " #ajaxd_" + div_id).attr("title");
// <td> element that will contain content
$("#" + div_id + " #ajaxd_" + div_id).load(jsrc);

// get css left value, if set
var cssleft = $("#" + div_id).css("left");
cssleft = parseInt(cssleft);
// if css left not set, use the div width to set position
if (isNaN(cssleft))
	{
	var divw = $("#" + div_id).css("width");
	if (typeof divw != "undefined")
		{
		divw = parseInt(divw);
		cssleft = (divw - 50) * -1;
		}
		else
		{
		cssleft = -300;
		}
	}
// get css top value, if set
var csstop = $("#" + div_id).css("top");
csstop = parseInt(csstop);
if (isNaN(csstop))
	{
	csstop = 15;
	}
//START position the div
var posleft = cssleft;
var postop = csstop;
var obj = this;
while (obj.offsetParent) 
	{
	posleft += obj.offsetLeft;
	postop += obj.offsetTop;
	obj = obj.offsetParent;
	}
// replace 0 with an integer to offset the vertical position 
postop += this.offsetHeight + 0;
if (document.body.currentStyle && document.body.currentStyle['marginTop']) 
	{
	postop += parseInt(document.body.currentStyle['marginTop']);
	}
postop = postop + "px";
posleft = posleft + "px";
$("#" + div_id).css({ top: postop, left: posleft, position: "absolute" });
//END position the div
	}
// END if position not yet set to "absolute", figure out positioning

if ($("#" + div_id).hasClass("slide"))
	{
	$("#" + div_id).slideDown();
	}
// slideDown doesn't always work
var disp = $("#" + div_id).css("display");
if (disp == "none")
	{
	$("#" + div_id).show();
	}
// 2nd part of toggle
    }, function () {
var div_id = $(this).attr("id");
div_id = div_id.replace("a_","d_");
if ($("#" + div_id).hasClass("slide"))
	{
	$("#" + div_id).slideUp();
	}
else
	{
	$("#" + div_id).hide();
	}
	});

$(".divcontroller_closer").click(function(event){
event.preventDefault();
var atitle = $(this).attr("title");
atitle = atitle.replace("d_","a_");
$("a[id=" + atitle + "]").trigger('click');
	});

});