var old_cssjs_onload = window.onload;
var window_onload = function () {;};

function getE(id) {
	return document.getElementById(id);
}
function getElHeight(el) {
	try{
		h = el.style.pixelHeight;
		if(!isDefined(h) || h < 1)
			h = el.offsetHeight;
		if(!isDefined(h) || h < 1)
			h = el.clientHeight;
	} catch(e) {
		h = el.offsetHeight;
	}
	return h;
}
function getElWidth(el) {
	try{
		w = el.style.pixelWidth;
		if(!isDefined(w) || w < 1)
			w = el.offsetWidth;
		if(!isDefined(w) || w < 1)
			w = el.clientWidth;
	} catch(e) {
		w = el.offsetWidth;
	}
	return w;
}

function getElY(obj) {
	var y = 0;
	while (obj) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return(y);
}

function getElX(obj) {
	var x = 0;
	while (obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return(x);
}
function getMouseXY(e) {
	if (!e) {
		var e = window.event;
	}
	x = e.clientX;
	y = e.clientY;

	return {'x':x,'y':y};
}
function returnfalse(e) { return false; }
function returntrue() { return true; }
function isIe() {
	if(navigator.appVersion.indexOf("MSIE")!=-1) { 
		return true;
	}
	return false;
}
var textsel_enabled = true;
function disableTextSel()
	{
		textsel_enabled = false;

		document.onselectstart=returnfalse;
		if (window.sidebar)
			{
				document.onmousedown=returnfalse;
				document.onclick=returntrue;
			}
	}
function enableTextSel()
	{
		textsel_enabled = true;

		document.onselectstart=null;
		if (window.sidebar)
			{
				document.onmousedown=null;
				document.onclick=null;
			}
	}
function noPx(v)
	{
		if(v) {
			return Math.round(v.substr(0, v.length-2));
		} else {
			return false;
		}
	}
function freshStyle(p)
	{
		if(isIe()) {
			var content = p.innerHTML;
			if(content != '') {
				p.innerHTML = '';
				setTimeout( function () {
					p.innerHTML = content; 					
					var elements = new Array ();
					try {
						var children = p.getElementsByTagName( "*" );
				
						for ( var i = 0; i < children.length; i++ ) {
							children[i].className = children[i].className;
						}
					} catch (err) { alert(err); } ;
				} , 100 );
			}
		}
	}
function getStyle( element, cssRule ) {
	if( document.defaultView && document.defaultView.getComputedStyle ) {
		var value = document.defaultView.getComputedStyle( element, '' ).getPropertyValue( cssRule );
	}
	else if ( element.currentStyle ) var value = element.currentStyle[ cssRule ];
	else                             var value = false;
	return value;
}

var loadNewJs_ray = new Array;
function loadNewJs(uri) {
	ajaxAdd( function () {
		ajaxGet(uri, function () {
			try {
				eval(ajaxLastResult_raw);
			} catch (err) { errorShow('::geral.inc.js::loadNewJs(String uri = '+uri+')::Err='+err); } ;
		} ) ;
	} );
}

var loadNewCSS_ray = new Array;
function loadNewCSS(uri)
	{
		if(loadNewCSS_ray[uri] == true)
			return false;
		
		loadNewCSS_ray[uri] = true;
		var css = document.createElement('link');
		css.type = 'text/css';
		css.rel = 'stylesheet';
		css.href = uri;
		document.getElementsByTagName('head')[0].appendChild(css);  
		
		return true;
	}

function fixCssLayout(e)
	{
		fixCssHeight();	

		try{ old_cssjs_onload(e); }catch(e){;}
		try{ window_onload(e); }catch(e){;}
		try{ fixImgSpacing(); }catch(e){;}
		try{ fixLeftMarginHiding(); }catch(e){;}
		try{ window.onresize=fixLeftMarginHiding; }catch(e){;}
	}

var fixcss_h_ray = new Array;
var fixcss_minh_ray = new Array;
var fixcss_h_ray_m = new Array;
var fixcss_flashloader = new Array;

function fixCssHeight()
	{
		// min height
		for(i in fixcss_minh_ray)
			{
				getE(i).style.height = null;
				
				if( getElHeight(getE(i)) < fixcss_minh_ray[i] )
					getE(i).style.height = fixcss_minh_ray[i] + 'px';
			}
		// flash loader
		for(i in fixcss_flashloader)
			{
				getE(i).innerHTML = getE(fixcss_flashloader[i]).innerHTML;
			}
			
		// inherit height fix -- !!! always last
		for(i in fixcss_h_ray)
			{
				if(getElHeight(getE(i)) <= getElHeight(getE(fixcss_h_ray[i])))
					getE(i).style.height = getElHeight(getE(fixcss_h_ray[i])) + 'px';
			}		
		// inherit height fix -- !!! always last
		for(i in fixcss_h_ray_m)
			{
				getE(i).style.height = (getElHeight(getE(fixcss_h_ray_m[i][0])) + fixcss_h_ray_m[i][1]) + 'px';
			}
	}

function fixImgSpacing () {
	if(navigator.appName != 'Microsoft Internet Explorer') {						
		var here_imgs = document.getElementsByTagName('body').item(0).getElementsByTagName('img');
		for(i=0;i<here_imgs.length;i++)
			{
				try {
					here_imgs[i].style.marginLeft = here_imgs[i].getAttribute('hspace') + 'pt';
					here_imgs[i].style.marginRight = here_imgs[i].getAttribute('hspace') + 'pt';
				} catch(e) {}
				try {
					here_imgs[i].style.marginTop = here_imgs[i].getAttribute('vspace') + 'pt';
					here_imgs[i].style.marginBottom = here_imgs[i].getAttribute('vspace') + 'pt';
				} catch(e) {}
			}
	}
}

function fixImgMargin (id) {
	if(navigator.appName != 'Microsoft Internet Explorer') {						
		var here_imgs = document.getElementById(id).getElementsByTagName('img');
		for(i=0;i<here_imgs.length;i++)
			{
				try {
					here_imgs[i].style.marginLeft = here_imgs[i].getAttribute('hspace') + 'pt';
					here_imgs[i].style.marginRight = here_imgs[i].getAttribute('hspace') + 'pt';
				} catch(e) {}
				try {
					here_imgs[i].style.marginTop = here_imgs[i].getAttribute('vspace') + 'pt';
					here_imgs[i].style.marginBottom = here_imgs[i].getAttribute('vspace') + 'pt';
				} catch(e) {}
			}
	}
}

function getWinSize(winObj) { // retorna o tamanho da janela argumento
	var myWidth = 0, myHeight = 0;
	if( typeof( winObj.innerWidth ) == 'number' ) {
		myWidth = winObj.innerWidth;
		myHeight = winObj.innerHeight;
	} else if( winObj.document.documentElement && ( winObj.document.documentElement.clientWidth || winObj.document.documentElement.clientHeight ) ) {
		myWidth = winObj.document.documentElement.clientWidth;
		myHeight = winObj.document.documentElement.clientHeight;
	} else if( winObj.document.body && ( winObj.document.body.clientWidth || winObj.document.body.clientHeight ) ) {
		myWidth = winObj.document.body.clientWidth;
		myHeight = winObj.document.body.clientHeight;
	}
	return {'w':myWidth,'h':myHeight};
}

// FIX BROWSER WINDOW HIDING
function fixLeftMarginHiding() {
	if(getE('Container').style.marginLeft && getE('Container').style.width) {
		var iwidth = noPx(getE('Container').style.marginLeft);
		var imargin = noPx(getE('Container').style.width);
	} else {
		var iwidth = noPx(getStyle(getE('Container'), 'width'));
		if(isIe()) {
			var imargin = noPx(getStyle(getE('Container'), 'marginLeft'));
		} else {
			var imargin = noPx(getStyle(getE('Container'), 'margin-left'));
		}
	}
	
	if(iwidth > getWinSize(window).w ) {
		if(imargin < 0) {
			getE('Container').style.marginLeft = 0;
			getE('Container').style.left = 0;
		}
	}
	else {
		if(noPx(getE('Container').style.leftMargin) == 0) {
			getE('Container').style.marginLeft = '-' + Math.round(iwidth / 2) + 'px';
			getE('Container').style.left = '50%';
		}
	}
}

// FIX IMG HEIGHT MIDDLE
function fix_valign_middle(obj) {
	
	var oh = getElHeight(obj);
	var ph = getElHeight(obj.parentNode);
	
	if(ph <= oh) { 
		nh = getElHeight(obj.parentNode.parentNode);
		
		if(oh <= nh)
			ph = nh;
	}
	
	obj.style.marginTop = ((ph-oh)/2) + 'px';
}
