/**
 * XXL AJAX JavaScript
 * baut auf prototype auf
 */


/**
 * Behandlung globaler AJAX Request Fehler
 */


/* for debugging */
Ajax.Responders.register(
{
	onFailure: function()
	{
		alert('Ein Fehler ist aufgetreten!');
	},

	onException: function(request, e)
	{
		alert(request.transport.responseText);
		alert(e.name +' *** error ***'+ e.message);
	}
});

/* ---------------------------
	SINGLE FUNCTIONS
--------------------------- */


/**
 * Klappt Single-Funktions-Reiter auf
 * lädt bei Bedarf den Inhalt per AJAX nach
 * sf = single_function
 */

var xxl_sf_active_id = -1;

function xxl_sf_activate_box(_id_str)
{
	var sf_icon = $('xxl_sf_' + _id_str);
	var sf_box = $('xxl_sf_' + _id_str + '_box');

	if (_id_str == xxl_sf_active_id)
	{
		sf_icon.className = '';
		sf_box.style.display = 'none';

		xxl_sf_active_id = -1;
	}
	else
	{
		if (xxl_sf_active_id != -1)
		{
			$('xxl_sf_' + xxl_sf_active_id).className = '';
			$('xxl_sf_' + xxl_sf_active_id + '_box').style.display = 'none';
		}

		sf_box.style.display = 'block';
		sf_icon.className = 'active';

		if(sf_box.innerHTML == '')
		{ //loading...
			var ar_id = _id_str.split('_');

			var contents_types_id = ar_id[0];
			var _id = ar_id[1];
			var _action = ar_id[2];

			var sf_loading = $('xxl_sf_' + contents_types_id + '_' + _id + '_loading');

			sf_loading.style.display = 'block';

			var post =
			{
				'action': _action,
				'contents_types_id': contents_types_id,
				'_id': _id
			};

			new Ajax.Request('api/sf_box',
			{
				parameters: post,

				onSuccess: function(transport)
				{
					var json = transport.responseText.evalJSON();

					sf_loading.style.display = 'none';
					sf_box.innerHTML = json.box;
				}
			});
		}

		xxl_sf_active_id = _id_str;
	}
}


/*
 * AJAX
 * Sendet das Empfehlungsemailformular ab
 */
function xxl_sf_mail_send(contents_types_id, _id)
{
	var email_regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (email_regexp.test($F('rec_email')))
	{
		new Ajax.Request('api/recommend',
		{
			parameters:
			{
				'contents_types_id': $F('rec_contents_types_id'),
				'_id': $F('rec_id'),
				'email': $F('rec_email'),
				'message': $F('rec_message')
			},

			onSuccess: function(transport)
			{
				alert('Die Nachricht wurde verschickt!');
				xxl_sf_activate_box(contents_types_id + '_' + _id + '_mail');
				//var json = transport.responseText.evalJSON();
				//$('xxl_sf_' + contents_types_id + '_' + _id + '_email_box').innerHTML = json.innerhtml;
			}
		});
	}
	else
	{
		alert('Bitte geben Sie eine gültige E-Mail-Adresse an!');
	}
}

/*
 * akitviert das rate feld
 */
function xxl_sf_activate_rate(contents_types_id, _id)
{
	xxl_sf_please_login(contents_types_id, _id, false);
	showhide_element('xxl_sf_' + contents_types_id + '_' + _id + '_rate');
}

/*
 * AJAX
 * sendet die Bewertung ab
 */
function xxl_sf_rate(contents_types_id, _id, rating)
{
	new Ajax.Request('api/rating/replace',
	{
		parameters:
		{
			'contents_types_id': contents_types_id,
			'_id': _id,
			'rating': rating
		},

		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON();

			$('xxl_sf_' + contents_types_id + '_' + _id + '_rate').hide();

			if (json.please_login)
			{
				xxl_sf_please_login(contents_types_id, _id, true);
			}

			if (json.rating)
			{
				$('xxl_sf_' + contents_types_id + '_' + _id + '_rating').innerHTML = json.rating;
			}
		}
	});
}

/*
 * onmouseover: markiert die Bewertungssterne
 */
