1 /**
  2  * @fileOverview Core BTM Object & methods
  3  * @author <a href="http://www.bobs-bits.com">Stephen Reay</a>
  4  * @version 1.0.5
  5  */
  6 
  7 
  8 /**
  9  * @namespace Core BTM Namespace Object
 10  * 
 11  * Current Supported browsers: Firefox version 2+, Safari version 3+, Opera version 9.3+, Internet Explorer version 6+
 12  *
 13  */
 14 var BTM ={};
 15 
 16 (function () {
 17 	/**
 18 	 * The BTM Library version number
 19 	 * @type {String}
 20 	 */
 21 	BTM.version = '1.0.5';
 22 
 23 	/**
 24 	 * An object identifying the external Library BTM uses (an object with 'name' and 'version' properties), or false if no supported Library detected
 25 	 * @type {Object|Boolean}
 26 	 */
 27 	BTM.library = false;
 28 
 29 	/**
 30 	 * @namespace The options Object for BTM
 31 	 * @see BTM.loadOption
 32 	 */
 33 	BTM.options = {};	
 34 	
 35 	/**
 36 	 * Load an option into {@link BTM.options} from the query string that referenced the BTM JS file. Boolean or Number values represented as strings will be converted to their native format
 37 	 * @param {String} name the name of the variable to set
 38 	 * @param {Mixed} defaultVal the default value for the option (used if a value can't be found in the query string)
 39 	 * @example
 40 	 * BTM.loadOption('runMappings', true); // Loads the option "runMappings", with a default value of true
 41 	 */
 42 	BTM.loadOption = function loadOption(name, defaultVal) {
 43 		if(Object.isUndefined(defaultVal)) {
 44 			throw new TypeError('You must define a default value');
 45 		}
 46 		
 47 		if (name in opts) {
 48 			var val = opts[name];
 49 			
 50 			//Basic type detection and conversion
 51 			switch(val) {
 52 				case 'true':
 53 					val = true;
 54 					break;
 55 					
 56 				case 'false':
 57 					val = false;
 58 					break;
 59 				
 60 				default:
 61 					if (/^[\d]+[\.]{0,1}[\d]*$/.test(val)) {
 62 						val = parseFloat(val);
 63 					}
 64 					break;
 65 			}
 66 			BTM.options[name] = val;
 67 		}
 68 		else {
 69 			BTM.options[name] = defaultVal;
 70 		}
 71 	};
 72 	
 73 	var scripts = document.getElementsByTagName('script');
 74 	var opts = false;
 75 	for (var i = 0; opts === false && i < scripts.length; i++) {
 76 		if (scripts[i].src && /btm(-packed){0,1}\.js/.test(scripts[i].src)) {
 77 			var url = scripts[i].src.parseURL();
 78 			var opts = url.search.parseQuery();
 79 			
 80 			/**
 81 			 * BTM debug mode flag
 82 			 * @field
 83 			 * @name BTM.options.debug
 84 			 * @type Boolean
 85  			 * @default false
 86 			 */
 87 			BTM.loadOption('debug', false);
 88 			
 89 			/**
 90 			 * BTM JavaScript directory location
 91 			 * @field
 92 			 * @name BTM.options.jsdir
 93 			 * @type String
 94 			 * @default auto-calculated
 95 			 */
 96 			BTM.loadOption('jsdir', scripts[i].src.split(/btm(-packed)?\.js/i)[0]);
 97 			
 98 			/**
 99 			 * BTM Image directory location
100 			 * @field
101 			 * @name BTM.options.imgdir
102 			 * @type String
103 			 * @default BTM.options.jsdir + '../images/'
104 			 */
105 			BTM.loadOption('imgdir', BTM.options.jsdir + '../images/');
106 		}
107 	}
108 	
109 	var libraryProps = [
110 		{
111 			'identifier' : window.Prototype,
112 			'name' : 'Prototype',
113 			'version': function () { return Prototype.Version; }
114 		},
115 		{
116 			'identifier' : window.jQuery,
117 			'name' : 'jQuery',
118 			'version': function () { return jQuery.fn.jquery; }
119 		},
120 		{
121 			'identifier' : window.cssQuery,
122 			'name' : 'cssQuery',
123 			'versionRegExp': /version ([\d.]*)/i,
124 			'versionRegExpIndex' : 1,
125 			'version': function() { return cssQuery.toString(); }
126 		}
127 	];
128 
129 	function detectLibrary(library) {
130 		if (!BTM.library && library.identifier) {
131 			BTM.library = {
132 							'name': library.name
133 						};
134 			if (typeof library.versionRegExp === 'undefined') {
135 				BTM.library.version = library.version();
136 			}
137 			else {
138 				BTM.library.version = library.version().match(library.versionRegExp)[library.versionRegExpIndex];
139 			}
140 			return;
141 		}
142 	}
143 	
144 	libraryProps.forEach(detectLibrary);
145 	
146 	/**
147 	 * Identity function, returns whatever is passed to it
148 	 * @param value value to be returned
149 	 * @returns the original argument passed
150 	 * @see {BTM.selfProp}
151 	 */
152 	BTM.self = function self(value) {
153 		return value;
154 	};
155 
156 	/**
157 	 * Special-Use Identity function, returns the specified member from whatever is passed to it
158 	 * @param value value to get member from
159 	 * @param name the member to return
160 	 * @returns the the specified value from the original argument passed
161 	 * @see {BTM.self}
162 	 */	
163 	BTM.selfProp = function selfProp(value, name) {
164 		return value[name];
165 	};
166 	
167 	/**
168 	 * Write a log message to the console
169 	 * @param mesage the message to write to the console
170 	 */
171 	BTM.log = function log(message, object) {
172 		if (BTM.options.debug && window.console && window.console.log) {
173 			window.console.log(message, object);
174 		}
175 	};
176 	
177 
178 	/**
179 	 * Write an error message to the console
180 	 * @param mesage the message to write to the console
181 	 */	
182 	BTM.error = function error(message, object) {
183 		if (BTM.options.debug && window.console && window.console.error) {
184 			window.console.error(message, object);
185 		}
186 	};
187 })();
188