pi.js 21.2 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
(function(_scope){
2

Ad Schellevis's avatar
Ad Schellevis committed
3 4 5 6 7 8
	/*
	 * pi.js
	 * 1.0
	 * Azer Koçulu <http://azer.kodfabrik.com>
	 * http://pi-js.googlecode.com
	 */
9

Ad Schellevis's avatar
Ad Schellevis committed
10 11 12
	_scope.pi = Object(3.14159265358979323846);
	var pi  = _scope.pi;
	pi.version = 1.0;
13

Ad Schellevis's avatar
Ad Schellevis committed
14 15 16 17 18 19 20 21 22
	pi.env = {
		ie: /MSIE/i.test(navigator.userAgent),
		ie6: /MSIE 6/i.test(navigator.userAgent),
		ie7: /MSIE 7/i.test(navigator.userAgent),
		ie8: /MSIE 8/i.test(navigator.userAgent),
		firefox: /Firefox/i.test(navigator.userAgent),
		opera: /Opera/i.test(navigator.userAgent),
		webkit: /Webkit/i.test(navigator.userAgent)
	};
23

Ad Schellevis's avatar
Ad Schellevis committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	pi.util = {
		IsArray:function(_object){
			return _object && _object != window && ( _object instanceof Array || ( typeof _object.length == "number" && typeof _object.item =="function" ) );
		},
		IsHash:function(_object){
			return _object && typeof _object=="object"&&(_object==window||_object instanceof Object)&&!_object.nodeName&&!pi.util.IsArray(_object);
		},
		DOMContentLoaded:[],
		AddEvent: function(_element,_eventName,_fn,_useCapture){
			_element[pi.env.ie.toggle("attachEvent","addEventListener")](pi.env.ie.toggle("on","")+_eventName,_fn,_useCapture||false);
			return pi.util.AddEvent.curry(this,_element);
		},
		RemoveEvent: function(_element,_eventName,_fn,_useCapture){
			return _element[pi.env.ie.toggle("detachEvent","removeEventListener")](pi.env.ie.toggle("on","")+_eventName,_fn,_useCapture||false);
		},
		GetWindowSize:function(){
			return {
				height:pi.env.ie?Math.max(document.documentElement.clientHeight,document.body.clientHeight):window.innerHeight,
				width:pi.env.ie?Math.max(document.documentElement.clientWidth,document.body.clientWidth):window.innerWidth
			};
		},
		Include:function(_url,_callback){
			var script = new pi.element("script").attribute.set("src",_url), callback = _callback||new Function, done = false, head = pi.get.byTag("head")[0];
			script.environment.getElement().onload = script.environment.getElement().onreadystatechange = function(){
				if(!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")){
					callback.call(this);
					done = true;
					head.removeChild(script.environment.getElement());
				}
			};
			script.insert(head);
		},
		Element:{
			addClass:function(_element,_class){
				if( !pi.util.Element.hasClass(_element,_class) )
					pi.util.Element.setClass(_element, pi.util.Element.getClass(_element) + " " + _class );
			},
			getClass:function(_element){
				return _element.getAttribute(pi.env.ie.toggle("className","class"))||"";
			},
			hasClass:function(_element,_class){
				return pi.util.Element.getClass(_element).split(" ").indexOf(_class)>-1;
			},
			removeClass:function(_element,_class){
				if( pi.util.Element.hasClass(_element,_class) )
					pi.util.Element.setClass(
70
						_element,
Ad Schellevis's avatar
Ad Schellevis committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
						pi.util.Element.getClass(_element,_class).split(" ").removeValue(_class).join(" ")
					);
			},
			setClass:function(_element,_value){
				_element.setAttribute( pi.env.ie.toggle("className","class"), _value );
			},
			toggleClass:function(){
				if(pi.util.Element.hasClass.apply(this,arguments))
					pi.util.Element.removeClass.apply(this,arguments);
				else
					pi.util.Element.addClass.apply(this,arguments);
			},
			getOpacity:function(_styleObject){
				var styleObject = _styleObject;
				if(!pi.env.ie)
					return styleObject["opacity"];
87

Ad Schellevis's avatar
Ad Schellevis committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				var alpha = styleObject["filter"].match(/opacity\=(\d+)/i);
				return alpha?alpha[1]/100:1;
			},
			setOpacity:function(_element,_value){
				if(!pi.env.ie)
					return pi.util.Element.addStyle(_element,{ "opacity":_value });
				_value*=100;
				pi.util.Element.addStyle(_element,{ "filter":"alpha(opacity="+_value+")" });
				return this._parent_;
			},
			getPosition:function(_element){
				var parent = _element,offsetLeft = 0, offsetTop = 0, view = pi.util.Element.getView(_element);
				while(parent&&parent!=document.body&&parent!=document.firstChild){
					offsetLeft +=parseInt(parent.offsetLeft);
					offsetTop += parseInt(parent.offsetTop);
					parent = parent.offsetParent;
				};
				return {
					"bottom":view["bottom"],
					"left":view["left"],
					"marginTop":view["marginTop"],
					"marginLeft":view["marginLeft"],
					"offsetLeft":offsetLeft,
					"offsetTop":offsetTop,
					"position":view["position"],
					"right":view["right"],
					"top":view["top"],
					"z-index":view["zIndex"]
				};
			},
			getSize:function(_element){
				var view = pi.util.Element.getView(_element);
				return {
					"height":view["height"],
					"offsetHeight":_element.offsetHeight,
					"offsetWidth":_element.offsetWidth,
					"width":view["width"]
				};
			},
			addStyle:function(_element,_style){
				for(var key in _style){
					key = key=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):key;
					if (key == "opacity" && pi.env.ie) {
						pi.util.Element.setOpacity(_element,_style[key]);
						continue;
					}
					_element.style[key] = _style[key];
				}
			},
			getStyle:function(_element,_property){
				_property = _property=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):_property;
				if(_property=="opacity"&&pi.env.ie)
					return pi.util.Element.getOpacity(_element.style);
				return typeof _property=="string"?_element.style[_property]:_element.style;
			},
			getView:function(_element,_property){
				var view = document.defaultView?document.defaultView.getComputedStyle(_element,null):_element.currentStyle;
				_property = _property=="float"?pi.env.ie.toggle("styleFloat","cssFloat"):_property;
				if(_property=="opacity"&&pi.env.ie)
					return pi.util.Element.getOpacity(_element,view);
				return typeof _property=="string"?view[_property]:view;
			}
		},
		CloneObject:function(_object,_fn){
			var tmp = {};
			for(var key in _object)
			{
				if( pi.util.IsArray( _object[key] ) ){
					tmp[key] = Array.prototype.clone.apply( _object[key] );
				} else
					if( pi.util.IsHash( _object[key] ) ){
						tmp[ key ] = pi.util.CloneObject(_object[key]);
						if(_fn)_fn.call(tmp,key,_object);
					} else
						tmp[key] = _object[key];
			}
			return tmp;
		},
		MergeObjects:function(_object,_source){
			for(var key in _source){
				var value = _source[key];
				if (pi.util.IsArray(_source[key])) {
					if(pi.util.IsArray( _object[key] )){
						Array.prototype.push.apply( _source[key], _object[key] );
					}
					else
						value = _source[key].clone();
				}
176
				else
Ad Schellevis's avatar
Ad Schellevis committed
177 178 179 180 181 182 183 184 185 186 187 188
					if (pi.util.IsHash(_source[key])) {
						if (pi.util.IsHash(_object[key])) {
							value = pi.util.MergeObjects(_object[key], _source[key]);
						} else {
							value = pi.util.CloneObject( _source[key] );
						}
					}
				_object[key] = value;
			};
			return _object;
		}
	};
