var Fotos = {

	OpenReplace: function(id) {
		if($('edit_comment'+id).style.display=='none') {
			$('edit_comment'+id).style.display='block';
			
			$('edit_comment_textfield'+id).style.display='none';
			$('edit_comment_speichern'+id).style.display='none';
			$('edit_comment_abbrechen'+id).style.display='none';
		} else {
			$('edit_comment'+id).style.display='none';
			
			$('edit_comment_textfield'+id).style.display='block';
			$('edit_comment_speichern'+id).style.display='block';
			$('edit_comment_abbrechen'+id).style.display='block';
		}
	},
	
	ReplaceSave: function(id,pid,message) {
		
		if (message != "") {
			$('edit_comment'+id).innerHTML = $('edit_comment_textfield'+id).value;
			
			new Ajax.Request('include/content/fotos/ajax.php?action=editcomment&pid='+pid,
								{
									method:'post',
									parameters:'value='+message,
									onSuccess:function(tr) {
										
									}
								}
							);
		}
		this.OpenReplace(id);
	
	}

}

function searchFriends() {
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method:'get',
			parameters:'action=searchfriends&search='+document.getElementById('freundesuche').value,
			onSuccess:function(tr) {

				document.getElementById('pin_friend').options.length = 0; // Select leeren.

				var tmp = tr.responseText.split('|||');
				if(tr.responseText != '') {
					for (j = 0; j < tmp.length; j++) {
						var iduser = tmp[j].split('||');
						NeuerEintrag = new Option(iduser[1], iduser[0]);
						document.getElementById('pin_friend').options[j] = NeuerEintrag;
					}
				}
			}
		}
	);
}

function changedays(month, year) {
	switch (month) {
		case "all": days = 0; break;
		case "0": days = 0; break;
		case "1": days = 31; break;
		case "2": if ((year % 4) == 0) { days = 29; } else { days = 28; } break;
		case "3": days = 31; break;
		case "4": days = 30; break;
		case "5": days = 31; break;
		case "6": days = 30; break;
		case "7": days = 31; break;
		case "8": days = 31; break;
		case "9": days = 30; break;
		case "10": days = 31; break;
		case "11": days = 30; break;
		case "12": days = 31; break;
	}
	claus = 31;
	while (claus > 0) {
		if (days < claus) {
			$F('form_date').elements['day'].options[claus].style.display = 'none';
			$F('form_date').elements['day'].options[claus].disabled = true;
		} else {
			$F('form_date').elements['day'].options[claus].style.display = 'block';
			$F('form_date').elements['day'].options[claus].disabled = false;
		}
		claus--;
	}
	if (days == 0) {
		$F('form_date').elements['day'].options[0].selected = true;
	}
	if ($F('form_date').elements['day'].options[$F('form_date').elements['day'].selectedIndex].value > days) {
		$F('form_date').elements['day'].options[0].selected = true;
	}
}

function showStars(nums) {
	$('myvote_img').src = './images/fotos/icon_vote'+nums+'.jpg';	
}

function showBarometer(nums) {
	$('mybaro_img').src = './images/partybarometer/regler_'+nums+'.jpg';	
}

function WriteComment(aid, pid) {
	
	$('comments_text').disabled = true;
	$('comments_button').disabled = true;
	$('comments_button').value = 'Bitte warten...';
	if (($('comments_text').value != "Kommentarfeld") && ($('comments_text').value != "")) {
		new Ajax.Request('include/content/fotos/ajax.php?',
			{
				method: 'get',
				parameters: 'action=writecomment&aid=' + aid + '&pid=' + pid + '&message=' + $('comments_text').value,
				onSuccess: function(tr) {
					var tmp = tr.responseText.split('|||');
					
					new Effect.Fade('picture_comment_ajax', { duration: 1.0, afterFinish: function() { $('picture_comment_ajax').innerHTML = tmp[0]; new Effect.Appear('picture_comment_ajax'); } });
					
					new Effect.Fade('comments_rahmen_ajax', { duration: 1.0, to: 0.01, afterFinish: function() { $('comments_rahmen_ajax').innerHTML = tmp[1]; new Effect.Appear('comments_rahmen_ajax', { from: 0.01 }); } });
					
					$('comments_text').value = '';
					new Effect.Fade('comments_text'); // ausblenden
					new Effect.Fade('comments_button'); // ausblenden
					new Effect.Fade('charsleft');
					$('comments_button').value = 'Abschicken';
				}
			}
		);
	} else {
		alert("Sie haben vergessen eine Nachricht einzugeben.");
		$('comments_text').disabled = false;
		$('comments_button').disabled = false;
		$('comments_button').value = 'Abschicken';
	}
}

