/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			ep.fn.sprite
 */

define("ep/fn/sprite", [
	"jquery",
	"ep",

	"jquery/fn/class"
], function ($, ep) {

	var spriteExprMap = {
			'ico_image':		/^(image\/png|image\/jpeg|image\/jpg|image\/gif|image\/bmp|image\/vnd.microsoft.icon|image\/tiff|image\/svg+xml|image\/x-win-bitmap)$/i,
			'ico_box':			/^(application\/octet-stream|text\/x-actionscript|audio\/basic|audio\/x-wav|audio\/mpeg|audio\/x-ms-wma|audio\/ogg|audio\/flac|application\/msword|application\/x-dot|application\/vnd.ms-excel|application\/vnd.ms-powerpoint|text\/plain|text\/css|application\/javascript|application\/json|application\/xml)$/i,
			'ico_file_avi':		/^(video\/x-msvideo|video\/mpeg|video\/mp4)$/i,
			'ico_file_wmv':		/^(video\/x-ms-wmv)$/i,
			'ico_file_swf':		/^(video\/x-flv|application\/x-shockwave-flash)$/i,
			'ico_file_mov':		/^(video\/quicktime|video\/ogg)$/i,
			'ico_file_rm':		/^(application\/vnd.rn-realmedia)$/i,
			'ico_file_ram':		/^(audio\/x-pn-realaudio)$/i,
			'ico_file_dcr':		/^(application\/x-director)$/i,
			'ico_file_pdf':		/^(application\/pdf)$/i,
			'ico_file_htm':		/^(text\/html)$/i,
			'ico_folder':		/^(application\/zip|application\/x-rar-compressed|application\/gzip|application\/x-tar)$/i
		},

		removeExpr = /(ep-sprite[\w\d-]*|[^\s]*ico_[^\s]*)/;

	ep.fn.extend({
		/**
		 * Add sprite icon classes to each of the set of matched elements.
		 * 
		 * The `.addSprite()` method adds classes, which set size an an background image (icon),
		 * to each of the set of matched elements.
		 * 
		 * ### Examples
		 * Create classes for arrow top-bottom icon in size of 16px to matched element.
		 * 
		 * JavaScript:
		 * 
		 *     ep('#icon').addSprite( 'arrow-tb', 's' );
		 * 
		 * 
		 * @param {String} name A sprite icon name.
		 * @param {String} [size] Size of the icon (xs, s, l, m, l, xl, 64, 128), Default: m.
		 * 
		 * @method addSprite
		 * @member jQuery
		 * 
		 * @since 6.11.0
		 */
		addSprite: function( name, size ){

			// get name by mimetype
			if(/\//.test(name)){
				var mime = name;
				$.each( spriteExprMap, function( i, expr ){
					if( expr.test(mime) ){
						mime = i;
						return false;
					}
				});
				name = mime===name ? 'unknow' : mime;
			}

			size = size || 'm';

			return this.each(function(){
				$(this)
					.removeClass(removeExpr)
					.addClass( 'ep-sprite ep-sprite-'+size + ( (/[^\s]*ico_[^\s]*/).test(name) ? ' '+name : ' ep-sprite-'+name ) );
			});
		},
		/**
		 * Remove sprite icon classes from each of the set of matched elements.
		 * 
		 * The `.removeSprite()` method removes classes, which were set by the `.addSprite()` method,
		 * from each of the set of matched elements.
		 * 
		 * ### Examples
		 * Remove sprite icon classes from matched element.
		 * 
		 * JavaScript:
		 * 
		 *     ep('#icon').removeSprite();
		 * 
		 * 
		 * @method removeSprite
		 * @member jQuery
		 * 
		 * @since 6.11.0
		 */
		removeSprite: function(){
			return this.removeClass(removeExpr);
		},
		/**
		 * Determine whether the first of the matched elements are assigned spite icon classes.
		 * 
		 * The `.hasSprite()` method determine whether the first of the matched elements are assigned
		 * the given sprite icon or spite icon classes in generell.
		 * 
		 * ### Examples
		 * Determine whether the matched element has the arrow top-bottom icon.
		 * 
		 * JavaScript:
		 * 
		 *     ep('#icon').hasSprite('arrow-tb');
		 * 
		 * 
		 * @param {String} name A sprite icon name.
		 * 
		 * @method hasSprite
		 * @member jQuery
		 * 
		 * @since 6.11.0
		 */
		hasSprite: function( name ){
			return this.hasClass( 'ep-sprite' + (name ? '-'+name : '') );
		}
	});

	return ep;

});