189

Ad Schellevis's avatar
Ad Schellevis committed
190 191 192 193 194 195 196
	pi.get = function(){
		return document.getElementById(arguments[0]);
	};
	pi.get.byTag = function(){
		return document.getElementsByTagName(arguments[0]);
	};
	pi.get.byClass = function(){ return document.getElementsByClassName.apply(document,arguments); };
197

Ad Schellevis's avatar
Ad Schellevis committed
198 199 200
	pi.base = function(){
		this.body = {};
		this.constructor = null;
201

Ad Schellevis's avatar
Ad Schellevis committed
202 203 204 205 206 207
		this.build = function(_skipClonning){
			var base = this, skipClonning = _skipClonning||false, _private = {},
				fn = function(){
					var _p = pi.util.CloneObject(_private);
					if(!skipClonning){
						for(var key in this){
208

Ad Schellevis's avatar
Ad Schellevis committed
209 210 211 212
							if(pi.util.IsArray( this[ key ] ) ){
								this[key] = Array.prototype.clone.apply( this[key] );
							} else
								if( pi.util.IsHash(this[key]) ){
213
									this[key] = pi.util.CloneObject(
Ad Schellevis's avatar
Ad Schellevis committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
										this[ key ],
										function(_key,_object){
											this[ _key ]._parent_ = this;
										}
									);
									this[key]._parent_ = this;
								}
						}
					};
					base.createAccessors( _p, this );
					if(base.constructor)
						return base.constructor.apply(this,arguments);
					return this;
				};
			this.movePrivateMembers(this.body,_private);
			if(this.constructor){
				fn["$Constructor"] = this.constructor;
			}
			fn.prototype = this.body;
			return fn;
		};
235

Ad Schellevis's avatar
Ad Schellevis committed
236 237 238
		this.createAccessors = function(_p, _branch){
			var getter = function(_property){ return this[_property]; },
				setter = function(_property,_value){ this[_property] = _value; return _branch._parent_||_branch; };
239

Ad Schellevis's avatar
Ad Schellevis committed
240 241 242 243 244 245
			for (var name in _p) {
				var isPrivate = name.substring(0, 1) == "_", title = name.substring(1, 2).toUpperCase() + name.substring(2);
				if (isPrivate) {
					_branch["get" + title] = getter.curry(_p,name);
					_branch["set" + title] = setter.curry(_p,name);
				}
246
				else
Ad Schellevis's avatar
Ad Schellevis committed
247 248 249 250
					if (pi.util.IsHash(_p[name])){
						if(!_branch[name])
							_branch[name] = {};
						this.createAccessors(_p[name], _branch[name]);
251
					}
Ad Schellevis's avatar
Ad Schellevis committed
252 253
			};
		};
254

Ad Schellevis's avatar
Ad Schellevis committed
255 256 257
		this.movePrivateMembers = function(_object, _branch){
			for (var name in _object) {
				var isPrivate = name.substring(0, 1) == "_";
258

Ad Schellevis's avatar
Ad Schellevis committed
259 260 261 262
				if (isPrivate) {
					_branch[name] = _object[name];
					delete _object[name];
				}
263
				else
Ad Schellevis's avatar
Ad Schellevis committed
264 265 266 267 268 269 270
					if (pi.util.IsHash(_object[name])){
						_branch[name] = {};
						this.movePrivateMembers(_object[name], _branch[name]);
					}
			};
		};
	};
