﻿var RBS = new Object;
RBS.System = new Object;
RBS.Dom = new Object;
RBS.Event = new Object;

if (typeof(dump) == "undefined") 
{
	function dump(arg) 
	{
		if (typeof(window.top.addContentText) != "undefined")
		{
			window.top.addContentText(arg);
		} 
		else if (typeof(window.opener) != "undefined") 
		{
		    if (typeof(window.opener.top) != "undefined")
		    {
			    if (typeof(window.opener.top.addContentText) != "undefined")
			    {
				    window.opener.top.addContentText(arg);
			    }
			} 
		}
	};
}

RBS.System.RegisterNamespace = function(NameSpase)
{
	var names = NameSpase.split(".");
	if (names.length < 2 || names[0] != "RBS") 
	{
		dump("Le NameSpace n'est pas valide :" + NameSpase);
	
	} 
	else 
	{
		var current = RBS;
		for(i = 1; i < names.length; i++) 
		{
			if (current[names[i]] == null) 
			{
				current[names[i]] = new Object;	
				current = current[names[i]];
			} 
		}
	}
}

RBS.System.Init = function(evt)
{
	RBS.System.version = "1.2.1.0";
	RBS.Event.lastEvent = null;
	RBS.Dom.body = (document.compatMode && document.compatMode.toLowerCase() != "backcompat") ? document.documentElement : (document.body || null);
	RBS.Localization = new ObjRBSCst();
}


//------------------------ Events Manager -----------------------

RBS.Event.AddEvent = function (element, eventName, handler) 
{
  if (typeof(element.attachEvent) != 'undefined') 
  {
    element.attachEvent("on" + eventName, handler);
  } 
  else if (typeof(element.addEventListener) != 'undefined')
  {
    element.addEventListener(eventName, handler, false);
  }
}

RBS.Event.DelEvent = function (element, eventName, handler) 
{
  if (typeof(element.detachEvent) != 'undefined') 
  {
    element.detachEvent("on" + eventName, handler);
  } 
  else if (typeof(element.removeEventListener) != 'undefined')
  {
    element.removeEventListener(eventName, handler, false);
  }
}

RBS.Event.AddEvent(window, "load", RBS.System.Init);

function RBS_Event_Event(evt) 
{
	this.e = (evt) ? evt : ((window.event) ? event : null);
	
	this.but = this.e.which || this.e.button || 0;	
	this.button = (this.e.type == 'mousedown') ? this.but : (RBS.Event.lastEvent && RBS.Event.lastEvent.button) ? RBS.Event.lastEvent.button : 0;
	
	this.type = this.e.type;
	this.src = this.e.target || this.e.srcElement || null;
	if (this.src)
	{
		if (this.src.nodeType == 3) {this.src = this.src.parentNode;}
		this.tag = (this.src.tagName || this.src).toString().toLowerCase();
	}
	
	this.coords = this.GetPageEventCoords();
		
	this.shiftKey = this.e.modifiers ? (this.e.modifiers & Event.SHIFT_MASK) : (this.e.shiftKey || false);
	this.ctrlKey = this.e.modifiers? (this.e.modifiers & Event.CONTROL_MASK) : (this.e.ctrlKey || false);
	
	RBS.Event.lastEvent = this;
}

RBS_Event_Event.prototype.toString = function()
{
	return "but:" + this.but + " button:" + this.button + " type:" + this.type + " tag:" + this.tag + 
		" coords.left:" + this.coords.left + " coords.top:" + this.coords.top + " shiftKey:" + this.shiftKey + " ctrlKey:" + this.ctrlKey;

}

RBS_Event_Event.prototype.GetPageEventCoords = function() 
{
	var coords = {left:0, top:0};
	if (this.e) {
		if (this.e.pageX) 
		{
			coords.left = this.e.pageX;
			coords.top = this.e.pageY;
		} 
		else if (this.e.clientX) 
		{
			coords.left = this.e.clientX + RBS.Dom.body.scrollLeft - RBS.Dom.body.clientLeft;
			coords.top =  this.e.clientY + RBS.Dom.body.scrollTop - RBS.Dom.body.clientTop;
			
			// include html element space, if applicable
			if (RBS.Dom.body.parentElement && RBS.Dom.body.parentElement.clientLeft) 
			{
				var bodParent = RBS.Dom.body.parentElement;
				coords.left += bodParent.scrollLeft - bodParent.clientLeft;
				coords.top += bodParent.scrollTop - bodParent.clientTop;
			}
		}
	}
	return coords;
}

RBS_Event_Event.prototype.Stop = function() 
{
	this.e.cancelBubble = true;
	this.e.returnValue = false;
	return false;
}

//------------------------ Element Manager -----------------------

RBS.Dom.CreateElement = function(tagname, parentElement) 
{
	var elem = document.createElement(tagname);
	if (parentElement != null)
	{
		parentElement.appendChild(elem);
	}
	return elem;
}

RBS.Dom.GetElement = function(ElementName) 
{
	var elem = null;
	if (typeof(ElementName) == "string") 
	{
		if (document.getElementById != null) 
		{
			elem = document.getElementById(ElementName);
		} 
		else if (document.all != null) 
		{
			elem = document.all[ElementName] || null;
		}
		else if (document.layers != null) 
		{
			elem = document.layers[ElementName] || null;
		}		
	} 
	else if (ElementName != null)
	{
		elem = (ElementName.Base == null) ? ElementName : ElementName.Base;	  
	}
	
	return elem;
}

