/**
 *
 * Based off the form generator for aweber this is to allow it to be easier to
 * update and manipulate.
 *
 * @author Keith Baker <keithtbaker@gmail.com>
 * @copyright Copyright (C) 2008, Keith Baker
 * @license MIT
 *
*/

var formGen = new Class({
	Implements: [Options],

	/**
	 * defaultOptions
	 *
	 * formContainer: null // this is to be passed in as $('containerID') of the div to insert your form
	 *
	 * The following variables get passed in through your initialization
	 * based on the information from the HTML code aweber provides you
	 * 	aweberId = "111111",
	 *	aweberUnit = "unit_name",
	 *	redirect = "http://somesite.com/thankyou"
	 *
	 * Change these based on your image
	 *	opacity: 		.5, 		// Set to .5 to have textboxes have background of image. Otherwise 1 
	 *	bgColor: 		'#fff', // Textfield background color in hex
	 *	textColor:	'#000', // Textfield text color
	 *	capColor: 	'#fff', // Caption text color
	 *	font: 			'arial',// Text font
	 *	textSize: 	11, 		// Font size, in pixels
	 *
	 * Aweber field names
	 * Email - 	"from"
	 * Name = 	"name"
	 *
	 * fd1Name: 'name',							// Field name for the first textfield
	 * fd1Cap: 	'Name:', 						// Caption for the first textfield
	 * fd2Name: 'from', 						// Field name for the second textfield
	 * fd2Cap: 	'Email:', 					// Caption for the second textfield
	 * imgSrc: 	'img/2sellers.jpg', // URL of the image to be used for the form
	 *
	 * showCaps: false // Set to false if the captions are in the image, true if they are not
	 *
	 * yPos: 215, 	// Textfield position, from the top of the image, in pixels
	 * xPos: 212, 	// Textfield position, from the left of the image, in pixels
	 * width: 200,	// Textfield width, in pixels
	 */
	defaultOptions: {
		formContainer: null,
		formH: 391 + 'px',
		formW: 432 + 'px',
		aweberId: null,
		aweberUnit: null,
		redirect: null,
		redirectId: null,
		opacity: .5,
		bgColor: '#fff',
		textColor: '#000',
		capColor: '#fff',
		font: 'arial',
		textSize: 11,
		fd1Name: 'name',
		fd1Cap: 'Name:',
		fd2Name: 'from',
		fd2Cap: 'Email:',
		imgSrc: null,
		showCaps: false,
		yPos: 215,
		xPos: 212,
		submitId: 'aweberSubmit',
		width: 200
	},

	/**
	 * initialize
	 * Initialize an instance of the Aweber Form
	 */
	initialize: function(options){
		// Set runtime options
		this.setOptions($merge(this.defaultOptions, options));

		var form = new Element('form',{
			'action': 'http://www.aweber.com/scripts/addlead.pl',
			'method': 'post',
			'name': 'aform',
			'styles':{
				'background': 'url(' + this.options.imgSrc + ') no-repeat 0 0',
				'height':	this.options.formH,
				'left': 0,
				'position': 'absolute',
				'top': 0,
				'width': this.options.formW
			}
		}).inject(this.options.formContainer);

/*		var formImage = new Element('img',{
			'src': this.options.imgSrc,
			'styles':{
				'left': 0,
				'position': 'absolute',
				'top': 0
			}
		}).inject(this.options.formContainer, 'top');*/

		var aweberForm = '<input type="hidden" name="meta_web_form_id" value="' + this.options.aweberId + '">';
		aweberForm += '<input type="hidden" name="meta_split_id" value="">';
		aweberForm += '<input type="hidden" name="unit" value="' + this.options.aweberUnit + '">';
		aweberForm += '<input type="hidden" name="redirect" value="' + this.options.redirect + '" id="' + this.options.redirectId + '">';
		aweberForm += '<input type="hidden" name="meta_redirect_onlist" value="">';
		aweberForm += '<input type="hidden" name="meta_adtracking" value="">';
		aweberForm += '<input type="hidden" name="meta_message" value="1">';
		aweberForm += '<input type="hidden" name="meta_required" value="from">';
		aweberForm += '<input type="hidden" name="meta_forward_vars" value="0">';
		if(this.options.showCaps){
			aweberForm += '<button type="submit" style="text-indent:-9999px;position: relative; top: ' + (this.options.yPos - 50)+'px !important!; top: ' + this.options.yPos + 'px; left: ' + this.options.xPos + 'px; color: #' + this.options.capColor + '; font-family: ' + this.options.font + '; font-weight: bold; font-size: ' + this.options.textSize + 'px; height: 20px; z-index: 8;">' + this.options.fd1Cap + '/>';
		} else {
			this.options.yPos -= 62;
		}
		aweberForm += '<input type="text" name="' + this.options.fd1Name+'" value="" style="position: relative; top: ' + (this.options.yPos - 5) + 'px; left: ' + this.options.xPos + 'px; height: 40px; width: ' + this.options.width + 'px; opacity: ' + this.options.opacity + '; filter: alpha(opacity=' + (this.options.opacity * 100) + '); background-color: #' + this.options.bgColor + '; color: #' + this.options.textColor + '; font: ' + this.options.font + '; font-size: 30px; border-width: 0px; z-index: 9;" />';
		if(this.options.showCaps){
			aweberForm += '<button type="submit" style="text-indent:-9999px;position: relative; top: ' + (this.options.yPos + 20) +'px !important!; top: ' + (this.options.yPos + 19) + 'px; left: ' + this.options.xPos + 'px; color: #' + this.options.capColor + '; font-family: ' + this.options.font + '; font-weight: bold; font-size: ' + this.options.textSize + 'pt; height: 20px; z-index: 8;">' + this.options.fd2Cap + '/>';
		} else {
			this.options.yPos += 62;
			this.options.xPos -= 200;
		}
		aweberForm += '<input type="text" name="' + this.options.fd2Name + '" value="" style="position: relative; top: ' + (this.options.yPos + 14) + 'px; left: ' + this.options.xPos + 'px; height: 40px; width: ' + this.options.width + 'px; opacity: ' + this.options.opacity + '; filter: alpha(opacity=' + (this.options.opacity * 100) + '); background-color: ' + this.options.bgColor + '; color: ' + this.options.textColor + '; font: ' + this.options.font + '; font-size: 30px; border-width: 0px; z-index: 9;" />';
		aweberForm += '<button type="submit" style="text-indent:-9999px;position: absolute; width: 320px; height: 46px; cursor: pointer;" id="' + this.options.submitId + '" onClick="document.aform.submit();"/>';

		this.options.formContainer.set({
				'styles':{
					'background-image': 'url(' + this.options.imgSrc + ')',
					'height':	this.options.formH,
					'position': 'relative',
					'width': this.options.formW
				}
		});
		form.set('html', aweberForm);

		/*yPos = this.options.formContainer.getCoordinates().top + 288;
		xPos = this.options.formContainer.getCoordinates().left + 90;*/
		yPos = 288;
		xPos = 90;

		$(this.options.submitId).setStyles({
			'top': yPos + 'px',
			'left': xPos + 'px'
		});
	}
});

