function get_current_div_id(use_this) {
	if($(use_this).attr('selectbox_lookup') != undefined) {
		return 'help_popup_select_'+$(use_this).attr("cat_id");
	} else if($(use_this).attr("mid") != undefined) {
		return 'help_popup_main_'+$(use_this).attr("cat_id");
	} else {
		return 'help_popup_'+$(use_this).attr("cat_id");
	}
}

function get_current_div(use_this) {
	//alert(get_current_div_id($(use_this)));
	return $('#'+get_current_div_id($(use_this)));
}

function build_help_popups() {
	$('.help_popup').each(function(i) {
		use_div_id = get_current_div_id($(this));
		$(this).append("<div id='"+use_div_id+"' info_loaded='0' class='helpWindow'><img src='images/icons/loader.gif'></div>");
	});

	$('.help_popup').hover(
		function() {
			current_div = get_current_div($(this));
			// over
			
				// load the information for this help popup
				
				use_cid = 0;
				if($(this).attr("selectbox_lookup") != undefined) {
					// we need to do a lookup for the ID to get since this is a selectbox
					// and can change when the users changes the option selected
					use_cid = document.getElementById('id_option_help_subid_'+$(this).attr("cat_id")).value;
					//alert(use_cid);
				}
				
				if($(this).attr("help_id") != undefined) {
					use_data = "help_id="+$(this).attr("help_id");
				} else if($(this).attr("mid") != undefined) {
					if(use_cid == 0) use_cid = $(this).attr("mid");
					use_data = "mid="+use_cid;
				} else {
					if(use_cid == 0) use_cid = $(this).attr("cat_id");
					use_data = "id="+use_cid;
				}
				
				if($(this).attr("internal") != undefined) {
					use_data += "&internal=1";
				}
				
				
				if($(this).attr("last_used_cid") != use_cid) {
				
					$(current_div).html("<img src='images/icons/loader.gif'>");
					
					// update the last used CID so we don't reload if it's the same
					$(this).attr("last_used_cid",use_cid);
					
					
	
					 $.ajax({
					   type: "POST",
					   url: "ajax.php?cmd=get_help_info",
					   data: use_data,
					   dataType: "html",
					   success: function(data){
							$(current_div).html(data);
							$(current_div).attr("info_loaded","1");
					   }
					 });
				}
			
			$(current_div).show();
			
		},
		function() {
			// out
			current_div = get_current_div($(this));
			$(current_div).hide();
		}
	);
}

$(document).ready(function() {

	build_help_popups();

});
