new Asset.javascript('/js/ufo.js', {id: 'Sufo'});
window.addEvent("domready", function() {
									 
	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>'); 
		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;
	si = window.setInterval(function() {
									 
		if (window.location.href.indexOf("#")+1 == window.location.href.length) window.location.hash = window['prevloc']; //zabezpieczenie przed pustym hashem
		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.getProperty('target') || el.getProperty('href').test(/^mailto/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)) {
				//zagrywki do slimboksa
			} 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(response){
					page = response.split('|-|');

					pageChange(addr,el);
				}}).request();
			}
			
			function pageChange(addr,el) {
				var Aaddr = addr.split('/');
				if (page[4] != "pl") var bodyid = Aaddr[2];
				else var bodyid = Aaddr[1];
				//alert(bodyid);
				if (bodyid.length == 0) bodyid = 'home';
				document.title = page[0];
				document.body.setProperty('id',bodyid);
				contentChange(addr,el);
				
			}

			function contentChange(addr,el) {

				if (page[4] == "pl") window.section = addr.split('/')[1];
				else window.section = addr.split('/')[2];
				var lang = document.body.getProperty('class')
				if (lang != page[4]) {
					bodyClassChange(page[4]);
					logoLink(page[4]);
					$('pages').setHTML(page[3]);
					$$('#pages a').each(changeHref);
					menuInit();
				}
				
				if (page[2]) { //submenu generating

					if (!$('content-wrapper').hasClass('float')) $('content-wrapper').addClass('float');
					if (!$('submenu')) {

						submenu = new Element('div').setProperty('id','submenu').setHTML(page[2]).injectAfter($('content'));
					} else if ((window.prevsection && window.section != window.prevsection) || $('submenu').getChildren().length == 0) {
						$('submenu').setHTML(page[2]);
					} 

				} else if ($('submenu')) {
					$('submenu').remove();
					$('content-wrapper').removeClass('float');
				}
				
				$('content').setHTML(page[1]);
				$('content').effect('opacity', {duration: 350}).start(0,1);
				if (el) el.setProperty('href',''+addr+'');
				$$('a').each(changeHref);
				submenuInit();
				Lightbox.init();
				showIntro();
				window.prevsection = window.section;

			}
			if ($('intro-wrapper')) $('intro-wrapper').remove();
			if (document.body.getProperty('id') == ('home')) {
				$('content').setOpacity(0); //szybkie ukrywanie przy przeladowaniu z nie-ajaksa na ajaksa poprzez strone glowna
				ajaxLoad();
			} else $('content').effect('opacity', {duration: 300, onComplete: ajaxLoad}).start(1,0); //START CHANGING PAGE
			if (!$('ajaxloader-wrapper')) ajw = new Element('div', {id: 'ajaxloader-wrapper'}).injectInside(document.body);
			
	}
	
	if (window.location.hash) getPage(window.location.hash.slice(1));
	/*intro new*/
	function showIntro() {
		if (document.body.getProperty('id') == ('home') && !$('intro-wrapper')) 							 	{  
			var Fintro = { 
				movie:'/flash/intro.swf?lang='+document.body.getProperty('class'), //ie tak keszuje ze nie przepuszcza innych wersji jez!
				width:558, 
				height:362,
				majorversion:8,
				build:0,
				wmode: 'transparent',
				flashvars:'lang='+document.body.getProperty('class') 				
			}; 
			UFO.create(Fintro, 'intro-container');
		}
	}
	showIntro();

	/*animacja menu pages*/
	function menuInit() {
		$$('#pages a').each(function(a){
			a.addEvent('mouseover', function() {
				a.effects({duration: 300, transition:Fx.Transitions.Sine.easeInOut}).start({'background-position': ['0 0', '0 -30'], 'opacity': [1, 1]});
			});
			a.addEvent('mouseout', function() {
				(function(){ a.effects({duration: 400, transition:Fx.Transitions.Sine.easeOut}).start({'background-position': ['0 -30', '0 0'], 'opacity': [1, 1]})}).delay(1100);
			});
	
		});
	}
	menuInit();
	
	/*animacja menu languages*/
	$$('#languages a').each(function(a){
		a.addEvent('mouseover', function() {
			a.effects({duration: 200, transition:Fx.Transitions.Sine.easeInOut}).start({'background-position': ['0 0', '0 -22'], 'opacity': [1, 1]}); 
		});
		a.addEvent('mouseout', function() {
			(function(){ a.effects({duration: 300, transition:Fx.Transitions.Sine.easeOut}).start({'background-position': ['0 -22', '0 0'], 'opacity': [1, 1]})}).delay(600);
		});

	});
	
	/*change of body class*/
	function bodyClassChange(newclass) {
		document.body.removeClass(document.body.getProperty('class'));
		document.body.addClass(newclass);
	}
	
	/*logo link*/
	function logoLink(link) {
		var alogo = $E('a', 'header'); 
		if (link != 'pl') alogo.setProperty('href','/'+link+'/');
		else alogo.setProperty('href','/');
	}

}); //end of domready

/*obsluga submenu*/
function submenuInit() {
	if ($('submenu')) {
		$$('#submenu a').each(function(a){
			a.addEvent('click', function(){
				$$('#submenu a').each(function(a2){
					a2.removeClass('active');
				});
				a.addClass('active');
				a.blur();
			});
			if (a.getProperty('href').replace(window.location.protocol+'//'+window.location.host,'') == window.location.hash.substr(1)) a.addClass('active');
			else a.removeClass('active');
		});
	}
}