var cs_eventManager = {
		addEvent: function(cs_target,cs_event,cs_function,cs_args) {
			if(!this[cs_function]) return this;

			if(typeof(cs_target.addEventListener)=="function") { // mozilla and opera
				cs_target.addEventListener(cs_event,function(event) {cs_eventManager[cs_function](cs_args)},false);
			}
			else if(typeof(cs_target.attachEvent)=="object") { // ie
				if(cs_event == "DOMContentLoaded") { cs_target = document; cs_event = "readystatechange"; }
				cs_target.attachEvent("on"+cs_event,function() {if(document.readyState=="complete")cs_eventManager[cs_function](cs_args)});
			}
			else { // unspecified browser fallback
				if(cs_event == "DOMContentLoaded") { cs_event = "load"; if(cs_target==document) cs_target=window; }
		    var cs_prev = cs_target["on"+cs_event];
	      cs_target["on"+cs_event] = function() {
          if (cs_prev) {
              var dummy = cs_prev();
              var dummy2 = dummy;
          }
          cs_eventManager[cs_function](cs_args);
	      }
	    }
	    return this;
		},
		addFn: function(cs_fn_nm,cs_fn) {
			this[cs_fn_nm] = cs_fn;
	    return this;
		}
}
cs_eventManager.addFn("getBgColor", function(arguments) {
			var b = document.getElementsByTagName('body')[0];
			var bc;
			if(b.currentStyle) { bc = b.currentStyle["backgroundColor"] }
			else if (window.getComputedStyle) { bc = document.defaultView.getComputedStyle(b,null).getPropertyValue("background-color"); }
			if(!bc || bc == "transparent") bc = "rgb(255,255,255)";
			var arr_cs_ids = arguments;
			for(var i = 0; i < arguments.length; i++) {
				var o = document.getElementById(arguments[i]);
				if(o && o.style)
				{
						if(o.style.setProperty) o.style.setProperty("background-color",bc,"");
						else o.style.backgroundColor = bc;
				}
			}

			/* this code copies and moves background image */
			var bi = (cs_getStyle(b,"background-image")) ? cs_getStyle(b,"background-image") : cs_getStyle(b,"background");
			if(bi && bi != "none")
			{
				var tw = document.getElementById("clickstream_top_wrapper");
				var lw = document.getElementById("clickstream_lt_wrapper");

				var bp = cs_getStyle(b,"background-position");
				if(!bp) {
					bp = (lw) ? lw.scrollWidth + "px" : "0px";
					bp += " ";
					bp += (tw) ? tw.scrollHeight + "px" : " 0px";
				}
				else if(bp.match("top") || bp.match("left")) {
					if(tw) bp = bp.replace("top",document.getElementById("clickstream_top_wrapper").scrollHeight+"px");
					if(lw) bp = bp.replace("left",document.getElementById("clickstream_lt_wrapper").scrollWidth+"px");
//					bp = bp.match(/ (.+)$/)[1] + " " + bp.match(/^(.+) /)[1];
				}
				else if(bp.match(/^(\d+)px/) && bp.match(/(\d+)px$/)) {
					if(tw) bp = bp.replace(/^(\d+)px/, parseInt(bp.match(/^(\d+)px/)[1]) + parseInt(document.getElementById("clickstream_top_wrapper").scrollHeight) + "px");
					if(lw) bp = bp.replace(/(\d+)px$/, parseInt(bp.match(/(\d+)px$/)[1]) + parseInt(document.getElementById("clickstream_lt_wrapper").scrollWidth) + "px");
				}
				cs_setStyle(b,"background-position",bp,"");
				cs_setStyle(document.getElementById("clickstream_inner_wrapper"),"background-color","transparent","");
				cs_setStyle(document.getElementById("clickstream_outer_wrapper"),"background-color","transparent","");

			}
		} );
cs_eventManager.addFn("getStyle", function(obj, style)
{
	var value = null;
	if (!obj)
		return value;
	if (window.getComputedStyle) {
		var compStyle = document.defaultView.getComputedStyle(obj,null);
		value = compStyle.getPropertyValue(style);
	}
	else if (obj.currentStyle) {
		value = obj.currentStyle[style];
	}
	if(!value) {
		style = style.match(/\-(\w)/)
				? style.replace(/\-(\w)/,style.match(/\-(\w)/)[1].toUpperCase())
				: style;
		if (obj[style])
			value = obj[style];
		else if (obj.style[style])
			value = obj.style[style];
	}
	return value;
});
cs_eventManager.addFn("setStyle", function(obj, name, value, importance) {
	if (!obj)
		return false;
	if (obj.style) {
		if (obj.style.setProperty)
			obj.style.setProperty(name, value, importance);
		else {
			name = name.match(/\-(\w)/)
				   ? name.replace(/\-(\w)/,
								  name.match(/\-(\w)/)[1].toUpperCase())
				   : name;
			obj.style[name] = value;
		}
	}
	if (obj[name])
		obj[name] = value;
});

