var logWin;

/**
 * trim a whitespace off the ends of a string or convert the string to null if it is all whitespace
 * @param value:String
 * @return the trimmed string or null
 */
function trimToNull(value){
	if(isNull(value))return value;
	var rv = value.replace(/^\s+|\s+$/g, '');
	if(rv == '')return null;
	return rv;

}

/**
 * safe logging that will not bomb if console is not defined
 * @param msg:String the message to log
 */
function log(msg) {
	try {

		if($.browser.msie){

			if(isNull(logWin)){
				logWin = window.open("", "_blank", "width=450, height=300, toolbar=no, titlebar=no, status=no, menubar=no, scrollbars=yes");
				logWin.document.body.innerText = "";
			}

			//logWin.document.body.innerHtml += msg + "<br>";
			logWin.document.write(msg + "<br>");

		}else{
			console.log(msg);
		}

	} catch (error) {
	}
}


/**
 * create a proxy of the function that sets the function's "this" scope to the specified object
 * @param scope:Object the object that should be referred to by "this"
 * @return Function the proxied function
 */
Function.prototype.createDelegate = function(scope) {
    var fn = this;
    return function() {
        // Forward to the original function using 'scope' as 'this'.
        return fn.apply(scope, arguments);
    }
};

/**
 *
 */
function addProperties (from, to){

	if(typeof (from) === 'undefined' || from == null || typeof (to) === 'undefined' || to == null)return;

	for(var prop in from){
		if(typeof from[prop] != "function")to[prop] = from[prop];
	}

};

/**
 *
 */
function isNull(value) {
	try{
		if(value==undefined)return true;
		var rv = typeof (value) === 'undefined' || value == null;
		return rv;
	}catch(e){
		return true;
	}
}

/**
 *
 */
function getKeyCode(e) {
	return e?e.which:event.keyCode;
};

/**
 *
 */
function newElement(type, props){

	var e = document.createElement(type);

	if(!isNull(props)){
		if(!isNull(props.attributes)){
			for(prop in props.attributes){
				if(prop == "text"){
					e.appendChild(document.createTextNode(props.attributes[prop]));
				}else if(prop == "clazz"){
					e.className = props.attributes[prop];
				}else{
					e.setAttribute(prop, props.attributes[prop]);
				}
			}
		}

		if(!isNull(props.handlers)){
			for(handler in props.handlers){
				e[handler] = props.handlers[handler];
			}
		}
	}

	return e;

}

/**
 *
 */
function setStyles(node, styles){
	for(var key in styles){
		node.style[key] = styles[key];
	}
}

/**
 *
 */
jQuery.fn.replaceClass = function(toReplace,replaceWith){

	return $(this).each(function(){

		return $(this).removeClass(toReplace).addClass(replaceWith);

	});

};

/**
 *
 * @param str
 * @return
 */
Boolean.parse = function (str) {
	  switch (str.toLowerCase ()) {
	    case "true":
	      return true;
	    default:
	      return false;
	  }
	};


/**
 *
 * @param selector
 * @param msg
 * @param cb
 * @return
 */
function showMessageDialog (selector, msg, cb)
{
  $(selector + " span").html(msg);

  $(selector).dialog(
    {
      bgiframe : true,
      modal : true,
      position: ['center','top'],
      buttons :{
        OK : function () {
    	  if(!isNull(cb))cb.call();
          $(this).dialog('close');
          $(this).dialog('destroy');
        }
      }
    }
  );
}

/**
 *
 * @return
 */
function viewSource(){
	var win = window.open();
	win.document.body.innerText = document.documentElement.innerHTML;
}

/**
 *
 **/
function getTarget(event){

	var target;

	//log("event = " + event);

	if (!isNull(event.target)) target = event.target;
	else if (event.srcElement) target = event.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
	target = target.parentNode;
	return target;
}

/**
 *
 * @return
 */
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
