/* Form Position Script, by A.Greenway 2009 allows for forms to be positioned automatically within a post using the form builder plugin. 
To use: When createing posts, to use the script, you must have an elemnt with an ID such as formBuilderCSSIDTemplate_1_Form_A
Replace the 1 with 1 - 5 and A with A-Z. 
This allows for 26x10=260 different form templates to be positioned automatically.
NOTE: You must also have the class set up in the style sheet for accurate results.
The form will be inserted, floating to the right hand side of an element with ID "form_cell   */

var form_placement_helper = {
    page_div: "#form_cell", //the element where the form will be inserted into. 
	form_heading_div: "#form_description", //table cell where the form heading and description will sit
	form_content_table: "#content_table_with_form", //table which the form will sit at the top of
    form_div: "",
	template_body: "#template_body",
	set_form_div: function () {
		alphabet = new Array();
		alphabet[0] = 'A';
		alphabet[1] = 'B';
		alphabet[2] = 'C';
		alphabet[3] = 'D';
		alphabet[4] = 'E';
		alphabet[5] = 'F';
		alphabet[6] = 'G';
		alphabet[7] = 'H';
		alphabet[8] = 'I';
		alphabet[9] = 'J';
		alphabet[10] = 'K';
		alphabet[11] = 'L';
		alphabet[12] = 'M';
		alphabet[13] = 'N';
		alphabet[14] = 'O';
		alphabet[15] = 'P';
		alphabet[16] = 'Q';
		alphabet[17] = 'R';
		alphabet[18] = 'S';
		alphabet[19] = 'T';
		alphabet[20] = 'U';
		alphabet[21] = 'V';
		alphabet[22] = 'W';
		alphabet[23] = 'X';
		alphabet[24] = 'Y';
		alphabet[25] = 'Z';
		
		for(var template = 0; template <= 10; template++){
			for(var form = 0; form <= 25; form++){
				var div = '#formBuilderCSSIDTemplate_' + template + '_Form_' + alphabet[form];
				if($(div).width() > 1){
					this.form_div = div;
					return true;
				}
			}
		}
		return false;
    },
	position_form: function(left, top){
		$(this.form_div).css('top',top);
		$(this.form_div).css('left',left);
	},
	fix_template_height: function(){
		var move_up = $(this.form_div).height();
		move_up = '-' + String((move_up)) + 'px';
		var new_template_height = $(this.template_body).height() - $(this.form_div).height();
		$(this.template_body).css('height', String(new_template_height) + 'px');
		//$('#footer').css('top', move_up);
	},
	get_top_section_height: function(){
		return $(this.form_content_table).height();
	},
	get_form_height: function(){
		return $(this.form_div).height();
	},
	get_form_width: function(){
		return $(this.form_div).width();
	},
	insert_spacer: function(width, height){
		if($('#form_cell')) {
			var form_spacer = '<div id = "form_spacer_div" style="height: ' + height + '; width: ' + width + ';">&nbsp;</div>';
			$('#form_cell').prepend($(form_spacer)); 
		} 
	}
	
}

$(document).ready(function(){
	if(form_placement_helper.set_form_div()){
		var form_height = String((form_placement_helper.get_form_height() + 0));
		var form_width = String((form_placement_helper.get_form_width() + 0));
		form_placement_helper.insert_spacer(form_width + 'px', form_height + 'px');
		var top = '-' + String((form_placement_helper.get_top_section_height() - $(form_placement_helper.form_heading_div).height() - 20)) + 'px';
		form_placement_helper.position_form(((364+(form_width/2)) - form_width + 0), top);
		form_placement_helper.fix_template_height();
		$(form_placement_helper.page_div).css('background-color', '#FFF');
	}
});