function giveVote(pid, stars) {
	
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=givevote&pid=' + pid + '&stars=' + stars,
			onSuccess: function(tr) {
				var tmp = tr.responseText.split('|||');
				
				new Effect.Fade('vote_img', { duration: 1.0, afterFinish: function() { $('vote_img').src = './images/fotos/icon_vote' + tmp[0] + '.jpg'; new Effect.Appear('vote_img'); } });
				
				new Effect.Fade('vote_count', { duration: 1.0, afterFinish: function() { $('vote_count').innerHTML = '(' + tmp[1] + ')'; new Effect.Appear('vote_count'); } });

				var bewertung_areas = $('bewertung_map').firstChild;
				while (bewertung_areas != null) {
					if (bewertung_areas.nodeName != '#text') {
						bewertung_areas.onmouseout = function () { showStars(stars); };
					}
					bewertung_areas = bewertung_areas.nextSibling;
				}
				$('myvote_img').src = './images/fotos/icon_vote' + stars + '.jpg';
			}
		}
	);
}

function giveBarometer(aid, bewertung) {
	
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=givebarometer&aid=' + aid + '&bewertung=' + bewertung,
			onSuccess: function(tr) {
				
				var tmp = tr.responseText.split('|||');
			
				new Effect.Fade('barometer_img', { duration: 1.0, afterFinish: function() { $('barometer_img').src = './images/partybarometer/pbm_' + tmp[0] + '.jpg'; new Effect.Appear('barometer_img'); } });
				new Effect.Fade('partybarometer_users', { duration: 1.0, afterFinish: function() { $('partybarometer_users').innerHTML = 'Es haben bisher ' + tmp[1] + ' User gevotet.'; new Effect.Appear('partybarometer_users'); } });

				var bewertung_areas = $('partybarometer_map').firstChild;
				while (bewertung_areas != null) {
					if (bewertung_areas.nodeName != '#text') {
						bewertung_areas.onmouseout = function () { showBarometer(bewertung); };
					}
					bewertung_areas = bewertung_areas.nextSibling;
				}
				$('mybaro_img').src = './images/partybarometer/regler_' + bewertung + '.jpg';
			}
		}
	);
}

function setPin(uid, aid, pid, x, y) {
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=setpin&uid=' + uid + '&aid=' + aid + '&pid=' + pid + '&x=' + x + '&y=' + y,
			onSuccess: function(tr) {
				
				if(tr.responseText != '') {
					var tmp = tr.responseText.split('|||');
					
					new Effect.Fade('pin_friends', { duration: 1.0 });
					
					if (tmp[0] == 'set') {
						
						new Effect.Fade('picture_pins_ajax', { duration: 1.0, afterFinish: function() { $('picture_pins_ajax').innerHTML = tmp[1]; new Effect.Appear('picture_pins_ajax'); } }); // Anzahl der Pins
						new Effect.Fade('img_pins_ajax', { duration: 1.0, afterFinish: function() { $('img_pins_ajax').src = './images/fotos/icon_pins.jpg'; new Effect.Appear('img_pins_ajax'); } }); // Bild vor Anzahl
						
						$('pins_ajax').innerHTML += tmp[2]; // Pin hinzufügen
						new Effect.Appear('new_pin' + tmp[4]); // Pin Appearen
						
						$('user_pins_ajax').innerHTML += tmp[3]; // Neuen Namen hinzufügen
						new Effect.Appear('new_user_pin' + tmp[4]); // Neuer Name
					
					}
					else if (tmp[0] == 'update') {
						
						new Effect.Fade('pins_ajax', { duration: 1.0, afterFinish: function() { $('pins_ajax').innerHTML = tmp[1]; new Effect.Appear('pins_ajax'); } });
						new Effect.Fade('user_pins_ajax', { duration: 1.0, afterFinish: function() { $('user_pins_ajax').innerHTML = tmp[2]; new Effect.Appear('user_pins_ajax'); } });
						
					}

				}
			}
		}
	);
}

