var NewsJSON = null;
var IndexProjects = null;
var IndexProjects_last = 0;
var IndexProjects_timeout = 0;
var IndexMailinglist_timeout = 0;
var IndexLogin_timeout = 0;

function SwitchNews(i)
{
	if(NewsJSON == null || NewsJSON.length < 1)
		return;
	if(i >= NewsJSON.length || i < 0)
		i = 0;
	
	var s = '<div class="title">';
	if(NewsJSON[i]['link'])
		s += '<a href="'+NewsJSON[i]['link']+'">';
	s += NewsJSON[i]['title'];
	if(NewsJSON[i]['link'])
		s += '</a>';
	s += "</div>" + NewsJSON[i]['content'];
	
	$('#news_item').slideUp(800, function() { $('#news_item').html(s).slideDown(1000); });
	
	setTimeout("SwitchNews(" + (i+1) + ");", 7*1000);
}

function SetIndexProject(i)
{
	if(IndexProjects == null || !IndexProjects[i])
		return;
	
	if(IndexProjects_timeout > 0)
		clearTimeout(IndexProjects_timeout);
	IndexProjects_timeout = 0;
	
	if(IndexProjects_last > 0 && IndexProjects[IndexProjects_last])
	{
		$('#projects_num'+IndexProjects_last).removeClass('projects_num_selected').addClass('projects_num');
		IndexProjects_last = 0;
	}
	
	$('#projects_img').fadeTo(200, 0.1, function() {
		$("#IndexProject_loading").css('display', 'block');
		$('#projects_img').attr("src", IndexProjects[i]['img']);
		$('#projects_img').attr("alt", IndexProjects[i]['name']);
		
		$('#projects_num'+i).removeClass('projects_num').addClass('projects_num_selected');
		IndexProjects_last = i;
		
		i++;
		if(!IndexProjects[i])
			i = 1;
		IndexProjects_timeout = setTimeout('SetIndexProject('+i+');', 10*1000);
	});
}

function FadeIndexProjectImg()
{
	if(IndexProjects == null)
		return;
	$("#IndexProject_loading").css('display', 'none');
	
	setTimeout('$("#projects_img").fadeTo(400, 1);', 1);
}

function SendIndexMailinglistForm()
{
	$('#mailinglist_submit').attr('disabled', "disabled");
	$('#mailinglist_loader').show();
	$.post('mailinglist?add', { 
		name : $('#mailinglist_name')[0].value,
		email : $('#mailinglist_email')[0].value
	}, function(data) {
		$('#mailinglist_form').hide();
		$('#mailinglist_loader').hide();
		
		$('#mailinglist_answer').html(data).show();
		if($("#mailinglist_answer input").length > 0)
			IndexMailinglist_timeout = setTimeout('ReturnIndexMailinglistForm();', 8000);
	});
}

function ReturnIndexMailinglistForm()
{
	if(IndexMailinglist_timeout > 0)
		clearTimeout(IndexMailinglist_timeout);
	IndexMailinglist_timeout = 0;
	
	$('#mailinglist_answer').hide();
	$('#mailinglist_submit').removeAttr('disabled');
	$('#mailinglist_form').show();
}

function SendIndexLoginForm()
{
	$('#login_submit').attr('disabled', "disabled");
	$('#remind_password_link').hide();
	$('#login_loader').show();
	$.post('panel?act=login&window=1', { 
		email : $('#login_email')[0].value,
		password : $('#login_password')[0].value
	}, function(data) {
		$('#login_form').hide();
		$('#login_loader').hide();
		$('#remind_password_link').show();
		
		$('#login_answer').html(data).show();
	});
}

function ReturnIndexLoginForm()
{
	if(IndexLogin_timeout > 0)
		clearTimeout(IndexLogin_timeout);
	IndexLogin_timeout = 0;
	
	$('#login_answer').hide();
	$('#login_submit').removeAttr('disabled');
	$('#login_form').show();
}

function SendContactForm()
{
	$('#contact_submit').attr('disabled', "disabled");
	$('#contact_loader').show();
	$.post('contact?send', { 
		name : $('#contact_name')[0].value,
		email : $('#contact_email')[0].value,
		phone : $('#contact_phone')[0].value,
		msg : $('#contact_msg')[0].value
	}, function(data) {
		$('#contact_form').hide();
		$('#contact_loader').hide();
		
		$('#contact_answer').html(data).show();
	});
}

function ReturnContactForm()
{
	$('#contact_answer').hide();
	$('#contact_submit').removeAttr('disabled');
	$('#contact_form').show();
}