271

Ad Schellevis's avatar
Ad Schellevis committed
272 273 274 275 276 277
	Function.prototype.extend = function(_prototype,_skipClonning){
		var object = new pi.base, superClass = this;
		if(_prototype["$Constructor"]){
			object.constructor = _prototype["$Constructor"];
			delete _prototype["$Constructor"];
		};
278

Ad Schellevis's avatar
Ad Schellevis committed
279 280 281 282 283
		object.body = superClass==pi.base?_prototype:pi.util.MergeObjects(_prototype,superClass.prototype,2);
		object.constructor=object.constructor||function(){
			if(superClass!=pi.base)
				superClass.apply(this,arguments);
		};
284

Ad Schellevis's avatar
Ad Schellevis committed
285 286
		return object.build(_skipClonning);
	};
287

Ad Schellevis's avatar
Ad Schellevis committed
288 289
	Function.prototype.curry = function(_scope){
		var fn = this, scope = _scope||window, args = Array.prototype.slice.call(arguments,1);
290 291
		return function(){
			return fn.apply(scope,args.concat( Array.prototype.slice.call(arguments,0) ));
Ad Schellevis's avatar
Ad Schellevis committed
292 293
		};
	};
294

Ad Schellevis's avatar
Ad Schellevis committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	pi.element = pi.base.extend({
		"$Constructor":function(_tag){
			this.environment.setElement(document.createElement(_tag||"DIV"));
			this.environment.getElement().pi = this;
			return this;
		},
		"clean":function(){
			var childs = this.child.get();
			while(childs.length){
				childs[0].parentNode.removeChild(childs[0]);
			}
		},
		"clone":function(_deep){
			return this.environment.getElement().cloneNode(_deep);
		},
		"insert":function(_element){
			_element = _element.environment?_element.environment.getElement():_element;
			_element.appendChild(this.environment.getElement());
			return this;
		},
		"insertAfter":function(_referenceElement){
			_referenceElement = _referenceElement.environment?_referenceElement.environment.getElement():_referenceElement;
			_referenceElement.nextSibling?this.insertBefore(_referenceElement.nextSibling):this.insert(_referenceElement.parentNode);
			return this;
		},
		"insertBefore":function(_referenceElement){
			_referenceElement = _referenceElement.environment?_referenceElement.environment.getElement():_referenceElement;
			_referenceElement.parentNode.insertBefore(this.environment.getElement(),_referenceElement);
			return this;
		},
		"query":function(_expression,_resultType,namespaceResolver,_result){
			return pi.xpath(_expression,_resultType||"ORDERED_NODE_SNAPSHOT_TYPE",this.environment.getElement(),_namespaceResolver,_result);
		},
		"remove":function(){
			this.environment.getParent().removeChild(
				this.environment.getElement()
			);
		},
		"update":function(_value){
				["textarea","input"].indexOf(this.environment.getName())>-1?
				(this.environment.getElement().value = _value):
				(this.environment.getElement().innerHTML = _value);
				return this;
		},
		"attribute":{
			"getAll":function(_name){
				return this._parent_.environment.getElement().attributes;
			},
			"clear":function(_name){
				this.set(_name,"");
				return this._parent_;
			},
			"get":function(_name){
				return this._parent_.environment.getElement().getAttribute(_name);
			},
			"has":function(_name){
				return pi.env.ie?(this.get(_name)!=null):this._parent_.environment.getElement().hasAttribute(_name);
			},
			"remove":function(_name){
				this._parent_.environment.getElement().removeAttribute(_name);
				return this._parent_;
			},
			"set":function(_name,_value){
				this._parent_.environment.getElement().setAttribute(_name,_value);
				return this._parent_;
			},
			"addClass":function(_classes){
				for (var i = 0; i < arguments.length; i++) {
					pi.util.Element.addClass(this._parent_.environment.getElement(),arguments[i]);
				};
				return this._parent_;
			},
			"clearClass":function(){
				this.setClass("");
				this._parent_;
			},
			"getClass":function(){
				return pi.util.Element.getClass( this._parent_.environment.getElement() );
			},
			"hasClass":function(_class){
				return pi.util.Element.hasClass( this._parent_.environment.getElement(), _class );
			},
			"setClass":function(_value){
				return pi.util.Element.setClass( this._parent_.environment.getElement(), _value );
			},
			"removeClass":function(_class){
				pi.util.Element.removeClass( this._parent_.environment.getElement(), _class );
				return this._parent_;
			},
			"toggleClass":function(_class){
				pi.util.Element.toggleClass( this._parent_.environment.getElement(), _class );
			}
		},
		"child":{
			"get":function(){
				return this._parent_.environment.getElement().childNodes;
			},
			"add":function(_elements){
				for (var i = 0; i < arguments.length; i++) {
					var el = arguments[i];
					this._parent_.environment.getElement().appendChild(
						el.environment ? el.environment.getElement() : el
					);
				}
				return this._parent_;
			},
			"addAfter":function(_element,_referenceElement){
				this.addBefore(
					_element.environment?_element.environment.getElement():_element,
					(_referenceElement.environment?_referenceElement.environment.getElement():_referenceElement).nextSibling
				);
				return this._parent_;
			},
			"addBefore":function(_element,_referenceElement){
				this._parent_.environment.getElement().insertBefore(
					_element.environment?_element.environment.getElement():_element,
					_referenceElement.environment?_referenceElement.environment.getElement():_referenceElement
				);
				return this._parent_;
			},
			"query":function(_tag,_attributeName,_attributeValue){
				return this._parent_.query(
					"{0}{1}".format( (_tag?"{0}".format(_tag):"/*"), _attributeName||_attributeValue?"[contains(concat(' ', @{0}, ' '),' {1} ')]".format(_attributeName||"",_attributeValue||""):"" )
				);
			},
			"remove":function(_element){
				this._parent_.environment.getElement().removeChild(_element.environment?_element.environment.getElement():_element);
			}
		},
		"environment":{
			"_element":null,
			"getParent":function(){
				return this.getElement().parentNode;
			},
			"getPosition":function(){
				return pi.util.Element.getPosition(this.getElement());
			},
			"getSize":function(){
				return pi.util.Element.getSize( this.getElement() );
			},
			"addStyle":function(_styleObject){
				pi.util.Element.addStyle(this.getElement(),_styleObject);
				return this._parent_;
			},
			"getStyle":function(_property){
				return pi.util.Element.getStyle(_property);
			},
			"getName":function(){
				return this.getElement().nodeName;
			},
			"getType":function(){
				return this.getElement().nodeType;
			},
			"getView":function(_property){
				return pi.util.Element.getView(this.getElement(),_property);
			}
		},
		"event":{
			"addListener":function(_event,_fn,_useCapture){
				pi.util.AddEvent(this._parent_.environment.getElement(),_event,_fn,_useCapture);
				return this._parent_;
			},
			"removeListener":function(_event,_fn,_useCapture){
				pi.util.RemoveEvent(this._parent_.environment.getElement(),_event,_fn,_useCapture);
				return this._parent_;
			}
		}
	});
