/* is used within "headerincludes" template */

if (typeof popupDiv == 'undefined') {
	var popupDiv = null;
	var cardpageDisabledTooltips = false;
	var cardpageIsAdmin = false;
	var cardpageIsAdminView = false;
	var cardpageOwner = 0;
	var serverTimestamp = 0;
	var paramStr = '';
	if ((sepPos = location.href.indexOf('?')) >= 0) {
		paramStr = location.href.substr(sepPos);
	}
	var baseAjaxLink = (paramStr == '' ? '?tz_ajax=1' : paramStr + '&tz_ajax=1');
	
	document.observe('dom:loaded', function() {
		if (!document.all) {
			$$('img').each(function(e) {
				e.hide();
				e.show();
			});
		}
		popupDiv = $(document.createElement('div')).writeAttribute('id', 'popupDiv').setStyle({position: 'absolute'});
		$$('body').first().observe('click', function() { if (popupDiv.parentNode != null && typeof popupDiv.parentNode == 'object') popupDiv.remove() });
		var newScrollTop = location.href.replace(/^.*?_scrollTo=(\d+).*?$/, '$1');
		if (parseInt(newScrollTop) > 0)
		{
			window.scrollTo(0, newScrollTop);
		}
	});

	Event.observe(window, 'load', function() {
		// $('loading').hide();
	});

}

if (typeof categorySetArray != 'object') {
	var categorySetArray = new Object();
}

if (typeof categoryMoveToArray != 'object') {
	var categoryMoveArray = new Object();
}

if (typeof categoryTradeToChipsArray != 'object') {
	var categoryTradeToChipsArray = new Object();
}

if (typeof categoryId == 'undefined') {
	var categoryId = 0;
}

if (typeof missingCards == 'undefined') {
	missingCards = null;
}

function setupShopBuyCardLink()
{
	$$('a#buyCardLink').first()
	.writeAttribute('href', 'javascript:void(0)').observe('click', function() {
		var cardFieldValue = $('buyCardField').value;
		if (cardFieldValue != '')
		{
			new Ajax.Request(baseAjaxLink + '&tz_wish='+cardFieldValue, {
				method: 'get',
				onSuccess: function(ajaxResponse) {
					checkAjaxResultReload(ajaxResponse);
				}
			});
		}
	});

	$$('a#buyChipLink').first()
	.writeAttribute('href', 'javascript:void(0)').observe('click', function() {
		new Ajax.Request(baseAjaxLink + '&tz_chip=1', {
			method: 'get',
			onSuccess: function(ajaxResponse) {
				checkAjaxResultReload(ajaxResponse);
			}
		})
	});
}


function checkAjaxResultReload(ajaxResponse)
{
	var responseText = ajaxResponse.responseText;
	if (responseText != '')
		alert(responseText.unescapeHTML());
	else
		reloadPageWithScrollOffset();
}


