var _i=0;
var _imgAll=0;
var _imgLoaded=0;
var _isRunningSlideshow = false;

function initMenu(e,b){
	if(!_d||!(b=$('menu')))return;
	b.onclick=collapseExpandWithAjax;
	b.onmouseover=showInsideTooltipInit
	expandCurrent();
}
function initOnLoad(e){
	initMenu(e)
	doContact();
	addToBottomButtons();
	$('lookButtons').onclick=reactToLookButtons
}
var info = '';
function expandCurrent(l,k,p,r){
	l = $('menu').getElementsByTagName('a'); 
	r = (window.location.hash ? new RegExp( window.location.hash.substring(1) + '$' ) : '')
	if(r) for(var i=0; i<l.length;i++) if( r.test( l[i].href) ){ r=l[i]; break; }
	if( !r.nodeName ) {
		p = new RegExp( window.location.pathname + '$' )
		for(var i=0; i<l.length;i++) if( p.test( l[i].href)||window.location.pathname.length==1  ){ l=l[i]; break; }
	}
	else {
		l = r;
		openIframe(0,l)
	}
	if( !/^a$/i.test(l.nodeName) || $('selected') ) return;
	markSelected(l)
}

function markSelected(l,p){
	l.id='selected';
	collapseExpand(l[_pN])
	k=l;
	p=0
	while(l.id!='menu'&&(l=l[_pN])){
		p+=l.offsetTop;
		if( /^ul/i.test(l.nodeName )  ){ 
			l.className='expanded'
		}
		else if( /^li/i.test(l.nodeName ) && /colla/i.test(l.firstChild[_c]) ) {
			l.firstChild[_c]='expanded'
		}
	}
	p=k.offsetTop;
	while(k.id!='menu'&&(k=k.offsetParent))p+=k.offsetTop;
	if(!_d.all)p/=3
	if( p > 203 ) {
		$('menu').scrollTop=p;
	}
}

function collapseExpand(x,ul){
	if( ( ul=x[_pN].getElementsByTagName('ul') ).length ) ul=ul[0]
	else ul = x;
	if( /expa/i.test(x[_c]) ) {
		x[_c]=ul[_c]='collapsed'
	} else if( /colla/i.test(x[_c]) ) {
		x[_c]=ul[_c]='expanded';
	}
}

function collapseExpandWithAjax( e,x ) {

	e=e||event;
	x = e.srcElement||e.target
	
	if ( x.nodeType == 3 ) x = x[_pN]; // safari bug
	
	if( !/sp/i.test(x.nodeName)  ) return
	
	if( /ajax_/i.test(x.id) ) {
		info='Pobieram informacje o galeriach';
		getAjaxSubmenu(x);	
	}
	collapseExpand(x);
}

function getAjaxSubmenu(x,y){
	if( !( y = x.id.match(/ajax_(\d{4})/) ) ) {
		// houston we have a problem	
		return false;
	}
	y=y[1]
	if( loadedYears[y] ) return;
	 advAJAX.get({
	 	url:c.HTTP_PATH + '/ajax.php',
	 	year: y,
	 	lang: c.CURRENT_LANG,
	 	advajax_uniqid: '',
	 	onLoading: showAjaxLoading,
	 	onComplete: showAjaxDone,
		onSuccess : function(obj){ buildGallerySubmenuForYear(obj, x ); loadedYears[y]=1; },
 		onError : function(obj) { alert("Error: " + obj.status); }
	});
}

function showAjaxLoading(d,s){
	d=_d.cE('div','loading','bg');
	s=clientSize();
	with(d.style){
		top=s[1]/2-170+'px'
		left=s[0]/2-150+'px'
	}
	d[_iH]='<div class="bg first"><div class="bg second"><img src="' + c.HTTP_PATH + '/resources/gfx/loading.gif" /> <br /> <br /> Trwa łączenie z serwerem <br />' + info +'</div></div>';
	_d.body.appendChild( d )
}

function showAjaxDone(d){
	if( d=$('loading') ) {
		document.body.removeChild(d);
	}
	info='Pobieranie zakończone!';
	showAjaxLoading();
	setTimeout( function(){ if( d=$('loading') )document.body.removeChild(d);	initMenu();}, 300 );
}

var loadedYears = {};
function buildGallerySubmenuForYear(x,span,t){
	span[_pN][_iH] += x.responseText;
}

function addEvent(O,E,F,x){return(x=O.addEventListener)?x(E,F,1):(x=O.attachEvent)?x('on'+E,F):!1}
addEvent(window,'load',initOnLoad);

function goNextPrev(e,n,p){
	if(!e) e = window.event;
	if(e.keyCode) e = e.keyCode;
	else if(e.which) e = e.which;
	switch( e ) {
		case 37:
			if(p)window.location.href=p
			break;
		case 32:
			if(_isRunningSlideshow)break;
		case 39:
			if(n)window.location.href=n
			break
	}
}

function preloadNextPrevImgs(n,img){
	if( n ) {
		img = new Image()
		img.src=n
	}
}

