var goto_table=null;
var main_page_array=new Array();

function set_html(uri)
{
	var i;
	for (i=0;i<main_page_array.length;i++) if (main_page_array[i].uri == uri) {
		document.getElementById('innerdiv').innerHTML = main_page_array[i].content;
		pager_remake_el(document.getElementById('innerdiv'));
		forms_remake();
      		init_menu('umenu',uri);
		document.body.scrollTop=0;
		return;
	}
	workframe.location.href='http://'+window.location.host+"/embedjs/"+uri;
}


function pager_remake_image_title(el)
{
	var v;
	if (el.nodeName.toUpperCase()=="IMG") {
		if (el.hasAttribute('title')) {
			v=el.getAttribute('title');
		}
		else v='';
		if (v=='') {
			if (el.hasAttribute('alt')) v=el.getAttribute('alt');
		}
		if (v != '') v+=' - ';
		v+='Kliknij aby obejrze\u0107 powi\u0119kszenie'
		el.setAttribute('title',v);
	}
	for (el=el.firstChild;el;el=el.nextSibling) pager_remake_image_title(el);
}

function pager_remake_el(el)
{
	var fl,cs,cl;
	if (el.nodeName.toUpperCase() == 'A') {
		cs=el.href;
		//cl=el.getAttribute('class');
		cl=el.className;
		if (cs) {
			var n,f;
			if (cl=='pop') {
				f='openpop'
				//pager_remake_image_title(el);
			}
			else {
				n=cs.lastIndexOf('/');
				cs=cs.substr(n+1);
				f='set_html';
			}
			n=cs.lastIndexOf('.');
			/*if (cs.substr(n+1)=='html') el.href="javascript:"+f+"('"+cs+"')";
			else */if (cs.substr(n+1)=='jpg' || cs.substr(n+1)=='png' || cs.substr(n+1)=='gif') {
				el.href="javascript:showimage('"+el.href+"')";
				if (el.target && el.target !='_self') el.target="";
			}
		}
	}
	for (fl=el.firstChild;fl;fl=fl.nextSibling) pager_remake_el(fl);
	
}
		
function pager_remake()
{
	pager_remake_el(document.body);
}


var popup_window,popup_image;

function openpop(a)
{
	if (popup_window) popup_window.close();
	popup_window=window.open(a,'POPUP_WINDOW','width=750,height=375,scrollbars=no');
}

function showimage(a)
{
	if (popup_window) popup_window.close();
	popup_image=a;
	popup_window=window.open("/popup_image.html",'POPUP_WINDOW','width=100,height=100');
}

function closepop()
{
	if (popup_window) popup_window.close();
}

function write_workframe()
{
	document.writeln('<input type="hidden" id="___srz_setia__stat_id">');
//	document.writeln('<iframe name="workframe" src="/workframe/dummy.html" scrolling=no width=1 height=1 style="visibility:hidden"></iframe>');
}

function init()
{
    
    if (!document.getElementById) return;
      var inn = document.getElementById('innerdiv');
      if (inn) {
      	var uri=window.location.pathname;
	if (uri == '/') uri="index.html";
	else uri=uri.substr(1);
	main_page_array[0]={uri:uri,content:inn.innerHTML};   	
	}
      equalizer('artcontainer');
      equalizer('brtcontainer');
      append_umenu('umenu');
      init_menu('umenu',window.location.pathname);
      pager_remake();
      forms_remake();
      dates_remake('datepart');
      remake_menusearch();
      if (window.opera) hack_opera();
      ELIB_replace_prices("przelicznik",this_comma,this_currency,alt_currency);
      add_findbyid();
      add_gotoopt();
      return;
}

function hack_opera()
{
	/* bug in Opera rendering engine */
	
	function remove_opera_spans(elem)
	{
		try {
			var li=elem.getElementsByTagName('span');
			var i;
			if (li) for (i=li.length-1;i>=0;i++) li.item(i).parentNode.removeChild(li.item(i));
		} catch(e){};
	}
	
	var el=document.getElementById('innerdiv');
	var ls=el.getElementsByTagName('ul');
	var i;
	for (i=0;i<ls.length;i++) if (ls.item(i).className == 'traceback') {
		remove_opera_spans(ls.item(i));
	}
}

