var _Garwin_Dom = function (){
	this.Type = "Garwin.Dom";
	this.browser = new this.browserObject();
	this.positions = new this.positionObject();
	this.images = new this.images();
	this.checkboxes = new this.checkboxes();
	this.css = new this.css();	
	this.divtools = this.divtoolsObject;
}
_Garwin_Dom.prototype = {
	getAttr: function(oNode, sAttr, sDefault)
	{
		if (oNode.getAttribute == null)
			return sDefault;
		var sVal = oNode.getAttribute(sAttr);
			
		if (sVal == null || sVal == '')
			return sDefault;
		else
			return sVal;
	},
	getById: function(sID, oCtl)
	{
		if (oCtl == null)
			oCtl = document;
		if (oCtl.getElementById) //(dnn.dom.browser.isType(dnn.dom.browser.InternetExplorer) == false)
			return oCtl.getElementById(sID);
		else if (oCtl.all)
			return oCtl.all(sID);
		else
			return null;
	},
	getByTagName: function(sTag, oCtl)
	{
		if (oCtl == null)
			oCtl = document;
		if (oCtl.getElementsByTagName) //(dnn.dom.browser.type == dnn.dom.browser.InternetExplorer)
			return oCtl.getElementsByTagName(sTag);
		else if (oCtl.all && oCtl.all.tags)
			return oCtl.all.tags(sTag);
		else
			return null;
	},
	getByClassName: function(sClassName, oCtl)
	{
		//BY KorRedDevil, Jeff Mott, rnd me
		//http://forums.devshed.com/javascript-development-115/javascript-get-all-elements-of-class-abc-24349.html
		var hasClassName = new RegExp("(?:^|\\s)" + sClassName + "(?:$|\\s)");
		var allElements = this.getByTagName("*",oCtl);
		var results = [];
		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(sClassName) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}
		return results;
	},
	getType:function(oCtl)
	{
		return(oCtl.tagName);
	},
	findParentByAttr:function(i_childElmt,i_TrgParentAttrName,i_TrgParentAttr)//http://www.randomsnippets.com/2008/06/26/how-to-find-and-access-parent-nodes-via-javascript/
	{
	    var testElmt = i_childElmt.parentNode;
	    while(testElmt.getAttribute(i_TrgParentAttrName) != i_TrgParentAttr) {
				testElmt = testElmt.parentNode;
			}
		return(testElmt);
	},
	findParentByTagName:function(i_childElmt,i_TrgParentTagName)
	{
	    var testElmt = i_childElmt.parentNode;
	    while(this.getType(testElmt) != i_TrgParentTagName) {
				testElmt = testElmt.parentNode;
			}
		return(testElmt);
	},
	getIndex:function(i_trgElmt)
	{
		var m_Elements = garwin.dom.getByTagName(i_trgElmt.tagName,i_trgElmt.parentElement);
	  for (var i=0;i<m_Elements.length;i++)
	  {
    	if (i_trgElmt == m_Elements[i])
      	return i;
    }
    return -1;
	},
	////////////THE FOLLOWING TO CHECK
	navigate: function(sURL, sTarget)
	{
		if (sTarget != null && sTarget.length > 0)
		{
			if (sTarget == '_blank')	//todo: handle more
				window.open(sURL);
			else
				document.frames[sTarget].location.href = sURL;
		}
		else
			window.location.href = sURL;
		return false;
	},
	////////////THE FOLLOWING TO CHECK
	getFormPostString: function(oCtl)
	{
		var sRet = '';
		if (oCtl != null)
		{
			if (oCtl.tagName && oCtl.tagName.toLowerCase() == 'form')	//if form, faster to loop elements collection
			{
				for (var i=0; i<oCtl.elements.length; i++)
					sRet += this.getElementPostString(oCtl.elements[i]);					
			}
			else
			{
				sRet = this.getElementPostString(oCtl);
				for (var i=0; i<oCtl.childNodes.length; i++)
					sRet += this.getFormPostString(oCtl.childNodes[i]);	//1.3 fix (calling self recursive insead of elementpoststring)
			}
		}
		return sRet;		
	},
	getElementPostString: function(oCtl)
	{
		var sTagName;
		if (oCtl.tagName)
			sTagName = oCtl.tagName.toLowerCase();
			
		if (sTagName == 'input') 
		{
			var sType = oCtl.type.toLowerCase();
			if (sType == 'text' || sType == 'password' || sType == 'hidden' || ((sType == 'checkbox' || sType == 'radio') && oCtl.checked)) 
				return oCtl.name + '=' + garwin.encode(oCtl.value) + '&';
		}
		else if (sTagName == 'select') 
		{
			for (var i=0; i<oCtl.options.length; i++) 
			{
				if (oCtl.options[i].selected) 
					return oCtl.name + '=' + garwin.encode(oCtl.options[i].value) + '&';
			}
		}
		else if (sTagName == 'textarea') 
			return oCtl.name + '=' + garwin.encode(oCtl.value) + '&';
		
		return '';
	}
}
//////////////////////////////////////////garwin.dom.images//////////////////////////////
_Garwin_Dom.prototype.images = function(){}
_Garwin_Dom.prototype.images.prototype =
{
	preload: function(imgObj,imgSrc) {
		//'imgObj' not compatible with chars : -
		//alert(imgObj+' '+imgSrc)
		if (document.images) {
			eval(imgObj+' = new Image()')
			eval(imgObj+'.src = "'+imgSrc+'"')
		}
	}
}
//////////////////////////////////////////garwin.dom.checkboxes//////////////////////////////
_Garwin_Dom.prototype.checkboxes = function(){}
_Garwin_Dom.prototype.checkboxes.prototype =
{
	getBoolCheckBoxes: function(i_frm,i_bool) {//get checked or unchecked checkboxes
		var a_CumulatedString = new Array();
		if(i_frm) {
			for (y=0;y<i_frm.length;y++) {
				//alert(y+" "+i_frm.elements[y].name+" "+i_frm.elements[y].type);
				if(i_frm.elements[y].type=='checkbox')
				{
					if (i_frm.elements[y].checked==i_bool){
						a_CumulatedString[a_CumulatedString.length]=i_frm.elements[y];
					}
				}
			}
		}
		/*if(a_CumulatedString.length>0)
		{
			alert(a_CumulatedString.join("|"));
			//return(a_CumulatedString.join("|"));
		}*/
		return(a_CumulatedString);
	}
}
//////////////////////////////////////////garwin.dom.css//////////////////////////////
_Garwin_Dom.prototype.css = function(){}
_Garwin_Dom.prototype.css.prototype =
{
	getRules: function(i_StyleSheetBlockIndex) {
		if (!document.styleSheets) return;
		var theRules = new Array();
		if (document.styleSheets[i_StyleSheetBlockIndex].cssRules)//Explorer Mac, Mozilla, Safari, and Opera.
			theRules = document.styleSheets[i_StyleSheetBlockIndex].cssRules
		else if (document.styleSheets[i_StyleSheetBlockIndex].rules)//Explorer (Win and Mac), and Safari.
			theRules = document.styleSheets[i_StyleSheetBlockIndex].rules
		return(theRules);
	},
	setOpacity: function( el, opacity){ 
		if(el.style.opacity != undefined){ 
			el.style.opacity = opacity; 
		}
		else if( el.style.MozOpacity != undefined){ 
			el.style.MozOpacity = opacity; 
		}
		else if ( el.style.filter != undefined){ 
			el.style.filter="alpha(opacity=" + Math.round(opacity * 100) + ")"; 
		} 
	} 
}
_Garwin_Dom.prototype.css.prototype.fade = function()
{
	this.fadeSteps = 20; 
	this.fadeDelay = 20; 
	this.fadeStep = 0; 	
}
_Garwin_Dom.prototype.css.prototype.fade.prototype = 
{
	In: function(i_fadeElem) { 
		//alert("In Begin this.fadeStep="+this.fadeStep+"/this.fadeSteps="+this.fadeSteps+" = "+(this.fadeStep/this.fadeSteps));
		//alert("In Begin arguments[0]="+arguments[0]);				
		//alert("In Begin");
		garwin.dom.css.setOpacity(i_fadeElem, (this.fadeStep/this.fadeSteps)); 
		this.fadeStep++; 
		if(this.fadeStep<(this.fadeSteps))
		{
			try
			{
				var m_Delegate = garwin.createDelegate(this, this.In);
				//alert("this.AlertGo="+this.AlertGo);
				window.setTimeout(function(){m_Delegate(i_fadeElem);}, 20);
			}
			catch(err)
			{
			  	//alert("In err="+err);	
					/*alert("In err arguments[0]="+arguments[0]);
					alert("In err this.fadeStep="+this.fadeStep+"<this.fadeSteps="+this.fadeSteps);
					alert("In err this="+this);
					alert("In err this.In="+this.In);
					alert("In err window.setTimeout="+window.setTimeout);
					alert("In err garwin.createDelegate(this, this.In)(i_fadeElem)="+function(){garwin.createDelegate(this, this.In)(i_fadeElem)});*/
			}
		}	
	}
} 
//////////////////////////////////////////THE FOLLOWING TO CHECK//////////////////////////////
//--- garwin.dom.browserObject 
_Garwin_Dom.prototype.browserObject = function()
{
	this.InternetExplorer = 'ie';
	this.Netscape = 'ns';
	this.Mozilla = 'mo';
	this.Opera = 'op';
	this.Safari = 'safari';
	this.Konqueror = 'kq';
	this.MacIE = 'macie';
	this.type = null;
	this.version = null; 
	/////
	var sAgent = navigator.userAgent.toLowerCase();
	//prompt("navigator.userAgent",navigator.userAgent);
	//prompt("navigator.appVersion",navigator.appVersion);
	/*
	navigator.userAgent;
	Internet Explorer 8	Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
	Firefox 3.5.7 		Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7
	Chrome 3.0.195.38	Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.0
	navigator.appVersion;
	Internet Explorer 8	4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
	Firefox 3.5.7 		5.0 (Windows; en-US)
	Chrome 3.0.195.38	5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.0
	*/		
	if (sAgent.indexOf('konqueror') != -1) 
		this.type = this.Konqueror;
	else if (sAgent.indexOf('opera') != -1) 
		this.type = this.Opera;
	else if (sAgent.indexOf('netscape') != -1) 
		this.type = this.Netscape;
	else if (sAgent.indexOf('msie') != -1)
	{
		if (sAgent.indexOf('mac') != -1)
			this.type = this.MacIE;
		else
			this.type = this.InternetExplorer;
	}
	else if (sAgent.indexOf('safari') != -1)
		this.type = 'safari';
			
	if (this.type == null)
		this.type = this.Mozilla;  
				
	if (this.type == this.InternetExplorer)
	{
		var temp=navigator.appVersion.split("MSIE");
		this.version=parseFloat(temp[1]);
	}
	else if (this.type == this.Netscape)
	{
		var temp=sAgent.split("netscape");
		this.version=parseFloat(temp[1].split("/")[1]);	
	}
	else
		this.version = parseFloat(navigator.appVersion);
}
_Garwin_Dom.prototype.browserObject.prototype =
{
	toString: function()
	{
		return this.type + ' ' + this.version;
	},
		
	isType: function()
	{
		for (var i=0; i<arguments.length; i++)
		{
			if (garwin.dom.browser.type == arguments[i])
				return true;
		}
		return false;
	}
}	
//--- End garwin.dom.browserObject
//////////////////////////////////////////////////////
_Garwin_Dom.prototype.positionObject = function (){this.Type = "Garwin.Dom.Positions";};
_Garwin_Dom.prototype.positionObject.prototype = {
	getctrlpos: function(i_control) {//WORKS ONLY WHEN ALL TAGS WITHIN PAGE FROM TARGET OBJ TO BODY ARE HTML CLASSIC TAGS. NO FUNKY TAGS
		for (var sumTop=0,sumLeft=0;i_control!=document.body;sumTop+=i_control.offsetTop,sumLeft+=i_control.offsetLeft, i_control=i_control.offsetParent);
		return {X:sumLeft,Y:sumTop}
	},
	getctrlpos2: function(i_control){//OTHER WAY TO DO getctrlpos:
		var sumLeft = i_control.offsetLeft;
		var sumTop = i_control.offsetTop;
	
		var parentEl=i_control.offsetParent;
		while (parentEl!=null){
			sumLeft += parentEl.offsetLeft 
			sumTop += parentEl.offsetTop;
			parentEl=parentEl.offsetParent;
		}
		return {X:sumLeft,Y:sumTop}
	},
	getmousepos: function(e) {//COMPATIBLE FF and IE WILL ONLY WORK AS EVENT HANDLER BECAUSE NEEDS THE EVENT ARGUMENT TO WORK WITH FF
		if (!e) var e = window.event;
		return {X:e.clientX,Y:e.clientY}
	},
	//OTHER WAY TO DO getmousepos: ACTUALLY EXACTLY THE SAME AS ABOVE WITH REGARDS TO BROWSER COMPATIBILITY 
	//BUT ABOVE MUCH BETTER SYNTAX
	//var curposy=window.event? event.clientY : e.clientY? e.clientY: "";
	//var curposx=window.event? event.clientX : e.clientX? e.clientX: "";
	getscrollbarpos: function() {//COMPATIBLE FF and IE
		var scrollX, scrollY;
		if (document.all)
		{
			if (!document.documentElement.scrollLeft)
			scrollX = document.body.scrollLeft;
			else
			scrollX = document.documentElement.scrollLeft;
			
			if (!document.documentElement.scrollTop)
			scrollY = document.body.scrollTop;
			else
			scrollY = document.documentElement.scrollTop;
		}
		else
		{
			scrollX = window.pageXOffset;
			scrollY = window.pageYOffset;
		}
		return {X:scrollX,Y:scrollY}
	},
	//OTHER WAY TO DO getscrollbarpos:TOCHECK IF BETTER
	//var dsocx=(window.pageXOffset)? pageXOffset: this.ietruebody().scrollLeft;
	//var dsocy=(window.pageYOffset)? pageYOffset : this.ietruebody().scrollTop;
	//function ietruebody()
	//{
	//	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	//}
	//////////////////////////////THE FOLLOWING TO CHECK////////////////////////////////////
	getctrlHeight:function (eSrc)
	{	
		if (eSrc.offsetHeight == null || eSrc.offsetHeight == 0)
		{
			if (eSrc.offsetParent == null)
				return 0;
			if (eSrc.offsetParent.offsetHeight == null || eSrc.offsetParent.offsetHeight == 0)
			{
				if (eSrc.offsetParent.offsetParent != null)
					return eSrc.offsetParent.offsetParent.offsetHeight; //needed for Konqueror
				else
					return 0;
			}
			else
				return eSrc.offsetParent.offsetHeight;
		}
		else
			return eSrc.offsetHeight;
	},
	getctrlWidth:function (eSrc)
	{
		if (eSrc.offsetWidth == null || eSrc.offsetWidth == 0)
		{
			if (eSrc.offsetParent == null)
				return 0;
			if (eSrc.offsetParent.offsetWidth == null || eSrc.offsetParent.offsetWidth == 0)
			{
				if (eSrc.offsetParent.offsetParent != null)
					return eSrc.offsetParent.offsetParent.offsetWidth; //needed for Konqueror
				else
					return 0;
			}
			else
				return eSrc.offsetParent.offsetWidth
	
		}
		else
			return eSrc.offsetWidth;
	}
};
//////////////////////////////////////
_Garwin_Dom.prototype.divtoolsObject = function(i_div)
{
	this.div = i_div;
	this.type = "divtoolsObject";
	this.preDestroyFn = null;
};
_Garwin_Dom.prototype.divtoolsObject.prototype = {
	creatediv:function()
	{
		this.div=document.createElement("div");
		this.div.setAttribute("id","_div_");
		this.div.style.position = "absolute";
		this.div.style.visibility = "hidden";
		this.div.style.zIndex = 100000;//To make sure always infront
		document.forms[0].appendChild(this.div);
		////
		var m_span = document.createElement("span");
		m_span.setAttribute("id","_span_");
		m_span.style.cssText = 'text-align:right;width:100%;';
		this.div.appendChild(m_span);
		////
		var m_close = document.createElement("a");
		m_close.setAttribute("id","_close_");
		m_close.id = '_close_';
		m_close.href = "#";
		m_close.innerText = "close";
		//var m_anchortext = document.createTextNode('close');
		//m_close.appendChild(m_anchortext);
		garwin.events.AddEvent(m_close,'click',garwin.createDelegate(this, this.destroydiv), true);
		m_span.appendChild(m_close);
		////
		var m_br = document.createElement("br");
		this.div.appendChild(m_br);		
	},
	destroydiv:function()
	{
		if(this.preDestroyFn!=null)
			this.preDestroyFn();
			
		document.forms[0].removeChild(this.div);	
		this.div=null;
	},
	showdiv:function()//COMPATIBLE FF and IE
	{
		if(this.div.style.visibility != "visible")
			this.div.style.visibility = "visible";
	},
	hidediv:function()//COMPATIBLE FF and IE
	{
		if(this.div.style.visibility != "hidden")
			this.div.style.visibility = "hidden";
	},
	addmessagediv:function(i_message)
	{
		var m_message = document.createTextNode(i_message);
		this.div.appendChild(m_message);
		//this.div.innerHTML+=i_message;
	},
	positiondiv:function(i_x,i_y)//COMPATIBLE FF and IE
	{
		this.div.style.left = i_x;
		this.div.style.top = i_y;
	},
	mousepositiondiv:function(e){
		var mousPos = garwin.dom.positions.getmousepos(e);
		mousPos.X += garwin.dom.positions.getscrollbarpos().X
		mousPos.Y += garwin.dom.positions.getscrollbarpos().Y
		this.positiondiv(mousPos.X+10+"px",mousPos.Y+10+"px");
		//var _this = this;
		//this.uptime=setTimeout(function(){_this.positiondiv(e);},10);
	},
	followscrollbar:function(){
		//var crtPos = garwin.dom.positions.getctrlpos(i_crt);
		var sbPos = garwin.dom.positions.getscrollbarpos();
		this.positiondiv(sbPos.X,sbPos.Y);
	}
};

garwin.dom = new _Garwin_Dom;

