/*-------------------------------------------------------------------------
|Amac: Controllerin boyutlari ile alakali baz scriptleri tutar.           |
|                                                                         |
|-------------------------------------------------------------------------|
|Function'lar:															  |
|		- AbsoluteOffsetLeft                                              |
|		- AbsoluteOffsetTop                                               |
|		- BoxsTotalWidth                                                  |
|		- CtrTreeWidth                                                    |
|		- GetCtr                                                          |
|		- getStyleAttr                                                    |
|		- InnerBoxWidth                                                   |
|		- InnerBoxHeight                                                  |
|		- OuterBoxWidth                                                   |
|		- OuterBoxHeight                                                  |
|                                                                         |
|-------------------------------------------------------------------------|
| Gerekli Dosyalar:                                                       |
|		- Utils.js                                                        |
|                                                                         |
|-------------------------------------------------------------------------|
|                    Gordion Bilgi Hizmet Ltd. Sti.                       |
|                      http://www.gordion.com.tr                          |
-------------------------------------------------------------------------*/


var isIE = (document.all) ? true : false;


//---------------------------------------------------------------------------------------------------
//Bir Control'un current style'inin istenilen attribute'unu verir.
//attr string olmalidir.
function getStyleAttr(ctr, attr)
{	
	//Guard
	if(!IsCtr(ctr) || typeof(attr) != "string")
		throw "getStyleAttr icin girilen ctr veya attr parametreleri duzgun degil.";
	
	//Internal table element'larin marginleri sifirdir.
	//bkz. http://www.w3.org/TR/REC-CSS2/tables.html#q7
	if((attr.indexOf("margin") != -1) && IsInternalTableElement(ctr))
		return 0;

	var ret;
	if(isIE)
	{
		//padding-left -> paddingLeft
		//border-top-width -> borderTopWidth
		var re = /(\w+)-(\w)(\w*)-(\w)(\w*)|(\w+)-(\w)(\w*)|(\w+)/;
		var ieStyleAttr = eval(attr.replace(re, "'$1' + '$2'.toUpperCase() + '$3' + '$4'.toUpperCase() + '$5$6' + '$7'.toUpperCase() + '$8$9'"));
		ret = eval("ctr.currentStyle." + ieStyleAttr);
	}
	else
		ret = document.defaultView.getComputedStyle(ctr, "").getPropertyValue(attr);
	

	if(isNumericStyle(attr))
	{ 
		if(isNaN(parseInt(ret)))
			return 0;
		ret = parseInt(ret);
	}
	
	return ret;
}

//---------------------------------------------------------------------------------------------------
//Parametre olarak aldigi attributun border padding gibi numeric olup olmadigini dondurur.
function isNumericStyle(attr)
{
	return	attr.indexOf("border")  != -1 ||
			attr.indexOf("padding") != -1 ||
			attr.indexOf("margin")  != -1 ||
			attr.indexOf("width")   != -1 ||
			attr.indexOf("height")  != -1 ;
}

//---------------------------------------------------------------------------------------------------
//Bir control'u cevreleyen boxlarin ikinci parametre tarafintaki toplam uzunlugu.
//Params:
//	- ctr: controlun kendisi
//	- dirc: yon. "top", "right", "bottom", "left" degerlerinden biri
function BoxsTotalWidth(ctr, dirc)
{
	var ctrmargin  = getStyleAttr(ctr, "margin-" + dirc);
	var ctrborder  = getStyleAttr(ctr, "border-" + dirc + "-width");
	var ctrpadding = getStyleAttr(ctr, "padding-" + dirc);

	return ctrmargin + ctrborder + ctrpadding;
}

//---------------------------------------------------------------------------------------------------
//control'un outerbox'inin (margin box) genisligi
function OuterBoxWidth(ctr)
{
	return ctr.offsetWidth 
				+ getStyleAttr(ctr, "margin-left") 
				+ getStyleAttr(ctr, "margin-right");
}

//---------------------------------------------------------------------------------------------------
//control'un outerbox'inin (margin box) yuksekligi
function OuterBoxHeight(ctr)
{	
	return ctr.offsetHeight 
				+ getStyleAttr(ctr, "margin-top") 
				+ getStyleAttr(ctr, "margin-bottom");
}

//---------------------------------------------------------------------------------------------------
//control'un innerbox'inin (style.width) genisligi
function InnerBoxWidth(ctr)
{
	return ctr.offsetWidth
				- getStyleAttr(ctr, "border-left-width")
				- getStyleAttr(ctr, "padding-left")
				- getStyleAttr(ctr, "border-right-width")
				- getStyleAttr(ctr, "padding-right");
}

//---------------------------------------------------------------------------------------------------
//control'un innerbox'inin (style.height) yuksekligi
function InnerBoxHeight(ctr)
{	
	return ctr.offsetHeight 
				- getStyleAttr(ctr, "border-top-width")
				- getStyleAttr(ctr, "padding-top")
				- getStyleAttr(ctr, "border-bottom-width")
				- getStyleAttr(ctr, "padding-bottom");
}