function xxl_sf_rate_mark(contents_types_id, _id, rating)
{
	for(var i=1; i<=5; i++)
	{
		var img = $('xxl_sf_' + contents_types_id + '_' + _id + '_rate_star_' + i);
		img.src = i > rating ? img.src.replace(/_active/, '_inactive') : img.src.replace(/_inactive/, '_active');
	}
}

/*
 * onmouseout: macht die Markierung der Bewertungssterne rückgängig
 */
function xxl_sf_rate_unmark(contents_types_id, _id)
{
	for(var i=1; i<=5; i++)
	{
		var img = $('xxl_sf_' + contents_types_id + '_' + _id + '_rate_star_' + i);
		img.src = img.src.replace(/_active/, '_inactive');
	}
}

function xxl_sf_please_login(contents_types_id, _id, show)
{
	var please_login = $('xxl_sf_' + contents_types_id + '_' + _id + '_please_login');
	please_login.style.display = show ? 'block' : 'none';
}

/*
 * AJAX
 * bookmarkt ein single
 */
function xxl_sf_bookmark(contents_types_id, _id)
{
	var post =
	{
		'contents_types_id': contents_types_id,
		'_id': _id
	};

	new Ajax.Request('api/bookmark/toggle',
	{
		parameters: post,

		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON();

			if (json.please_login)
			{
				xxl_sf_please_login(contents_types_id, _id, true);
			}
			else
			{
				var img = $('xxl_sf_' + contents_types_id + '_' + _id + '_bookmark');
				img.src = json.bookmarked ? img.src.replace(/_inactive/, '_active') : img.src.replace(/_active/, '_inactive');
			}
		}
	});
}

function xxl_bookmarklist_remove(_id, contents_types_id, element)
{
	new Ajax.Request('api/bookmark/toggle',
	{
		parameters:
		{
			'_id': _id,
			'contents_types_id': contents_types_id
		},

		onSuccess: function(transport)
		{
			$(element).up('.item').remove();
		}
	});

}

/* Get default Advertisement */
function xxl_default_adtech(host)
{
	var ret = '';

	new Ajax.Request('api/adtech/s_contentad',
	{
		parameters:
		{
			'host': host
		},

		onSuccess: function(transport)
		{
			var response = transport.responseText;
			ret = response;
		},

		onComplete: function()
		{
			xxl_set_default_adtech(ret);
		}

	});
}


/**
 * submit login
 */
function xxl_login_submit(location)
{
	$('xxl_login_info').update();
	$('xxl_login_button').hide();
	$('xxl_login_loading').show();

	new Ajax.Request('api/login',
	{
		parameters: $('xxl_login_form').serialize(true),

		onSuccess: function(transport)
		{
			var response = transport.responseText.evalJSON();

			if (response.js.is_auth)
			{
				window.location.href = location;
			}
			else
			{
				$('xxl_login_loading').hide();
				$('xxl_login_button').show();
				$('xxl_login_info').update(response.js.error);
			}
		}
	});

	return false;
}

/**
 * logout
 */
function xxl_logout()
{
	new Ajax.Request('api/logout',
	{
		onSuccess: function(transport)
		{
			var response = transport.responseText.evalJSON();

			if (!response.js.is_auth)
			{
				window.location.href = 'http://www.magnus.de';
			}
			else
			{

			}
		}
	});

	return false;
}

/**
 * fetch comments
 */
function xxl_comments_fetch(contents_id, contents_types_id, comments_page)
{
	new Ajax.Updater('xxl-comments-inner', 'api/comment/fetch',
	{
		parameters:
		{
			'_id': contents_id,
			'contents_types_id': contents_types_id,
			'comments_page': comments_page
		},

		onFailure: function()
			{
				alert('failed :-(');
			},

		onSuccess: function(transport)
		{
		}
	});

	return false;
}

/**
 * insert comment
 */
function xxl_comments_insert()
{
	if ($('xxl-comment').value != "")
	{
		new Ajax.Updater('xxl-comments-inner', 'api/comment/insert',
		{
			parameters: $('xxl-comment-insert').serialize(true),

			onFailure: function()
				{
					alert('failed :-(');
				},

			onSuccess: function(transport)
			{
				$('xxl-comment').value = "";
			}
		});
	}

	return false;
}