463

Ad Schellevis's avatar
Ad Schellevis committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	pi.xhr = new pi.base;
	pi.xhr.constructor = function(){
		var api;
		if(!window.XMLHttpRequest){
			var names = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
			for (var i = 0; i < names.length; i++) {
				try {
					this.environment.setApi(new ActiveXObject(names[i]));
					break;
				} catch (e) { continue; }
			}
		}
		else
			this.environment.setApi(new XMLHttpRequest());
		this.environment.getApi().onreadystatechange=this.event.readystatechange.curry(this);
		return this;
	};
	pi.xhr.body = {
		"abort":function(){
			this.environment.getApi().abort();
		},
		"send":function(){
486
			var url = this.environment.getUrl(), data = this.environment.getData(),dataUrl = "";
Ad Schellevis's avatar
Ad Schellevis committed
487 488 489

			for (var key in data)
				dataUrl += "{0}={1}&".format(key, data[key]);
490

Ad Schellevis's avatar
Ad Schellevis committed
491 492
			if (this.environment.getType()=="GET")
				url += (url.search("\\?")==-1?"?":"&")+"{0}".format(dataUrl);
493

Ad Schellevis's avatar
Ad Schellevis committed
494
			this.environment.getApi().open(this.environment.getType(),url,this.environment.getAsync());
495

Ad Schellevis's avatar
Ad Schellevis committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
			for(var key in this.environment.getHeader())
				this.environment.getApi().setRequestHeader(key,this.environment.getHeader()[key]);

			this.environment.getApi().send(this.environment.getType()=="GET"?"":dataUrl);
		}
	};
	pi.xhr.body.environment = {
		"_async":true, "_api":null, "_cache":true, "_callback":[], "_channel":null, "_data":{}, "_header":{}, "_mimeType":null, "_multipart":false, "_type":"GET", "_timeout":0, "_url":"",
		"addCallback": function(_options,_fn){
			this.getCallback().push({ "fn":_fn, "options":_options  });
		},
		"addHeader": function(_key,_value){
			this.getHeader()[_key] = _value;
		},
		"addData": function(_key,_value){
			this.getData()[_key] = _value;
		},
		"changeCache":function(_value){
			if(_value==false){
				this.addData("forceCache",Math.round(Math.random()*10000));
			}
			this.setCache(_value);
		},
		"changeType": function(_value){
			if(_value=="POST"){
				this.addHeader("Content-Type","application/x-www-form-urlencoded");
			}
			this.setType(_value);
		}
	};
	pi.xhr.body.event = {
		"readystatechange":function(){
			var readyState = this.environment.getApi().readyState;
			var callback=this.environment.getCallback();

			for (var i = 0; i < callback.length; i++) {
				if(callback[i].options.readyState.indexOf(readyState)>-1)
					 callback[i].fn.apply(this);
			}
		}
	};
	pi.xhr = pi.xhr.build();