function delPin(pinid, pid, uid, cookie, set_uid) {
	if (Flag_togglePins == 1) {
		if (uid == cookie || set_uid == cookie) {
			if (confirm("Soll die Verlinkung wirklich gelöscht werden?")) {
				new Ajax.Request('include/content/fotos/ajax.php?',
					{
						method: 'get',
						parameters: 'action=delpin&pinid=' + pinid + '&pid=' + pid,
						onSuccess: function(tr) {
							
							if(tr.responseText != '') {
								var tmp = tr.responseText.split('|||');
								
								new Effect.Fade('picture_pins_ajax', { duration: 1.0, afterFinish: function() { $('picture_pins_ajax').innerHTML = tmp[0]; new Effect.Appear('picture_pins_ajax'); } }); // Anzahl der Pins
								
								new Effect.Fade('img_pins_ajax', { duration: 1.0, afterFinish: function() { $('img_pins_ajax').src = './images/fotos/icon_pins.jpg'; new Effect.Appear('img_pins_ajax'); } }); // Bild vor Anzahl
								
								new Effect.Fade('pins_ajax', { duration: 1.0, afterFinish: function() { $('pins_ajax').innerHTML = tmp[1]; new Effect.Appear('pins_ajax'); } }); // Pins im Bild
								
								new Effect.Fade('user_pins_ajax', { duration: 1.0, afterFinish: function() { $('user_pins_ajax').innerHTML = tmp[2]; new Effect.Appear('user_pins_ajax'); } }); // Namen der Pins
			
							}
						}
					}
				);
			}
		} else {
			alert("Diese Verlinkung kann von dir nicht entfernt werden.");	
		}
	} else {
		location.href = "index.php?inc=community&section=profil&uid="+uid;
	}
}

function showNextPicture (dir, aid, aktpid, nextimg, text) {
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=nextpic&aid=' + aid + '&pid=' + aktpid + '&nextimg=' + nextimg,
			onSuccess: function(tr) {
				if (tr.responseText != "Ende") {
					var tmp = tr.responseText.split('|||');
					
					updatePicInfos(tmp[0], aid);
					
					$('nav_nextpic').onclick = function () { showNextPicture(dir, aid, tmp[0], tmp[1], text); };
					$('nav_prevpic').onclick = function () { showPrevPicture(dir, aid, tmp[0], tmp[1], text); };
					
					new Effect.Fade('akt_pic', { duration: 1.0, afterFinish: function() { $('akt_pic').src = './include/Thumbnail.php?size=600x600&folder=.' + dir + '&image=' + tmp[1] + '&overlay=logo&text=' + text + '&defimg=nouser.gif'; new Effect.Appear('akt_pic'); } });
					
				} else {
					alert('Ende der Galerie erreicht.');	
				}
			}
		}
	);
	
}

