window.addEvent("domready", function() {
									 
	//$('imgmap').setProperty('src','/img/spacer.png');
	if (window.ie) {
		$('imgmap').setProperty('src','/style/img/spacer.png');
		$('imgmap').setStyle("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/style/img/menu_hollowed.png', sizingMethod='scale')");
		$('menu').setStyle('background-image','url(/style/img/menu_bg.png)');
	} else {
		//(function(){alert('one second later...')}).delay(1000);
		//(function(){$('menu').setStyle('background','transparent url("/img/menu.png") no-repeat')}).delay(300);
	}
	
	function menuhover(element, index, array) { 
    	
		this.href = element.href.replace(location.protocol+'//'+location.host,'');
		if (location.pathname.indexOf(this.href) != -1 || location.hash.indexOf(this.href) != -1) {
			def = (index+1)*-290+'px';
		}
		
		element.addEvent('mouseover',function() {
			$('imgmap').setStyle('background-position',(index+1)*-290+'px');
		});
	}

	$$('area').each(menuhover);
	$('imgmap').setStyle('background-position',def); //default hover
	$('imgmap').addEvent('mouseout',function() { 
		$('imgmap').setStyle('background-position',def);
	});
	
	//SF Silnik
	if (window.ie) {	//HASH-STORY
		span = new Element('span');
		span.setHTML('<iframe name="iframe" style="width:1px;height:1px;border:0" src="/js/iframe.php?hash='+location.hash+'"></iframe>'); //bug -> nie przyjmuje atrybutu name dla iframe przy probie utworzenia elementu
		span.injectInside(document.body);
	}
	
	msg = new Element('div'); //<div id="msg"></div>
		msg.setProperty('id','msg');
		msg.injectInside($('menu'));
	
	//LOCATION LISTENER 
	window['prevloc'] = window.location.hash;
	//alert(window.location.href.indexOf("#")+':'+window.location.href.length);
	si = window.setInterval(function() {
									 
		if (window.location.href.indexOf("#")+1 == window.location.href.length) window.location.hash = window['prevloc']; //zabezpieczenie przed pustym hashem
		var loc = window.location.hash;
		//$('msg').setHTML(loc+'<br />'+window['prevloc']+'<br /><small>'+window.history.length+'</small>'); //debug
		if (loc != window['prevloc']) {
			//$('msg').setStyle('color','red'); //debug
			getPage(loc.slice(1));
			window['prevloc'] = loc;
		}
		//else $('msg').setStyle('color','black'); //debug
	}, 300);
	
	function changeHref(el) {	//HREF CHANGER
		
		el.blur();
		
		if (el.rel && el.rel == 'mediabox') {
			el.addEvent('click', function(event) {
				//alert('!');
				window.open('http://sadeckiewopr.pl/wideoplayer.php?wideo='+el.href, 'WIDEO', 'width=800, height=600, addressbar=0, toolbar=0, statusbar=0');				
				new Event(event).stop();
			});
		} else if (el.getProperty('target') || el.getProperty('href').test(/^mailto/i) || el.getProperty('href').test(/.pdf/i)) {
								 
		}
		else if (el.getProperty('href').indexOf("http://") != -1 && el.getProperty('href').indexOf(window.location.protocol+'//'+window.location.host) == -1) el.addEvent('click', function(event) { //otwieranie linkow zewnetrznych w nowym oknie
			el.setProperty('target', '_blank'); 
		});
		else el.addEvent('click', function(event) {
			
			if (el.rel && el.rel.test(/^lightbox/i)) {
			} else {
				href = el.getProperty('href').replace(window.location.protocol+'//'+window.location.host+'',''); //konieczne bo je..any ie dodaje protokol + host
				if (window.location.pathname.length == 1) {
					if (window.ie) {
						iframe.location.href = "/js/iframe.php?hash="+href;
					} else window.location.hash = href;
				} else window.location = '/#'+href;
			}
			new Event(event).stop();		
		});
	}
	
	$$('a','area').each(changeHref);
	
	function getPage(addr,el) {	//GETPAGE

			if (addr == "") addr = "/";
			
			function ajaxLoad() {
				new Ajax('/function/getPage.php?address='+addr,{onComplete: function(responseText){
					page = responseText.split('|-|');
					//document.title = page[0];
					pageChange(addr,el);
				}}).request();
			}
			
			function pageChange(addr,el) {
				//var bodyid = addr.slice(1,-1);
				var Aaddr = addr.split('/');
				var bodyid = Aaddr[1];
				//alert(bodyid);
				if (bodyid.length == 0) bodyid = 'home';
				if (($('home') && bodyid != 'home') || (!$('home') && bodyid == 'home')) {
					if ($('home')) { hstart = 397; hstop = 267; mstart = 233; mstop = 103; 
					} else { hstart = 267; hstop = 397; mstart = 103; mstop = 233;  }
					//document.body.effect('opacity').start(1,0.5);
					//$('menu').effect('opacity').start(0,1);
					this.time = 0;
					(function (){$('header').effect('opacity').start(1,0.01)}).delay(this.time = this.time+0); 
					//disabled (function (){$('menu').effect('top').start(mstart,mstop)}).delay(this.time = this.time+500);
					//disabled (function (){$('header').effect('height').start(hstart,hstop)}).delay(this.time = this.time+500);
					(function (){document.body.setProperty('id',bodyid)}).delay(this.time = this.time+500);
					(function (){$('header').effect('opacity').start(0.01,1)}).delay(this.time = this.time+0);
					(function (){contentChange(addr,el)}).delay(this.time = this.time+500);
					//new Fx.Slide('menu').toggle() //toggle the slider up and down.
					//document.body.setProperty('id',bodyid);
					//contentChange(addr,el);
				} else {
					document.body.setProperty('id',bodyid);
					contentChange(addr,el);
				}
			}

			function contentChange(addr,el) {
				document.title = page[0];
				$('content').setHTML(page[1]);
				$('content').effect('opacity', {duration: 200}).start(0.01,1);
				if (el) el.setProperty('href',''+addr+'');
				$$('a').each(changeHref);
				Lightbox.init();
			}

			$('content').effect('opacity', {duration: 200, onComplete: ajaxLoad}).start(1,0.01); //START CHAGING PAGE
			//ajaxLoad(addr);
			$$('area').each(menuhover);
			$('imgmap').setStyle('background-position',def);
	}
	
	if (window.location.hash) getPage(window.location.hash.slice(1));

}); //end of domready
