// attach window.onload (non-destructive stack)
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

function swapNode(a, b) {
	var nextSibling = a.nextSibling;
	var parentNode = a.parentNode;
	b.parentNode.replaceChild(a, b);
	a.parentNode.insertBefore(b, nextSibling);  	
}

function getBrowserWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	} else if (document.body) {
		return document.body.clientWidth;
	}
	return 0;
}

function setStylesheet(styleTitle) {
	var currTag;
	if (document.getElementsByTagName) {
		for (var i = 0; (currTag = document.getElementsByTagName('link')[i]); i++) {
			if (currTag.getAttribute('rel').indexOf('style') != -1 && currTag.getAttribute('title')) {
				currTag.disabled = true;
				if(currTag.getAttribute('title') == styleTitle) {
					currTag.disabled = false;
				}
			}
		}
	}	
	return true;
}

function browserCheck() {
	var ua = navigator.userAgent;
	var b = navigator.appName;
	if (b=="Netscape") this.b = "ns";
	else if (b=="Microsoft Internet Explorer") this.b = "ie";
	else this.b = b;		
	if(this.opera = (ua.indexOf('Opera')>0)) { this.b = 'opera'; }	
	this.v = parseInt(navigator.appVersion);
	
	if(ua.indexOf('Win')>0) {
		this.platform = 'win';
	} else if(ua.indexOf('Mac')>0) {
		this.platform = 'mac';
	} else {
		this.platform = 'unknown';
	}
	
	this.ns = (this.b=="ns" && this.v>=4);
	this.ns4 = (this.b=="ns" && this.v==4);
	this.ns5 = (this.b=="ns" && this.v==5);
	this.ie = (this.b=="ie" && this.v>=4);
	this.ie4 = (ua.indexOf('MSIE 4')>0);
	this.ie5 = (ua.indexOf('MSIE 5')>0);
	this.ie6 = (ua.indexOf('MSIE 6')>0);
	if (this.ie5) this.v = 5;
	if (this.ie6) this.v = 6;
	this.min = (this.ns || this.ie);
}

String.prototype.reduce = function(l, p) {
	var words = this.split(' ');
	var output = new Array();
	var outputLeft = 0;
	for(var i = 0; i < words.length; i++) {
		var word = words[i];
		if((outputLeft + word.length) < l) {
			output.push(word);
			outputLeft += (word.length+1);
		} else {
			break;
		}
	}
	return output.join(' ')+p;
}

// sortByProperty()
// Sort the array order based on a specific property of each array element,
// in forward or reverse order.
function _sortByProperty(property,rev) {
	var fn = function(a,b) {
		if (a[property] < b[property]) {
			return (rev)? 1:-1;
		} else if (a[property] > b[property]) {
			return (rev)? -1:1;
		} else {
			return 0;
		}
	}
	this.sort(fn);
}

Array.prototype.sortByProperty = _sortByProperty;

// Cookie handling functions
function getExpiryDate(nodays){
	var today = new Date();
	var nomilli = Date.parse(today);
	today.setTime(nomilli+nodays*24*60*60*1000);
	return today.toUTCString();
}

function setCookie(name, value, duration){
	document.cookie = name+"="+escape(value)+";expires="+getExpiryDate(duration);
}

function getCookie(cookiename) {
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return ""; 
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length; 
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function delCookie(cookiename) {
	setCookie(name, 0, -1);
}

// events using: http://www.scottandrew.com/weblog/articles/cbs-events

function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be attached");
	}
}

function removeEvent(obj, evType, fn, useCapture){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be removed");
	}
}

// getParams
function getQueryArgs(global) {
	var args = {};
	var loc = window.location.href;
	var q = loc.indexOf("?");
	if (q==-1) return false;
	loc = loc.substring(q+1);
	var pairs = loc.split("&");
	for (var i=0; i<pairs.length;i++){
		if (global) eval(pairs[i]);
			var keyval = pairs[i].split("=");
		args[keyval[0]] = unescape(keyval[1]);
	}
	return args;
}

function openWin(uri, name, width, height) {
	var n=window.open(uri, name, 
		'width='+width+',height='+height
		+',left='+(screen.width -width )/2
		+',top=' +(screen.height-height)/2
		+',status=0,menubar=0,toolbar=0,scrollbars=1,resizable=1'
	);
	n.focus();
}