RBS.Dom.HasClass = function(Element, ClassName) 
{
	var i, list;
	
	if (Element.className == null) 
		return false; 
	
	list = Element.className.split(" ");
	for (i = 0; i < list.length; i++)
		if (list[i].indexOf(ClassName) != -1) 
			return true;
			
	return false;
}

RBS.Dom.RemoveClass = function(Element, ClassName) 
{
	var i, curList, newList;	
	if (Element.className == null) return "";

	newList = new Array();
	curList = Element.className.split(" ");
	for (i = 0; i < curList.length; i++)
		if (curList[i] != ClassName)
			newList.push(curList[i]);
			
	Element.className = newList.join(" ");
	return Element.className;
}

RBS.Dom.AddClass = function(Element, ClassName) 
{
	RBS.Dom.RemoveClass(Element, ClassName);
	if (Element.className == null || Element.className == "")
	{
		Element.className = ClassName;
	} 
	else
	{
		Element.className = Element.className + " " + ClassName;
	}
	return Element.className;
}

RBS.Dom.ToggleClass = function(node, className) 
{
	if (RBS.Dom.HasClass(node, className))
	{
		RBS.Dom.RemoveClass(node, className);		
	} else {
		RBS.Dom.AddClass(node, className);
	}
}

RBS.Dom.GetLeft = function(Element)
{
	var result = 0;	
	while (Element)
	{
		result += parseInt(Element.offsetLeft);
		Element = Element.offsetParent;
	}
	return result;
}

RBS.Dom.GetTop = function(Element)
{
	var result = 0;
	while (Element)
	{
		result += parseInt(Element.offsetTop);
		Element = Element.offsetParent;
	}
	return result;
}

RBS.Dom.GetCoord = function(Element)
{
	return {left: RBS.Dom.GetLeft(Element), top: RBS.Dom.GetTop(Element)};
}


RBS.Dom.GetWidth = function(Element)
{
    var result = 0;
    if (Element.offsetWidth) 
    {
        result = Element.offsetWidth;
    } 
    else if (Element.clip && Element.clip.width) 
    {
        result = Element.clip.width;
    } 
    else if (Element.style && Element.style.pixelWidth) 
    {
        result = Element.style.pixelWidth;
    }
    
    return parseInt(result);
}
   
RBS.Dom.GetHeight = function(Element)
{
    var result = 0;
    
    if (Element.offsetHeight) 
    {
        result = Element.offsetHeight;
    } 
    else if (Element.clip && Element.clip.height) 
    {
        result = Element.clip.height;
    } 
    else if (Element.style && Element.style.pixelHeight) 
    {
        result = Element.style.pixelHeight;
    }
    return parseInt(result);
}

RBS.Dom.Show = function(Element)
{
	Element.style.visibility = "visible";
}

RBS.Dom.Hide = function(Element)
{
	Element.style.visibility = "hidden";
}

RBS.Dom.MoveTo = function (Element, x, y) 
{
    Element.style.left = x + "px";
    Element.style.top = y + "px";
}

RBS.Dom.GetContainerWith = function (node, tagName, className) 
{
  while (node != null) 
  {
    if (node.tagName != null && node.tagName == tagName && RBS.Dom.HasClass(node, className))
    {
		return node;
	}
    node = node.parentNode;
  }
  return null;
}

RBS.Dom.GetInsideWindowWidth = function() 
{
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (RBS.Dom.body.clientWidth) {
        return RBS.Dom.body.clientWidth;
    } else if (RBS.Dom.body.parentElement.clientWidth) {
        return RBS.Dom.body.parentElement.clientWidth;
    }
    return 0;
}
   
RBS.Dom.GetInsideWindowHeight = function () {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (RBS.Dom.body.clientHeight) {
        return RBS.Dom.body.clientHeight;
    } else if (RBS.Dom.body.parentElement.clientHeight) {
        return RBS.Dom.body.parentElement.clientHeight;
    }
    return 0;
}

function RBS_Dom_Element(ElementName) {

    this.Base = RBS.Dom.GetElement(ElementName);
	if (this.Base != null) 
	{
		this.id = (this.Base.id == null) ? this.Base.name : this.Base.id;
		this.className = this.Base.className;
	}
}

RBS_Dom_Element.prototype.toString = function()
{
	return "Base:" + this.Base + " id:" + this.id + " className:" + this.className;
}

RBS_Dom_Element.prototype.HasClass = function(ClassName) 
{
	return RBS.Dom.HasClass(this, ClassName);
}

RBS_Dom_Element.prototype.RemoveClass = function(ClassName) 
{
	RBS.Dom.RemoveClass(this, ClassName);
	this.Base.className = this.className;
}

RBS_Dom_Element.prototype.AddClass = function(ClassName) 
{
	RBS.Dom.AddClass(this, ClassName);
	this.Base.className = this.className;
}

RBS_Dom_Element.prototype.GetLeft = function()
{
	return RBS.Dom.GetLeft(this.Base);
}

RBS_Dom_Element.prototype.GetTop = function()
{
	return RBS.Dom.GetTop(this.Base);
}

RBS_Dom_Element.prototype.GetWidth = function()
{
	return RBS.Dom.GetWidth(this.Base);
}
   
RBS_Dom_Element.prototype.GetHeight = function()
{
	return RBS.Dom.GetHeight(this.Base);
}

RBS_Dom_Element.prototype.MoveTo = function (x, y) 
{
    RBS.Dom.MoveTo(this.Base, x, y);
}