function showInsideTooltipInit(e,x,a){
	x=gTarget(e)
	if( !/^span/i.test(x.nodeName) || !(a=x.id.match( /^no_(\d+)/)) ) return;
	showInsideTooltip(e,x,_aTitles[a[1]])
}

/** look buttons **/
function expandContent(hid){
	if(!hid&&!/print/i.test(_d.body[_c]))addClass(_d.body,'print')
	else rmClass(_d.body,'print')
}
function moveMenuToRight(x){
	if(x=$('left'))x.id='right'
	setCookie('lrID', 'right', 5 );
}
function moveMenuToLeft(x){
	if(x=$('right'))x.id='left'	
	setCookie('lrID', 'left', 5 );
}
function reactToLookButtons(x){
	x=gTarget(x)
	if( /^li/i.test(x.nodeName) ) {
		switch(x.id) {
			case 'lp': expandContent(); break;
			case 'lr': moveMenuToRight();expandContent(1); break;
			case 'rl': moveMenuToLeft();expandContent(1); break;
		}	
	}
}
function doContact(d,e){
	if($('contact'))return;
	e = _d.cE('div'); e[_iH]='<div id="contact"><a href="/contact/" title="Kontakt"> Kontakt </a></div><div id="lookButtons"><ul><li id="rl"></li><li id="lr"></li><li id="lp"></li></ul></div>'
	_d.body.insertBefore( e, _d.body.firstChild )
}
function addToBottomButtons(b,li){
	if(window.location.pathname.length!=1 && window.location.pathname != '/zend-certified-engineer.html' )return;
	b = $('bottomButtons')
	if(!(b.childNodes.length%3))return
	li = _d.cE('li')
	li.innerHTML='<a href="http://pr.blogflux.com/"><img src="http://pr.blogflux.com/pr.php" alt="Google PageRank Checker Tool" /></a>'
	b.appendChild(li);
}

function openIframe(e,node,source,i) {
	if( (i=$('selected' ) ) ) i.id='';
	if(e&&(i=gTarget(e,/^span/i))){
		i[_pN].id='selected'
	}
	function doWindowBar(c){
		c = _d.cE('div','', 'but')
		c.innerHTML='x'
		c.style.cursor='pointer'
		c.onclick=function(){
			i.hidden()
		}
		clos = _d.cE('div','windowBar','close');
		clos.appendChild(c);
		return clos;
	}
	function doFooter(f){
		f = _d.cE('div','iframeFooter','footer');
		f.innerHTML='<a target="_blank" href="#">Nowe okno</a><a href="#1">Zrodlo</a><a href="#0">Wyglad</a>';
		f.onclick=function(e,ifr){
			e=gTarget(e)
			if(!/#\d$/.test(e.href))return!0;
			e=parseInt(e.href.substring(e.href.length-1));
			for(j=0,ifr=i.getElementsByTagName('iframe');j<ifr.length;j++)if(e!=j)ifr[j].hide();else ifr[j].show();
			return!1;
		}
		return f;
	}
	function catchKey(e){
		e=e||event
		e=e.keyCode||e.which;
		switch( e ) {
			case 27: // escape key
				i.hidden();
				break;
		}
		window.onkeydown=null;
	}
	window.onkeydown=catchKey
	if(!(i=$('iframeViewer'))) {
		i = _d.cE('div','iframeViewer');
		i.hidden();
		//i.appendChild(_d.cE('b','','cn tl'));
		//i.appendChild(_d.cE('b','','cn tr'));
		//i.appendChild(_d.cE('b','','cn bl'));
		//i.appendChild(_d.cE('b','','cn br'));
		
		i.appendChild(doWindowBar());
		
		c = _d.cE('div','','frameHolder')
		
		c.appendChild(_d.cE('iframe'));
		
		ii = _d.cE('iframe')
		c.appendChild(ii.hide());

		c.appendChild(_d.cE('div','iframeCurtain','iframeCurtain').hide());
		i.appendChild(c)
		
		i.appendChild(doFooter());
		document.body.appendChild(i)
		
		t = new Drag( 'windowBar','iframeViewer',null );
		t._onDragStart=function(){$('iframeCurtain').show();}
		t._onDragFinished=function(){$('iframeCurtain').hide();}
	}
	$('iframeFooter').getElementsByTagName('a')[0].href=node.href
	ii = i.getElementsByTagName('iframe')
	ii[0].show();
	ii[0].onload = function(e){
		window.location.hash=window.frames[0].location.pathname;
		i.visible();
		addEvent((this.contentDocument||this.document),'keydown',catchKey)
	};
	addEvent(ii[0],'load', ii[0].onload )
	ii[0].src=node.href
	ii[1].src=source?source:node.href.replace('/p/', '/f/p/' )
	ii[1].hide()
	ii[1].onload = function(e){
		addEvent((this.contentDocument||this.document),'keydown',catchKey)
	};
	addEvent(ii[1],'load', ii[1].onload )
}