function forms_remake()
{
	for (i=0;i<document.forms.length;i++) {
		if (document.forms[i]['statid']) continue;
		var el=document.createElement('input');
		el.setAttribute('type','hidden');
		el.setAttribute('name','statid');
		el.setAttribute('value',document.getElementById('___srz_setia__stat_id').value);
		document.forms[i].appendChild(el);
	}

}

function hide_all_ul(a)
{
	var b,s;
	for (b=a.firstChild;b;b=b.nextSibling) hide_all_ul(b);
	if (a.nodeName.toLowerCase()=='ul') a.style.display="none";
}

function open_first_ul(li)
{
	if (li.nodeName.toLowerCase()=='ul') {
		li.style.display="block";
		return;
	}
	for (li=li.firstChild;li;li=li.nextSibling) open_first_ul(li);
}

function find_li(a,loc,ela)
{
	var nm=a.nodeName.toLowerCase();
	if (nm == 'li') ela.value=a;
	if (nm == 'a') {
		var s=a.href;
		if (s.indexOf("set_html('")>=0) {
			s=s.substr(s.indexOf("'")+1);
			s=s.substr(0,s.indexOf("'"));
		}
		else {
			var n;
			n=s.lastIndexOf('/');
			if (n>=0) s=s.substr(n+1);
		}
		if (s.indexOf(loc)==0) return true;
		return false;
	}
	
	for (a=a.firstChild;a;a=a.nextSibling) if (find_li(a,loc,ela)) return true;
	return false;
}

function do_menu(a,loc)
{
	if (a.nodeName.toLowerCase() != 'li') return;
	hide_all_ul(a);
	var z={value:null};
	var li=find_li(a,loc,z);
	if (!li) return;
	li=z.value;
	var el;
	open_first_ul(li);
	for (li=li.parentNode;li;li=li.parentNode) {
		if (li.nodeName.toLowerCase()=="ul") {
			li.style.display="block";
		}
	}
}


function init_umenu(el,loc)
{
	var fl;
	var nr;
	nr=loc.lastIndexOf('/');
	if (nr>=0) loc=loc.substr(nr+1);
	if (loc == '') loc="index.html";
	for (fl=el.firstChild;fl;fl=fl.nextSibling)
		do_menu(fl,loc);
}
function init_menu(id,loc)
{
	var el=document.getElementById(id);
	if (!el) return;
	var fl=document.getElementById('referer_menu');
	if (fl) loc=fl.getAttribute('name');
	if (el.nodeName.toLowerCase() != 'ul') return;
	init_umenu(el,loc)
}

function create_umenu_ul(arr)
{
	var el=document.createElement('ul');
	var i,fl,a;
	for (i=0;i<arr.length;i++) {
		fl=document.createElement('li');
		if (arr[i].sub) fl.className='umenusub';
		a=document.createElement('a');
		a.setAttribute('href',arr[i].url);
		a.appendChild(document.createTextNode(arr[i].name));
		fl.appendChild(a);
		if (arr[i].sub) fl.appendChild(create_umenu_ul(arr[i].sub));
		el.appendChild(fl);
	}
	return el;
	
}

function append_umenu_item(el,arr)
{
	if (!arr) return;
	el.appendChild(create_umenu_ul(arr));
}

function append_umenu(id)
{
	var el=document.getElementById(id);
	if (!el) return;
	var i;
	for (el=el.firstChild;el;el=el.nextSibling) {
		var fl,s='',n;
		for (fl=el.firstChild;fl;fl=fl.nextSibling) if (fl.nodeName.toLowerCase()=='a') {
			s=fl.href;
			break;
		}
		if (s=='') continue;
		n=s.lastIndexOf('/');
		if (n>=0) s=s.substr(n+1);
		for (i=0;i<categories_array.length;i++) if (categories_array[i].url==s) {
			append_umenu_item(el,categories_array[i].sub);
			break;
		}
	}
}

function gotourl(a)
{
	window.location=a
}