538

Ad Schellevis's avatar
Ad Schellevis committed
539 540 541
	/*
	 * xml.xhr.get
	 */
542

Ad Schellevis's avatar
Ad Schellevis committed
543 544 545 546 547 548 549
	pi.xhr.get = function(_url,_returnPiObject){
		var request = new pi.xhr();
		request.environment.setAsync(false);
		request.environment.setUrl(_url);
		request.send();
		return _returnPiObject?request:request.environment.getApi();
	};
550

Ad Schellevis's avatar
Ad Schellevis committed
551 552 553
	/*
	 * pi.xpath
	 */
554

Ad Schellevis's avatar
Ad Schellevis committed
555
	pi.xpath = function(_expression,_resultType,_contextNode,_namespaceResolver,_result){
556
		var contextNode = _contextNode||document,
Ad Schellevis's avatar
Ad Schellevis committed
557
		expression = _expression||"",
558
		namespaceResolver = _namespaceResolver||null,
Ad Schellevis's avatar
Ad Schellevis committed
559 560 561 562
		result=_result||null,
		resultType=_resultType||"ANY_TYPE";
		return document.evaluate(expression, contextNode, namespaceResolver, XPathResult[resultType], result);
	};
563

Ad Schellevis's avatar
Ad Schellevis committed
564 565 566 567 568
	Array.prototype.clone = function(){
		var tmp = [];
		Array.prototype.push.apply(tmp,this);
		tmp.forEach(function(item,index,object){
			if(item instanceof Array)
569
			object[index] = object[index].clone();
Ad Schellevis's avatar
Ad Schellevis committed
570 571 572 573 574 575 576 577 578 579
		});
	    return tmp;
	};
	Array.prototype.count = function(_value){
		var count = 0;
		this.forEach(function(){
			count+=Number(arguments[0]==_value);
		});
		return count;
	};
