/**
 * WickedJS
 * 
 * A simple collection of utility methods for day-to-day
 * web dev.  There are many great libs out there--Prototype,
 * jQuery, etc.--but I found myself needing such tiny subsets of
 * their functionality that I made my own, much smaller, lib.
 * 
 * Feel free to use any and all of this code.  It's hardly original.
 */

// 1 = parse for media tags (see the parseMediaTags method)
// 0 = do not parse
var WICKED_PARSE_MEDIA	= 0;


var Wicked = {
	version: 0.3
};



/**
 * Allow string-based or object-based parameters
 * 
 * @param {Object} element
 */
Wicked.getElement = function( element )
{
	if ( typeof element == 'string' )
		element	= document.getElementById( element );
	return element;
}


/**
 * Get the true top/left position of any element
 * on the page.
 * 
 * @param {Object} element	A DOM element object or element ID
 */
Wicked.getElementPosition = function( element )
{
	element = Wicked.getElement( element );

	var eTop	= 0;
	var eLeft	= 0;
	do {
		eTop	+= element.offsetTop || 0;
		eLeft	+= element.offsetLeft || 0;
		element	= element.offsetParent;
	} while ( element );
	var pos	= {
		top: eTop,
		left: eLeft
	};
	return pos;
};

/**
 * Set an element's absolute position
 * 
 * @param {Object} element	A DOM element object or element ID
 * @param {int} top		pass null to not affect the target's top value
 * @param {int} left	pass null to not affect the target's left value
 */
Wicked.setElementPosition = function( element, top, left )
{
	element = Wicked.getElement( element );
	if ( left != null )
		element.style.left	= left + 'px';
	if ( top != null )
		element.style.top	= top + 'px';
}

/**
 * Get the height and width of an element
 * 
 * @param {Object} element	A DOM element object or element ID
 */
Wicked.getElementDimensions = function( element )
{
	element	= Wicked.getElement( element );

	var dim	= {
		height: element.offsetHeight,
		width: element.offsetWidth
	};
	return dim;
}

/**
 * Return the HTML needed to display a YouTube video
 * 
 * @param {Object} videoId	The YouTube video ID
 * @param {Object} width
 * @param {Object} height
 * @param {Object} autoplay	1 = autoplay, 0 = no autoplay
 */
Wicked.getYouTubeVideo = function( videoId, width, height, autoplay )
{
	var html =	'<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
				'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ' +
				'width="' + width + '" height="' + height + '" id="YoutubeVideo">' + "\n" +
				'<param name="allowScriptAccess" value="sameDomain" /> ' +
				'<param name="wmode" value="transparent" />' +
				'<param name="allowFullScreen" value="false" /> ' +
				'<param name="movie" value="http://www.youtube.com/v/' + videoId + '&hl=en&fs=1&autoplay=' + autoplay + '" /> ' +
				'<param name="quality" value="high" /> ' +
				'<param name="bgcolor" value="#313339" /> ' + "\n" +
				'<embed src="http://www.youtube.com/v/' + videoId + '&hl=en&fs=1&autoplay=' + autoplay + '" quality="high" wmode="transparent" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="YoutubeVideo" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> ' +
				'</object></div>';
	return html;
};

/**
 * Determines whether or not the passed argument
 * has the passed class name
 * 
 * @param {Object} obj	A DOM object element or element ID
 * @param {Object} name
 */
Wicked.hasClassName = function( element, name )
{
	element = Wicked.getElement( element );
	if ( element.className )
	{
		var classList = element.className.split(' ');
		for ( var c = 0; c < classList.length; c++ )
		{
			if ( classList[c] == name )
				return true;
		}
	}
	return false;
}

/**
 * Parses special YouTube and SWF tags, to allow easy addition
 * of YouTube and Flash/Flex files in pages via a CMS and
 * still have the pages validate.
 * 
 * Currently this parses P tags that use the following formats:
 * 
 * For YouTube:  <p class="youtube">videoId:width:height:autoplay</p>
 *     example:  <p class="youtube">4pXfHLUlZf4:480:295:0</p>
 * 
 * For a SWF: <p class="swf">/path/to/file.swf:width:height</p>
 *   example: <p class="swf">/swfs/MyAnimation.swf:600:400</p>
 */
Wicked.parseMediaTags = function()
{
	var tags	= document.getElementsByTagName('p');
	for ( var t = 0; t < tags.length; t++ )
	{
		var tag	= tags[t];
		if ( Wicked.hasClassName( tag, 'swf' ) )
		{
			var args	= tag.innerHTML.split(':');
			var path	= args[0];
			var width	= args[1];
			var height	= args[2];

			var html	=	'<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
							'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ' +
							'width="' + width + '" height="' + height + '" id="YoutubeVideo">' + "\n" +
							'<param name="allowScriptAccess" value="sameDomain" /> ' +
							'<param name="wmode" value="transparent" />' +
							'<param name="allowFullScreen" value="false" /> ' +
							'<param name="movie" value="' + path + '" /> ' +
							'<param name="quality" value="high" /> ' +
							'<param name="bgcolor" value="#313339" /> ' + "\n" +
							'<embed src="' + path + '" quality="high" wmode="transparent" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="Flash File" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> ' +
							'</object></div>';
			tag.style.display	= 'block';
			tag.innerHTML		= html;
		} else if ( Wicked.hasClassName( tag, 'youtube' ) ) {
			var args	= tag.innerHTML.split(':');
			var id		= args[0];
			var width	= args[1];
			var height	= args[2];
			var auto	= args[3];
			if ( !auto )
				auto = '0';

			var html	= Wicked.getYouTubeVideo( id, width, height, auto );
			tag.style.display	= 'block';
			tag.innerHTML		= html;
		}
	}
}

var _originalOnLoad	= window.onload;
window.onload = function() {
	if ( WICKED_PARSE_MEDIA == 1)
		Wicked.parseMediaTags();
	if ( _originalOnLoad )
		_originalOnLoad(arguments);
};