function weather(a)
{
	var w=window.open('http://www.wunderground.com/global/stations/'+a+'.html','POGODA');
	w.focus();
}

function current_height(el)
{

	var s;
	if (el.currentStyle) {
		return el.offsetHeight;
	} else if (document.defaultView && document.defaultView.getComputedStyle) {
		s = document.defaultView.getComputedStyle(el,'');
		return Math.ceil(parseFloat(s.height.replace(/[^0-9.]/g,'')));
	} else return null;
}

function equalizer(id)
{
	var div=document.getElementById(id);
	if (!div) return;
	var el=div.firstChild,fl;
	var h1,h2;
	for (;;) {
		while (el && el.nodeName.toLowerCase() != 'span') el=el.nextSibling;
		if (!el) break;
		fl=el.nextSibling;
		while (fl && fl.nodeName.toLowerCase() != 'span') fl=fl.nextSibling;
		if (!fl) break;
		h1=current_height(el.firstChild);
		if (!h1) break;
		h2=current_height(fl.firstChild);
		if (h1<h2) el.firstChild.style.height=h2+'px';
		if (h1>h2) fl.firstChild.style.height=h1+'px';
		el=fl.nextSibling;
	}
}

function remake_dates(classid,forma)
{
	var el=[];
	ELIB_getNamedElements(el,forma,classid);
	var i;
	for (i=0;i<el.length;i++) replace_datepart(el[i]);
}

function replace_datepart(element)
{
	var span=document.createElement('span');
	var id=element.getAttribute('id');
	var nam=element.getAttribute('name');
	
	var ihid=document.createElement('input');
	ihid.setAttribute('type','hidden');
	ihid.setAttribute('name',nam);
	ihid.setAttribute('value','');
	span.appendChild(ihid);
	var el1=document.createElement('select');
	var i;
	
	for (i=1;i<=31;i++) {
		var opt=document.createElement('option');
		opt.setAttribute('value',i);
		opt.appendChild(document.createTextNode(i));
		el1.appendChild(opt);
	}
	span.appendChild(el1);
	el1=document.createElement('select');
	for (i=1;i<=12;i++) {
		var opt=document.createElement('option');
		opt.setAttribute('value',i);
		opt.appendChild(document.createTextNode(i));
		el1.appendChild(opt);
	}
	span.appendChild(el1);
	el1=document.createElement('select');
	var d=new Date();
	var y=d.getFullYear();
	for (i=y;i<=y+1;i++) {
		var opt=document.createElement('option');
		opt.setAttribute('value',i);
		opt.appendChild(document.createTextNode(i));
		el1.appendChild(opt);
	}
	span.appendChild(el1);
	ihid=document.createElement('input');
	ihid.setAttribute('type','hidden');
	ihid.setAttribute('name','checkit');
	ihid.setAttribute('value',nam+':datecombo:');
	span.appendChild(ihid);
	element.parentNode.replaceChild(span,element);
}

function dates_remake(classid)
{
	var i;
	for (i=0;i<document.forms.length;i++) remake_dates(classid,document.forms[i]);
}

function menusearon()
{
	msear(true);
}

function menusearout()
{
	msear(false);
}

function msear(how)
{
	var div=document.getElementById('menusearch');
	div.style.color=how?'black':'#aa0000';
	for (div=div.firstChild;div;div=div.nextSibling) if (div.nodeName.toLowerCase()=='div') break;
	if (!div) return;
	div.style.left=how?'20px':'-2000px';

}

function remake_menusearch()
{
	var div=document.getElementById('menusearch');
	if (!div) return;
	if (div.nodeName.toLowerCase()!='div') return;
	div.onmouseover=menusearon;
	div.onmouseout=menusearout;
	var ls=div.getElementsByTagName('a');
	var i;
	for (i=0;i<ls.length;i++) {
		ls.item(i).onfocus=menusearon;
		ls.item(i).onblur=menusearout;
	}
}


/* find id */

/* xmlhttprequest */

var ajm_misio;