function reloadPageWithScrollOffset()
{
	var newLocation = location.href.replace(/[&\?]_scrollTo=\d*/, '');
	newLocation = newLocation.replace(/#[a-z0-9_-]*/i, '');
	location.href = newLocation + (newLocation.indexOf('?')!=-1 ? '&' : '?') + '_scrollTo='
		+ $$('body').first().cumulativeScrollOffset()['top'];
}


function setupLotteryTooltip()
{
	$$('div#lottery img[ticket_no!=""]').each(function(imgElement)
	{
		imgElement.setStyle({cursor: 'pointer'}).observe('click', function(imgElemClickEvent)
		{
			Event.stop(imgElemClickEvent);
			var imgElement = Event.element(imgElemClickEvent);
			var ticketNo = imgElement.readAttribute('ticket_no');
			var imgElementPos = imgElement.positionedOffset();
			var tooltipTop = imgElementPos['top'] - imgElement.parentNode.scrollTop + 25;
			var tooltipLeft = imgElementPos['left'] - imgElement.parentNode.scrollLeft + 20;
			popupDiv.setStyle({
				position: 'absolute',
				height: '15px',
				width: '120px',
				top: tooltipTop + 'px',
				left: tooltipLeft + 'px',
				display: 'block'
			});
			popupDiv.innerHTML = '';
			var aElement = $(document.createElement('a'))
				.writeAttribute('href', 'javascript:void(0)')
				.writeAttribute('ticket_no', ticketNo)
				.observe('click', function(aClickEvent){
					var aElement = Event.element(aClickEvent);
					var ticketNo = aElement.readAttribute('ticket_no');
					new Ajax.Request(baseAjaxLink + '&tz_buy_lottery='+ticketNo,
					{
						method: 'GET',
						onSuccess: function(ajaxResponse) {
							checkAjaxResultReload(ajaxResponse);
						}
					});
				})
				.insert('Dieses Los kaufen!');
			popupDiv.insert(aElement);
			imgElement.insert({before: popupDiv});
		});
	});
}


function setupShopTooltip()
{
	$$('div[shop_category!=""] img').each(function(imgElement)
	{
		imgElement.setStyle({cursor: 'pointer'});
		imgElement.observe('click', function(imgElemClickEvent)
		{
			Event.stop(imgElemClickEvent);
			var imgElement = Event.element(imgElemClickEvent);
			var divElement = imgElement.up('div[shop_price!=""]');
			var shopPrice = divElement.readAttribute('shop_price');
			var shopCategory = divElement.readAttribute('shop_category');
			var tempImgSrcParts = imgElement.readAttribute('src').split('/');
			var cardDetails = tempImgSrcParts[tempImgSrcParts.length-1].replace(/([a-z0-9])([0-9]{2})\.png/, '$1,$2').split(',');
			var imgElementPos = imgElement.positionedOffset();
			var tooltipTop = imgElementPos['top'] - imgElement.parentNode.scrollTop + 25;
			var tooltipLeft = imgElementPos['left'] - imgElement.parentNode.scrollLeft + 20;
			popupDiv.setStyle({
				position: 'absolute',
				height: '15px',
				width: '250px',
				top: tooltipTop + 'px',
				left: tooltipLeft + 'px',
				display: 'block'
			});
			popupDiv.innerHTML = '';
			var aElement = $(document.createElement('a'))
				.writeAttribute('href', 'javascript:void(0)')
				.writeAttribute('shop_category', shopCategory)
				.writeAttribute('set_name', cardDetails[0])
				.writeAttribute('card_number', cardDetails[1])
				.observe('click', function(aClickEvent){
					var aElement = Event.element(aClickEvent);
					var shopCategoryId = aElement.readAttribute('shop_category');
					new Ajax.Request(baseAjaxLink + '&tz_shop_category='+shopCategory+'&tz_setname='+aElement.readAttribute('set_name')+'&tz_cardnumber='+aElement.readAttribute('card_number'),
					{
						method: 'GET',
						onSuccess: function(ajaxResponse) {
							checkAjaxResultReload(ajaxResponse);
						}
					});
				})
				.insert('Diese Karte f&uuml;r '+shopPrice+' Cheese kaufen!');
			popupDiv.insert(aElement);
			// $$('body').first().insert({top: popupDiv});
			imgElement.insert({before: popupDiv});
		});
	});
}


function setupDoublesJourneyTooltip()
{
	$$('div#double_cards img').each(function(imgElement)
	{
		imgElement.setStyle({cursor: 'pointer'});
		imgElement.observe('click', function(imgElemClickEvent)
		{
			Event.stop(imgElemClickEvent);
			var imgElement = Event.element(imgElemClickEvent);
			var tempImgSrcParts = imgElement.readAttribute('src').split('/');
			var cardDetails = tempImgSrcParts[tempImgSrcParts.length-1].replace(/([a-z0-9])([0-9]{2})\.png/, '$1,$2').split(',');
			var imgElementPos = imgElement.cumulativeOffset();
			var tooltipTop = imgElementPos['top'] + 25;
			var tooltipLeft = imgElementPos['left'] + 20;
			popupDiv.setStyle({
				position: 'absolute',
				height: '30px',
				top: tooltipTop + 'px',
				left: tooltipLeft + 'px',
				display: 'block'
			});
			popupDiv.innerHTML = '';
			var aElement = $(document.createElement('a'))
				.writeAttribute('href', 'javascript:void(0)')
				.writeAttribute('set_name', cardDetails[0])
				.writeAttribute('card_number', cardDetails[1])
				.observe('click', function(aClickEvent){
					var aElement = Event.findElement(aClickEvent, 'a');
					new Ajax.Request(baseAjaxLink + '&tz_double_card=1&tz_setname='+aElement.readAttribute('set_name')+'&tz_cardnumber='+aElement.readAttribute('card_number'),
					{
						method: 'GET',
						onSuccess: function(ajaxResponse) {
							checkAjaxResultReload(ajaxResponse);
						}
					});
				})
				.insert('<b>Nur diese</b> Karte umtauschen');
			var aElementAll = $(document.createElement('a'))
				.writeAttribute('href', 'javascript:void(0)')
				.writeAttribute('set_name', cardDetails[0])
				.writeAttribute('card_number', cardDetails[1])
				.observe('click', function(aClickEvent){
					if (confirm('Bist du dir wirklich sicher, dass du ALLE Karten umtauschen willst?'))
					{
						new Ajax.Request(baseAjaxLink + '&tz_double_card=all',
						{
							method: 'GET',
							onSuccess: function(ajaxResponse) {
								checkAjaxResultReload(ajaxResponse);
							}
						});
					}
				})
				.insert('<b>Alle</b> Karten umtauschen');
			popupDiv
				.insert(aElement)
				.insert('<br />')
				.insert(aElementAll);

			$$('body').first().insert({top: popupDiv});
		});
	});
}

// Function returns given timespan (in seconds, e.g. difference between two unix timestamps)
// as seconds.
function getTimespanString(timespan)
{
	if (timespan > 86400)
	{
		return Math.floor(timespan / 86400) + ' T.'
	}
	else if (timespan > 3600)
	{
		return Math.ceil(timespan / 3600) + ' Std.';
	}
	else if (timespan > 60)
	{
		return Math.ceil(timespan / 60) + ' Min.';
	}
	else
	{
		return timespan + ' Sek.';
	}
}


function handleImgClick(imgElemClickEvent)
{
	// stop event bubbling
	imgElemClickEvent.stop();
	// find <img ... /> element
	var clickedImgElement = Event.element(imgElemClickEvent);
	// check if "new card" tooltip should be displayed
	var divNewCard = clickedImgElement.up('div#new_cards');
	var isNewCardTooltip = (typeof divNewCard != 'undefined');
	var isSearchCardTooltip = (clickedImgElement.readAttribute('is_searchcard') == '1');
	var isWishCardTooltip = (clickedImgElement.readAttribute('is_wishcard') == '1');
	var isFlaskTooltip = (clickedImgElement.readAttribute('is_flask') == '1');
	var categoryJsId = clickedImgElement.readAttribute('category');
	var categoryId = clickedImgElement.readAttribute('categoryid');
	// find set name
	var tempImgSrcParts = clickedImgElement.src.split('/');
	var cardDetails = tempImgSrcParts[tempImgSrcParts.length-1].replace(/([a-z]+)([0-9]{2})\.png/, '$1,$2').split(',');
	// show div
	var clickedImgElementPosition = clickedImgElement.cumulativeOffset();
	var clickedImgElementScrollPos =  clickedImgElement.cumulativeScrollOffset();
	var tooltipTop = clickedImgElementPosition['top'] + 25;
	var tooltipLeft = clickedImgElementPosition['left'] + 20;
	if (isNewCardTooltip) {
		popupDiv.setStyle({
			position: 'fixed'
		});
	} else if (isSearchCardTooltip) {
		popupDiv.setStyle({
			position: 'absolute'
		});
	} else {
		popupDiv.setStyle({
			position: 'absolute'
		});
	}
	popupDiv.setStyle({
		top: tooltipTop+'px',
		left: tooltipLeft+'px',
		display: 'block'
	});
	popupDiv.innerHTML = '';
	if (!isSearchCardTooltip && !isWishCardTooltip && !isFlaskTooltip && !cardpageIsAdminView) {
		// check if more than one card of this set is in card page
		var allowMoveSet = typeof categorySetArray[categoryJsId][cardDetails[0]] != 'undefined';
		var popupDivCard = $(document.createElement('div'));
		var popupDivSet = $(document.createElement('div'));
		var firstCategory = true;
		var tempTime = new Date();
		popupDivCard.insert('<strong>Verschiebe diese Karte nach:</strong><br />');
		if (allowMoveSet) popupDivSet.insert('<strong>Verschiebe Set nach:</strong><br />');
		for (var moveCategoryId in categoryMoveArray[categoryJsId])
		{
			var aElement = $(document.createElement('a'));
			aElement.insert(categoryMoveArray[categoryJsId][moveCategoryId]).writeAttribute('href', 'javascript:void(0)').writeAttribute('targetcategory', moveCategoryId).writeAttribute('set', cardDetails[0]).writeAttribute('cardnumber', cardDetails[1]);
			aElement.observe('click', function(event) {
				var linkElement = Event.findElement(event, 'a');
				var targetCategory = linkElement.readAttribute('targetcategory');
				var setName = linkElement.readAttribute('set');
				var cardNumber = linkElement.readAttribute('cardnumber');
				$('loading').show();
				new Ajax.Request(baseAjaxLink + '&old_category='+categoryId+'&new_category='+targetCategory+'&tz_setname='+setName+'&tz_cardnumber='+cardNumber, {
					method: 'get',
					onSuccess: function(ajaxResponse) {
						$('containerCardPage').replace(ajaxResponse.responseText);
						refreshTzHeader();
					},
					onComplete: function() {
						$('loading').hide();
					}
				});
			});
			popupDivCard.insert((!firstCategory?' - ':'')).insert(aElement);
			if (allowMoveSet)
			{
				var aElement = $(document.createElement('a'));
				aElement.insert(categoryMoveArray[categoryJsId][moveCategoryId]).writeAttribute('href', 'javascript:void(0)').writeAttribute('targetcategory', moveCategoryId).writeAttribute('set', cardDetails[0]).writeAttribute('cardnumber', parseInt(cardDetails[1]));
				aElement.observe('click', function(event) {
					var linkElement = Event.findElement(event, 'a');
					var targetCategory = linkElement.readAttribute('targetcategory');
					var setName = linkElement.readAttribute('set');
					$('loading').show();
					new Ajax.Request(baseAjaxLink + '&tz_moveset=1&old_category='+categoryId+'&new_category='+targetCategory+'&tz_setname='+setName, {
						method: 'get',
						onSuccess: function(ajaxResponse) {
							$('containerCardPage').replace(ajaxResponse.responseText);
							refreshTzHeader();
						},
						onComplete: function() {
							$('loading').hide();
						}
					});
				});
				popupDivSet.insert((!firstCategory?' - ':'')).insert(aElement);
			}
			firstCategory = false;
		}
		// link for trading card to chips

		popupDiv.insert(popupDivCard);
		if (allowMoveSet) popupDiv.insert('<br />').insert(popupDivSet);
		if (typeof categoryTradeToChipsArray[categoryId] != 'undefined')
		{
			var aChipsElement = $(document.createElement('a'));
			aChipsElement.insert('Hier umtauschen').writeAttribute('href', 'javascript:void(0)').writeAttribute('set', cardDetails[0]).writeAttribute('cardnumber', cardDetails[1]);
			aChipsElement.observe('click', function(event) {
				var linkElement = Event.findElement(event, 'a');
				var setName = linkElement.readAttribute('set');
				var cardNumber = linkElement.readAttribute('cardnumber');
				new Ajax.Request(baseAjaxLink + '&tz_chips=1&old_category='+categoryId+'&tz_setname='+setName+'&tz_cardnumber='+cardNumber, {
					method: 'get',
					onSuccess: function(ajaxResponse) {
						$('containerCardPage').replace(ajaxResponse.responseText);
						refreshTzHeader();
					}
				});
			});
			popupDiv.insert('<br /><strong>Tausche Karte in 1 K&auml;sekr&auml;cker</strong><br />').insert(aChipsElement);
		}
		$$('body').first().insert({top: popupDiv});
	} else if (isSearchCardTooltip && !cardpageIsAdminView) {
		var aElement = $(document.createElement('a'));
		var setName = clickedImgElement.up('div[setname!=""]').readAttribute('setname');
		new Ajax.Request(baseAjaxLink + '&searchcard_set='+setName, {
			method: 'get',
			onSuccess: function (ajaxResponse) {
				$('containerCardPage').replace(ajaxResponse.responseText);
			}
		});
		popupDiv.hide();
	} else if (isWishCardTooltip && !cardpageIsAdminView) {
		popupDiv.innerHTML = '<strong>Wishkarte umtauschen</strong><br />' +
			'Hier siehst du alle Karten, die in deinem Collect-Bereich fehlen:<br />';
		var tempHTML = '<div style="width:400px;height:145px;overflow-y:scroll;">';
		var testCount = 0;
		var tempCardNoHTML = '';
		for (missingCardSet in missingCards) {
			var missingCardNumbers = missingCards[missingCardSet];
			testCount++;
			tempHTML += '&raquo; <strong>'+missingCardSet.toUpperCase()+':</strong> ';
			tempCardNoHTML = '';
			for(i=0; i < missingCards[missingCardSet].length; i++) {
				tempCardNoHTML += (tempCardNoHTML != '' ? ', ' : '') + '<a class="_wishcardChoose" set="'+missingCardSet+'">' + missingCards[missingCardSet][i] + '</a>';
			}
			tempHTML += tempCardNoHTML + '<br />';
		}
		tempHTML += '</div><br />... oder suche dir hier eine Karte aus:<br />' +
			'<input type="text" class="textbox" id="wishcardChooseCard" /> ' +
			'<a id="wishcardChooseCardLink" href="javascript:void(0)">Diese Karte nehmen</a>';
		popupDiv.innerHTML += tempHTML;
		$$('body').first().insert({top: popupDiv});
		$('wishcardChooseCard').observe('click', function(e) { e.stop() });
		$$('a._wishcardChoose').each(function(a) {
			a.setStyle({cursor:'pointer'}).observe('click', function(e) {
				var aElement = e.element();
				var setName = aElement.readAttribute('set');
				var cardNo = parseInt(aElement.innerHTML);
				new Ajax.Request(baseAjaxLink + '&tz_trade_wishcard='+cardDetails[1]+'&tz_set='+setName+'&tz_cardnumber='+cardNo, {
					method: 'get',
					onSuccess: function(ajaxResponse) {
						$('containerCardPage').replace(ajaxResponse.responseText);
						refreshTzHeader();
					}
				});
			});
		});
		$('wishcardChooseCardLink').observe('click', function(e) {
			e.stop();
			new Ajax.Request(baseAjaxLink + '&tz_trade_wishcard='+cardDetails[1]+'&tz_input='+encodeURIComponent($('wishcardChooseCard').value), {
				method: 'get',
				onSuccess: function(ajaxResponse) {
					if (ajaxResponse.responseText.substr(0,1) == '!') {
						alert(ajaxResponse.responseText.substr(1));
					} else {
						$('containerCardPage').replace(ajaxResponse.responseText);
						refreshTzHeader();
						popupDiv.remove();
					}
				}
			});
		});
	} else if (isFlaskTooltip) {
		var flaskEntryId = clickedImgElement.readAttribute('flask_entry');
		var flaskExpires = clickedImgElement.readAttribute('flask_expires');
		var flaskTitle = clickedImgElement.readAttribute('title');
		if (flaskExpires != '')
		{	
			popupDiv.innerHTML = '<strong>Aktives Fl&auml;schchen</strong><br />' +
				'Noch ' + getTimespanString(flaskExpires - serverTimestamp)	+ ' aktiv';
		}
		else
		{
			popupDiv.innerHTML = '<strong>' + flaskTitle + ' trinken</strong><br />';
			var aElement = $(document.createElement('a')).writeAttribute('href', 'javascript:void(0)').writeAttribute('flask_entry', flaskEntryId).observe('click', function(e){
				var aElement = e.element();
				var flaskId = aElement.readAttribute('flask_entry');
				new Ajax.Request(baseAjaxLink + '&tz_flask=' + flaskId, {
					method: 'get',
					onSuccess: function(ajaxResponse) {
						$('containerCardPage').replace(ajaxResponse.responseText);
						refreshTzHeader();
					}
				});
			}).insert('Jetzt trinken!');
			popupDiv.insert(aElement);
			if (typeof flaskIsOneActive != 'undefined' && flaskIsOneActive == true) {
				popupDiv.insert('<br /><br />Aktuell ist ein Fl&auml;schchen aktiv. Wenn du dieses trinkst,<br />beendest du den Effekt des aktiven Fl&auml;schchens.');
			}
		}
		$$('body').first().insert({top: popupDiv});
	} else if (cardpageIsAdminView) {
		popupDiv.innerHTML = '<strong>Admin-Optionen</strong><br />';
		var aElement = $(document.createElement('a'));
		aElement.insert('Karte entfernen').writeAttribute('href', 'javascript:void(0)').writeAttribute('set', cardDetails[0]).writeAttribute('cardnumber', cardDetails[1]).writeAttribute('category', categoryId).observe('click', function(e){
			var aElement = e.element();
			var setName = aElement.readAttribute('set');
			var cardNo = aElement.readAttribute('cardnumber');
			var categoryId = aElement.readAttribute('category');
			new Ajax.Request(baseAjaxLink + '&tz_admin_remove=1&tz_owner=' + cardpageOwner + '&tz_set=' + setName + '&tz_cardnumber=' + cardNo + '&old_category=' + categoryId, {
				method: 'get',
				onSuccess: function(ajaxResponse) {
					$('containerCardPage').replace(ajaxResponse.responseText);
					refreshTzHeader();
				}
			});
		});
		popupDiv.insert(aElement);
		$$('body').first().insert({top: popupDiv});
	}
}


function setupTooltip(categoryJsId, categoryId)
{
	if (cardpageDisabledTooltips) {
		// not "my" cardpage
		if (!cardpageIsAdmin) {
			return;
		} else {
			cardpageIsAdminView = true;
		}
	}
	if (typeof categorySetArray == 'undefined' || typeof categoryMoveArray == 'undefined' || typeof popupDiv == 'undefined')
	{
		return false;
	}
	else
	{
		if (categoryJsId == 'new_cards')
		{
			$('closebutton').observe('click', function() { $('new_cards').hide(); });
		}
		var flaskIdx = 0;
		$(categoryJsId).getElementsBySelector('img').each(function(imgElem){
			imgElem.writeAttribute('category', categoryJsId).writeAttribute('categoryid', categoryId);
			var imgSrc = imgElem.readAttribute('src').toLowerCase();
			if (imgSrc.indexOf('/searchcards/') != -1) imgElem.writeAttribute('is_searchcard', '1');
			else if (imgSrc.indexOf('/wishcards/') != -1) imgElem.writeAttribute('is_wishcard', '1');
			else if (imgSrc.indexOf('/zauberflaschen/') != -1)
			{
				imgElem.writeAttribute('is_flask', '1');
				imgElem.writeAttribute('flask_entry', flaskEntryIds[flaskIdx]);
				imgElem.writeAttribute('flask_expires', flaskExpires[flaskIdx] == false ? '' : flaskExpires[flaskIdx]);
				flaskIdx++;
			}
			if (imgSrc.indexOf('-master') == -1) {
				imgElem.stopObserving();
				imgElem.observe('click', handleImgClick);
			}
		});
		return true;
	}
}


function refreshTzHeader()
{
	new Ajax.Request(baseAjaxLink + '&tz_header_update=1', {
		method: 'get',
		onSuccess: function(ajaxResponse){
			$('headerTzUser').innerHTML = ajaxResponse.responseText;
		}
	});
}


function setupSearchcardSelect()
{
	$('closebutton').observe('click', function() { $('searchcard_select').hide(); });
		$('searchcard_select').getElementsBySelector('a').each(function(linkElem) {
			if (linkElem.readAttribute('set') != '' && linkElem.readAttribute('searchcard_number') != '') {
				linkElem.writeAttribute('href', 'javascript:void(0)');
				linkElem.observe('click', function(event) {
					var linkElem = Event.findElement(event, 'a');
					new Ajax.Request(baseAjaxLink + '&searchcard_set='+linkElem.readAttribute('set')+'&searchcard_number='+linkElem.readAttribute('searchcard_number'), {
						method: 'get',
						onSuccess: function (ajaxResponse) {
							$('containerCardPage').replace(ajaxResponse.responseText);
							refreshTzHeader();
						}
					});
				});
			}
		});
}

function helpShow(topic)
{
	var url = location.href + (location.href.indexOf('?')>0 ? '&' : '?') + 'tz_ajax=1&tz_helptopic=' + topic;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(ajaxResponse) {
			var helpDiv = $(document.createElement('div'));
			helpDiv.writeAttribute('id', 'help_layer').insert('<div id="closebutton"></div>')
				.insert(ajaxResponse.responseText);

			$$('body').first().insert(helpDiv);
			$('closebutton').observe('click', function() {
				$('help_layer').remove();
			});
		}
	});
}


