﻿var wm = wm || {};
wm.Utils = wm.Utils || {};

//Events
wm.Utils.addEvent = function(pTarget, pEventType, pEventHandler){
	if (pTarget.addEventListener){
			pTarget.addEventListener(pEventType, pEventHandler, false);
	}
	else if (pTarget.attachEvent){
		pTarget.attachEvent("on" + pEventType, pEventHandler);
	}
}

//Prototyping
Array.prototype.wmRemove = function(p){
	for (var i = 0; i < this.length; i++){
		if (p == this[i]){
			this.splice(i, 1);
			break;
		}
	}
}

Array.prototype.wmContains = function(p){
	for (var i = 0; i < this.length; i++){
		if (p == this[i]){
			return true;
		}
	}
	return false;
}

String.prototype.wmTrim = function(){
	return this.replace(/^\s+|\s+$/g, '')
}

//Positioning

//Handy
wm.Utils.getParentByTagName = function(pParentTagName, pChild){
	var parent = pChild.parentNode;
	while(parent.tagName.toLowerCase() != pParentTagName.toLowerCase()){
		parent = parent.parentNode;
	}
	
	return parent;
}

wm.Utils.getElementsByClass = function(pCssClass, pStartNode, pTagName){
	var elements = new Array();
	if (pStartNode == null) { pStartNode = document; }
	if (pTagName == null) { pTagName = '*'; }
	var elms = pStartNode.getElementsByTagName(pTagName);
	var rxPattern = new RegExp("(^|\\\\s)" + pCssClass + "(\\\\s|$)");
	for (i = 0, j = 0; i < elms.length; i++) {
		if (rxPattern.test(elms[i].className)) {
			elements[j] = elms[i];
			j++;
		}
	}
	return elements;
}

wm.Utils.pause = function(msecs){
	var start = new Date().getTime();
	var current = start;
	
	while (current - start < msecs){
		current = new Date().getTime();
	}
}


wm.Utils.QueryString = function(pQueryString){
	//if pQueryString is null then current querystring is used.
	//If passing a pQueryString it does not matter whether it is prefixed with "?".
	
	//If adding a key that already exist, that key will be overwritten with the new value (keys are case insensitive)
	//If removing a key that does not exist, then there will be no change to the QueryString object
	
	//Usage Example 1 : var qs = new QueryString(); qs.add("myQsKey", "myQsValue");  myLink.href = location.href.split("?")[0] + qs.toString(); --this will set the href of myLink to the current url however with myQsKey=myQsValue added to the querystring part
	//Usage Example 2 : var qs = new QueryString(); var myQsValue = qs.params["myQsKey"];
	//Usage Example 3 : var qs = new QueryString(); qs.remove("myQsKey")
	
	var qs = (pQueryString != null) ? pQueryString : document.location.search;
	if (qs.length > 0 && qs.substring(0, 1) == "?"){
		qs = qs.substring(1, qs.length);
	}
	
	this.keys = [];
	this.values = [];
	this.params = {};

	if (qs.length > 0){
		var keyPairs = qs.split('&');
	
		for (var kp = 0; kp < keyPairs.length; kp++){
			var keyPair = keyPairs[kp];
			var key = "";
			var value = "";
			if (keyPair.indexOf("=") == -1){
				key = keyPair.toLowerCase();
				value = "";
			}
			else{
				key = keyPair.split("=")[0].toLowerCase();
				value = keyPair.split("=")[1];
			}
			this.params[key] = value;
			this.keys[kp] = key;
			this.values[kp] = value;
		}
	}
	
	this.indexOfKey = function(pKey){
		for (var k = 0; k < this.keys.length; k++){
			if (this.keys[k] == pKey.toLowerCase()){
				return k;
			}
		}
		
		return -1;
	}
	
	this.contains = function(pKey){
		for (var k = 0; k < this.keys.length; k++){
			if (this.keys[k].toLowerCase() == pKey.toLowerCase()){
				return true;
			}
		}
		
		return false;
	}
	
	this.remove = function(pKey){
		var indexOfKeyToRemove = this.indexOfKey(pKey);
		if (indexOfKeyToRemove > -1){
			this.keys.splice(indexOfKeyToRemove, 1);
			this.values.splice(indexOfKeyToRemove, 1);
		}
	}
	
	this.add = function(pKey, pValue){
		var indexOfKeyToAdd = this.indexOfKey(pKey);
		if (indexOfKeyToAdd == -1){
			this.keys.push(pKey);
			this.values.push(pValue);
		}
		else{
			this.values[indexOfKeyToAdd] = pValue;
		}
		
		this.params[pKey.toLowerCase()] = pValue;
	}
	
	this.toString = function(){
		var qsResult = "";
		for (var k = 0; k < this.keys.length; k++){
			if (k > 0){qsResult += "&";}
			qsResult += this.keys[k] + "=" + escape(this.values[k]);
		}
		
		if (qsResult != ""){qsResult = "?" + qsResult;}
		
		return qsResult;
	}
}