function showPrevPicture(dir, aid, aktpid, nextimg, text) {
	
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=prevpic&aid=' + aid + '&pid=' + aktpid + '&nextimg=' + nextimg,
			onSuccess: function(tr) {
				if (tr.responseText != "Ende") {
					var tmp = tr.responseText.split('|||');
					
					updatePicInfos(tmp[0], aid);
					
					$('nav_nextpic').onclick = function () { showNextPicture(dir, aid, tmp[0], tmp[1], text); };
					$('nav_prevpic').onclick = function () { showPrevPicture(dir, aid, tmp[0], tmp[1], text); };
					
					//new Effect.Fade('akt_pic', { duration: 1.0, afterFinish: function() { 
					$('akt_pic').src = './include/Thumbnail.php?size=600x600&folder=.' + dir + '&image=' + tmp[1] + '&overlay=logo&text=' + text + '&defimg=nouser.gif';
					//new Effect.Appear('akt_pic'); } });
				
				} else {
					alert('Anfang der Galerie erreicht.');	
				}
			}
		}
	);
}

function startDiaShow(dir, aid, aktpid, nextpid, nextimg, text) {
	
	updateCounterForDiashow();
	
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=nextpic&aid=' + aid + '&pid=' + nextpid + '&nextimg=' + nextimg,
			onSuccess: function(tr) {
				
				var tmp = tr.responseText.split('|||');
				updatePicInfos(nextpid, aid);
				$('nav_prevpic').style.display = '';
				$('nav_prevpic').href = 'index.php?inc=fotos&section=picture&aid='+aid+'&pid='+aktpid;

				$('akt_pic').src = './include/Thumbnail.php?size=600x600&folder=.' + dir + '&image=' + nextimg + '&overlay=logo&text=' + text + '&defimg=nouser.gif';

				if (tmp[0] != "Ende") {
					
					$('nav_diashow').innerHTML = 'Diashow pausieren';
					$('nav_nextpic').href = 'index.php?inc=fotos&section=picture&aid='+aid+'&pid='+tmp[0];
					$('nav_diashow').onclick = function () { endDiaShow(dir, aid, nextpid, tmp[0], tmp[1], text); };
					
					diaShow = setTimeout(function()  { startDiaShow(dir, aid, nextpid, tmp[0], tmp[1], text) }, 5000);
					
				} else {
					
					doatdiashowend(dir, aid, aktpid, text);
					
				}
			}
		}
	);
}

var firstCount = true;
function updateCounterForDiashow() {
	if (firstCount) {
		new Ajax.Request('include/content/fotos/ajax.php?',
			{
				method: 'get',
				parameters: 'action=countdiashow',
			}
		);
	}
	firstCount = false;
}

function endDiaShow(dir, aid, aktpid, nextpid, nextimg, text) {
	clearTimeout(diaShow);
	$('nav_diashow').innerHTML = 'Diashow starten';
	$('nav_diashow').onclick = function () { startDiaShow(dir, aid, aktpid, nextpid, nextimg, text) };
	$('nav_nextpic').href = 'index.php?inc=fotos&section=picture&aid='+aid+'&pid='+nextpid;
}

function doatdiashowend(dir, aid, prevpid, text) {
	clearTimeout(diaShow);
	$('nav_diashow').innerHTML = 'Diashow starten';
	$('nav_diashow').style.display = 'none';
	$('nav_nextpic').style.display = 'none';
}

