/**
 * library to create easy html code for usage of
 * the great scriptaculous and prototype api
 * 
 * @author	Stefan Seifarth <info@polycoder.de>
 * @version	1.0
 * @created	2006-12-02
 * @modified 2006-12-26
 * @requires prototype.js and scriptaculous api
 */


/**
 * initialze vars for prototype.js usage
 */ 
var HTMLTag = Class.create();
HTMLTag.Attribute = Class.create();

/**
 * set attributes as object
 *
 * @param	string		attribute name
 * @param	string 		attribute value
 * @see		HTMLTag
 */
HTMLTag.Attribute.prototype = {
	initialize: function ( sAttrName, sAttrValue ) {
		this.name = sAttrName;
		this.value = sAttrValue;
	}
}




/**
 * html tag object generate a valid html tag with given
 * attributes and content
 *
 * @param	string		tag name
 * @param	mixed		content: 'string' content, object: new HTML Tag Object, arra
 * @param	mixed		boolean false: if no attributes, array with object fieldAttribute for short tag generation
 * @param	boolean		contentless tag? true/false
 *
 * @example usage
 * var myLabel = new HTMLTag ('label', {
 *							  content: 'my description for field', 
 *							  attributes: new HTMLTag.Attribute ( 'for', 'fromFieldId')
 * 							  }
 * );
 * alert( myLabel.getHTML() );
 *
 * this example return following string:
 * <label for="fromFieldId">my description for field</label>
 *
 *
 * example for nested tags
 * var myLabel = new HTMLTag ('label', {
 * 							  content: new HTMLTag ('strong', { content: 'my description' } ),
 *							  attributes: [
 *							  	 new HTMLTag.Attribute ('for', 'formFieldId'),
 *								 new HTMLTag.Attribute ('id', 'myLabelTag')
 *							  ]
 *							  }
 * );
 *
 * returns:
 * <label for="formFieldId" id="myLabelTag"><strong>my description</strong></label>
 */
HTMLTag.prototype = {
	// class constructor - prototype.js
	initialize: function ( sTagName, oArguments ) {
		this.sTagName = sTagName;
		this.mOptions = oArguments || {};
		this.bContentLess = this.mOptions.contentless || false;
		this.aAttributes = new Array();
		this.mContent = this.mOptions.content || false;
		
		// check if predefined attributes are given
		// and store it to array
		if ( typeof(this.mOptions.attributes) == 'object' && this.mOptions.attributes.length > 0 ) {
			for ( i = 0; i < this.mOptions.attributes.length; i++ ) {
				this.aAttributes.push(this.mOptions.attributes[i]);
			}
		} else if (typeof(this.mOptions.attributes) == 'object' && !this.mOptions.attributes.length) {
			this.aAttributes.push(this.mOptions.attributes);
		}
	},
	
	// set attribute to object
	setAttribute: function ( sAttrName, sAttrValue ) {
		this.aAttributes.push ( new HTMLTag.Attribute ( sAttrName, sAttrValue) );
	},
	
	
	// build html tag string
	getHTML: function () {
		var sReturn = '';
		var sContent = this.mContent;
		var sAttr = '';
		var aAttr = new Array();
		
		// get attributes
		for ( i = 0; i < this.aAttributes.length; i++ ) {
			aAttr.push( this.aAttributes[i].name + '="' + this.aAttributes[i].value + '"' );
		}
		
		if ( aAttr.length > 0 ) {
			sAttr = ' ' + aAttr.join(' ');
		}
	
		// check if content is an other html tag object
		if ( typeof(this.mContent) == 'object' ) {
			sContent = this.mContent.getHTML();
		}
	
		// build html tag string
		if (this.bContentLess == true) {
			sReturn = '<' + this.sTagName + sAttr + ' />';
		} else {
			sReturn = '<' + this.sTagName + sAttr + '>' + sContent + '</' + this.sTagName + '>';
		}
		
		return sReturn;
	}
	
}


