function checkNavigationArrows(nav_row_done_moves,nav_row_done_singles,nav_row_full_moves,nav_row_single_item){
			// left arrow
			if(nav_row_done_moves == 0 && nav_row_done_singles == 0){
				$("#nav-left a").addClass('inactive');
			}
			else{
				$("#nav-left a").removeClass('inactive');
			}
			
		  // right arrow
			if((nav_row_done_moves+1) == nav_row_full_moves && nav_row_single_item == nav_row_done_singles){
				$("#nav-right a").addClass('inactive');
			}
			else{
				$("#nav-right a").removeClass('inactive');
			}
}

 function init_slider() {


	var slideTime = 7000;
	var paused = false;
	var navClicked = false;

	var slideshow = $('#main-slides').cycle({
			activePagerClass: 'active',
	    fx: 'scrollHorz',
	    speed: 1000,
	    timeout: 0,
			pager:  '#nav', 
			onPagerEvent: function(curElement, nextElement, options, forwardFlag) { 
				slideshow.cycle('pause');
				paused = true;
				if($(nextElement).children('.sub-slides').length != 0){
					
				}	
				else{
					$('#subnav').html('');
				}
      },
	    before: function(curElement, nextElement, options, forwardFlag) {	
			
		    if($(nextElement).children('.sub-slides').length != 0) {
					$('#subnav').fadeIn();
					
					if(paused) slideTime = 0;
					  
						var inner = $(nextElement).children('.sub-slides').cycle({
								activePagerClass: 'active',
								fx: 'scrollHorz',
								timeout: slideTime,
								autostop: true,
								end: function() { 
									slideshow.cycle('next');
								},
								pager:  '#subnav',
								onPagerEvent: function() { 
											inner.cycle('pause');
											paused = true;
								}
						});
						
			}
			else {
					if(!paused) {
						setTimeout(function(){slideshow.cycle('next'); }, slideTime);
						$('#subnav').html('');
					}
		    }
	    },
			after: function(curElement, nextElement, options, forwardFlag) {	
				if(!navClicked){
					if(options.nextSlide == 1){
						nav_row_done_moves = 0;
						nav_row_done_singles = 0;
						$("#nav").animate({
						left: '0px'
						});
					}
					else{
						if(options.nextSlide%nav_per_row == 1){
							if((nav_row_done_moves+1) < nav_row_full_moves){
									$("#nav").animate({
										left: '-='+nav_row_width*nav_per_row
									});
									++nav_row_done_moves;
							}
							else{
									if(nav_row_done_singles < nav_row_single_item){
										$("#nav").animate({
											left: '-='+nav_row_width
										});
										++nav_row_done_singles;
									}
							}
						}
						else {
						
						}
		
					}

					checkNavigationArrows(nav_row_done_moves,nav_row_done_singles,nav_row_full_moves,nav_row_single_item);
				}
			}
	});
	
	
	$("#nav a:eq(0)").addClass("avon");
	$("#nav a:eq(1)").addClass("citroen");
	$("#nav a:eq(2)").addClass("rozowe_okulary");
	$("#nav a:eq(3)").addClass("db");
	$("#nav a:eq(4)").addClass("getin");
	$("#nav a:eq(5)").addClass("smakija");
	$("#nav a:eq(6)").addClass("sygma");
		
		 
		 
		
	$("#nav a:eq(7)").addClass("test");
	$("#nav a:eq(8)").addClass("test");
	$("#nav a:eq(9)").addClass("test");
	$("#nav a:eq(10)").addClass("test");
	$("#nav a:eq(11)").addClass("test");
	$("#nav a:eq(12)").addClass("test");
	 
	 
	 var nav_count = $("#nav a").length;
	 var nav_per_row = 5;
	 var nav_row_width = 160;
	 var nav_row_full_moves = Math.floor(nav_count/nav_per_row);
	 var nav_row_single_item = nav_count-((nav_row_full_moves)*nav_per_row);
	 var nav_row_done_moves = 0;
	 var nav_row_done_singles = 0;
	 
	 checkNavigationArrows(nav_row_done_moves,nav_row_done_singles,nav_row_full_moves,nav_row_single_item);
	 
	 $("#nav-left a").click(function (event) {
			event.preventDefault();					  
			if((nav_row_done_moves) > 0){
					$("#nav").animate({
						left: '+='+nav_row_width*nav_per_row
					});
					--nav_row_done_moves;
			}
			else{
					if(nav_row_done_singles > 0){
						$("#nav").animate({
							left: '+='+nav_row_width
						});
						--nav_row_done_singles;
					}
			}
			checkNavigationArrows(nav_row_done_moves,nav_row_done_singles,nav_row_full_moves,nav_row_single_item);
    });
	 
	 $("#nav-right a").click(function (event) { 
			event.preventDefault();	
			if((nav_row_done_moves+1) < nav_row_full_moves){
					$("#nav").animate({
						left: '-='+nav_row_width*nav_per_row
					});
					++nav_row_done_moves;
			}
			else{
					if(nav_row_done_singles < nav_row_single_item){
						$("#nav").animate({
							left: '-='+nav_row_width
						});
						++nav_row_done_singles;
					}
			}
			checkNavigationArrows(nav_row_done_moves,nav_row_done_singles,nav_row_full_moves,nav_row_single_item);
			navClicked = true;
    });

 }
