window.addEvent('domready', function(){
	
	$ES('a').each(function(el) {
		el.addEvent('focus', function(e){
			this.blur();
		});
	});
	
	$$('div[class=menu-item]').each(function(el,i){
		var sel = el.getElement('div[class=submenu-item]');
		if(sel != null){
			el.addEvents({
				'mouseenter':function(){
					var pos0 = $$('div[class=menu-item]')[0].getPosition().x;
					if(sel.getElements('ul').length > 0) {
						this.addClass('sel');
						sel.setStyle('display','block');
						var tab = sel.getElement('table');
						var width = tab.getSize().size.x;
						var pos_el = el.getPosition().x;
						var left = (pos_el-pos0) - 20;
						var over = left + width - 958;
						if(over > 0) left -= over;
						tab.setStyle('margin-left', left+'px');
					}
				},
				'mouseleave':function(){
					if(this.hasClass('sel')){
						this.removeClass('sel');
						sel.setStyle('display','none');
					}
				}
			});
		}
	});
	
	var sub_tmr, last_el;
	if($defined($('shopMenu'))){
		$('shopMenu').getElements('div.lev1').each(function(el){
			var id = el.getProperty('id').split('_')[2];
			var subs = $$('div[id^=node_'+id+'_]');
			if(subs.length){
				el.addEvent('mouseenter',function(){
					if($('subbox')) {
						$('subbox').remove();
						//last_el.setStyle('background', '');
						if(sub_tmr) $clear(sub_tmr);
					}
					var subbox = new Element('div',{
						id: 'subbox',
						styles: {
							position: 'absolute',
							top: el.getTop()-$('shopMenu').getTop()-1,
							left: '170px',
							width: '201px',
							height: 'auto',
							background: '#009900',
							opacity: '0.9',
							'z-index': '1000'
						},
						events: {
							'mouseenter':function(){
								$clear(sub_tmr);
							},
							'mouseleave':function(){
								$('subbox').remove();
								//last_el.setStyle('background', '');
							}
						}
					}).injectInside('shopMenu');
					subs.each(function(d,i){
						var a = d.getElements('a')[0];
						var div = new Element('div',{
							styles:{
								'border': 'solid 1px #009900',
								'border-top': (i==0 ? 'solid 1px #009900' : 'none')
							}
						}).adopt(new Element('a',{
							href: a.getProperty('href'),
							'class': 'indt-sub'
						}).setText(a.getText())).injectInside(subbox);
					});
					//el.setStyle('background', '#EFF5E7');
					last_el = el;
				});
				el.addEvent('mouseleave',function(){
					if($('subbox')){
						sub_tmr = (function(){$('subbox').remove();/*last_el.setStyle('background','')*/}).delay(250);
					}
				});
			}
		});
	}
	
	$ES('div[id^=js_bann-]').each(function(el,i){
		el.setStyle('visibility','hidden');
		banns[i] = el.getProperty('id').split('-')[1];
	});
	if(banns.length){
		$('banns').addEvents({
			'mouseenter':function(){
				$('banns-arrow-l').setStyle('display', 'block');
				$('banns-arrow-r').setStyle('display', 'block');
			},
			'mouseleave':function(){
				$('banns-arrow-l').setStyle('display', '');
				$('banns-arrow-r').setStyle('display', '');
			}
		});
		$('banns-arrow-l').setAttribute('title', 'Wstecz');
		$('banns-arrow-l').addEvent('click',function(){
			$clear(pb);
			rotator(-1);
			pb = rotator.periodical(pt);
		});
		$('banns-arrow-r').setAttribute('title', 'Dalej');
		$('banns-arrow-r').addEvent('click',function(){
			$clear(pb);
			rotator(1);
			pb = rotator.periodical(pt);
		});
		setVis(0);
		pb = rotator.periodical(pt);
	}
	
	$('searchPhrase').addEvent('focus',function(){
		$('searchExt').setStyle('display','block');
		$(document.body).addEvent('click',function(e){
			var x = $($pick(e.target,e.srcElement));
			if($('searchExt').getStyle('display')!='none' && x.getParent()!=$('searchForm') && x.getParent()!=$('searchExt')){
				$('searchExt').setStyle('display','');
				$(document.body).removeEvents();
			}
		});
	});
	
	$ES('a[rel=goBack]').each(function(el, i){
		el.addEvent('click', function(e){
			new Event(e).stop();
			history.go(-1);
		});
	});
	
	if($('tableCart')) {
		$('tableCart').getElements('tr[class=bg]').each(function(el){
			el.addEvent('mouseover', function(){
				this.setStyle('background', '#fafafa');
			});
			el.addEvent('mouseout', function(){
				this.setStyle('background', '');
			});
		});
		if($E('a[rel=delCartItem]')) {
			$ES('a[rel=delCartItem]').each(function(el){
				el.addEvent('click', function(e){
					if(!confirm('Na pewno usunąć ten produkt z koszyka?')) {
						new Event(e).stop();
					}
				});
			});
			$ES('a[rel=mvCartItem]').each(function(el){
				el.addEvent('click', function(e){
					if(!confirm('Na pewno przenieść ten produkt do przechowalni?')) {
						new Event(e).stop();
					}
				});
			});
			$E('input[id=js_delCart]').addEvent('click', function(e){
				if(!confirm('Na pewno usunąć całą zawartość koszyka?')) {
					new Event(e).stop();
				}
			});
		}
	}
	
	if($('tableShips')) {
		$('tableShips').getElements('tr[class=bg]').each(function(el){
			el.addEvent('mouseover', function(){
				this.setStyle('background', '#fafafa');
			});
			el.addEvent('mouseout', function(){
				this.setStyle('background', '');
			});
		});
	}
	
	$ES('input[class^=set-hide-]').each(function(el){
		var a = el.getProperty('class').substr(8);
		var s = el.checked;
		$ES('tr[class=hideMe'+a+']').each(function(el){
			el.setStyle('display', s ? '' : 'none');
		});
		el.addEvent('click', function(){
			var s = el.checked;
			$ES('tr[class=hideMe'+a+']').each(function(el){
				el.setStyle('display', s ? '' : 'none');
			});
		});
	});
	
	$ES('a[rel^=popup-]').each(function(el){
		var a=el.getProperty('rel').substr(6);
		var dims=a.split('_');
		var w=dims[0]; var h=dims[1];
		var l=(window.screen.width-w)/2;
		var t=(window.screen.height-h)/3;
		var params='resizable=no,scrollbars=yes';
		el.addEvent('click', function(e){
			new Event(e).stop();
			window.open(el.href, '', 'width='+w+', height='+h+', left='+l+', top='+t+', '+params);
		});
	});
	
	if($('arrow-left')) {
		var lp; var rp;
		$('arrow-left').getElement('img').setStyle('cursor', 'pointer');
		$('arrow-left').getElement('img').addEvent('mouseover',function(){
			lp = (function(){$('scroller-cont').scrollLeft -= 9;}).periodical(50);
		});
		$('arrow-left').getElement('img').addEvent('mouseout',function(){
			$clear(lp);
		});
		$('arrow-left').getElement('img').addEvent('click',function(){
			$('scroller-cont').scrollLeft -= 270;
		});
		$('arrow-right').getElement('img').setStyle('cursor', 'pointer');
		$('arrow-right').getElement('img').addEvent('mouseover',function(){
			rp = (function(){$('scroller-cont').scrollLeft += 9;}).periodical(50);
		});
		$('arrow-right').getElement('img').addEvent('mouseout',function(){
			$clear(rp);
		});
		$('arrow-right').getElement('img').addEvent('click',function(){
			$('scroller-cont').scrollLeft += 270;
		});
	}
	
	$$('a[rel=tip]').each(function(el){
		var i = 'prod';
		var tmp = el.getAttribute('title').split('|');
		if(tmp[1] != undefined){
			var html = '<strong>'+tmp[0]+'</strong><br/>Cena: '+tmp[1]+' zł';
		} else {
			var html = tmp[0];
		}
		el.setAttribute('title', '');
		el.setStyle('cursor', 'pointer');
		el.addEvents({
			'mouseover':function(){
				new Element('div',{
					'id': 'tip_'+i,
					'class': 'tip'
				}).setHTML(html).injectAfter('container');
				$('tip_'+i).setOpacity(0);
				new Fx.Style('tip_'+i, 'opacity', {duration:500}).start(0,0.9);
			},
			'mouseout':function(){
				$('tip_'+i).remove();
			},
			'mousemove':function(e){
				if(!$defined($('tip_'+i))) return;
				var w = $('tip_'+i).getSize().size.x;
				var h = $('tip_'+i).getSize().size.y;
				if(window.ie){
					var x = window.getScrollLeft() + e.clientX + 10;
					var y = window.getScrollTop() + e.clientY - (h+5);
				} else {
					var x = e.pageX + 10;
					var y = e.pageY - (h+5);
				}
				var win_w = window.getWidth();
				if(x+w > win_w) {
					x = win_w - w;
				}
				$('tip_'+i).setStyles({
					left: x+'px',
					top: y+'px'
				});
			}
		});
	});
	
	var _initBox = function(name){
		if(!$defined($('box-'+name))) return;
		var el = $('box-'+name);
		var px = 131;
		var max = parseInt($('box-scroller-'+name).getStyle('width')) - px*7;
		if(max < 0) return;
		var marginChange = new Fx.Style('box-scroller-'+name, 'margin-left', {duration:1000});
		var arrow_right = el.getElement('div.box-special-arrow-right').getElement('img');
		var arrow_left = el.getElement('div.box-special-arrow-left').getElement('img');
		arrow_right.setStyle('cursor', 'pointer');
		arrow_left.setStyle('cursor', 'pointer');
		arrow_right.addEvent('click',function(){
			var ml = parseInt($('box-scroller-'+name).getStyle('margin-left'));
			var scrl = max + ml;
			var num = Math.floor(scrl/px);
			marginChange.start(ml - px*(num >= 7 ? 7 : num));
		});
		arrow_left.addEvent('click',function(){
			var ml = parseInt($('box-scroller-'+name).getStyle('margin-left'));
			var scrl = - ml;
			var num = Math.floor(scrl/px);
			marginChange.start(ml + px*(num >= 7 ? 7 : num));
		});
	}
	
	_initBox('special');
	_initBox('new');
	_initBox('top');
	_initBox('brands');
	
	var lastPrev = 0;
	var delayNr;
	$$('a[rel^=js_prodPreview-]').each(function(el){
		var id = el.getProperty('rel').split('-')[1];
		var list = (el.getProperty('rel').split('-')[2] == '2');
		el.addEvent('mouseenter',function(){
			if($defined($('prev'+lastPrev))){
				$('prev'+lastPrev).setStyle('display', 'none');
			}
			lastPrev = id;
			$('prev'+id).setStyles({
				display: 'block',
				position: 'absolute',
				'z-index': '200'
			});
			var pos = {x: el.getPosition().x, y: el.getParent('table').getPosition().y};
			if(window.getWidth() < pos.x+100+420) {
				$('prev'+id).setStyles({left: pos.x-430, top: pos.y-22});
			} else {
				$('prev'+id).setStyles({left: pos.x+(list?100:100), top: pos.y-(list?0:22)});
			}
		});
		el.addEvent('mouseleave',function(){
			$('prev'+id).setStyle('display', 'none');
		});
		
	});
	
	$$('a[rel^=js_print-]').each(function(el){
		var id = el.getProperty('rel').split('-')[1];
		el.addEvent('click',function(e){
			new Event(e).stop();
			var w = window.open('', 'print_'+id, 'width=450,height=250,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=no');
			w.document.write($(id).innerHTML);
			w.document.close();
			w.focus();
			w.print();
			w.close();
		});
	});
	
	$('box-logos').getElements('img').each(function(el){
		new Image().src = el.src.replace('.png', '-col.png');
		el.addEvents({
			'mouseover':function(){
				this.src = this.src.replace('.png', '-col.png');
			},
			'mouseout':function(){
				this.src = this.src.replace('-col.png', '.png');
			}
		});
	});
	
	if($('fb_widget')){
		var fbx = new Fx.Styles($('fb_widget'),{duration:500,wait:false});
		$('fb_widget').addEvents({
			'click':function(){
				fbx.start({right:0});
			},
			'mouseleave':function(){
				fbx.start({right:-292});
			}
		})
	}
	
});

var banns = [];
var cur_bann = 0;
var pb = 0;
var pt = 6000;

var setVis = function(cur, last) {
	for(var i=0; i<banns.length; i++){
		if(i!=cur && i!=last){
			$('js_bann-'+banns[i]).setStyles({opacity:0});
		}
	}
	var fx1 = new Fx.Styles($('js_bann-'+banns[cur]),{duration:500,wait:false});
	fx1.start({opacity:1});
	if($defined(last)){
		var fx2 = new Fx.Styles($('js_bann-'+banns[last]),{duration:500,wait:false});
		fx2.start({opacity:0});
	}
}
var rotator = function(add){
	if(!$defined(add)) add = 1;
	var cond = (add > 0 ? cur_bann == banns.length-1 : cur_bann == 0);
	var nx = (add > 0 ? 0 : banns.length-1);
	var last = cur_bann;
	cur_bann += add;
	if(cond){
		cur_bann = nx;
	}
	setVis(cur_bann, last);
}

