window.addEvent('domready', function(){
	
	var map_links = {
		poznan: "http://maps.google.com/maps?f=q&source=s_q&hl=pl&geocode=&q=Miel%C5%BCy%C5%84skiego+20G,+61-725+Pozna%C5%84&sll=37.0625,-95.677068&sspn=32.527387,79.101563&ie=UTF8&ll=52.409723,16.92225&spn=0.012226,0.038624&z=15&iwloc=A",
		bydgoszcz: "http://maps.google.com/maps?f=q&source=s_q&hl=pl&geocode=&q=ul.+Chodkiewicza+89-91,+85-950+Bydgoszcz&sll=52.409723,16.92225&sspn=0.012226,0.038624&g=Miel%C5%BCy%C5%84skiego+20G,+61-725+Pozna%C5%84&ie=UTF8&ll=53.13359,18.034744&spn=0.012024,0.038624&z=15&iwloc=A",
		warszawa: "http://maps.google.com/maps?f=q&source=s_q&hl=pl&geocode=&q=ul.+Wolska+191,+01-258+Warszawa+&sll=53.13359,18.034744&sspn=0.012024,0.038624&ie=UTF8&ll=52.224382,20.932002&spn=0.012277,0.038624&z=15&iwloc=A"
	};
	if($('mapa_miasto')) $('mapa_miasto').setProperty('href', map_links['poznan']);
	$$('a[rel^=mapa_]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			var city = el.getProperty('rel').substring(5, el.getProperty('rel').length);
			var img = this.getParent().getElement('img');
			img.src = img.src.substring(0, img.src.lastIndexOf('/')+1)+"mapa_"+city+".gif";
			$('mapa_miasto').setProperty('href', map_links[city]);
		});
	});
	
	$$('a[rel^=nwsltr_]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			$('form_nwsltr').opt.value = el.getProperty('rel').substring(7, el.getProperty('rel').length);
			$('form_nwsltr').submit();
		});
	});
	
	$$('a[rel^=showfaq_]').each(function(el){
		var a = el.getProperty('rel').split('_');
		var id = a[1];
		if(a[2] != "open") {
			$('faq_'+id).setStyle('display', 'none');
		}
		el.addEvent('click', function(e){
			new Event(e).stop();
			var s = $('faq_'+id).getStyle('display');
			$('faq_'+id).setStyle('display', s=='none' ? '' : 'none');
		});
	});
	
	var submit_form = function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			var par = (el.get('tag')=='a' ? 'rel' : 'id');
			$(this.getProperty(par).substring(7)).submit();
		});
	}
	
	$$('input[id^=submit_]').each(function(el){
		submit_form(el);
	});
	$$('a[rel^=submit_]').each(function(el){
		submit_form(el);
	});
	
	$$('a[rel^=reset_]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			$(this.getProperty('rel').substring(6)).reset();
		});
	});
	
	$$('a[rel=print]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			window.print();
		});
	});
	
	$$('img[id^=calendar-]').each(function(el){
		var name = el.getProperty('id').substr(9);
		var TextDateCalendar = new MooCal(name);
		el.style.cursor = "pointer";
		el.addEvent('click', function(e){
			new Event(e).stop();
			var pos = this.getPosition();
			var x = pos.x;
			var y = pos.y;
			TextDateCalendar.show(x,y);
		});
	});
	
	$$('*[class^=hide]').each(function(el){
		el.setStyle('display', 'none');
	});
	
	/* ------------------------------------------------------------------------------- */
	
	var tmp = window.getSize();
	var tmp2 = $('main').getSize();
	var ratio_max = Math.floor(100*(tmp.x-tmp2.x)/tmp.x)/100;
	
	if(ratio_max <= 0) {
		$('slider').setStyle('display', 'none');
	}
	
	$('slider').setStyles({
		display: 'block',
		position: 'absolute',
		'z-index': 10,
		top: '0',
		left: '1',
		cursor: 'pointer'
	});
	
	var ratio = 0;
	var myDrag = new Drag('slider', {
		snap: 0,
		limit: {x:[1,149], y:[0,0]},
		onSnap: function(el){
			intid = (function(){
				var pos = parseInt($('slider').getStyle('left'));
				ratio = Math.floor((pos-1)/1.48)/100;
				Cookie.write('width', ratio, {duration: 365});
				setWidth(ratio);
			}).periodical(25);
		},
		onComplete: function(el){
			$('slider').setStyle('left', 149*ratio);
			$clear(intid);
		}
	});
	if(Browser.Engine.trident) {//IE
		myDrag.handles.ondragstart = function() {
			return false;
		}
	}
	
	if($('middle')) {
		var items = ['main', 'top', 'middle', 'tresc', 'szkolenia', 'elearning', 'polecamy', 'klientarea'];
	} else {
		var items = ['main', 'top', 'box_podstrona', 'podstrona_prawa', 'podstrona_stopka', 'tab_szkolenia'];
	}
	var widths = [];
	var widths2 = [];
	for(var i=0; i<items.length; i++) {
		if($(items[i])) {
			var tmp = $(items[i]).getSize();
			widths[i] = tmp.x;
		}
	}
	
	var setWidth = function(ratio){
		if(ratio < 0) return;
		for(var i=0; i<items.length; i++) {
			if($(items[i])) {
				if(items[i] == 'podstrona_prawa') {
					$(items[i]).setStyle('width', Math.floor(widths[i]*(1.4*ratio_max*ratio+1)));
				} else {
					$(items[i]).setStyle('width', Math.floor(widths[i]*(ratio_max*ratio+1)));
				}
			}
		}
	}
	
	var cookie_ratio = parseFloat(Cookie.read('width'));
	if(cookie_ratio > 0 && cookie_ratio <= 1) {
		$('slider').setStyle('left', 149*cookie_ratio);
		setWidth(cookie_ratio);
	}
	
	/* ------------------------------------------------------------------------------- */

	init_recom();
	
	$$('body').setStyle('visibility', 'visible');
	
});

function set_display(id, a){
	var s = id.checked;
	$$('*[id^='+a+']').setStyle('display', !s ? 'none' : '');
}

function init_recom() {
	$$('a[rel^=load_train_]').each(function(el){
		var id = el.getProperty('rel').substring(11);
		el.addEvent('click', function(e){
			new Event(e).stop();
			var req = new Request({  
				method: 'get',
				url: 'ajax/home_get_recom/'+id, 
				onRequest: function() {
					$('polecamy_bg').setStyle('background', 'url(img/ajax-loader.gif) no-repeat center center');
				},  
				onComplete: function(res) {
					$('polecamy_box').set('html', res);
					init_recom();
				}
			}).send();
		});
	});
}

