//===========================================
//          Global functions
//===========================================

/**
 * Returns true if the namespace is defined, false otherwise
 * 
 * @param name	 the namespace to check for
 * @param object the parent namespace
 * @returns true if the namespace is defined, false otherwise
 */
function co_isDef (name, object) {
    return (typeof (object || co_global)[name] != "undefined");
}

//===========================================
//          Bootstrap CitrixOnline (cw)
//===========================================

//define global namespace
var co_global = this;

//define "CitrixOnline" namespace
if (!co_isDef("CitrixOnline")) {
    var CitrixOnline = {};
}

//save a reference to global
CitrixOnline.global = function () {return co_global;}


//===========================================
//          Core
//===========================================

//define namespace
if (!co_isDef("Core", CitrixOnline)) {
    CitrixOnline.Core = {};
}

CitrixOnline.Core.isIE = navigator.appName.indexOf("Microsoft") != -1;

CitrixOnline.Core.include = function(url) {
    var js = CitrixOnline.Core._getEmptyScript();
    js.setAttribute('src', url);
    CitrixOnline.Core._doInclude(js);
    return js;
}

CitrixOnline.Core.removeInclude = function(script) {
    var htmlDoc = document.getElementsByTagName('head').item(0);
    script.removeAttribute('src');
    htmlDoc.removeChild(script);
    if (CitrixOnline.Core._scriptPlaceReference == script)
        CitrixOnline.Core._scriptPlaceReference = null;
}

CitrixOnline.Core.addUrlParam = function (url, name, value) {
  // TODO: Make smarter
  var q_pos = url.indexOf('?');
  return url + (q_pos != -1 ? "&" : "?") + name + "=" + value;
}

// ---- Core privates ---
CitrixOnline.Core._scriptPlaceReference = null;

CitrixOnline.Core._getEmptyScript = function() {
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    return js;
}

CitrixOnline.Core._doInclude = function(script) {
    var htmlDoc = document.getElementsByTagName('head').item(0);
    htmlDoc.insertBefore(script, CitrixOnline.Core._scriptPlaceReference);
    if (CitrixOnline.Core._scriptPlaceReference == null)
        CitrixOnline.Core._scriptPlaceReference = script;
}
// Bootstrapping includes
CitrixOnline.Core._doInclude(CitrixOnline.Core._getEmptyScript());



//===========================================
//          Event
//===========================================

if (!co_isDef("Event", CitrixOnline)) {
    CitrixOnline.Event = {};
}

CitrixOnline.Event.addEvent = function (obj, eventName, callback, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(eventName, callback, useCapture);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + eventName, callback);
	} else {
		obj['on'+eventName] = callback;
	}
}

// Not good name as also cancel propagation
CitrixOnline.Event.killDefault = function (event) {
	event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
	//if (_isIE) parent.frames.location.replace('javascript: parent.falseframe');
}
      