function updatePicInfos(pid, aid) {
	
	new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=updatepicinfos&aid=' + aid + '&pid=' + pid,
			onSuccess: function(tr) {
					var tmp = tr.responseText.split('|||');
					
					new Effect.Fade('picture_clicks_ajax', { duration: 1.0, afterFinish: function() { $('picture_clicks_ajax').innerHTML = tmp[0]; new Effect.Appear('picture_clicks_ajax'); } });
					new Effect.Fade('img_clicks_ajax', { duration: 1.0, afterFinish: function() { $('img_clicks_ajax').src = './images/fotos/icon_klicks.jpg'; new Effect.Appear('img_clicks_ajax'); } });
					
					new Effect.Fade('picture_pins_ajax', { duration: 1.0, afterFinish: function() { $('picture_pins_ajax').innerHTML = tmp[1]; new Effect.Appear('picture_pins_ajax'); } });
					new Effect.Fade('img_pins_ajax', { duration: 1.0, afterFinish: function() { $('img_pins_ajax').src = './images/fotos/icon_pins.jpg'; new Effect.Appear('img_pins_ajax'); } });
					
					new Effect.Fade('picture_comment_ajax', { duration: 1.0, afterFinish: function() { $('picture_comment_ajax').innerHTML = tmp[2]; new Effect.Appear('picture_comment_ajax'); } });
					new Effect.Fade('img_comments_ajax', { duration: 1.0, afterFinish: function() { $('img_comments_ajax').src = './images/fotos/icon_comments.jpg'; new Effect.Appear('img_comments_ajax'); } });
					
					new Effect.Fade('vote_img', { duration: 1.0, afterFinish: function() { $('vote_img').src = './images/fotos/icon_vote' + tmp[3] + '.jpg'; new Effect.Appear('vote_img'); } });
				
					new Effect.Fade('vote_count', { duration: 1.0, afterFinish: function() { $('vote_count').innerHTML = '(' + tmp[4] + ')'; new Effect.Appear('vote_count'); } });
					
					var bewertung_areas = $('bewertung_map').firstChild;
					while (bewertung_areas != null) {
						if (bewertung_areas.nodeName != '#text') {
							bewertung_areas.onmouseout = function () { showStars(tmp[5]); };
						}
						bewertung_areas = bewertung_areas.nextSibling;
					}
					
					new Effect.Fade('myvote_img', { duration: 1.0, afterFinish: function() { $('myvote_img').src = './images/fotos/icon_vote' + tmp[5] + '.jpg'; new Effect.Appear('myvote_img'); } });
					
					new Effect.Fade('picture_shooter_ajax', { duration: 1.0, afterFinish: function() { $('picture_shooter_ajax').innerHTML = tmp[6]; new Effect.Appear('picture_shooter_ajax'); } });
					new Effect.Fade('img_shooter_ajax', { duration: 1.0, afterFinish: function() { $('img_shooter_ajax').src = './images/fotos/icon_shooter.jpg'; new Effect.Appear('img_shooter_ajax'); } });
					
					new Effect.Fade('pins_ajax', { duration: 1.0, afterFinish: function() { $('pins_ajax').innerHTML = tmp[7]; new Effect.Appear('pins_ajax'); } });
					
					new Effect.Fade('user_pins_ajax', { duration: 1.0, afterFinish: function() { $('user_pins_ajax').innerHTML = tmp[8]; new Effect.Appear('user_pins_ajax'); } });
					
					new Effect.Fade('comments_rahmen_ajax', { duration: 1.0, to: 0.01, afterFinish: function() { $('comments_rahmen_ajax').innerHTML = tmp[9]; new Effect.Appear('comments_rahmen_ajax', { from: 0.01 }); } });

					
					if ($('comments_text').disabled == false) {
					
						if (tmp[10] == 'ausblenden') {
							
							new Effect.Fade('comments_text');
							new Effect.Fade('charsleft');
							new Effect.Fade('comments_button');
							$('comments_text').disabled = true;
							$('comments_button').disabled = true;
							
						}
					}
					else if ($('comments_text').disabled == true) {

						if (tmp[10] == 'einblenden') {
							
							$('comments_text').disabled = false;
							$('comments_button').disabled = false;
							new Effect.Appear('comments_text');
							new Effect.Appear('comments_button');
							new Effect.Appear('charsleft');
							
						}
					}
			}
		}
	);
	
}