/**
 * collection of simple tag elements
 * for simple creating of html or xml tags
 * on the fly
 *
 * @see 		HTMLTag.prototype
 * @example	
 * 
 * alert (
 *		Tag.img ('images/myimage.jpg', { alt:'my alt text', id:'12345' })
 * );
 *
 * returns:
 * <img alt="my alt text" id="12345" src="images/myimage.jpg" />
 */
var Tag = {
	// collection of simple tag elements

	// set given attributes
	setAttributes: function ( oArgs, aForbiddenArgs ) {
		// set clean attributes
		var aAttributes = new Array();
		// search for given arguments
		for (var sArg in oArgs) {
			// check if attribute is in forbidden list
			// if not, add attribute to list
			if ( aForbiddenArgs.indexOf(sArg) == -1 ) {
				aAttributes.push(new HTMLTag.Attribute (sArg, oArgs[sArg]));
			}
		}
		return aAttributes;
	},
	
	// img tag
	img: function ( sSrc, oArguments ) {
		// check attributes
		aAttributes = Tag.setAttributes(oArguments, ['src', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('src', sSrc) );

		var oTag = new HTMLTag ( 'img', { attributes: aAttributes, contentless: true } );
		return oTag.getHTML();
	},
	
	// a tag with href attribute
	link: function ( sHref, sContent, oArguments ) {
		// check attributes
		aAttributes = Tag.setAttributes(oArguments, ['href', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('href', sHref) );
		
		var oTag = new HTMLTag ( 'a', { attributes: aAttributes, content: sContent } );
		return oTag.getHTML();
	},
	
	// a tag without href but name attribute
	anchor: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Tag.setAttributes(oArguments, ['name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );
		
		var oTag = new HTMLTag ( 'a', { attributes: aAttributes, content: sContent, contentless:true } );
		return oTag.getHTML();
	}
}


/**
 * collection of simple form elements
 * for simple creating of form-field-tags in html or xml
 * on the fly
 *
 * @see 		HTMLTag.prototype
 * @example	
 * 
 * alert (
 *		Field.text ('myTextfield', { value:'my Value', id:'12345' })
 * );
 *
 * returns:
 * <input type="text" id="12345" value="my Value" name="myTextfield" />
 */
var Field = {
	// collection of form field elements

	// set given attributes
	setAttributes: function ( oArgs, aForbiddenArgs ) {
		// set clean attributes
		var aAttributes = new Array();
		// search for given arguments
		for (var sArg in oArgs) {
			// check if attribute is in forbidden list
			// if not, add attribute to list
			if ( aForbiddenArgs.indexOf(sArg) == -1 ) {
				aAttributes.push(new HTMLTag.Attribute (sArg, oArgs[sArg]));
			}
		}
		return aAttributes;
	},
	
	// password field
	label: function ( sId, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['for', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('for', sId) );

		var oTag = new HTMLTag ( 'input', { attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	// password field
	password: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'password') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	
	// hidden field
	hidden: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'hidden') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	
	// text field
	text: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'text') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	
	// textarea field
	textarea: function ( sName, sValue, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless', 'value']);
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );
		
		var oTag = new HTMLTag ( 'textarea', { content:sValue, contentless:false, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	
	// radio field
	radio: function ( sName, sValue, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless', 'value']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'radio') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );
		aAttributes.push( new HTMLTag.Attribute ('value', sValue) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	// checkbox field
	checkbox: function ( sName, sValue, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless', 'value']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'checkbox') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );
		aAttributes.push( new HTMLTag.Attribute ('value', sValue) );
		
		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	// submit field
	submit: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'submit') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	// reset field
	reset: function ( sName, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'reset') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	},
	
	// image field
	image: function ( sName, sSource, oArguments ) {
		// check attributes
		aAttributes = Field.setAttributes(oArguments, ['type', 'name', 'contentless', 'src']);
		aAttributes.push( new HTMLTag.Attribute ('type', 'image') );
		aAttributes.push( new HTMLTag.Attribute ('name', sName) );
		aAttributes.push( new HTMLTag.Attribute ('src', sSource) );

		var oTag = new HTMLTag ( 'input', { contentless:true, attributes: aAttributes } );
		return oTag.getHTML();
	}
}
