///* auto image sizer */

function ELIB_init_images()
{
	var ELIB_request;
	var ELIB_image_sizes;
	var ELIB_popup_window;
	
	function xmlh()
	{
		var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		return xmlhttp;
	}


	
	function ELIB_remake_images()
	{
		try {
			if (ELIB_request.readyState != 4) return;
			eval(ELIB_request.responseText);
			var lista=document.body.getElementsByTagName('a');
			for (i=0;i<lista.length;i++) {
				if (!lista[i].onclick && lista[i].href.match(/\.(jpe?g|png|gif)$/i)) {
					if (ELIB_image_sizes[lista[i].href]) lista[i].onclick=ELIB_click;
				}
			}
			lista=document.body.getElementsByTagName('area');
			for (i=0;i<lista.length;i++) {
				if (!lista[i].onclick && lista[i].href.match(/\.(jpe?g|png|gif)$/i)) {
					if (ELIB_image_sizes[lista[i].href]) lista[i].onclick=ELIB_click;
				}
			}
		}
		catch(e) { 
		}
	}
	
	function ELIB_click()
	{
		try {
			var w=0,h=0;
			var s=ELIB_image_sizes[this.href];
			if (s) {
				w=s.w+30;
				h=s.h+30;
			}
			if (ELIB_popup_window && ELIB_popup_window.close && !ELIB_popup_window.closed) {
				ELIB_popup_window.close();
			}
			
			var misio=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	misio=true;
@end @*/
			var str="dependent=yes,toolbar=no,resizable=yes";
			if (w && h) {
				var sb=0;
				if (w>screen.availWidth) {w=screen.availWidth;sb=1;}
				if (h>screen.availHeight) {h=screen.availHeight;sb=1;}
				str+=",width="+w+",height="+h;
				if (sb) str+=",scrollbars=yes";
				var x,y;
				x=Math.floor((screen.availWidth-w)/2);
				y=Math.floor((screen.availHeight-h)/2);
				if (x>20) {
					str+=(misio?',left=':',screenX=')+x;
				}
				if (y>20) {
					str+=(misio?',top=':',screenY=')+y;
				}
			}
			ELIB_popup_window=window.open(this.href,"_blank",str);
			if (!ELIB_popup_window) return true;
			var doc=ELIB_popup_window.document;
			doc.open();
			var alt="",title="";
			if (this.alt) alt=this.alt;
			if (this.title) alt=this.title;
			doc.write(
	  			'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
				'<html id="popup">' +
				'<head><head><title>' + title + '</title>' +
				'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
				'<style type="text/css">' +
				'body,html {margin:0px;padding:0px}\n'+
				'p {text-align:center;margin:0px;padding:15px 0 0 0}\n'+
				'img {display:inline}\n'+
				'</style>'+
				'</head><body><p><img src="'+this.href+'" alt="'+alt+'"></p>' +
				'<script type="text/javascript"> document.body.onmouseup = function(e) {var ev = e||event; if ((ev.button||ev.which) < 2) self.close();} ' +
				'</script></body></html>'
				);
			doc.close();
			if (misio) event.returnValue=false;
			return false;
		}
		catch(e) {
			return true;
		}
	}
	try {
		var arr=new Array();
		var i,j,str;
		if (!document.body.getElementsByTagName) return;
		var lista=document.body.getElementsByTagName('a');
		for (i=0;i<lista.length;i++) {
			if (!lista[i].onclick && lista[i].href.match(/\.(jpe?g|png|gif)/i)) {
				for (j=0;j<arr.length;j++) if (arr[j]==lista[i].href) break;
				if (j>=arr.length) arr[j]=lista[i].href;
			}
		}
		lista=document.body.getElementsByTagName('area');
		for (i=0;i<lista.length;i++) {
			if (!lista[i].onclick && lista[i].href.match(/\.(jpe?g|png|gif)/i)) {
				for (j=0;j<arr.length;j++) if (arr[j]==lista[i].href) break;
				if (j>=arr.length) arr[j]=lista[i].href;
			}
		}
		if (!arr.length) return;
		if (!(ELIB_request=xmlh())) return;
		for (i=0;i<arr.length;i++) {
			if (i) str+='&url=';else str='url=';
			try {
				str+=encodeURIComponent(arr[i]);
			}
			catch(e) {
				str+=escape(arr[i]);
			}
		}
		ELIB_request.open("POST","/imgsizer");
		ELIB_request.onreadystatechange=ELIB_remake_images;
		ELIB_request.send(str);
	}
	catch(e) {
	}
}


//
var enable_popup=false;

var last_mode;

function make_tx_link(txt,ref,title)
{
	var str='<a href=\"'+ref+'" class="menulink">'+txt+'</a>';
	return str;
}

function make_ul_menu(tabela,tt,divid,lpos,toexist)
{
	var i,dv,str;
	str='';
	dv=document.getElementById(divid);
	if (!dv) {
		dv=document.createElement('div');
		dv.setAttribute('id',divid);
	}
	else {
		while (dv.firstChild) dv.removeChild(dv.firstChild);
	}
	for (i=0;i<tabela.length;i++) {
		if (last_mode) {
			if ((tabela[i][lpos] & last_mode) == 0) continue;
		}
		str+=make_tx_link(tabela[i][0],tabela[i][1],tt?tabela[i][2]:false);
	}
	dv.innerHTML=str;
	return dv;
}


