/**
 * Beautiful Background Image Navigation with jQuery
 * http://tympanus.net/codrops/2010/05/05/beautiful-background-image-navigation-with-jquery/
 */
var $j = jQuery.noConflict();

$j(document).ready(function() {

				

                /* position of the <li> that is currently shown */
//#########################################################################################################################
                //var current = 3;  // TUTAJ WSTAWIĆ AKTUALNIE WYŚWIETLANE I BĘDZIE DZIAŁAŁO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//#########################################################################################################################
				//$j('#bg1').addClass("aktywne", 100);
				

				var loaded  = 0;

				for(var i = 0; i <9; ++i)

					$j('<img />').load(function(){

						++loaded;

						if(loaded == 3){

							$j('#bg1,#bg2,#bg3,#bg4,#bg5,#bg6,#bg7,#bg8').mouseover(function(e){

								

								var $this = $j(this);

								/* if we hover the current one, then don't do anything */

								if($this.parent().index() == current)

									return;



								/* item is bg1 or bg2 or bg3, depending where we are hovering */

								var item = e.target.id;

								var cx;

								cx  = current + 1;

								
								switch (item) {
									case 'bg1': ix  = 1; break;
									case 'bg2': ix  = 2; break;
									case 'bg3': ix  = 3; break;
									case 'bg4': ix  = 4; break;
									case 'bg5': ix  = 5; break;
									case 'bg6': ix  = 6; break;
									case 'bg7': ix  = 7; break;
									case 'bg8': ix  = 8; break;
									default: ix  = -2;
								}

//$j('#bg'+parseInt(cx)).removeClass("aktywne");
$j('.aktywne').removeClass("aktywne");
$j('#'+item).addClass("aktywne");
$j('div.sub'+parseInt(ix)).css("display", "block");
								/*

								this is the sub menu overlay. Let's hide the current one

								if we hover the first <li> or if we come from the last one,

								then the overlay should move left -> right,

								otherwise right->left

								 */

								if(ix < cx)

									$j('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-123px 0)"},300,function(){

										$j(this).find('li').hide();

									});

								else

									$j('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(123px 0)"},300,function(){

										$j(this).find('li').hide();

									});



								if(ix > cx){

									/* if we hover the first <li> or if we come from the last one, then the images should move left -> right */

									$j('#menu > li').animate({backgroundPosition:"(-988px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8 bg9').addClass(item);

									move(1,item);

								}

								else{

									/* if we hover the first <li> or if we come from the last one, then the images should move right -> left */

									$j('#menu > li').animate({backgroundPosition:"(988px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8 bg9').addClass(item);

									move(0,item);

								}
								

								/* change the current element */

								current = $this.parent().index();

								

								/* let's make the overlay of the current one appear */

							   

								$j('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){

									$j(this).find('li').fadeIn().css("display", "block");

								});

							});

						}	

					}).attr('src', 'http://gminazamosc.pl/wp-content/themes/gminny/images_menu/'+i+'.jpg');

				

							

                /*

                dir:1 - move left->right

                dir:0 - move right->left

                 */

                function move(dir,item){

                    if(dir){

                        $j('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},150);

                        $j('#bg2').parent().stop().animate({backgroundPosition:"(-123px 0)"},160);

                        $j('#bg3').parent().stop().animate({backgroundPosition:"(-246px 0)"},170);

                        $j('#bg4').parent().stop().animate({backgroundPosition:"(-369px 0)"},180);

                        $j('#bg5').parent().stop().animate({backgroundPosition:"(-492px 0)"},190);

                        $j('#bg6').parent().stop().animate({backgroundPosition:"(-615px 0)"},200);

                        $j('#bg7').parent().stop().animate({backgroundPosition:"(-738px 0)"},300);

                        $j('#bg8').parent().stop().animate({backgroundPosition:"(-861px 0)"},400,function(){

                            $j('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8 bg9').addClass(item);

                        });

                    }

                    else{

                        $j('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400,function(){

                            $j('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8 bg9').addClass(item);

                        });

                        $j('#bg2').parent().stop().animate({backgroundPosition:"(-123px 0)"},300);

                        $j('#bg3').parent().stop().animate({backgroundPosition:"(-246px 0)"},200);

                        $j('#bg4').parent().stop().animate({backgroundPosition:"(-369px 0)"},190);

                        $j('#bg5').parent().stop().animate({backgroundPosition:"(-492px 0)"},180);

                        $j('#bg6').parent().stop().animate({backgroundPosition:"(-615px 0)"},170);

                        $j('#bg7').parent().stop().animate({backgroundPosition:"(-738px 0)"},160);

                        $j('#bg8').parent().stop().animate({backgroundPosition:"(-861px 0)"},150);

                    }

                }

            });
