/* public.js */


/* _lib.js-ből kiemelve ottani függvény teljes másolata; Opera9 csak így volt hajlandó betölteni a függvényt */
/**
 * $ getViewport.js,v 1.5 2005/01/03 13:22:09 gyuris Exp $
 * @package  lib.getViewport
 * @author   Gyuris Gellért
 * @see      browserCheck
 */
/**
 * getViewport - visszaaja a tartalomban rendelkezésre álló terület a görgetősávok NÉLKÜL.
 * XXX - át kell alakítani MINDEN böngészőre egyenként és külön a css1compat és az backcompat||quirksmode
 * @return Object  Az adatokat tartalmazó értékek számmal.
 */
function getViewport() {
	var nWidth, nHeight, nScollTop, nScollHeight;
	
	if ( is.compatMode == 'css1compat' ) {
		
		if ( is.ie8 || is.ie7 || is.ie6 || is.geckoRv >= 1.5 || ( is.opera && is.appVer >= 7 ) ) {
			nWidth = document.documentElement.clientWidth;
			nHeight = document.documentElement.clientHeight;
			nScollTop = document.documentElement.scrollTop;
			nScollHeight = document.documentElement.scrollHeight;
		};
		if ( is.opera && is.appVer >= 7 ) {
			nHeight = window.innerHeight //document.documentElement.clientHeight;
		};
		if ( is.gecko ) {
			nWidth = ( document.body.scrollWidth >= document.body.clientWidth ) ? document.body.clientWidth : window.innerWidth;
			nHeight = ( document.body.scrollHeight >= document.body.clientHeight ) ? document.body.clientHeight : window.innerHeight;
			if ( is.geckoRv >= 1.7 ) {
				nWidth = ( document.documentElement.scrollWidth >= document.documentElement.clientWidth ) ? document.documentElement.clientWidth : window.innerWidth;
				nHeight = ( document.documentElement.scrollHeight >= document.documentElement.clientHeight ) ? document.documentElement.clientHeight : window.innerHeight;
			}
			nScollTop = window.scrollY;
			nScollHeight = document.documentElement.scrollHeight;
		};
	}
	else {
		if ( is.ie ) {
			nWidth = document.body.clientWidth;
			nHeight = document.body.clientHeight;
			nScollTop = document.body.scrollTop;
			nScollHeight = document.body.scrollHeight;
		}
		else if ( is.khtml ) {
			nWidth = window.innerWidth;
			nHeight = window.innerHeight;
		}
		else if ( is.bss ) {
			nWidth = ( document.body.scrollWidth >= document.body.clientWidth ) ? document.body.clientWidth : window.innerWidth;
			nHeight = ( document.body.scrollHeight >= document.body.clientHeight ) ? document.body.clientHeight : window.innerHeight;
			nScollTop = window.scrollY;
			nScollHeight = document.documentElement.scrollHeight;
		};
		if ( ( is.opera && is.appVer >= 7 ) || is.khtml ) {
			nScollTop = document.documentElement.scrollTop;
			nScollHeight = document.documentElement.scrollHeight;
		};
	};
	// kompatibilitás miatt
	window.strictInnerWidth   = nWidth;
	window.strictInnerHeight  = nHeight;
	window.strictScrollTop    = nScollTop;
	window.strictScrollHeight = nScollHeight;
	//alert(nWidth+' : '+nHeight+' : '+nScollTop+' : '+nScollHeight);
	return { width        : nWidth,
	         height       : nHeight,
	         scrollTop    : nScollTop,
	         scrollHeight : nScollHeight };
};


/* 3. szint, menükezelés */
var Menu = {
	sUrl: '',
	sMenuId: 'main-menu',
	closeAll: function() {
		var aSubLevel, i = 0;
		aSubLevel = document.getElementById( Menu.sMenuId ).getElementsByTagName( 'ul' );
		for ( i; i < aSubLevel.length; i++ ) {
			addClass( aSubLevel[i], 'hidden' );
		}
	},
	
	/* oldalbetöltéskor az url-t vizsgálva kinyitja és megjelöli az aktív menüpontot */
	checkSelf: function() {
		var elActMenuItem, i = 0;
		Menu.sUrl = document.location.href.substring( document.location.href.lastIndexOf('/') + 1, document.location.href.lastIndexOf('.') );
		elActMenuItem = document.getElementById( 'menu_' + Menu.sUrl );
		if ( !elActMenuItem ) {
			return false;
		}
		if ( getNextElement( elActMenuItem ) && getNextElement( elActMenuItem ).tagName == 'UL' ) {
			removeClass( getNextElement( elActMenuItem ), 'hidden' );
			
		} 
		while ( elActMenuItem && elActMenuItem.nodeName == 'A' ) {
			addClass( elActMenuItem, 'active' );
			removeClass( elActMenuItem.parentNode.parentNode, 'hidden' );
			elActMenuItem = getPreviousElement( elActMenuItem.parentNode.parentNode );
		}
	},
	
	openMenu: function( elA ) {
		Menu.closeAll();
		removeClass( getNextElement( elA ), 'hidden' );
	},
	
	/* lekezeli a menüpontokon az onclick eseményt; bezártat kinyitja, nyitottat bezárja */
	openClose: function( elA ) {
		if ( getNextElement( elA ) && hasClass( getNextElement( elA ), 'hidden' ) ) {
			Menu.openMenu( elA );
		}
		else if ( null == getNextElement( elA ) ) {
			return true;
		}
		else {
			addClass( getNextElement( elA ), 'hidden' );
		}
	},
	init: function() {
		if ( !document.getElementById( Menu.sMenuId ) ) {
			return false;
		}
		var aA, i = 0;
		Menu.closeAll();
		aA = document.getElementById( Menu.sMenuId ).getElementsByTagName( 'a' );
		for ( i = 0; i < aA.length; i++ ) {
			addEvent( aA[i], 'click', function( evt ) {
				evt = evt || window.event || '';
				if ( evt ) {
					elA = evt.target || evt.srcElement;
					Menu.openClose( elA );
					pageHeight.init(); /* SzJ 2008.06.12. menü lenyitáskor is újrapozícionálja a láblécet */
				}
			} );
		}
		Menu.checkSelf();
	}
}


