Event.observe(window, "load", function() {
	init();
});

// Initialisierung
function init() {
	
	var autocomplete = document.createAttribute('autocomplete');
	autocomplete.nodeValue = 'off';
	
	if(document.cocktailform) {
				
		document.cocktailform.setAttributeNode(autocomplete);
		document.cocktailform.onsubmit = searchItem;
		document.cocktailform.q.onkeyup = searchItem;
		document.cocktailform.q.focus();
		
		updateFromHash();
		
		gmaps.loadIndexBars();
	}
	
	if(document.add) {
		document.add.cocktail.focus();		
		document.add.setAttributeNode(autocomplete);

	}
	
	if(document.cocktailsuchen) {
		document.cocktailsuchen.cocktail.focus();
		document.cocktailsuchen.onsubmit = function() { return false; }
		document.cocktailsuchen.setAttributeNode(autocomplete);
	}
	
	if(document.kontakt) {
		document.kontakt.name.focus();	
	}
	
	if(document.cocktailmixer_login) {
		document.cocktailmixer_login.setAttributeNode(autocomplete);
		document.cocktailmixer_login.onsubmit = searchItemQuick;
		document.cocktailmixer_login.q.onkeyup = searchItemQuick;
		document.cocktailmixer_login.q.focus();
	}
	
	if(document.login_form) {
		document.login_form.username.focus();
	}
	
	if(document.barsearch_form) {
		gmaps.loadBars();
		gmaps.load();
	}
}

// versteckt ein Element
function hide(id) {
	$(id).style.display = 'none';	
}

// macht ein Element sichtbar
function show(id) {
	if($(id).style.display == 'block') {
		hide(id);
	}
	else {
		$(id).style.display = 'block';
	}
}

// Submit ohne Submitbutton
function check_submit(form) {
	if(window.event) {
		var key = window.event.keyCode; 
		if(key == 13) { 
			form.submit();
		}
	}
}

function searchItemQuick() {
	if($('q').value.length > 2) {
		new Ajax.Request('ajax/cocktailmixer.php', {
			method: 'get',
			parameters: { action: 'searchItemQuick', q: $('q').value },
			onLoading: function() {
				$('load1').innerHTML = '<img src="images/load_grey.gif" alt="" />';
			},
			onSuccess: function(t) {
				$('load1').innerHTML = '';
				$('searchresults').innerHTML = t.responseText;
			}
		});
	}
	else {
		$('searchresults').innerHTML = '<i>z.B. Batida de Coco</i>';
		$('load1').innerHTML = '';
	}
	
	$('load1').innerHTML = '';
	return false;
}

function delete_user_picture(id) {
	if(confirm('Wirklich löschen?')) {
		new Ajax.Request('ajax/user.php', {
			method: 'get',
			parameters: { picture_id: id }
		});
		
		$('foto_' + id).hide();
	}
}

// Cocktailbar eines Benutzers
var bookmark = {
	add: function(id) {
		new Ajax.Request('ajax/bookmark.php', {
			method: 'get',
			parameters: { action: 'add', cocktail_id: id },
			onLoading: function() {
				if($('bookmark'))
					$('bookmark').innerHTML = '<img src="images/load_white.gif" alt="" />';
			},
			onSuccess: function() {
				if($('bookmark'))
					$('bookmark').innerHTML = '<span>Cocktail wurde in deine Cocktailbar aufgenommen</span>';
			}
		});
	},
	remove: function(id) {
		new Ajax.Request('ajax/bookmark.php', {
			method: 'get',
			parameters: { action: 'remove', cocktail_id: id },
			onLoading: function() {
				if($('bookmark_' + id))
					$('a_bookmark_' + id).innerHTML = '<img src="images/load_white.gif" alt="" />';
			},
			onSuccess: function() {
				if($('bookmark_' + id))
					$('bookmark_' + id).hide();
			}
		});
	}	
}

