/*
	Author: Stephen W. Cote
	Email: wranlon@hotmail.com
	
	Copyright 2002, All Rights Reserved.
	
	Do not copy, archive, or distribute without prior written consent of the author.
*/

_js_package("org.cote.js.dom.css");
org.cote.js.dom.css={

	_get_offset_client_height:function(o){
		var r=0,m;
		if(o){
			m=parseInt(o.style.borderLeftHeight) + parseInt(o.style.borderRightHeight);
			if(!isNaN(m)){
				r += m;
			}
			m=parseInt(o.style.paddingTop) + parseInt(o.style.paddingBottom);
			if(!isNaN(m)){
				r += m;
			}
		}
		return r;

	},
	_get_offset_client_width:function(o){
		var r=0,m;
		if(o){
			m=parseInt(o.style.borderLeftWidth) + parseInt(o.style.borderRightWidth);
			if(!isNaN(m)){
				r += m;
			}
			m=parseInt(o.style.paddingLeft) + parseInt(o.style.paddingRight);
			if(!isNaN(m)){
				r += m;
			}
		}
		return r;

	},
	_gabs_top:function(o){
		var c=o,t=0;
		while(c!=null && c.nodeName.toLowerCase()!="body"){
			t+=c.offsetTop;
			c=c.offsetParent;
		}
		return t;
	},

	_gabs_left:function(o){
		var c=o,l=0;

		while(c!=null && c.nodeName.toLowerCase()!="body"){
			l+=c.offsetLeft;
			c=c.offsetParent;
		}	
		return l;
	},
	StyleManager:{

		_nullStyle:function(o,b){
			o.style.backgroundImage="";
			o.style.backgroundColor="";
			o.style.margin="0px";
			if(b){
				o.style.width="0px";
				o.style.height="0px";
			}
			o.style.padding="0px";
			o.style.border="0px none";
		},

		newInstance:function(owner){
/*
			var o=org.cote.js._forName(
				"style_manager",
				owner,
				{
					"position":"absolute",
					"border":"1 solid black",
					"borderWidth":1,
					"borderStyle":"solid",
					"borderColor":"black",
					"backgroundColor":"#55FF55",
					"overflow":"auto"
				}
			);
*/
			o = {
				owner:owner,
				styles:[],
				default_styles:[],
				base_name:null,
				style_source:null,
				style_names:{}
/*				
				,
				default_style_data:{
					position:"absolute",
					border:"1 solid black",
					borderWidth:1,
					borderStyle:"solid",
					"borderColor":"black",
					"backgroundColor":"#55FF55",
					"overflow":"auto"
				}
*/
			};
/*
			o.object_version="0.1b";
			o.object_type="spec_style_manager";
			o.object_id=org.cote.js._get_gunid();
*/
			o.style_source = org.cote.js.dom.css.StyleSource;
			
			o.setStyleNames=function(c){
				var t = this,v=null,i;
				if(typeof t.owner != "object" || t.style_source==null) return v;

				v=t.style_source.extractStyleNames(c);
				if(v){
					for(i in v){
						t.owner.object_config.style_names[i]=v[i];
					}
				}
				/*
					else class not found
				*/
				return v;
			};

			o.getStyle=function(s,c,b){
				/*
					s = style
					c = class
					b = switch
				*/
				c=(c)?c:"default-style";
				var v=null,t=this,q;
				if(t.styles[c] && t.styles[c].styles[s]){
					v=t.styles[c].styles[s];
				}
				else{
					if(c){
						q=t.queryStyle(c);
						if(q && q[s]){
							v=q[s];
						}
					}
				}
				if(b) v=parseInt(v);
				return v;
			};
			o.putStyle=function(s,v,c){
				c=(c)?c:"default-style";
				var t=this;
				if(t.styles[c]) t.styles[c].styles[s]=v;
			};
			o.setStyle=function(o,s,v,c,b){
				/*
					o = object
					s = name
					v = value
					c = class
					b = bit
				
				*/
				c=(c)?c:"default-style";
				var t=this,d;
				if(!t.styles[c] && o){
					t.styles[c]={"obj":o,"styles":{}};
				}
				if(!o && !t.styles[c]){
					return;
				}
				o=(o)?o:t.styles[c].obj;
				if(t.styles[c] && o){
					d=t.styles[c]["styles"];
					d[s]=v;
					o.style[s]=v;
				}
			};
		
			o.queryStyle=function(s){
				var t=this,v;
				if(t.style_source==null) return;
				v=t.style_source.extractStyle(s);
				if(v) t.setDefaultStyles(v);
				return v;
			};
		
			o.init=function(o,c){
				c=(c)?c:"default-style";
				var t=this,v,i;
				if(c){
					v=t.queryStyle(c);
					if(!v) return false;
				}
				else{
					alert("mdiStyleManager.js::CMDIStyleManager::init = Class name not specified.");
				}
			
				if(o) t.default_styles[c]={"obj":o,"styles":t.getDefaultStyles()};
				for(i in t.default_styles[c].styles){
					t.setStyle(o,i,t.default_styles[c].styles[i],c);
				}
				return true;
			};
		
			o.applyStyle=function(o,c){
				c=(c)?c:"default-style";
				var t=this;
				if(!t.styles[c] && o) t.init(o,c);
				if(t.styles[c]) t._applyStyle(o,c);
			};
		
			o._applyStyle=function(o,c){
				c=(c)?c:"default-style";
				var t=this,s,v,i,u;
				s=t.styles[c];
				v=s.styles;
				o=(o)?o:s.obj;
				if(v && o){
					for(i in v){
						u=v[i];
						o.style[i]=v[i];
					}
				}
			};
		
			o.setDefaultStyles=function(v){
				this.default_style_data=v;
			};
			o.getDefaultStyles=function(){
				return this.default_style_data;
			};






			/*
			org.cote.js._application_scope.add_object(o);
			*/
			
			return o;
/*			CEH.addEventListener(this,"windowClose","_handle_window_close");*/
		
		}	
	},
	_add_style_buffer:function(o){
		o["_load_style_names"]=function(c){
			c=(c)?c:null;
			var t=this;
			if(typeof t.style_manager == "object")
				return t.style_manager.setStyleNames(c);
			return null;
		}
	},
	StyleSource:null,
	StyleSourceImpl:function(p){
		var t=this,_j=org.cote.js;
		t.xdom=null;
		
		_j._implements(t,"base_object","dom_style_source","0.1b");

		t.url = p;
		t.load=function(p){
			var _j=org.cote.js;
			if(typeof p!="string") p = t.url;
			/* org.cote.js.util.logger.api.LoggerFactory.log("Load xml: " + p + " / " + t.request_id,"200.4");*/

			_j.xml.getXml(p,t._handle_load_xml,1,t.object_id);
		};
		t._handle_load_xml=function(n,x){


			/* org.cote.js.util.logger.api.LoggerFactory.log("Handle load xml: " + t.request_id,"200.4");*/
			if(x.id == t.object_id){
				t.xdom=x.xdom;
				t.ready_state = 3;
				if(t.xdom != null) t.ready_state = 4;
				org.cote.js.message.MessageService.publish("onstyleload",this);
			}

		};
		t.setStyleSource=function(x){
			this.xdom=x;
		};
		t.extractStyleNames=function(s){
			var t=this,a,l,o=null,r=null,i=0;
			if(t.xdom==null) return r;
			a=t.xdom.getElementsByTagName("stylenames");
			l=a.length;

			for(;i<l;i++){
				if(a[i].getAttribute("name")==s){
					o=a[i];
					break;
				}
			}
			if(typeof o=="object"){
				a=o.getElementsByTagName("stylename");
				l=a.length;
				r={};
				for(i=0;i<l;i++){
					/*var sVal=this.applyValueMods(aElements[i].getAttribute("value"));*/
					r[a[i].getAttribute("name")]=a[i].getAttribute("value");
				}
			}
			return r;
		};
		t.extractStyle=function(s){
			var t=this,a,l,o,r=null,i=0,v;
			a=t.xdom.getElementsByTagName("style-data");
			l=a.length;


			for(;i<l;i++){
				if(a[i].parentNode.getAttribute("name")==s){
					o=a[i];
					break;
				}
			}
			if(o){
				a=o.getElementsByTagName("style");
				l=a.length;
				v={};
				for(i=0;i<l;i++){
/*					var sVal=this.applyValueMods(aElements[i].getAttribute("value"));		*/
					v[a[i].getAttribute("name")]=a[i].getAttribute("value");
				}
			}
			return v;
		};
	
		org.cote.js._application_scope.add_object(t);

	}
};
	
	
		