580

Ad Schellevis's avatar
Ad Schellevis committed
581 582 583 584
	Array.prototype.forEach = Array.prototype.forEach||function(_function){
		for(var i=0; i<this.length; i++)
			_function.apply(this,[this[i],i,this]);
	};
585

Ad Schellevis's avatar
Ad Schellevis committed
586 587 588
	Array.prototype.getLastItem = function(){
		return this[this.length-1];
	};
589

Ad Schellevis's avatar
Ad Schellevis committed
590 591 592 593 594 595 596 597 598
	Array.prototype.indexOf = Array.prototype.indexOf||function(_value){
		var index = -1;
		for(var i=0; i<this.length; i++)
			if(this[i]==_value){
				index = i;
				break;
			}
		return index;
	};
599

Ad Schellevis's avatar
Ad Schellevis committed
600 601 602 603 604
	Array.prototype.remove = function(_index){
		var array = this.slice(0,_index);
		Array.prototype.push.apply(array,this.slice(_index+1));
		return array;
	};
605

Ad Schellevis's avatar
Ad Schellevis committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	Array.prototype.removeValue = function(_value){
		return this.remove(this.indexOf(_value));
	};

	Boolean.prototype.toggle = function(){
		return this==true?arguments[0]:arguments[1];
	};

	Number.prototype.base = function(_system){
		var remain = this%_system;
		if(this==remain)return String.fromCharCode(this+(this>9?87:48));
		return ((this-remain)/_system).base(_system)+String.fromCharCode(remain+(remain>9?87:48));
	};
	Number.prototype.decimal = function(_system){
		var result = 0, digit = String(this).split("");
		for(var i=0; i<digit.length; i++)
		{
			digit[i]=parseInt((digit[i].charCodeAt(0)>58)?digit[i].charCodeAt(0)-87:digit[i]);
			result += digit[i]*(Math.pow(_system,digit.length-1-i));
		}
		return result;
	};
	Number.prototype.range = function(_pattern){
		for(
630 631 632
			var value = String(this), isFloat = /\./i.test(value),
			i = isFloat.toggle(parseInt(value.split(".")[0]),0),
			end = parseInt(value.split(".")[isFloat.toggle(1,0)]),
Ad Schellevis's avatar
Ad Schellevis committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
			array = []; i<end; i++
		){
			array.push(
				Boolean(_pattern)==false?i:(typeof _pattern=="function"?_pattern(i):_pattern[i])
			);
		}
		return array;
	};

	String.prototype.escape = function(){
		return escape(this);
	};

	String.prototype.format = function(){
		var values = arguments;
		return this.replace(/\{(\d)\}/g,function(){
			return values[arguments[1]];
		});
	};
652

Ad Schellevis's avatar
Ad Schellevis committed
653 654 655 656 657 658 659
	String.prototype.leftpad = function(_len,_ch){
		var str=this;
		var ch = Boolean(_ch)==false?" ":_ch;
		while(str.length<_len)
			str=ch+str;
		return str;
	};
660

Ad Schellevis's avatar
Ad Schellevis committed
661 662 663
	String.prototype.toggle = function(_value,_other){
		return this==_value?_value:_other;
	};
664

Ad Schellevis's avatar
Ad Schellevis committed
665 666 667 668 669 670
	String.prototype.unicode = function(){
		var str="", obj = this.split("");
		for(var i=obj.length-1; i>=0; i--)
			str="\\u{0}{1}".format(String(obj[i].charCodeAt(0).base(16)).leftpad(4,"0"),str);
		return str;
	};
671

Ad Schellevis's avatar
Ad Schellevis committed
672 673 674 675 676 677 678 679 680
	pi.util.AddEvent(
		pi.env.ie?window:document,
		pi.env.ie?"load":"DOMContentLoaded",
		function(){
			for(var i=0; i<pi.util.DOMContentLoaded.length; i++){
				pi.util.DOMContentLoaded[ i ]();
			}
		}
	);
681 682

})(window);