var pageHeight = {
	nPlusHeight: 169 + 36, /* fölös magasság */
	init: function() {
		var elContent =	document.getElementById('pg-content'),
		    elLeft = document.getElementById('level-3-left'),
		    elRight = document.getElementById('level-3-right'),
			windowDim = getViewport();
		//alert(elContent.offsetHeight);
		if ( windowDim.height > elLeft.offsetHeight + pageHeight.nPlusHeight && windowDim.height > elRight.offsetHeight + pageHeight.nPlusHeight && windowDim.height > elContent.offsetHeight ) {
			elContent.style.height = windowDim.height - pageHeight.nPlusHeight + 'px';
			// rolunk magassag bug ie7 alatt fix
			// if(document.body.id=='rolunk') elContent.style.height = '1200px';
		}
		else if ( elLeft.offsetHeight > elRight.offsetHeight ) {
			if ( elLeft.offsetHeight + pageHeight.nPlusHeight > elContent.offsetHeight ) {
				elContent.style.height = elLeft.offsetHeight + 12 + 'px';
			} 
		}
		else {
			if ( elRight.offsetHeight + pageHeight.nPlusHeight > elContent.offsetHeight ) {
				elContent.style.height = elRight.offsetHeight + 12 + 'px';
			}
		}
	
	}
}

/* csak a 3. szintű oldalakon szabad meghívódnia */
addEvent( window, 'load', function() {
	Menu.init();
	pageHeight.init();
	
	addEvent( document.getElementById( 'font-size-smaller' ), 'click', function() {
		fontSizer(-1,'px');
		return false;
	} );
	addEvent( document.getElementById( 'font-size-bigger' ), 'click', function() {
		fontSizer(1,'px');
		return false;
	} );
	fontSizer(0,'px');
	
	/* popup indítása a megfelelő oldalakon */
	var actual_body = document.getElementsByTagName('body')[0];
	
	/*
	if (actual_body.id=='magan_gefi_online_kerelmek')
	{
		NewWindow('popup/xy.html', 'popup', 500, 300, 0);
	}
	*/
	
	
	
	
} );

addEvent( window, 'resize', function() {
	//setTimeout('pageHeight.init()', 5000);
	pageHeight.init();
} );


// popup ablak kezelése
var win = null;

// összeg számformázás

function NewWindow( myurl, myname, w, h, scroll ) {
	leftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	topPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+topPosition+',left='+leftPosition+',scrollbars='+scroll+',resizable=no';
	win = window.open(myurl, myname, settings);
}

	function FormatNumberBy3(num, decpoint, sep) {
	  // check for missing parameters and use defaults if so
	  if (arguments.length == 2) {
		sep = ",";
	  }
	  if (arguments.length == 1) {
		sep = ",";
		decpoint = ".";
	  }
	  // need a string for operations
	  num = num.toString();
	  // separate the whole number and the fraction if possible
	  a = num.split(decpoint);
	  x = a[0]; // decimal
	  y = a[1]; // fraction
	  z = "";
	
	
	  if (typeof(x) != "undefined") {
		// reverse the digits. regexp works from left to right.
		for (i=x.length-1;i>=0;i--)
		  z += x.charAt(i);
		// add seperators. but undo the trailing one, if there
		z = z.replace(/(\d{3})/g, "$1" + sep);
		if (z.slice(-sep.length) == sep)
		  z = z.slice(0, -sep.length);
		x = "";
		// reverse again to get back the number
		for (i=z.length-1;i>=0;i--)
		  x += z.charAt(i);
		// add the fraction back in, if it was there
		if (typeof(y) != "undefined" && y.length > 0)
		  x += decpoint + y;
	  }
	  return x;
	}

	
function format_number(sender) {
	sender.setAttribute('maxlength', 20);
	var temp = parseInt(sender.value.replace(/\./g,''));
	//console.log(temp);
	if (isNaN(temp)) {
		sender.value='';
		return false;
	}
	sender.value = FormatNumberBy3(temp,',','.');
}