var gallerySettings2 = {
    'speed': 6000,
	'fadeSpeed' : 1000,
	'childrenImg' : null,
	'childrenTxt' : null,
	'currentChild' : 0,
	'nextChild' : 1,
	'visaBollar' : false,
	'visaPilar' : true,
	'timer' : null,
	'running' : false,
	'forcenext' : -1
};
 
 
function startGallery2()
{
	var slider = $('#slider2');
	var kids = slider.children();
	
	kids.each(function()
	{
		var child = $(this);
		var url = '';
		if(!child.is('img'))
		{
			if(child.is('a'))
			{
				url = child.attr('href');
			}
			child = child.find('img:first');
		}
		//var txt = child.attr('title');
		var filnamn = child.attr('src');
		
		if (url != '')
		{
			//Bildruta
			$('<div></div>').attr('class','slideBild').css(
			{
				"background-image":"url('"+filnamn+"')",
				"cursor":"pointer"
			}).click(function()
			{
				document.location.href = url;				
			}).appendTo('#bildspelBildRuta2');
			
			//Textruta
			/*$('<div></div>').attr('class','slideText').css("cursor","pointer").click(function()
			{
				document.location.href = url;
			}).html(txt).appendTo('#bildspelTextRuta2');*/
		}
		else
		{
			//Bildruta
			$('<div></div>').attr('class','slideBild').css("background-image","url('"+filnamn+"')").appendTo('#bildspelBildRuta2');
			//Textruta
			//$('<div></div>').attr('class','slideText').html(txt).appendTo('#bildspelTextRuta2');
		}		
	});
	
	//$('<div></div>').attr('id','textRutaCover').html("<p>&nbsp;</p>").appendTo('#bildspelTextRuta2');
	
	gallerySettings2.childrenImg = $('#bildspelBildRuta2').children();
	//gallerySettings2.childrenTxt = $('#bildspelTextRuta2').children();
		
	//Bollar
	if(gallerySettings2.visaBollar)
	{
		var nivoControl = $('<div class="nivo-controlNav"></div>');
		$('#bildspel2').append(nivoControl);
		
		for(var i = 0; i < gallerySettings2.childrenImg.length; i++)
		{
			nivoControl.append('<a class="nivo-control" rel="'+ i +'">'+ i +'</a>');	
		}
		//Set initial active link
		$('.nivo-controlNav a:first').addClass('active');
		
		$('.nivo-controlNav a').live('click', function()
		{
			return restartGalleryAt2(parseInt($(this).attr('rel')));
		});
	}
	
	// Pilar
	if(gallerySettings2.visaPilar)
	{
		$('#bildspel2').append('<div class="nivo-directionNav"><a class="nivo-prevNav">Prev</a><a class="nivo-nextNav">Next</a></div>');
		
	
				
		$('#bildspel2 a.nivo-prevNav').live('click', function()
		{
			var prevSlide = gallerySettings2.currentChild - 1;
			if (prevSlide < 0) prevSlide = 0;
			return restartGalleryAt2(prevSlide);
		});
		
		$('#bildspel2 a.nivo-nextNav').live('click', function()
		{
			return restartGalleryAt2(gallerySettings2.nextChild);
		});
	}
	
	// Kör igång bildspelet	
	
	gallerySettings2.currentChild = gallerySettings2.childrenImg.length - 1;
	
	restartGalleryAt2(0);	
}

function restartGalleryAt2(i)
{
	if (gallerySettings2.running)
	{
		gallerySettings2.forcenext = i;
		return false;
	}
	gallerySettings2.forcenext = -1;
	gallerySettings2.nextChild = i;
	runGallery2();
	
	try
	{
		clearTimeout(gallerySettings2.timer);
	}
	catch(err){}
	gallerySettings2.timer = setInterval('runGallery2()',gallerySettings2.speed);
	
	return false;
}

function runGallery2()
{
	//$('#bildspelstat').html('Current:'+gallerySettings.currentChild+'<br>Next:'+gallerySettings.nextChild);
	
	gallerySettings2.running = true;
	
	$(gallerySettings2.childrenImg[gallerySettings2.currentChild]).fadeOut(gallerySettings2.fadeSpeed);
	$(gallerySettings2.childrenImg[gallerySettings2.nextChild]).fadeIn(gallerySettings2.fadeSpeed, function()
	{
		if($(this)[0].style.removeAttribute)
			$(this)[0].style.removeAttribute('filter');
			
		gallerySettings2.currentChild = gallerySettings2.nextChild;
		gallerySettings2.nextChild++;
		
		if (gallerySettings2.nextChild == gallerySettings2.childrenImg.length)
			gallerySettings2.nextChild = 0;
			
		if(gallerySettings2.visaBollar)
		{
			$('#bildspel2 .nivo-controlNav a').removeClass('active');
			$('#bildspel2 .nivo-controlNav a:eq('+ gallerySettings2.currentChild +')').addClass('active');
		}		
			
		gallerySettings2.running = false;
		
		if (gallerySettings2.forcenext > -1)
		{
			restartGalleryAt2(gallerySettings2.forcenext);
			return false;
		}
	});	
	
	/*
	$('#textRutaCover2').fadeIn(0.5 * gallerySettings2.fadeSpeed, function()
	{
		$(gallerySettings2.childrenTxt[gallerySettings2.currentChild]).css('display','none');
		$(gallerySettings2.childrenTxt[gallerySettings2.nextChild]).css('display','block');
			
		$('#textRutaCover2').fadeOut(0.5 * gallerySettings2.fadeSpeed);
		
		gallerySettings2.currentChild = gallerySettings2.nextChild;
		gallerySettings2.nextChild++;
		
		if (gallerySettings2.nextChild == gallerySettings2.childrenImg.length)
			gallerySettings2.nextChild = 0;
			
		if(gallerySettings2.visaBollar)
		{
			$('#bildspel2 .nivo-controlNav a').removeClass('active');
			$('#bildspel2 .nivo-controlNav a:eq('+ gallerySettings2.currentChild +')').addClass('active');
		}		
			
		gallerySettings2.running = false;
		
		if (gallerySettings2.forcenext > -1)
		{
			restartGalleryAt2(gallerySettings2.forcenext);
			return false;
		}
	});
	
	*/
	
	
	
}