var Flag_togglePins = 0;
function togglePins() {
	var bild = $('akt_pic');
	if (Flag_togglePins == 0) {
		bild.onmouseover = function () {
			bild.style.cursor = 'crosshair';
		}
		bild.onmouseout = function () {
			bild.style.cursor = '';
		}
		bild.onclick = setpin_activate;
		$('nav_pins').style.backgroundColor = '#ADD699';
		$('nav_pins').innerHTML = 'Bearbeitung beenden';
		Flag_togglePins = 1;
	} else {
		bild.onmouseover = function () {
			bild.style.cursor = '';
			new Effect.Fade('pins_ajax', { duration: 1.0, queue: 'end' });
		}
		bild.onmouseout = function () {
			bild.style.cursor = '';
			new Effect.Appear('pins_ajax', { duration: 1.0, queue: 'end' });
		}
		
		bild.onclick = undefined;
		$('nav_pins').style.backgroundColor = '';
		$('nav_pins').innerHTML = 'Verlinkung bearbeiten';
		new Effect.Fade('pin_friends', { duration: 1.0 });
		Flag_togglePins = 0;
	}
}

function setpin_activate(e) {
	if(!e) e = window.event;
	var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body;
	if (e.pageX) {
		x = e.pageX;
		y = e.pageY;
	}
	else {
		x = e.clientX + body.scrollLeft  - body.clientLeft;
		y = e.clientY + body.scrollTop - body.clientTop;
	}
	document.getElementById('setpin_x').value = x - 32; // pins sind 64 px breit und hoch
	document.getElementById('setpin_y').value = y - 32;
	document.getElementById('pin_friends').style.left = x + 'px';
	document.getElementById('pin_friends').style.top = y + 'px';
	
	new Effect.Appear('pin_friends');
}

function picdel_request(aid, pid) {
		new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=picdel_request&aid=' + aid + '&pid=' + pid + '&grund=' + $('picdel_grund').value + '&email=' + $('picdel_email').value,
			onSuccess: function(tr) {
				alert('Antrag wurde gesendet.');
				new Effect.SlideUp('delpic_form');
				Flag_sendDelrequest=0;
				document.getElementById('picdel_grund').value = 'Grund';
				document.getElementById('picdel_email').value = 'E-Mail';
			}
			
		}
		);
}

function showfriend_picture(aid, pid) {
		new Ajax.Request('include/content/fotos/ajax.php?',
		{
			method: 'get',
			parameters: 'action=showfriend_picture&aid=' + aid + '&pid=' + pid + '&kommentar=' + decodeURIComponent($('showpic_comment').value) + '&email=' + decodeURIComponent($('showpic_email').value),
			onSuccess: function(tr) {
				alert(tr.responseText);
				new Effect.SlideUp('showfriend_form');
				document.getElementById('showpic_email').value = 'E-Mail';
				document.getElementById('showpic_comment').value = 'Kommentar';
			}
			
		}
		);
}

function Toggle_BlindDown(id) {	
	if(document.getElementById(id).style.display=='none') {
		
		if (id == 'showfriend_form' && document.getElementById('delpic_form').style.display!='none') {
			new Effect.BlindUp('delpic_form', { duration: 0.5, queue: 'end' });
		}
		
		if (id == 'delpic_form' && document.getElementById('showfriend_form').style.display!='none') {
			new Effect.BlindUp('showfriend_form', { duration: 0.5, queue: 'end' });
		}
		
		new Effect.BlindDown(id, { duration: 0.5, queue: 'end' });
	} else {
		new Effect.BlindUp(id, { duration: 0.5, queue: 'end' });
	}
}

function hideInfoLayer() {
	if (document.getElementById('infolayercheck').checked) {
		new Ajax.Request('include/content/fotos/ajax.php?',
			{
				method: 'get',
				parameters: 'action=hideinfolayer',
				onSuccess: function(tr) {
					new Effect.Fade('infolayer', { duration: 0.5, queue: 'end' });
				}
			} );
	} else {
		new Effect.Fade('infolayer', { duration: 0.5, queue: 'end' });
	}
}

function hideRegInfoLayer() {
	new Effect.Fade('infolayer', { duration: 0.5, queue: 'end' });

}

function inputlimit(element, limit) {
	if (element.value.length > limit) {
		element.value = element.value.substring(0, limit);
	}
	else { 
		document.getElementById('charsleft').innerHTML = 'Noch ' + (limit - element.value.length) + ' Zeichen übrig.';
	}
}
