
$(document).ready(
	function()
	{	
		var random_id = $('#promo ul a').eq(0).attr('rel');
		collectRandom(random_id);	
	}
);

function selectOnlineProgram(el)
{
	$('#online_programs a').removeClass('act');
	$(el).addClass('act');
	$('#program_id').val($(el).attr('rel'));
}


var collect, count, last_zindex, last_id;
var last_index = 0;
function overRandom(el)
{	
	$('#promo ul a').removeClass('act');
	$(el).addClass('act');
	
	var random_id = $(el).attr('rel');
	$('#photo_' + last_id).stop();	
	collectRandom(random_id);
}

function collectRandom(random_id)
{

	var els = $('.photo_random_' + random_id);

	collect = [];
	var n = 100;
	els.each(function()
	{	
		n--;
		$(this).css('z-index', n);
		collect.push($(this).attr('rel'));		
	});

	last_index = 0;
	
	$('.photo_random_' + random_id).show();
	$('.photo_random').not('.photo_random_' + random_id).hide();	
	
	count = collect.length - 1;
	if (count != 0) startRandom();
}



function startRandom()
{
	if (last_index > count) last_index = 0;
	last_id = collect[last_index];
	last_index++;

	
	$('#photo_' + last_id).fadeOut(5000, function()
	{	
		if (count == 1)
		{
			var next_zindex = last_index;
			if (next_zindex > count) next_zindex = 0;	
			var next_id = collect[next_zindex];
			var zindex = $('#photo_' + next_id).css('z-index');
			
			$('#photo_' + last_id).css('z-index', zindex).show();
			$('#photo_' + next_id).css('z-index', zindex+1);		
		}
		else if (count == 2)		
		{
			var next_index = last_index;
			if (next_index > count) next_index = 0;	

			var middle_index = last_index+1;
			if (middle_index > count) middle_index = 0;	
			
			var next_id = collect[next_index];
			var middle_id = collect[middle_index];
			
			var zindex = $('#photo_' + next_id).css('z-index');

			$('#photo_' + last_id).css('z-index', zindex-2).show();
			$('#photo_' + middle_id).css('z-index', zindex-1).show();
			$('#photo_' + next_id).css('z-index', zindex);		
		}

		startRandom();
	});
}




function showSubscribe(el)
{
	var pos = $(el).offset();
	pos.left = pos.left - 100;
	pos.top = pos.top + 20;
	$('#subscribe_email').val('').removeClass('error_ins');
	$('#subscribe_email_error').html('');
	$('#subscribe').css({ top: pos.top + 'px', left: pos.left + 'px' }).slideDown();
}

function hideSubscribe()
{
	$('#subscribe').slideUp();
}

function sendSubscribe()
{
	var email = $('#subscribe_email').val();
	$.ajax({
		url: '/webAjax/news/addSubscribe/?email=' + email,
		success: function(data)
		{
			$('#subscribe_email_response').html(data);
			$('#subscribeForm').slideUp(function()
			{
				$('#subscribe_email').val('');
				$('#subscribe_sent').slideDown();
			});
		}		
	});	
}
