var animated = false;
var play = false;
function getLink (href)
{
	stPos = href.indexOf ('news/');
	href = href.substr (stPos + 5);
	var sidn = href.substr (0, href.indexOf ('/'));
	play = false;
	$('#slider').stopTime ();
	$.get (
		'/run/ajax_get_link.php?id='+sidn,
		function (response)
		{
			//$('#upper').html (response);
			//$('#upper').css ('margin-left', -Math.ceil ($('#upper').innerWidth () / 2));
			//$('#upper').fadeTo (1);
			play = true;
			$('#slider').everyTime ('4s', function ()
			{
				if (play)
				{
					$(this).nextItem ();
				}
			});
		}
	);	
}
$.fn.nextItem = function ()
{
	if (animated)
	{
		return;
	}
	animated = true;
	var slider = $(this);
	var first = slider.find ('div:first'), second = $('#slider div:eq(1)'), last = $('#slider div:eq(2)');
	second.animate ({left:40}, 400);
	last.animate ({left:0}, 400);
	getLink (first.find ('a').attr ('href'));
	//$('#upper').animate ({opacity:0}, 400);
	first.animate ({width:0, height:0, opacity:'0', marginTop:75, left:-20}, 400, function ()
	{
		last.animate ({width:maxWidth, height:maxHeight, marginTop: 0, left: 0}, 400).css ({zIndex:2});
		var next = slider.find ('div:eq(3)');
		next.show ().css ({width:0, height:0, marginTop:75, left:120, opacity:0.2, zIndex:1}).
		animate ({width:minWidth, height:minHeight, marginTop:20, left:-105, opacity:1}, 400, function ()
		{
	
		});
		second.css ({zIndex:1}).animate ({left:105, marginTop:20, width:minWidth, height:minHeight}, 400, function ()
		{
			animated = false;
		});
		first.remove ();
		slider.append (first);	
		//$('#upper').animate ({opacity:1}, 400);
	});
}
var minWidth = 160, minHeight = 124, maxWidth = 212, maxHeight = 160;
$(document).ready (function ()
{
	getLink ($('#slider').find ('div:first a').attr ('href'));
	$('#slider div:gt(2)').hide ();
	$('#slider').find ('div:first').css ({color:'green', left:105, zIndex:1,width:minWidth, height:minHeight, marginTop: 20});
	$('#slider').find ('div:eq(1)').css ({color:'red', marginTop: 0, left:0, zIndex:2,width:maxWidth, height:maxHeight});
	$('#slider').find ('div:eq(2)').css ({color:'green', left:-105, zIndex:1,width:minWidth, height:minHeight, marginTop: 20});
});
