/*
 *  Onoko JavaScript Utilities
 */
function findObject(target, key, value) {
	for(var i = 0; i < target.length; i++){
		var row = target[i];
		if(row[key] == value) {
			return row;
		}
	}
}

function findObjectSmart(target, keyvaluepairs) {
	for(var i = 0; i < target.length; i++){
		var row = target[i];
		var found = true;
		for (var key in keyvaluepairs) {
			var value = keyvaluepairs[key];
			if(row[key] != value) {
				found = false;
				break;
			}
		}
		if (found) {
			return row;
		}
	}
}

function dumpObject(object, depth, max) {
	depth = depth || 0;
	max = max || 2;
	
	if (depth > max) {
		return false;
	}
	
	var indent = "";
	for (var i = 0; i < depth; i++) {
		indent += "  ";
	}
	
	var output = "";  
	for (var key in object) {
		output += "\n" + indent + key + ": ";
		switch (typeof object[key]){
			case "object": output += dumpObject(object[key], depth + 1, max); break;
			case "function": output += "function"; break;
			default: output += object[key]; break;
		}
	}
	return output;
}

function cloneObject(obj) {
	if(obj == null || typeof(obj) != 'object') {
		return obj;
	}
	var temp = new obj.constructor(); // changed (twice)
	for(var key in obj) {
		temp[key] = cloneObject(obj[key]);
	}
	return temp;
}

function blank(item) {
	return item == null || item == '';
}

function equalNotNul(item, new_item) {
	return item != null && item != new_item;
}

function equalNotNullArray(item, new_item, id_string) {
	if(isArray(id_string)) {
		for (var i in id_string) {
			if (!equalNotNullArray(item,new_item,id_string[i])) {
				return false;
			}
		}
		return true;
	} else if (id_string) {
		return item[id_string] && new_item[id_string] && item[id_string] == new_item[id_string];
	}
}

function countObject(obj) {
	var count = 0;
	if (obj) {
		for (var i in obj) {
			if (obj.hasOwnProperty(i)) {
				++count;
			}
		}
	}
	return count;
}

function inArray(value, array, insensitive, first) {
	var duplicate = array.slice(0);
	if(insensitive) { for(var i in duplicate){ duplicate[i] = duplicate[i].toLowerCase(); } }
	for(var i in duplicate) {
		if(first) {
			if(duplicate[i].indexOf(value) == 0) {
				return i;
			}
		} else {
			if(duplicate[i] == value) {
				return i;
			}
		}
	}
	return false;
}

function magnitude(x) {
	return (x<0?-x:x);
}

function isNumber(n) {
	return !isNaN(parseFloat(n));
}

function isString(s) {
	return typeof(s)=='string';
}

function isArray(a) {
	return !isString(a) && a.length > 0
}

function TimeZoneInfo() {
	var now = new Date();
	var year = now.getFullYear();
	var june = new Date(Date.UTC(year, 6, 30, 0, 0, 0, 0));
	var dec = new Date(Date.UTC(year, 12, 30, 0, 0, 0, 0));
	//Use < ecause getTimezoneOffset is returns negative, eg. UTC+1 = -60
	//Could equally be called isNorthanHemisphere
	this.forwardInSummer = june.getTimezoneOffset() < dec.getTimezoneOffset();
	this.currentOffset = now.getTimezoneOffset();
	if (this.forwardInSummer) {
		this.isDaylightSavings = june.getTimezoneOffset() == this.currentOffset;
		this.normalOffset = dec.getTimezoneOffset();
		this.DSTOffset = june.getTimezoneOffset();
	} else {
		this.isDaylightSavings = dec.getTimezoneOffset() == this.currentOffset;
		this.normalOffset = june.getTimezoneOffset();
		this.DSTOffset = dec.getTimezoneOffset();
	}
}

//Get default timezone from time zone info
function getDefaultTimezone() {
	//PHP map: http://codingforums.com/showthread.php?t=139888
}

//Get default locale from combination of IP address (and timezone???)
function getDefaultLocale() {
	//PHP http://php.net/manual/en/function.setlocale.php
}

function trim(s) {
	return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function random(max) {
	return Math.floor(Math.random()*(max+1));
}

function randomarray(arr) {
	return arr[random(arr.length-1)];
}

function htmlentities(s) {
	return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

function autohyperlink(s, target) {
	var hlink = /(\s|^)(ht|f)tps?:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/gi;
	return (s.replace(hlink, function ($0,$1,$2) {
			s = ($0.match(/^\s/) ? $0.substring(1,$0.length) : $0);
			// remove trailing dots, if any
			while (s.length>0 && s.charAt(s.length-1)=='.')
				s=s.substring(0,s.length-1);
				// add hlink
				return ' ' + '<a href="'+s+'"'+(target?' target="'+target+'"':'')+'>'+s+'</a>';
			}
		) 
	);
}

function htmllinebreaks(s) {
	return s.replace(/\r\n|\n|\r/g,'<br />');
}

function htmldisplay(s) {
	return autohyperlink(htmllinebreaks(htmlentities(s)),'_blank');
}

function isMobile() {
	//Keep in order of market share for efficiency
	var agents = ['android','blackberry','windows phone os 7','webos'];
	var ua = navigator.userAgent.toLowerCase();
	if (ua.match(/like Mac OS X/i)) {
		return true;
	} else {
		for (var i in agents) {
			if (ua.indexOf(agents[i]) > -1) {
				return true;
			}
		}
		return false;
	}
}

function lowerCaseSort(a,b) {
	var al=a.toLowerCase(),bl=b.toLowerCase();
	return al==bl?(a==b?0:a<b?-1:1):al<bl?-1:1;
}
