var doc			= null;
var req;

function loadXMLDoc(url)
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange()
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			doc  = req.responseXML.documentElement;
			var notWhitespace	= /\S/;
			var childNode		= null;

			//czyścimy białe znaki

			for ( var x = 0; x < doc.childNodes.length; x++ ) {
				childNode = doc.childNodes[ x ];
				if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
					// that is, if it's a whitespace text node
					doc.removeChild( doc.childNodes[ x ] );
					x--
				}
				else {
					for ( var x2 = 0; x2 < doc.childNodes[ x ].childNodes.length; x2++ ) {
						childNode = doc.childNodes[ x ].childNodes[ x2 ];
						if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
							// that is, if it's a whitespace text node
							doc.childNodes[ x ].removeChild( doc.childNodes[ x ].childNodes[ x2 ] );
							x2--
						}
						else {
							for ( var x3 = 0; x3 < doc.childNodes[ x ].childNodes[ x2 ].childNodes.length; x3++ ) {
								childNode = doc.childNodes[ x ].childNodes[ x2 ].childNodes[ x3 ];
								if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
									// that is, if it's a whitespace text node
									doc.childNodes[ x ].childNodes[ x2 ].removeChild( doc.childNodes[ x ].childNodes[ x2 ].childNodes[ x3 ] );
									x3--
								}
							}
						}
					}
				}
			}

			onXMLLoadDone();
		}
		else {
			//alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}

}

var type = 0;

function SetAnalizy( new_type ) {
	type = new_type;
	
	loadXMLDoc( "/poland/homepage/polish/files/analizy_files.xml?" + Math.random() );
}

function SetAnalizyPress(  ) {
	SetAnalizy( 2 );
}


function onXMLLoadDone() {
	var url = '', date = '';
	var nodeFile = null;

	switch( type ) {
		case 0: //main page
			//ostatni rok, ostatni miesiąc, ostatni plik
			nodeFile = doc.lastChild.lastChild.lastChild;
			if ( nodeFile.childNodes[ 0 ].textContent ) {
				url		= nodeFile.childNodes[ 2 ].textContent;
				date	= nodeFile.childNodes[ 0 ].textContent;
			}
			else {
				url		= nodeFile.childNodes[ 2 ].text;
				date	= nodeFile.childNodes[ 0 ].text;
			}
			document.getElementById( 'analizy_link1' ).href = url;
			document.getElementById( 'analizy_link2' ).href = url;
			//document.getElementById( 'analizy_link3' ).href = url;
			document.getElementById( 'analizy_date' ).innerHTML = '<br/>'+ date;
			break;
		case 1: //Analizy makroekonomiczne
			var year_tmpl = '<div class="mod_bg_" style="margin-top:0px"><div class="mod_left_"><span class="mod_header">year</span></div><div class="mod_right_"></div></div>';
			//var month_tmpl = '<div class="header" style="display:block; height:18px;"><div style="float:left; display:inline;">month</div><div style="float: right; display: inline;"><a id="div_id_txt" class="karty_rozwin_txt" href="javascript:sh(\'div_id\');">zwiń</a></div></div>'
			var file_tmpl = '<li style="list-style-image:url(/poland/homepage/polish/images/li_c_b.gif) !important; list-style-position: outside !important; margin-left:20px !important;"><a href="url_a" target="_blank">name</a></li>';
			var str = '';

			for ( var x = (doc.childNodes.length - 1) ; x >= 0 ; x-- ) { //years
				yearNode = doc.childNodes[ x ];
				str += year_tmpl.replace( "year", yearNode.attributes[ 0 ].value );				
				for ( var x2 = ( yearNode.childNodes.length - 1 ); x2 >= 0; x2-- ) { //miesąc
					monthNode = yearNode.childNodes[ x2 ];
					var div_id = "block" +x + "_" + x2;					
					if (x2 == ( yearNode.childNodes.length - 1 )&&(x == (doc.childNodes.length - 1))) {var month_tmpl = '<div class="header" style="display:block; height:18px;"><div style="float:left; display:inline;">month</div><div style="float: right; display: inline;"><a id="div_id_txt" class="karty_zwin_txt" href="javascript:sh(\'div_id\');">zwiń</a></div></div>'}
					else {var month_tmpl = '<div class="header" style="display:block; height:18px;"><div style="float:left; display:inline;">month</div><div style="float: right; display: inline;"><a id="div_id_txt" class="karty_rozwin_txt" href="javascript:sh(\'div_id\');">rozwiń</a></div></div>'}
					str += month_tmpl.replace( "month", monthNode.attributes[ 0 ].value ).replace( /div_id/g, div_id );

					if (x2 == ( yearNode.childNodes.length - 1 )&&(x == (doc.childNodes.length - 1))) {str += '<div style="display:block;" id="'+ div_id +'"><ul class="zobacz_li">';}
						else {str += '<div style="display:none;" id="'+ div_id +'"><ul class="zobacz_li">';};

					for ( var x3 = ( monthNode.childNodes.length - 1 ); x3 >= 0 ; x3-- ) { //files
						fileNode = monthNode.childNodes[ x3 ];
						var href = '', name = '';
						if ( fileNode.childNodes[ 0 ].textContent ) {
							href	= fileNode.childNodes[ 2 ].textContent;
							name	= fileNode.childNodes[ 1 ].textContent;
						}
						else {
							href	= fileNode.childNodes[ 2 ].text;
							name	= fileNode.childNodes[ 1 ].text;
						}
						str += file_tmpl.replace( "name", name ).replace( "url_a", href );
					}
					
					str += '</ul></div><br />';
				}

			}
			
			document.getElementById( 'div_files' ).innerHTML = str;
			break;
		case 2: //main page
			//odpalamy orginalną funkcję dla onload
			initFilter();

			//ostatni rok, ostatni miesiąc, ostatni plik
			nodeFile = doc.lastChild.lastChild.lastChild;
			if ( nodeFile.childNodes[ 0 ].textContent ) {
				url		= nodeFile.childNodes[ 2 ].textContent;
				date	= nodeFile.childNodes[ 0 ].textContent;
			}
			else {
				url		= nodeFile.childNodes[ 2 ].text;
				date	= nodeFile.childNodes[ 0 ].text;
			}
			var tbody = document.getElementById( 'elemContent' );
			//tbody.children[ 0 ].children[ 0 ].;
			tbody.children[ 0 ].children[ 0 ].children[ 0 ].innerHTML = date;
			tbody.children[ 0 ].children[ 0 ].children[ 2 ].href = url;
			tbody.children[ 0 ].children[ 0 ].children[ 5 ].children[ 0 ].href = url;
  
			break;
	}
}

function sh(divek) {
	txt =document.getElementById(divek+"_txt");
	div=document.getElementById(divek);
	if(div.style.display=='block') {
		div.style.display='none';
		if(txt) {
			txt.innerHTML="rozwiń";
			txt.className="karty_rozwin_txt";
		}
	}
	else {
		div.style.display='block';
		if(txt)	{
			txt.innerHTML="zwiń";
			txt.className="karty_zwin_txt";
		}
	}
}