function userInitialize()
{
	var userDiv = $$('div#user_initialize');
	if (userDiv.size() > 0)
	{
		var userDiv = userDiv.first();
		userDiv.select('a').each(function(a_elem) {
			a_elem.observe('click', function(e) {
				var userDiv = e.findElement('div');
				var userStartSet = userDiv.select('select[name=starter_wishset]').first();
				if (!(userStartSet.selectedIndex >= 0)) {
					alert('Du musst dir ein Set aussuchen!');
				} else {
					var selectedSetId = userStartSet.options[userStartSet.selectedIndex].value;
					new Ajax.Request(baseAjaxLink + '&tz_initialize=1&tz_set_id='+selectedSetId, {
						method: 'get',
						onSuccess: function(ajaxResponse) {
							var locationHref = location.href;
							location.href = locationHref;
						}
					});
				}
			});
		});
	}
}

function setupPointshopLinks()
{
	$$('a[categoryid!=""]').each(function(e){
		e.writeAttribute('href', 'javascript:void(0)').observe('click', function(e){
			var linkElem = e.findElement('a');
			var categoryId = linkElem.readAttribute('categoryid');
			if (categoryId != '') {
				new Ajax.Request(baseAjaxLink + '&tz_pointshop=1&tz_category=' + categoryId, {
					method: 'get', 
					onSuccess: function(ajaxResponse) {
						if (ajaxResponse.responseText.substr(0, 1) == '.') {
							alert(ajaxResponse.responseText.substr(1));							
							ajaxResponse.responseText = '';
						}
						checkAjaxResultReload(ajaxResponse);
					}
				});
			}
		});
	});
}

function setupWordCrusher()
{
	$('wordcrusher_link').writeAttribute('href', 'javascript:void(0)').observe('click', function(){
		var w = $('wordcrusher').value;
		if (w != '') {
			new Ajax.Request(baseAjaxLink + '&tz_wordcrusher=' + encodeURI(w), {
				method: 'get',
				onSuccess: function(r){
					$('wordcrusher_output').show().down('span').innerHTML = r.responseText;
				}
			});
		} else {
			alert('Du musst einen Text eingeben den du auch "crushen" kannst.');
		}
	});
}

function setupBirthdayShop()
{
	$('birthdayShopLink').writeAttribute('href', 'javascript:void(0)').observe('click', function(){
		new Ajax.Request(baseAjaxLink + '&tz_birthdayshop=1', {
			method: 'get', onSuccess: checkAjaxResultReload
		});		
	});
}