/**
 * show polls results
 */
function xxl_polls_show_results(polls_id)
{
	new Ajax.Updater('xxl-poll-'+polls_id+'-inner', 'api/poll/results',
	{
		parameters:
			{
				'polls_id': polls_id
			},

		onFailure: function()
			{
				alert('failed :-(');
			},

		onSuccess: function(transport)
		{
		}
	});

	return false;
}

/**
 * show polls front
 */
function xxl_polls_show_front(polls_id)
{
	new Ajax.Request('api/poll/front',
	{
		parameters:
			{
				'polls_id': polls_id
			},

		onFailure: function()
			{
				alert('failed :-(');
			},

		onSuccess: function(transport)
		{
			$('xxl-poll-'+polls_id+'-inner').up('div').replace(transport.responseText);
		}
	});

	return false;
}

/**
 * insert polls answer
 */
function xxl_polls_answer_insert(polls_id)
{
	var radioGrp = document['forms']['xxl-poll-'+polls_id]['xxl-poll-'+polls_id+'-value'];
	for(i=0; i < radioGrp.length; i++){
		if (radioGrp[i].checked == true) {
			var polls_answers_id = radioGrp[i].value;
		}
	}

	if (polls_answers_id)
	{
		new Ajax.Updater('xxl-poll-'+polls_id+'-inner', 'api/poll/insert',
		{
			parameters:
				{
					'polls_id': polls_id,
					'polls_answers_id': polls_answers_id
				},

			onFailure: function()
				{
					alert('failed :-(');
				},

			onSuccess: function(transport)
			{
			}
		});
	}

	return false;
}

/**
 * Downloads
 */
function xxl_dl_populate_version(downloads_id)
{
	var platforms_id = $F('xxl_dl_select_platform');

	new Ajax.Updater('xxl_dl_select_version', 'api/download/populate_version',
	{
		parameters:
			{
				'downloads_id': downloads_id,
				'platforms_id': platforms_id
			},

		onFailure: function()
			{
				alert('failed :-(');
			},

		onComplete: function()
		{
			xxl_dl_select_version();
		}
	});
}

function xxl_dl_populate_version1(downloads_id)
{
	var platforms_id = $F('xxl_dl_select_platform1');

	new Ajax.Updater('xxl_dl_select_version1', 'api/download/populate_version',
	{
		parameters:
			{
				'downloads_id': downloads_id,
				'platforms_id': platforms_id
			},

		onFailure: function()
			{
				alert('failed :-(');
			},

		onComplete: function()
		{
			xxl_dl_select_version1();
		}
	});
}

function xxl_dl_select_version()
{
	version = $F('xxl_dl_select_version').split('|');

	$('xxl_dl_versions_id').value = version[0];
	$('xxl_dl_ext_url').value = version[2];
	formatted_filesize = (version[1] / 1024 / 1024).toFixed(1);
	$('xxl-filesize').update(formatted_filesize);

	dl_time = xxl_get_download_time(version[1], 1024);

	var dl_time_str = "";

	if (dl_time[0] != 0)
	{
		dl_time_str += dl_time[0] + " Min. ";
	}

	dl_time_str += dl_time[1] + " Sek.";

	$('xxl_dl_time').update(dl_time_str);
}


function xxl_dl_select_version1()
{
	version = $F('xxl_dl_select_version1').split('|');

	$('xxl_dl_versions_id1').value = version[0];
	$('xxl_dl_ext_url1').value = version[2];
	formatted_filesize = (version[1] / 1024 / 1024).toFixed(1);
	$('xxl-filesize1').update(formatted_filesize);
		
	dl_time = xxl_get_download_time(version[1], 1024);

	var dl_time_str = "";

	if (dl_time[0] != 0)
	{
		dl_time_str += dl_time[0] + " Min. ";
	}

	dl_time_str += dl_time[1] + " Sek.";

	$('xxl_dl_time1').update(dl_time_str);
}


