var events = new Array();
var loaded = new Array();

$(document).ready(function(){
	

		$('#close-gewinnspiel').click(function(){
			$('#gewinnspiel').fadeOut();
		});
	
	$('#showvip').click(function(){
		$('.vipcard-form').slideDown("fast");
	});
	
	$.datepicker.regional['de'] = {
		closeText: 'schließen',
		prevText: '&#x3c;zurück',
		nextText: 'Vor&#x3e;',
		currentText: 'heute',
		monthNames: ['Januar','Februar','März','April','Mai','Juni',
		'Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
		'Jul','Aug','Sep','Okt','Nov','Dez'],
		dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		weekHeader: 'Wo',
		dateFormat: 'dd.mm.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['de']);


	Cufon.replace('h1', { fontFamily: 'TitilliumText22L-Light', hover: true });
	Cufon.replace('#newsletter h1', {fontFamily: 'TitilliumText22L-Medium', textShadow: '0 1px #1d5293'});
	Cufon.replace('#vip-container .vip-headline h2', {fontFamily: 'TitilliumText22L-Medium', textShadow: '0 1px #1d5293'});
	Cufon.replace('h2', { fontFamily: 'TitilliumText22L-Medium', hover: true });

	$("#location-bilder > div").hide();
	$('#location-bilder > div ul li a').lightBox({disableNavigation:true});
	$("ul.tabnav li").click(function() {
		$("#location-bilder > div").hide();
		var activeTab = $(this).find("area").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	Galleria.loadTheme('/v2/js/galleria.classic.min.js');
	$('#galleria').galleria({
	            transition: 'fade',
	            transitionSpeed: 300,
	            preload: 3,
	            clicknext: true
	});

	$('#flip').jcoverflip({
		current: 2,
          beforeCss: function( el, container, offset ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 260 - 115*offset + 20*offset )+'px', bottom: '58px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
            ];
          },
          afterCss: function( el, container, offset ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 160 + 115*offset )+'px', bottom: '58px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
            ];
          },
          currentCss: function( el, container ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 125 )+'px', bottom: '52px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ).css('z-index', 5000), { width: '250px',  }, { } )
            ];
          },
          change: function(event, ui){
            jQuery('#scrollbar').slider('value', ui.to);
          }

	});

	$('#scrollbar').slider({
		min: 0,
		max: $('#flip').jcoverflip('length')-1,
		value: $('#flip').jcoverflip('current'),
		slide: function(e, ui) {
			$('#flip').jcoverflip('current', ui.value);
		}
	});

	$('.datepicker').datepicker({
			showWeek: true,
			yearRange: 'c-1:c+1',
			altField: 'input[name="date"]',
			altFormat: 'yy-mm-dd',
			dateFormat: 'yy-mm-dd',
			showOtherMonths: true,
			beforeShowDay: getCalendarEvents,
			onSelect: function(dateText, inst) {
				showEvent(dateText);
			}
	});

	$('.datepicker2').datepicker({
			showWeek: true,
			yearRange: 'c-1:c+1',
			altField: 'input[name="date"]',
			altFormat: 'yy-mm-dd',
			dateFormat: 'yy-mm-dd',
			showOtherMonths: true
	});

	$('#guestlist').live('click', function(){
		var id		= $(this).attr('rel');
		var self	= $(this);

		$.ajax({
		  url: "?request=guestlist&val="+id,
		  success: function(s){
			$(self).replaceWith('<span class="guestlist-error">'+s+'</span>');
		  }
		});
	});


	$('.flyer').parent().parent().bind( 'mouseenter mouseleave', function() {
		$('.flyer', this).toggle(mod++ % 2 == 0);
	});

	$('input[name="date"]').change(function() {
		$('.datepicker,.datepicker2').datepicker("setDate" , $(this).val());
	});

	$("button[name='change']").click( function() {
		getData('change', $(this).val());
	});


});


function getData (action, val) {

	$('form')[0].reset();
	$.ajax({data: {'request': action, 'val': val}, dataType: 'json', success: useData});
}

function useData (json) {

	if ( !$(':submit[name="change"]').length )
		$(':submit[name="add"]').after('<input type="submit" class="submit" name="change" value="Ändern">');

	$.each(json, function (field, value) {

		if ($(':input[name="'+field+'"][type!="checkbox"]').val(value).attr('type') == 'hidden')
			$(':input[name="'+field+'"]').trigger('change');

		if ( value == 1 )
			$(':input[name="'+field+'"][type="checkbox"]').attr('checked', 'checked');

		else
			$(':input[name="'+field+'"][type="checkbox"]').removeAttr('checked');



	});
}


function getCalendarEvents (date) {

	day 	= $.datepicker.formatDate('yy-mm-dd', date);
	month 	= $.datepicker.formatDate('yy-mm', date);

	if ( !loaded[month] ) {
		$.ajax({
		  async: false,
		  url: '?request=events&val='+month,
		  dataType: 'json',
		  success: function (data) {
		  	events[data.month] = data.data;
		  }
		});
		loaded[month] = true;
	}

	if ( events[month] && day in events[month] )
		return new Array(true, '', events[month][day]);

	return new Array(false);
}

function showEvent (event) {

	$('#dialog').load('?request=event&val='+event, function() {
		$(this).dialog({
			modal: true,
			width: 642,
			height: 800,
			show: 'slide'
		});
	});

}