function xmlh()
{
	var xmlhttp=false;
	ajm_misio=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  ajm_misio=true;
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   ajm_misio=true;
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function add_findbyid()
{
	if (!xmlh()) return;
	var el=document.getElementById('elmenu');
	if (!el) return;
	var uls;
	uls=el.getElementsByTagName('ul');
	if (!uls || uls.length <1) return;
	var ul=uls.item(0);
	var pa=window.location.pathname;
	var lang='pl';
	if (pa.search(/^\/[a-z][a-z]\//)==0) lang=pa.substr(1,2);
	var que;
	switch(lang) {
		case 'pl': que='Szukaj wg. ID';break;
		case 'no': que='Søk med ID';break;
		case 'en': que='Search by ID';break;
		default:return;
	}
	var li=document.createElement('li');
	var a=document.createElement('a');
	a.setAttribute('href','javascript:search_by_id()');
	a.appendChild(document.createTextNode(que));
	li.appendChild(a);
	ul.appendChild(li);
}

var http_request;
var this_path;

function sbi_end()
{
	try {
		if (http_request.readyState != 4) return;
		if (http_request.status==200) {
			window.location=this_path;
			return;
		}
		if (http_request.status == 404) {
			var lang='pl';
			var pa=window.location.pathname;
			if (pa.search(/^\/[a-z][a-z]\//)==0) lang=pa.substr(1,2);
			var que;
			switch(lang) {
				case 'pl': que='B\u0142\u0119dny ID';break;
				case 'no': que='Uriktig ID';break;
				default: que='Bad ID number';break;
			}
			alert(que);
			return;
		}
		alert("Server error: "+server.status);
	}
	catch(e) {
		alert(e);
	}
	

}

function search_by_id()
{
	var pa=window.location.pathname;
	var lang='pl';
	if (pa.search(/^\/[a-z][a-z]\//)==0) lang=pa.substr(1,2);
	var que;
	switch(lang) {
		case 'pl': que='Podaj ID nieruchomo\u015Bci';break;
		case 'en': que='Enter ID of immobility';break;
		case 'no': que='Skrive ID av eiendom';break;
		default: return;
	}
	var s=prompt(que,'');
	s=s.replace(/[^0-9]/g,'');
	if (!s) return;
	try {
		http_request=xmlh();
		if (!http_request) return;
		http_request.open("HEAD",this_path="/"+lang+"/id_"+s+".html");
		http_request.onreadystatechange=sbi_end;
		http_request.send(null);
	}
	catch(e) {
		alert(e);
		return;
	}
		
	
}

var goto_selektor;
function goto_func()
{
	var n=goto_selektor.selectedIndex;
	if (n<1) return;
	window.location=goto_table[n-1][0];
}
function add_gotoopt()
{
	if (!goto_table) return;
	if (goto_table.length<1) return;
	var idul=document.getElementById('wykul');
	if (!idul) return;
	var forma=document.createElement('form');
	var selekt=document.createElement('select');
	var optio=document.createElement('option');
	optio.appendChild(document.createTextNode(goto_name));
	selekt.appendChild(optio);
	var i;
	for (i=0;i<goto_table.length;i++) {
		optio=document.createElement('option');
		optio.appendChild(document.createTextNode(goto_table[i][1]));
		selekt.appendChild(optio);
	}
	forma.appendChild(selekt);
	idul.parentNode.insertBefore(forma,idul);
	selekt.onchange=goto_func;
	selekt.onclick=function() {
		setTimeout('goto_func()',200);
	}
	goto_selektor=selekt;
}

function chama()
{
	if (document.getElementById('pemail').checked) {
		document.getElementById('email').disabled=false;
		document.getElementById('chamali').style.display="block";
	}
	else {
		document.getElementById('email').disabled=true;
		document.getElementById('chamali').style.display="none";
	}
}

function chamb()
{
	var el=document.getElementById('skadsie');
	if (!el) return;
	var n=el.selectedIndex;if (n<0) n=0;
	if (n<el.options.length-2) {
		document.getElementById('chamalispa').style.display='none';
		document.getElementById('skad_szczeg').disabled=true;
	}
	else {
		document.getElementById('chamalispa').style.display='block';
		document.getElementById('skad_szczeg').disabled=false;
	}
}