function xxl_signup()
{
	new Ajax.Request('api/signup',
	{
		parameters:
		{
			'username':			$F('xxl-signup-username'),
			'title':			$F('xxl-signup-title'),
			'firstname':		$F('xxl-signup-firstname'),
			'lastname':			$F('xxl-signup-lastname'),
			'email':			$F('xxl-signup-email'),
			'password':			$F('xxl-signup-password'),
			'password_repeat':	$F('xxl-signup-password-repeat'),
			'agb':				$F('xxl-signup-agb'),
			'datenschutz':		$F('xxl-signup-datenschutz'),
			'bonus_gmx':		$F('xxl-signup-gmx'),
			'bonus_softwareload':	$F('xxl-signup-softwareload')
		},

		onSuccess: function(transport)
		{
			var response = transport.responseText.evalJSON();

			if (response.failures)
			{
				$('xxl-signup-username-failure').update(response.failures.username);
				$('xxl-signup-password-failure').update(response.failures.password);
				$('xxl-signup-name-failure').update(response.failures.name);
				$('xxl-signup-email-failure').update(response.failures.email);
				$('xxl-signup-conditions-failure').update(response.failures.conditions);
			}
			else
			{
				$('xxl-signup-page').hide();
				$('xxl-signup-message').down('.inner').innerHTML = response.message;
				$('xxl-signup-message').show();
				scroll(0,0);
			}
		}
	});
}

function xxl_profile_change_password()
{
	form = $('xxl_profile_change_password').serialize(true);

	if (form.pwd_current=="")
	{
		$('xxl_profil_change_password_error').innerHTML = 'Bitte geben Sie Ihr aktuelles Passwort an.';
		$('xxl_profil_change_password_error').show();
		$('xxl_profil_change_password_success').hide();
	}
	else if (form.pwd_new=="")
	{
		$('xxl_profil_change_password_error').innerHTML = 'Bitte geben Sie das gewünschte neue Passwort an.';
		$('xxl_profil_change_password_error').show();
		$('xxl_profil_change_password_success').hide();
	}
	else if (form.pwd_new != form.pwd_new_check)
	{
		$('xxl_profil_change_password_error').innerHTML = 'Die eingegebenen Passwörter stimmen nicht überein. Bitte wiederholen Sie die Eingabe.';
		$('xxl_profil_change_password_error').show();
		$('xxl_profil_change_password_success').hide();
	}
	else
	{
		new Ajax.Request('api/profile_change_password',
		{
			parameters:
			{
				'pwd_current':	form.pwd_current,
				'pwd_new':	form.pwd_new
			},

			onSuccess: function(transport)
			{
				var response = transport.responseText.evalJSON();

				if (response.js.success == 1)
				{
					$('xxl_profil_change_password_error').hide();
					$('xxl_profil_change_password_success').innerHTML = 'Ihr Passwort wurde erfolgreich geändert.';
					$('xxl_profil_change_password_success').show();
					$('xxl_pwd_current').value = '';
					$('xxl_pwd_new').value = '';
					$('xxl_pwd_new_check').value = '';
				}
				else
				{
					$('xxl_profil_change_password_error').innerHTML = response.js.error_msg;
					$('xxl_profil_change_password_error').show();
					$('xxl_profil_change_password_success').hide();
				}
			}
		});
	}

}

function xxl_profile_change_email()
{
	new Ajax.Request('api/profile_change_email',
	{
		parameters:
		{
			'email':	$F('xxl_new_email')
		},

		onSuccess: function(transport)
		{
			var response = transport.responseText.evalJSON();

			if (response.js.success == 1)
			{
				$('xxl_profil_change_email_error').hide();
				$('xxl_profil_change_email_success').innerHTML = 'Wir haben Ihnen soeben eine E-Mail mit einem Bestätigungslink an die neue Adresse geschickt. Um Ihre neue E-Mail-Adresse zu aktivieren, klicken Sie bitte den Link in der E-Mail.';
				$('xxl_profil_change_email_success').show();
				$('xxl_new_email').value = '';
			}
			else
			{
				$('xxl_profil_change_email_error').innerHTML = response.js.error_msg;
				$('xxl_profil_change_email_error').show();
				$('xxl_profil_change_email_success').hide();
			}
		}
	});
}