function do_news_toggle(id)
{
	if($('#' + id + ' #short_text').css('display') == 'none')
	{
		$('#' + id + ' #short_text').show();
		$('#' + id + ' #long_text').hide();
	}
	else
	{
		$('#' + id + ' #short_text').hide();
		$('#' + id + ' #long_text').show();
	}
}

function do_information_toggle(content_id, edit_id)
{
	var contentOBJ = document.getElementById(content_id);
	var editOBJ    = document.getElementById(edit_id);

	if(editOBJ.style.display == 'none')
	{
		editOBJ.style.display 	 = 'block';
		contentOBJ.style.display = 'none';
	}
	else
	{
		editOBJ.style.display    = 'none';
		contentOBJ.style.display = 'block';
	}
}

function do_attributes_toggle(content_id, edit_id)
{
	var contentOBJ = document.getElementById(content_id);
	var editOBJ    = document.getElementById(edit_id);

	if(editOBJ.style.display == 'none')
	{
		editOBJ.style.display 	 = 'block';
		contentOBJ.style.display = 'none';
	}
	else
	{
		editOBJ.style.display    = 'none';
		contentOBJ.style.display = 'block';
	}
}

//Bindet alle Fancyboxes
function bind_fancybox()
{
	$('a.iframe').each(function(){
		var width_match = $(this).attr('href').match(/width=[0-9]+/i);
		var height_match = $(this).attr('href').match(/height=[0-9]+/i);
		if (width_match !== null)
		{
			var fWidth = parseInt(width_match[0].replace('width=',''));
		} else {
			var fWidth = 500;
		}

		if (height_match !== null)
		{
			var fHeight = parseInt(height_match[0].replace('height=',''));
		} else {
			var fHeight = 300;
		}

		$(this).fancybox({
			'frameWidth':fWidth,
			'frameHeight':fHeight,
			'hideOnContentClick': true,
			'showCloseButton': true,
			'overlayColor': '#065080',
			'overlayOpacity': 0.85,
			'padding': 0
		});
	});

	$("a.iframe-last-minute").fancybox({
		'hideOnContentClick': true,
		'frameWidth': 530,
		'frameHeight': 331,
		'showCloseButton': true
	});

	$("a.iframe-multiupload").fancybox({
		'hideOnContentClick': true,
		'frameWidth': 530,
		'frameHeight': 400,
		'showCloseButton': true
	});

	$("a.iframe-places").fancybox({
		'hideOnContentClick': true,
		'frameWidth': 600,
		'frameHeight': 650,
		'showCloseButton': true,
		'callbackOnClose': function(){
			window.location.reload();
		}
	});

	$("a.inline").fancybox({
		'hideOnContentClick': true,
		'frameWidth': 500,
		'showCloseButton': true,
		'overlayColor': '#065080',
		'overlayOpacity': 0.85,
		'padding': 0
	});

	$("a.inline_stay").fancybox({
		'frameWidth': 500,
		'showCloseButton': true,
		'hideOnContentClick': false
	});

	$("a.message").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 500,
		'frameHeight': 100,
		'showCloseButton': true
	});
}

function do_login()
{
	$('#login_message').hide();
	ajax_post('/home/login', {
		'email': $('#login_email').val(),
		'password': $('#login_password').val(),
	}, null, $('#login_loader'), null, login_result, login_result);
	return false;
}

function login_result(data)
{
	if (typeof(data.status) == 'undefined')
	{
		var message = 'Unbekannter Login-Fehler';
	} else if (data.status == 'error')
	{
		message = data.message;
	} else {
		window.location.reload();
	}
	$('#login_message').html(message).fadeIn('slow');
}

//Result-Funktion nach Ajax-Aufruf
function ajax_result(data, element, mode)
{
	if (element)
	{
		switch (mode)
		{
			case 'html': element.html(data.content); break;
			case 'html_fade': element.html(data.content).fadeIn('slow'); break;
			case 'replace': element.replaceWith(data.content); break;
			case 'append': element.append(data.content); break;
			case 'prepend': element.prepend(data.content); break;
			case 'alert': alert(data.content); break;
			case 'delete': element.remove(); break;
			case 'delete_fade': element.fadeOut('slow', function(){
				$(this).remove();
			}); break;
		};
	}
}

//Ajax-Fehler
function ajax_error(xhr, ajaxOptions, thrownError)
{
	alert(xhr.statusText);
}

//Ajax-Call, ajax_url = Url zum aufrufen, element = welches manipuliert wird, loader = loader image, mode = 'html, replace, append, prepend, alert etc.',
//async = ist die Übertragung asynchron, onsuccess = Funktion die bei erfolg aufgerufen wird
function ajax_call(ajax_url, element, loader, mode, async, onsuccess)
{
	if (loader)
	{
		loader.show();
	}
	$.ajax({
		url: ajax_url,
		async: async,
		dataType: 'json',
		success: function(data){
			if (loader)
			{
				loader.hide();
			}
			ajax_result(data, element, mode);
			if(onsuccess)
			{
				onsuccess(data);
			}
		},
		error: function(xhr, ajaxOptions, thrownError){
			if (loader)
			{
				loader.hide();
			}
			ajax_error(xhr, ajaxOptions, thrownError);
		}
	});
}

//Ajax-Post, ähnlich wie oben
function ajax_post(post_url, post_data, element, loader, mode, onsuccess, onfail)
{
	if (loader)
	{
		loader.show();
	}
	$.post(post_url, post_data,
		function response(data){
			if (loader)
			{
				loader.hide();
			}
			ajax_result(data, element, mode);
			if (data.status == 'ok')
			{
				if(onsuccess)
				{
					onsuccess(data);
				}
			} else {
				if(onfail)
				{
					onfail(data);
				}
			}
		},'json');
}

Cufon.replace('.cufon', {
	hover: true
});

$(function(){
	$('div.heyday_container div.heyday').click(function(){
		window.location = $(this).find('a.heyday_link').attr('href');
	})

	$('input.preval').focus(function(){
		var cur_val = $(this).val();
		var preval = $(this).attr('alt');
		if (preval == cur_val)
		{
			$(this).val('').removeClass('preval');
		}
	}).blur(function(){
		var cur_val = $(this).val();
		var preval = $(this).attr('alt');
		if (cur_val == '')
		{
			$(this).val(preval).addClass('preval');
		}
	});

	$('a.show_extended').click(function(){
		$(this).prevAll('ul.cat_list.extended').first().slideToggle();
	});

	$("a.write-message, a.write-message-watchlist").fancybox({
		'hideOnContentClick': true,
		'frameWidth': 500,
		'frameHeight': 400,
		'showCloseButton': true
	});

	$('#login-email').focus(function(){
		if ($('#login-password').val() == '#<>-1*,'){
			$('#login-password').val('');
		}
	});

	bind_fancybox();

	$('div#login_submit img').click(do_login);
	$('form#frm_login').submit(do_login);
	$('#login_email').keypress(function(event){
		if (event.which == '13')
		{
			do_login();
		}
	});
	$('#login_password').keypress(function(event){
		if (event.which == '13')
		{
			do_login();
		}
	});
})
