//body onload handler:
$(document).ready(function(){
	//http://fancybox.net/howto
	var config = {
		overlayOpacity: 0.5
	}
	$('.newspic a').fancybox(config);
	$('.fancy').fancybox(config);
	
	
	//Avatargrößen in den News fixen!
	resizeCommentAvatars();	//einmal instant
	setTimeout('resizeCommentAvatars()', 2500); //nach 2,5sek
	setTimeout('resizeCommentAvatars()', 7000); //und nach 7sek nochmal!
	//in der Hoffnung das alle geladen sind
	//nach 10sek greißen die Ajaxcomments und das wirds eh wieder ausgeführt :P
})

/*
	########################################################
			FUNKTIONEN WERDEN AB HIER DEFINIERT
	########################################################
*/
// Zeigt ein Objekt mit übergebener ID an, falls es gerade versteckt ist und umgekehrt
function show_hide(id) {
	$('#'+id).toggle();
}

// Zeigt ein Objekt mit übergebener ID an, falls es gerade versteckt ist und umgekehrt UND ändert auch ein übergebenes Bild
function show_hide_image(id, image_id, image_visible, image_invisible) {

	if (document.getElementById(id).style.display == 'block') {
		document.getElementById(image_id).src = image_invisible;
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(image_id).src = image_visible;
		document.getElementById(id).style.display = 'block';
	}
}

// Speichert das Öffnen / Schließen eines Menüs in der linken Navi.
function toggle_menu(uid, mid, status) {
	
	// Ein / Ausklappeffekt
	$("#lmenu"+mid).slideToggle("slow");
	// Status zum richtigen Speichern vorbereiten & Pfeil ändern
	if (status == 'none')
	{
		status = 'block';
		document.getElementById('img_'+mid).src = '/images/menu_up.gif';
	} else
	{
		status = 'none';
		document.getElementById('img_'+mid).src = '/images/menu_down.gif';
	}
	
	// Speichern
	var data = {
		module: "save_menusettings",
		tkn: rand(5),
		uid: uid,
		mid: mid,
		status: status
	}
	
	//speichern!
	$.get('/ajax.php', data, function(result) {
		//DEBUG: alert(result);
	});
}


//öffnet ein Popupfenster:
function Fenster(modul, suffix)
{
	var url = '/popup.php?action='+modul;
	
	if(suffix != '')
		url += '&arg='+suffix;
		
	var width = (screen.width-150)/2;
	var height = (screen.height-50)/2;
	args = 'width=150,height=50,scrollbars=yes,resizable=yes,left='+width+',top='+height;
	fenster = window.open(url, "fenster1", args);
	fenster.focus();
}

function Drucken(modul, id)
{
	url = '/print.php?action='+modul+'&id='+id;
	var width = (screen.width-640)/2;
	var height = (screen.height-480)/2;
	args = 'width=640,height=480,scrollbars=yes,resizable=yes,left='+width+',top='+height;
	fenster = window.open(url, "druckfenster", args);
	fenster.focus();
}

function showLightbox(imagePath, imageID) {
	document.getElementById("lightbox_overlay").style.height = document.body.offsetHeight + "px";
	document.getElementById("lightbox_overlay").style.display = "block";
	document.getElementById("pic_fullscreen").src = imagePath;
	loadRating(imageID);
	loadComments(imageID);
	return false;
}

function rand(length) {
	var token = new String(Math.random());
	length+=2;
	return token.substring(2,length);
}

function reportBrokenLink(id)
{
	if(confirm("Möchten Sie Mirror #"+id+" wirklich melden?"))
	{
		var data = {
			mirror_id: id,
			mirror_url: document.location.href
		}
		
		$.post('/ajax.php?module=reportLink', data, function(result){
			alert(result);
		});
	}
}

function reportBrokenModLink(type)
{
	if(confirm("Möchten Sie den Link (Mod-"+type+") wirklich melden?"))
	{
		var data = {
			link_url: document.location.href,
			link_type: type
		}
		
		$.post('/ajax.php?module=reportModLink', data, function(result){
			alert(result);
		});
	}
}

function resizeCommentAvatars()
{
	$('#comments .comment .frame img').each(function(){
		var img = $(this);
		if(img.width() > 125) {
			img.width(125);
		}
		
		if(img.height() > 150) {
			img.width(150);
		}
	});
	
	//if(console && console.info)
	//	console.info(counter + ' of '+total+' Avatars resized');
}


function hideieinfo() {$('#ieinfo').fadeOut();}
function ieinfo() {$('#ieinfo').fadeIn();}

//Socialtoolbar object:
var socialToolbar = {
	"tweet": function(url) {
		window.open(url, 'tweetThis', 'width=800,height=480,scrollbars=1')
	}
}

