/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.

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

	var interval;
	
	$j("#control").click(function()
	{
		
		if ($j(this).css("background-image")=='url("http://sputnik-germes.ru/skins/sp/images/btn_pause.png")' || $j(this).css("background-image")=='url("http://www.sputnik-germes.ru/skins/sp/images/btn_pause.png")')
		{
			stopAnimation();
			$j(this).css({ "background-image" : "url(/skins/sp/images/btn_play.png)" });
			return false;
		}
		
		if ($j(this).css("background-image")=='url("http://sputnik-germes.ru/skins/sp/images/btn_play.png")' || $j(this).css("background-image")=='url("http://www.sputnik-germes.ru/skins/sp/images/btn_play.png")')
		{

			// Change the background image to "pause"
			$j(this).css({ "background-image" : "url(/skins/sp/images/btn_pause.png)" });
			
			// Show the next image
			navigate("next");
			
			// Start playing the animation
			interval = setInterval(function()
			{
				navigate("next");
			}, slideshowSpeed);
			return false;
		}
	});

	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var $html_link = '';

	if (($j.browser.msie && $j.browser.version == '8.0') || ($j.browser.msie && $j.browser.version == '7.0'))
	{
		for (ii=0;ii<=photos.length-2;ii++)
		{
			$html_link += "<div id='"+ii+"' class='nawbutton'></div> ";
		}
	}
	else
	{
		for (ii=0;ii<=photos.length-1;ii++)
		{
			$html_link += "<div id='"+ii+"' class='nawbutton'></div> ";
		}
	}
	

	$j("#headernav-outer").html($html_link);

		// обработчик клика на нав. квадрате
		$j('.nawbutton').click(function()
		{
			navigate(this.id);
			$j("#control").css({ "background-image" : "url(/skins/sp/images/btn_play.png)" });
			$j(this).css({ "background-image" : "url(/skins/sp/images/sb_ico_sel.png)" });
			// Clear the interval
			clearInterval(interval);
		});
		// закрашиваем нав. квадраты в зеленый
		$j('.nawbutton').mouseenter(function()
		{
			$j(this).css({ "background-image" : "url(/skins/sp/images/sb_ico_how.png)" });
		});
		// ... в красный
		$j('.nawbutton').mouseleave(function()
		{
			if ($j(this).css("background-image")=='url("http://sputnik-germes.ru/skins/sp/images/sb_ico_how.png")' || $j(this).css("background-image")=='url("http://www.sputnik-germes.ru/skins/sp/images/sb_ico_how.png")')
			{
				$j(this).css({ "background-image" : "url(/skins/sp/images/sb_ico_all.png)" });
			}
		});

	
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating)
		{
			return;
		}

		// Check which current image we need to show
		if(direction == "next")
		{
			currentImg++;
			
			if (($j.browser.msie) && ($j.browser.version == '8.0'))
			{
				if(currentImg == photos.length)
				{
					currentImg = 1;
				}
			}
			else
			{
				if(currentImg == photos.length + 1)
				{
					currentImg = 1;
				}
			}
			
		}
		if (direction != "next")
		{
			currentImg = parseInt(direction)+1;
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}

		showImage(photos[currentImg - 1], currentContainer, activeContainer);
	};
	
	var currentZindex = 999;
	var showImage = function(photoObject, currentContainer, activeContainer) {
	
		$j('.nawbutton').css({ "background-image" : "url(/skins/sp/images/sb_ico_all.png)" });
		$j('#'+photoObject.idx).css({ "background-image" : "url(/skins/sp/images/sb_ico_sel.png)" });
		animating = true;

		if (currentZindex==99)
		{
			currentZindex = 999;
		}
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		//alert (photoObject.idx);

		$j("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$j("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$j("#firstline").html(photoObject.firstline);
		$j("#secondline").html(photoObject.secondline);
		$j("#linkedurl").attr("href", photoObject.url);
			

		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$j("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function()
			{
				$j("#firstline").css(photoObject.flcss);
				$j("#secondline").css(photoObject.slcss);
				//$j("#headertxt").css({"top" : photoObject.pos_top+'px'});
				$j("#headertxt").css({"left" : photoObject.pos_left+'px'});
				$j("#headertxt").css({"display" : "block"});
				animating = false;
			}, 50);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$j("#control").css({ "background-image" : "url(/skins/sp/images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function()
	{
		navigate("next");
	}, slideshowSpeed);

	
});