//---------------------------------------------------------------------------------------------------
//document.getElementById'nin kisaltilmisi.
//Ayrica Custom dim functionlarini da object'e ekler,
//bu nedenle sadece control'larla kullanilmali.
//Parametre olarak conrtol'un kendisini veya ID'sini alir.
function GetCtr(idctr)
{
	var ctr;
	if(typeof(idctr) == "string")	
		ctr = document.getElementById(idctr);
	else if(typeof(idctr) == "object")
		ctr = idctr;
	else
		throw "GetCtr parametre olarak sadece id veya control'unkendisi alabilir.";
	
	if(typeof(ctr) == "undefined" || ctr == null)	
		return null;
		
	ctr.GetStyleAttr   = function(sty) {return getStyleAttr(this, sty)};
	ctr.BoxsTotalWidth = function(dirc) {return BoxsTotalWidth(this, dirc);};
	ctr.InnerBoxWidth  = function() {return InnerBoxWidth(this);};
	ctr.InnerBoxHeight = function() {return InnerBoxHeight(this);};
	ctr.OuterBoxWidth  = function() {return OuterBoxWidth(this);};
	ctr.OuterBoxHeight = function() {return OuterBoxHeight(this);};
	
	return ctr;
}

//---------------------------------------------------------------------------------------------------
//TESTlerde kullanilir. Bir Controlun 
//inner ve outer box'larinin width'lerini 
//belli bir level'a kadar childlari ile gosterir.
function CtrTreeWidth(ctr, level, ind)
{
	//Recurence'in bitis condution'u
	if(level < 0) return "";
	//Guard
	if(!IsCtr(ctr)) return "";
	
	var id = ctr.getAttribute("ID");
	var tagName = ctr.tagName;
	
	if(typeof(ind)     != "string")	ind     = "";
	if(typeof(id)      != "string")	id      = "";
	if(typeof(tagName) != "string")	tagName = "";

	var str = "";
	str += ind + id + " " + tagName + "    o " + OuterBoxWidth(ctr) + "\n";
	str += ind + id + " " + tagName + " offs " + ctr.offsetWidth + "\n";
	str += ind + id + " " + tagName + "    i " + InnerBoxWidth(ctr) + "\n";
	
	/* 
	*  str += ind + id + " " + tagName + " xl  " + BoxsTotalWidth(ctr, "left") + "\n";
	*  str += ind + id + " " + tagName + " xr  " + BoxsTotalWidth(ctr, "rigth") + "\n";
	*  str += ind + id + " " + tagName + " w  " + getStyleAttr(ctr, "width") + "\n";
	*  str += ind + id + " " + tagName + " m  " + getStyleAttr(ctr, "margin-left") + "\n";
	*  str += ind + id + " " + tagName + " b  " + getStyleAttr(ctr, "border-left-width") + "\n";
	*  str += ind + id + " " + tagName + " p  " + getStyleAttr(ctr, "padding-left") + "\n";
	*/
	str += "\n";
	
	if(ctr.hasChildNodes())
	{
		for(var i = 0; i < ctr.childNodes.length; i++)
		{
			str += CtrTreeWidth(ctr.childNodes.item(i), level - 1, ind + "\t");
		}
	}
	
	return str;	
}


//--------------------------------------------------------------------------------------------------------------
//Parametre olarak verilen control'un cercevenin ust tarafina
//olan offset mesafesini bulur. Bu mesafe <body>'nin padding'ini
//icerirken control'un de margin'ini icerir, <body>'nin border
//ve margini ile control'un border ve padding'i bu mesafenin 
//icerisinde degillerdir.
function AbsoluteOffsetTop(ctr)
{
	if(ctr.tagName.toUpperCase() == "BODY" || ctr.tagName.toUpperCase() == "HTML")
		return 0;
	
	var parentBorder = getStyleAttr(ctr.offsetParent, "border-top-width");
	if(isNaN(parentBorder))
		parentBorder = 0;
	
	return AbsoluteOffsetTop(ctr.offsetParent) + parentBorder + ctr.offsetTop;
}

//--------------------------------------------------------------------------------------------------------------
//Parametre olarak verilen control'un cercevenin sol tarafina
//olan offset mesafesini bulur. Bu mesafe <body>'nin padding'ini
//icerirken control'un de margin'ini icerir, <body>'nin border
//ve margini ile control'un border ve padding'i bu mesafenin 
//icerisinde degillerdir.
function AbsoluteOffsetLeft(ctr)
{
	if(ctr.tagName.toUpperCase() == "BODY" || ctr.tagName.toUpperCase() == "HTML")
		return 0;

	var parentBorder = getStyleAttr(ctr.offsetParent, "border-left-width");
	if(isNaN(parentBorder))
		parentBorder = 0;
	
	//relative positionlamada ie'da bug var
	if(isIE && ctr.currentStyle.position == "relative")
		return AbsoluteOffsetLeft(ctr.offsetParent) + parentBorder;
		
	return AbsoluteOffsetLeft(ctr.offsetParent) + parentBorder + ctr.offsetLeft;
}