function make_side_menu()
{
	var el=document.getElementById('noscript_div');
	if (el) el.parentNode.removeChild(el);
	
	var fl;
	
	el=document.createElement('div');
	el.setAttribute('id','left_div');
	
		
	fl=document.createElement('div');
	fl.setAttribute('id','produ_oberdiv');
	fl.appendChild(make_ul_menu(producenci,false,'produ_div',4));
	el.appendChild(fl);

	fl=document.createElement('div');
	fl.setAttribute('id','nowosci_oberdiv');
	fl.appendChild(make_ul_menu(nowosci,true,'nowosci_div',3));
	el.appendChild(fl);
	
	var td=document.getElementById('div_oberinner');
	td.parentNode.insertBefore(el,td);

	el=document.createElement('div');
	el.setAttribute('id','right_div');

	fl=document.createElement('div');
	fl.setAttribute('id','asort_oberdiv');
	fl.appendChild(make_ul_menu(asortyment,false,'asort_div',4));
	el.appendChild(fl);
	
	
	fl=document.createElement('div');
	fl.setAttribute('id','promocje_oberdiv');
	fl.appendChild(make_ul_menu(promocje,false,'promocje_div',3));
	el.appendChild(fl);

	if (td.nextChild) td.parentNode.insertBefore(el,td.nextChild);
	else td.parentNode.appendChild(el);
}

function remake_dom_menu()
{
	var this_mode;
	var el=document.getElementById('hifi_mode');
	if (el) this_mode=el.getAttribute('name');
	else this_mode='klasa';
	if (this_mode.indexOf('hifi')==0) this_mode=1;else
	if (this_mode.indexOf('kino')==0) this_mode=2;else this_mode=false;
	if (last_mode == this_mode) return;
	last_mode=this_mode;
	make_ul_menu(asortyment,false,'asort_div',4,true);
	make_ul_menu(producenci,false,'produ_div',4,true);
	make_ul_menu(nowosci,true,'nowosci_div',3,true);
	make_ul_menu(promocje,true,'promocje_div',3,true);
}

function init()
{
	if (!document.getElementById) return;
	var el=document.getElementById('hifi_mode');
	var this_mode;
	if (el) this_mode=el.getAttribute('name');
	else this_mode='klasa';
	last_mode=false;
	if (this_mode.indexOf('hifi')==0) last_mode=1;else
	if (this_mode.indexOf('kino')==0) last_mode=2;
	make_side_menu();
	pager_remake_el(document.body);
	ELIB_init_images();
	if (typeof(enable_popup) != "undefined" && enable_popup && document.cookie.indexOf("trampop")<0) {
                document.cookie="xtrampop=vide; PATH=/";
                 window.open("/pop_up.shtml","AUTO_POPUP","width=512,height=384,resizable");
        }

	var el=document.getElementById('searchform');
	if (el) {
		el.target="workframe";
		el.action="poszukaj.zhtml";
	}
	
	el=document.getElementById('search_ref');
	if (el) el.href="javascript:search_engine()";
	/*
	var el=document.createElement('iframe');
	el.setAttribute('src','/workframe/dummy.html');
	el.setAttribute('name','workframe');
	el.setAttribute('style','visibility:hidden;width:1px;height:1px');
	document.body.appendChild(el);
	*/
	var inn=document.getElementById('divinner');
      	var uri=window.location.pathname;
	if (uri == '/') uri="index.html";
	else uri=uri.substr(1);
	main_page_array[0]={uri:uri,content:inn.innerHTML};   
	on_resize();
	
	//window.captureEvents(Event.RESIZE); 
 	window.onresize=on_resize;
}


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('divinner').innerHTML = main_page_array[i].content;
		pager_remake_el(document.getElementById('divinner'));
		var el=document.getElementById('searchform');
		if (el) {
			el.target="workframe";
			el.action="poszukaj.zhtml";
		}

		remake_dom_menu();
		window.scrollTo(0,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');
		if (cs) {
			var n,f,i;
			i=el.href.indexOf("/szukaj.zhtml");
			if (i>=0) {
				el.href=cs.substr(0,i+1)+'po'+cs.substr(i+1);
				el.target="workframe";
				return;
			}
			i=el.href.indexOf("/poszukaj.zhtml");
			if (i>=0) {
				el.target="workframe";
				return;
			}
			n=cs.lastIndexOf('/');
			cs=cs.substr(n+1);
			n=cs.lastIndexOf('.');
			f='set_html';
			if (cl=='pop') {
				f='openpop'
				pager_remake_image_title(el);
			}
			if (cs.substr(n+1)=='html') el.href="javascript:"+f+"('"+cs+"')";
			/*else if (cs.substr(n+1)=='jpg') el.href="javascript:showimage('"+el.href+"')";*/
		}
	}
	for (fl=el.firstChild;fl;fl=fl.nextSibling) pager_remake_el(fl);
	
}
		
function write_workframe()
{
	if (!document.getElementById) return;
	document.writeln('<iframe name="workframe" src="/workframe/dummy.html" scrolling=no width=1 height=1 style="visibility:hidden"></iframe>');
}

function on_resize()
{
	var di=document.getElementById('ober_div');
	var w=document.body.clientWidth;
	if (window.innerWidth) w=window.innerWidth;
	w=Math.round((w-724)/2-30);if (w<0) w=0;
	di.style.marginLeft=w+'px';
	

}

var search_window;
var last_word;
function search_engine()
{
	workframe.location.href="poszukaj.zhtml";
	return;
	if (search_window) search_window.close();
	if (!last_word) last_word='';
	var q=prompt("Podaj szukane słowo",last_word);
	if (!q) return;
	last_word=q;
	search_window=window.open('/dtse/search.cgi?q='+escape(q),'TOPA_SZUKAJ','width=420,height=400,scrollbars=yes,resizable=yes');
}