cs_eventManager.addFn("adjustStyle", function(id, prop, value)
{
	var obj = document.getElementById(id);
	if (obj)
		obj.style[prop] = value;
});

cs_eventManager.addFn("adjustWidth", function(id)
{
	var obj = document.getElementById(id);
	if (obj) {
		var scrollWidth = document.body.scrollWidth;
		if ("MSIE" == cs_eventManager.getBrowserName())
			{
			obj.style.width = obj.scrollWidth  < document.body.scrollWidth
						  ? scrollWidth + "px" :  document.body.clientWidth + "px";
			}
		else
			{
			obj.style.width = document.getElementById("clickstream_inner_wrapper").clientWidth + "px";
			}
	}
});

cs_eventManager.addFn("adjustHeight", function(id)
{
	var obj = document.getElementById(id);
	if (obj) {
		var scrollHeight = document.body.scrollHeight;
		if ("MSIE" == cs_eventManager.getBrowserName())
			{
			obj.style.height = obj.scrollHeight  < scrollHeight
						  ? scrollHeight + "px" : document.body.clientHeight + "px";
			}
		else
			{
			obj.style.height = document.body.clientHeight < scrollHeight
						  ? scrollHeight + "px" : document.getElementById("clickstream_inner_wrapper").clientHeight + "px";
			}
	}

});

cs_eventManager.addFn("adjustRightPos", function(id)
{
	var obj = document.getElementById(id);
	if (obj) {
		var scrollWidth = document.body.scrollWidth;
		var clientWidth = document.body.clientWidth;
		if ("MSIE" == cs_eventManager.getBrowserName())
			{
			cs_eventManager.adjustStyle("clickstream_inner_wrapper", "width", "auto");
			obj.style.left = obj.offsetLeft  < scrollWidth
						  ? (document.getElementById("clickstream_inner_wrapper").scrollWidth) + "px" : "auto";
			}
		else
			{
			document.getElementById("clickstream_outer_wrapper").style.width = clientWidth - parseInt(obj.style.width) + "px";
		 	obj.style.left = 	document.getElementById("clickstream_outer_wrapper").scrollWidth + "px";
			}

	cs_eventManager.adjustHeight(id);
	}
});

cs_eventManager.addFn("getBrowserName", function() {
	var x;
	
	if (navigator.userAgent.match("MSIE"))
		{
		x = "MSIE";
		}
	else if (navigator.userAgent.match("Firefox"))
		{
		x = "Firefox";
		}
	else if (navigator.userAgent.match("Chrome"))
		{
		x = "Chrome";
		}
	else if (navigator.userAgent.match("Opera"))
		{
		x = "Opera";
		}
	else if (navigator.userAgent.match("Safari"))
		{
		x = "Safari";
		}
	else 
		{
		x =	NULL;
		}

	return x;
});

cs_eventManager.addFn("debug", function(id) {
	alert("Body scrollWidth: " + document.body.scrollWidth
		  + "; clientWidth: " + document.body.clientWidth);
});

function cs_getStyle(obj,style) {
	var value = null;
	if(!obj) return value;
	if(window.getComputedStyle) {
		value = document.defaultView.getComputedStyle(obj,null).getPropertyValue(style);
	}
	else if(obj.currentStyle) {
		value = obj.currentStyle[style];
	}
	if(!value) {
		style = (style.match(/\-(\w)/)) ? style.replace(/\-(\w)/,style.match(/\-(\w)/)[1].toUpperCase()) : style;
		if(obj[style]) value = obj[style];
		else if(obj.style[style]) value = obj.style[style];
	}
	return value;
}
function cs_setStyle(obj,name,value,importance) {
	if(!obj) return false;
	if(obj.style) {
		if(obj.style.setProperty) obj.style.setProperty(name,value,importance);
		else {
			name = (name.match(/\-(\w)/)) ? name.replace(/\-(\w)/,name.match(/\-(\w)/)[1].toUpperCase()) : name;
			obj.style[name] = value;
		}
	}
	if(obj[name]) obj[name] = value;
}
