var csXInterface = {

	_getBrowser:
		function() {
			if (document.all)
				return "IE";
			else if (document.getElementById)
				return "FF";
			else
				return false;
		},

	_get:
		function(objectID) {
			return(document.getElementById(objectID));	
		},

	_maxWidth:
		function() {
			if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			var browserWinWidth = window.innerWidth;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			browserWinWidth = document.documentElement.clientWidth;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			browserWinWidth = document.body.clientWidth;
			}
			return browserWinWidth;
		},
		
	_maxHeight:
		function() {
			if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			var browserWinHeight = window.innerHeight;	
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			browserWinHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			browserWinHeight = document.body.clientHeight;
			}
			return browserWinHeight;
		},

	_mouseXY:
		function (e) { // by Kravvitz of DynamicSiteSolutions.com
		  var x=0,y=0;
		  e=e||window.event;
		  if(e.pageX || e.pageY) {
			x=e.pageX;
			y=e.pageY;
		  } else if(typeof(e.clientX)=='number') {
			var dE=document.documentElement;
			x=e.clientX+document.body.scrollLeft+(dE?dE.scrollLeft:0);
			y=e.clientY+document.body.scrollTop+(dE?dE.scrollTop:0);
		  }
		  return [x,y];
		},

	/***********************************************
	* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
	* This notice MUST stay intact for legal use
	* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
	***********************************************/

	_disableSelection:
		function(target) {
			if (typeof target.onselectstart!="undefined") //IE route
				target.onselectstart=function(){return false}
			else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
				target.style.MozUserSelect="none"
			else //All other route (ie: Opera)
				target.onmousedown=function(){return false}
		},

	csWindow:
		function(Parameters) {
	
			/* Settings */
			this.Width = Parameters.Width;
			this.Height = Parameters.Height;
			this.AutoCenter = Parameters.AutoCenter;
			this.TitleBar = new csXInterface.csWindowTitleBar(Parameters.Title);
			this.Theme = new csXThemes[Parameters.Theme];
			this.WindowID = Parameters.WindowID;
			this.Controls = new Array();
			this.Icon = "themes/" + this.Theme.ThemeAlias + "/" + Parameters.Icon;
			this.IsBeingDragged = false;
			this.IsBeingResized = false;
			this._dragXYSource = [0, 0];
			this._originalPosition = [0, 0];
			this._originalSize = [this.Width, this.Height];
			this.Draggable = Parameters.Draggable;
			this.Resizable = Parameters.Resizable;
			this.ScrollableArea = Parameters.ScrollableArea;

			/* Renderer */
			this.Render = function(IsModal) {
				
				var Target = document.createElement("div");
				Target.id = this.WindowID;

				if (csXInterface._get(this.WindowID)) {
					alert("ERROR: La ventana " + this.WindowID + " ya existe.");
					return false;
				}

				with (Target.style) {
					width = this.Width + "px";
					height = this.Height + "px";
					border = this.Theme.WindowBorder;
					position = "absolute";	
					background = this.Theme.WindowBackground;

					if (this.AutoCenter) {
						left = (csXInterface._maxWidth() / 2 - this.Width / 2) + "px";
						top = (csXInterface._maxHeight() / 2 - this.Height / 2) + "px";
					}
				}
				
				TargetShadow = document.createElement("div");
				TargetShadow.id = this.WindowID + "_shadow";
				with (TargetShadow.style) {
					position = "absolute";
					left = (parseInt(Target.style.left) + this.Theme.ShadowOffset) + "px";
					top = (parseInt(Target.style.top) + this.Theme.ShadowOffset) + "px";
					width = this.Width + "px";
					height = this.Height + "px";
					zOrder = -1;
					background = this.Theme.ShadowColor;
				}

				if (csXInterface._getBrowser() == "FF") TargetShadow.style.opacity = this.Theme.ShadowOpacity;
				
				document.body.appendChild(TargetShadow);

				TitleBarContainer = document.createElement("div");
				TitleBarContainer.id = this.WindowID + "_titlebar";
				TitleBarContainer.innerHTML = this.TitleBar.Caption;
				with (TitleBarContainer.style) {
					textAlign = this.Theme.TitleBarCaptionAlign;
					background = this.Theme.TitleBarBackground;
					font = this.Theme.TitleBarFont;
					color = this.Theme.TitleBarColor;
					padding = this.Theme.TitleBarInnerPadding;
					borderTop = this.Theme.TitleBarBorders["Top"];
					borderBottom = this.Theme.TitleBarBorders["Bottom"];
					borderLeft = this.Theme.TitleBarBorders["Left"];
					borderRight = this.Theme.TitleBarBorders["Right"];
					cursor = "default";
				}

				window.onmousemove = this.MoveResizeWindow;

				if (this.Draggable) {
					TitleBarContainer.onmousedown = this.BeginDrag;
					TitleBarContainer.onmouseup = this.StopDrag;
				}

				csXInterface._disableSelection(TitleBarContainer);
				
				IconTag = document.createElement("img");
				with (IconTag) {
					src = this.Icon;
					align = "left";
					style.margin = (parseInt(this.Theme.TitleBarInnerPadding) - 1) + "px";
				}
				
				CloseButton = document.createElement("img");
				with (CloseButton) {
					id = this.WindowID + "_close";
					src = "themes/" + this.Theme.ThemeAlias + "/close.png";
					align = "right";
					style.margin = (parseInt(this.Theme.TitleBarInnerPadding) - 1) + "px";
					style.cursor = "pointer";
					title = "Cerrar";
					onclick = this.Destroy;
				}

				if (IsModal) {
					GrayLayer = document.createElement("div");
					GrayLayer.id = this.WindowID + "_modalLayer";
					with (GrayLayer.style) {
						position = "absolute";
						left = "0px";
						top = "0px";
						background = "black";						
						width = csXInterface._maxWidth() + "px";
						height = csXInterface._maxHeight() + "px";
						if (csXInterface._getBrowser() == "FF") opacity = .35;
					}
					document.body.appendChild(GrayLayer);
				}

				ControlLayer = document.createElement("div");
				ControlLayer.id = this.WindowID + "_controls";
				with (ControlLayer.style) {
					padding = (parseInt(this.Theme.TitleBarInnerPadding) * 3) + "px";
					font = this.Theme.DefaultFont;
					if (this.ScrollableArea) {
						position = "absolute";
						height = (this.Height - 70) + "px";
						border = "1px solid silver";
						overflow = "auto";
					}
				}

				if (this.Resizable) {
					ResizeCorner = document.createElement("div");
					ResizeCornerImage = document.createElement("img");
					ResizeCornerImage.src = "themes/" + this.Theme.ThemeAlias + "/resize-corner.gif";
					
					with (ResizeCorner) {
						id = this.WindowID + "_rc";
						style.position = "relative";
						style.width = ResizeCornerImage.width + "px";
						style.height = ResizeCornerImage.height + "px";
						style.left = (this.Width - parseInt(style.width)) + "px";
						style.top = (this.Height - parseInt(style.height) - CloseButton.height - this.Theme.ShadowOffset - parseInt(this.Theme.TitleBarInnerPadding)/2 - 35) + "px";
						style.cursor = "se-resize";
						style.background = "url(themes/" + this.Theme.ThemeAlias + "/resize-corner.gif)";
						onmousedown = this.BeginResize;
						onmouseup = this.StopResize;
					}
					csXInterface._disableSelection(ResizeCorner);	
					ResizeCornerImage = null;			
				}
			
				with (Target) {
					appendChild(IconTag);
					appendChild(CloseButton);
					appendChild(TitleBarContainer);
					appendChild(ControlLayer);
					if (typeof ResizeCorner != "undefined") appendChild(ResizeCorner);
				}
				
				document.body.appendChild(Target);
				
				for (c = 0; c < this.Controls.length; c++) {
					this.Controls[c].Render(ControlLayer.id);	
				}
			

			}
			
			if (this.Resizable) {
				this.BeginResize = function(e) {
					if (!e) var e = window.event;
					csXInterface.csWindow.IsBeingResized = true;
					csXInterface.csWindow._dragXYSource = csXInterface._mouseXY(e);
					AffectedWindow = csXInterface._get(Parameters.WindowID);
					csXInterface.csWindow._originalSize = [parseInt(AffectedWindow.style.width), parseInt(AffectedWindow.style.height)];
					document.body.style.cursor = "se-resize";
				}
				
				this.StopResize = function() {
					csXInterface.csWindow.IsBeingResized = false;
					csXInterface.csWindow._dragXYSource = [0, 0];
					csXInterface.csWindow._originalSize = [csXInterface.csWindow.Width, csXInterface.csWindow.Height];
					document.body.style.cursor = "default";
				}				
			}
			
			if (this.Draggable) {
				this.BeginDrag = function(e) {
					if (!e) var e = window.event;
					csXInterface.csWindow.IsBeingDragged = true;
					csXInterface.csWindow._dragXYSource = csXInterface._mouseXY(e);
					AffectedWindow = csXInterface._get(Parameters.WindowID);
					csXInterface.csWindow._originalPosition = [parseInt(AffectedWindow.style.left), parseInt(AffectedWindow.style.top)];
					document.body.style.cursor = "move";
				}
			
				this.MoveResizeWindow = function(e) {
					if (!e) var e = window.event;
					MousePosition = csXInterface._mouseXY(e);
					FirstMousePosition = csXInterface.csWindow._dragXYSource;
					AffectedWindow = csXInterface._get(Parameters["WindowID"]);
					AffectedWindowShadow = csXInterface._get(Parameters["WindowID"]+"_shadow");
					ControlLayer = csXInterface._get(Parameters["WindowID"]+"_controls");
					Theme = new csXThemes[Parameters["Theme"]];

					if (csXInterface.csWindow.IsBeingDragged) {
						OriginalPosition = csXInterface.csWindow._originalPosition;
						NewX = OriginalPosition[0] - (FirstMousePosition[0] - MousePosition[0]);
						NewY = OriginalPosition[1] - (FirstMousePosition[1] - MousePosition[1])
						if (NewX + parseInt(AffectedWindow.style.width) + 10 > csXInterface._maxWidth()
						 || NewX < 0) {
						 	AllowXDragging = false;
						 	AllowYDragging = true;
						} else if (NewY + parseInt(AffectedWindow.style.height) + 10 > csXInterface._maxHeight() 
						 || NewY < 0) {
						 	AllowXDragging = true;
						 	AllowYDragging = false;
						} else {
						  	AllowXDragging = true;
						  	AllowYDragging = true;
						}
						if (AllowXDragging && !(NewX + parseInt(AffectedWindow.style.width) + 10 > csXInterface._maxWidth()
						 || NewX < 0)) { 
							AffectedWindow.style.left = NewX + "px"; 
							AffectedWindowShadow.style.left = (NewX + Theme.ShadowOffset) + "px";
						}
						if (AllowYDragging && !(NewY + parseInt(AffectedWindow.style.height) + 10 > csXInterface._maxHeight() 
						 || NewY < 0)) {
							AffectedWindow.style.top = NewY + "px"; 
							AffectedWindowShadow.style.top = (NewY + Theme.ShadowOffset) + "px";
						}
					}
					
					if (csXInterface.csWindow.IsBeingResized) {
						OriginalSize = csXInterface.csWindow._originalSize;
						ResizeCorner = csXInterface._get(Parameters.WindowID+"_rc");
						NewWidth = (OriginalSize[0] - (FirstMousePosition[0] - MousePosition[0]));
						NewHeight = (OriginalSize[1] - (FirstMousePosition[1] - MousePosition[1]));
						AffectedWindow.style.width = NewWidth + "px";				
						AffectedWindow.style.height = NewHeight + "px";				
						AffectedWindowShadow.style.width = AffectedWindow.style.width;
						AffectedWindowShadow.style.height = AffectedWindow.style.height;
						csXInterface.csWindow.Width = NewWidth;
						csXInterface.csWindow.Height = NewHeight;
						ControlLayer.style.height = (NewHeight - 70) + "px";
						ResizeCorner.style.left = (NewWidth - parseInt(ResizeCorner.style.width)) + "px";
						if (ControlLayer.style.overflow == "auto") {
							ResizeCorner.style.top = (NewHeight - parseInt(ResizeCorner.style.height) - csXInterface._get(Parameters["WindowID"]+"_close").height - Theme.ShadowOffset - parseInt(Theme.TitleBarInnerPadding)/2) + "px";
						} else {
							ResizeCorner.style.top = "10px";
						}
					}
				}

				this.StopDrag = function() {
					csXInterface.csWindow.IsBeingDragged = false;
					csXInterface.csWindow._dragXYSource = [0, 0];
					csXInterface.csWindow._originalPosition = [0, 0];
					document.body.style.cursor = "default";
				}
			}
			
			this.Destroy = function() {
				Target = csXInterface._get(Parameters.WindowID);
				if (!Target.removeNode)
					Target.parentNode.removeChild(Target);
				else
					Target.removeNode(true);

				ModalLayer = csXInterface._get(Parameters.WindowID + "_modalLayer");
				if (ModalLayer)
					if (!ModalLayer.removeNode)
						ModalLayer.parentNode.removeChild(ModalLayer);
					else
						ModalLayer.removeNode(true);
				
				ShadowLayer = csXInterface._get(Parameters.WindowID + "_shadow");
				if (!ShadowLayer.removeNode)
					ShadowLayer.parentNode.removeChild(ShadowLayer);
				else
					ShadowLayer.removeNode(true);
			}
			
			/* Control add */
			this.AddControl = function(Type) {

				NewControl = new csXControls["cs" + Type];
				ID = this.Controls.push(NewControl);
				return(ID - 1);
			
			}
			
		},
		
	csWindowTitleBar:
		function(Caption, TextAlign, Icon, Background) {
			
			this.Caption = Caption;
			this.TextAlign = TextAlign;
			this.Icon = Icon;
			this.Background = Background;
			
		}
		
}

var csXControls = {

	csLabel:
		function(Text) {
		
			this.Text = Text;
			this.Render = function(dropTargetID) {

				Target = csXInterface._get(dropTargetID);
				TextLabel = document.createElement("div");
				TextLabel.innerHTML = this.Text;
				Target.appendChild(TextLabel);

			}
		
		}
}

var csXThemes = {

	Default: function() {

		this.ThemeName = "Default";
		this.ThemeAlias = "default";
		this.WindowBorder = "1px outset silver";
		this.WindowBackground = "white";
		this.ShadowOffset = 8;
		this.ShadowColor = "black";
		this.ShadowOpacity = .3;
		this.TitleBarBackground = "#0099CC";
		this.TitleBarCaptionAlign = "center";
		this.TitleBarFont = "bold 8pt Tahoma";
		this.TitleBarColor = "white";
		this.TitleBarInnerPadding = "4px";
		this.TitleBarBorders = new Array();
		this.DefaultFont = "8pt Tahoma";

		this.TitleBarBorders = {
			Top: "",
			Bottom: "3px solid #006699",
			Left: "",
			Right: ""
		};

	}

};

