$(document).ready(function() {
	var items = $('#shop option');
	$('#category').change(function(){
		var currentVal = $(this).val();
		if(currentVal!=0){
			$('#shop option').remove();
			$('#shop').append(items.first());
			$.each(items, function(index, el){
				if($(el).attr('title')==currentVal){
					$('#shop').append(el);
				}
			});
		}else{
			$.each(items, function(index, el){
					$('#shop').append(el);
			});
		}
		$('#shop option').eq(0).attr('selected','selected');
	});


	$('#goto').click(function(){
		if($('#shop').val()!=0){
			location.href=jsUrlBase+'Sklep/'+$('#shop').val();
		}
		else if($('#category').val()){
			location.href=jsUrlBase+'Sklepy/index/'+$('#category').val();
		}else{
			alert('Wybierz branżę');
		}
	});


	$('.scroll-pane').jScrollPane({
		verticalDragMinHeight: 52,
		verticalDragMaxHeight: 52
	});

	$('a[rel=lightbox]').lightBox();

	$('#toggle').click(function(event) {
		event.preventDefault();
		$('#poziom2').toggle();
		$('#poziom3').toggle();
		$(this).text($(this).text()=='Godziny otwarcia'?'Zobacz promocję':'Godziny otwarcia')
	});




	$('#boxesbg').after('<div id="boxesbg_conteiner"></div><div id="boxesbg_position"></div>');
	var startH = $('#boxesbg').height();
	$('#boxesbg_conteiner').height(startH);
	var offset = $('#boxesbg_conteiner').position();
	var st = true;
	var minH=24;


	$('#boxesbg, #boxesbg_position').css({
		position: 'fixed',
		bottom: 0,
		height: minH,
		zIndex: 100,
		left: 0
	}).hover(function(){
		if(st){
			$(this).stop().animate({
				height: startH
			});
		}
	},function(){
		if(st){
			$(this).stop().animate({
				height: minH
			});
		}
	});

	$('#boxesbg_position').css({
		position: 'fixed',
		bottom: 0,
		height: minH,
	//	background: '#ffffff',
		width: '100%',
		zIndex: -1,
		paddingTop: 5,
		paddingBottom: 10,
		left: 0
	});

	$(window).bind('resize scroll load', function(){
		offset = $('#boxesbg_conteiner').offset();
		boff =  $('#boxesbg_position').offset();

		if(boff.top>=offset.top){
			st = false;
			$('#boxesbg').css({
				position: 'absolute',
				top: offset.top,
				height: startH
			});

		}else{
			st = true;
			$('#boxesbg').css({
				position: 'fixed',
				bottom: 0,
				height: minH,
				top: 'auto'
			});

		}
	});

	var hover = false;
	var menutimeout;
	var subtimeout=300;
	var alwaysShow = false;
	$('.menu_arrow').css({
		opacity: 0,
		left: ($(window).width()-960)/2
	});
	//$('.sklep-hover').first().addClass('show')
	$('.sklep-hover').each(function(){
		if($(this).hasClass('show')){
			var itemOffset = $(this).offset();
			var rel = $(this).attr('rel');
			submenuShow(rel, itemOffset.left + ($(this).outerWidth()/2)-13 );
			alwaysShow = $(this);
			$('#top').css({
				marginBottom: 40
			});
		}
	});

	$('.sklep-hover').hover(function(){
		var itemOffset = $(this).offset();
		var rel = $(this).attr('rel');
		submenuShow(rel, itemOffset.left + ($(this).outerWidth()/2)-13 );
	}, function(){
		submenuHide();
	});

	$('.submenu').hover(function(){
		hover = true;
		clearTimeout(menutimeout);
	}, function(){
		submenuHide();
	});
	function submenuHide(){
		hover=false;
		menutimeout=setTimeout(ukryjmenu, subtimeout)
	}
	function submenuShow(rel, itemOffset){
		hover = true;
		$('.submenu').html($('#'+rel).html());
		$('.submenu').stop().show().animate({opacity: 1},250);
		$('.menu_arrow').stop().animate({
			opacity: 1,
			left: itemOffset
		}, 300).show();
		clearTimeout(menutimeout);
	}
	function ukryjmenu () {
		if(!hover){
			if(alwaysShow){
				var itemOffset = $(alwaysShow).offset();
				var rel = $(alwaysShow).attr('rel');
				submenuShow(rel, itemOffset.left + ($(alwaysShow).outerWidth()/2)-13 );
			}else{
				$('.submenu').stop().animate({opacity: 0},300, function(){$(this).hide()});
				$('.menu_arrow').stop().animate({
					opacity: 0
				},200);
			}
		}
	}




});