var gallery = {
	
	path: 'images/cocktail/',
	preview: 4,
	images: '',
	start:  0,
	
	init: function(id, type) {
		
		if(type == 'user')
			gallery.path = 'images/user/';
		
		new Ajax.Request('ajax/gallery.php', {
			method: 'get',
			parameters: { id: id, type: type },
			onLoading: function() {
				
			},
			onSuccess: function(transport) {
				// Response in ein Array schreiben (gallery.images)
				if(transport.responseText.length > 0) {
					gallery.images = transport.responseText.split("\n");
					if(gallery.images.length > 0)
						// Das erste Bild anzeigen
						$('fotowrap').innerHTML = '<img src="' + gallery.path + 'thumb-200_' + gallery.images[0] + '" alt="" />';
						
					if(gallery.images.length > 1)
						// Galerie Navigation bauen
						gallery.navigation();
				}
				else
					if(type == 'cocktail')
						$('fotowrap').innerHTML = '<img src="images/standard_cocktail.jpg" alt="" />';
					else
						$('fotowrap').innerHTML = '<img src="images/standard_profilbild.jpg" alt="" />';
			}
		});
	},
	
	navigation: function() {
		
		if($('fotowrap').next()) {
			$('fotowrap').next().remove();
		}
		
		var ul = document.createElement('ul');
			ul.className = 'gallery_navigation';
		
		// Navigation nach links
		if(gallery.images.length > 4) {
			var li = document.createElement('li');
			var img = document.createElement('img');
			
			img.src = 'images/arrow_left.gif';
			img.title = 'zurück';
			img.onclick = gallery.prev;
			
			li.appendChild(img);
			ul.appendChild(li);
		}
		
		// Teilarray
		var preview = gallery.images.slice(gallery.start, (gallery.start + gallery.preview));
		preview.each(function(s) {
			if(s) {
				li  = document.createElement('li');
				img = document.createElement('img');
				
				img.src     = gallery.path + 'thumb-40_' + s;
				img.title   = 'Bild anzeigen';
				img.onclick = function() {
					$('fotowrap').innerHTML = '<img src="' + gallery.path + 'thumb-200_' + s + '" alt="" />';
				};
				
				li.appendChild(img);
				ul.appendChild(li);
			}
		});
		
		// Navigation nach rechts
		if(gallery.images.length > 4) {
			li = document.createElement('li');
			img = document.createElement('img');
			
			img.src = 'images/arrow_right.gif';
			img.title = 'weiter';
			img.onclick = gallery.next;
			
			li.appendChild(img);
			ul.appendChild(li);
		}
		
		$('fotobox').appendChild(ul);
	},
	
	next: function() {
		if((gallery.start + gallery.preview + 1) <= gallery.images.length) {
			gallery.start++;
			gallery.navigation();
		}			
	},
	
	prev: function() {
		if(gallery.start > 0) {
			gallery.start--;
			gallery.navigation();
		}
	}
}

/* Function zur URL Generierung */
function make_filename(subject) {

	var subject = subject.toLowerCase();
	
	search = ['ä', 'ö', 'ü', 'ß', ' & ', ' ', '(', ')', '.', '’', '\'', '´', '`'];
	replace = ['ae', 'oe', 'ue', 'sz', '-', '-', '', '', '', '', '', '', ''];

	var f = search, r = replace, s = subject;  
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;  
   
	while (j = 0, i--) {  
		if (s[i]) {  
			while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};  
		}  
	};  
   
	return sa ? s : s[0]; 
}

var guestbook = {
	/* Gästebucheintrag löschen */
	remove: function(id) {
		if(confirm('Wirklich löschen?')) {
			new Ajax.Request('ajax/gaestebuch.php', {
				method: 'get',
				parameters: { action: 'delete', id: id },
				onSuccess: function(t) {
					if(t.responseText == '1') {
						$('comment' + id).hide();
					}
				}
			});
		}
		
		return false;
	},
	
	showAllEntries: function(id) {
		
		$('allEntries').toggle();
		
		new Ajax.Updater('allEntries', 'ajax/gaestebuch.php', {
			method: 'get',
			parameters: { action: 'showAllEntries', profil_id: id }
		});
		
		return false;
	}
}