var now = new Date()
var calendarYear = now.getFullYear()
var calendarMonth = now.getMonth()

function resetCalendar() {
	$('#calendar .ui-datepicker-next').removeAttr('onclick').click(function () {
		$.datepicker._adjustDate('#calendar', +1, 'M')
		$('#calendar td').removeAttr('onclick')
		return false
	})

	$('#calendar .ui-datepicker-prev').removeAttr('onclick').click(function () {
		$.datepicker._adjustDate('#calendar', -1, 'M')
		$('#calendar td').removeAttr('onclick')
		return false
	})

	$('#calendar td').removeAttr('onclick')

	$('#calendar td').hover(function() {
		$t = $(this)
		var time = ($t.position().top+84 != $('#calendar #indicator').position().top) ? 300 : 0
		$('#calendar #indicator').stop()
			.animate({
				top: $t.position().top+84,
				width: $t.width(),
				height: $t.height(),
				left: $t.position().left+30
			}, time, 'easeOutBounce', function () {
				//$('#calendar #indicator').animate({left: $t.position().left+30}, 300, 'easeOutBounce')
			})
		return false
	}, function () {})

	var year = calendarYear
	var month = calendarMonth-1
	var days_in_month = 32 - new Date(year, month, 32).getDate()
	var date_from = new Date(year, month, 1)
	var date_to = new Date(year, month, days_in_month)
	//alert([year, month, days_in_month, date_from, date_to])
	$('#calendar .event').remove()
	$.getJSON('/wydarzenia/kalendarz?callback=?', {
		from: date_from.getTime()/1000.0,
		to: date_to.getTime()/1000.0
	}, function (data) {
		$('#calendar td').not('.ui-datepicker-unselectable').each(function () {
			$(this).attr('id', 'calendarDay-'+$(this).text())
		})
		// dodac funkcje ktora nie powiela dla dwoch eventow w danym dniu
		
		var events = {}
		
		$(data.events).each(function (a, b) {
			var day = parseInt(b.day)
			
			$('#calendar #calendarDay-'+day).addClass('events')//.position()
			
			if (typeof events[day] == 'undefined') {
				events[day] = [b]
			} else {
				events[day].push(b)
			}
			
		})
		
		var template = '<li{class}><div class="item"><span class="date"><a href="{url}">{date}</a></span><span class="title"><a href="{url}">{title} (więcej...)</a></span></div></li>'
		
		for (i in events) {
			var tip = ''
			
			var count = events[i].length
			for (j in events[i]) {
				var t = template
				if (j == count-1 && j == 0) { t = t.replace('{class}', ' class="one"') }
				if (j == 0) { t = t.replace('{class}', ' class="first"') }
				if (j == count-1) { t = t.replace('{class}', ' class="last"') }
				tip += t.replace('{date}', events[i][j].date).replace('{title}', events[i][j].title).replace('{url}', events[i][j].url).replace('{url}', events[i][j].url).replace('{class}', '')
			}
			//console.log(tip)
			var pos = $('#calendar #calendarDay-'+i).position()
			$('<div>&nbsp;</div>').attr('id', 'event-'+i).addClass('event').appendTo('#calendar').css({
				position: 'absolute',
				top: pos.top+84,
				left: pos.left+30,
				width: $('#calendar #calendarDay-'+i).width(),
				height: $('#calendar #calendarDay-'+i).height(),
				opacity: 0,
				'z-index': -2
			})
			$('#event-'+i).stop().animate({top: pos.top+84, opacity: 1.0}, 600, 'easeOutBounce')
			
			$('#calendar #calendarDay-'+i).qtip({
				content: '<ul id="event-list-'+i+'" class="event-tooltip">'+tip+'</ul>',
				show: 'mouseover',
				hide: { when: 'mouseout', fixed: true },
				style: {
					//name: 'cream',
					padding: 0,
					background: '#fff2d2',
					border: {
						width: 2,
						radius: 5,
						color: '#f0d9a7'
					}
				},
				position: {
					corner: {
						target: 'bottomMiddle',
						tooltip: 'bottomMiddle'
					}
				},
				api: {
					onShow: function (self, event) {
						//console.log($(this).find('li').size())
						$('.qtip li').hover(function () { $(this).addClass('hover') }, function () { $(this).removeClass('hover') }).click(function () {
							window.location = $(this).find('a').eq(0).attr('href')
						})
					}
				}
			})
		}
		/*$('#calendar .events').hover(function () {
			$(this).qtip({
				content: 'This is an active list element',
				show: 'mouseover',
				hide: { when: 'mouseout', fixed: true },
				style: {
					name: 'cream',
					padding: 15,
					border: {
						width: 2,
						radius: 5//,
						//color: '#A2D959'
					}
				},
				position: {
					corner: {
						target: 'topMiddle',
						tooltip: 'bottomRight'
					}
				}
			})
		})*/
	})
}

$(function () {

	$(".news-gallery li a, a[rel^='prettyPhoto'], a[rel^='powieksz']").prettyPhoto();
	$("#calendar").css({position: 'relative', 'z-index': 4})

	$('#intro').flash(
		{ src: '/static/frontend/flash/Banner.swf',
			width: 593,
			height: 255,
			wmode: 'transparent'},
		{ version: 9 }
	)
	
	$('#sponsorzy').flash(
		{ src: '/static/frontend/flash/Supporters.swf',
			width: 225,
			height: 150,
			wmode: 'transparent'},
		{ version: 9 }
	)

	
	$.datepicker.setDefaults($.datepicker.regional['pl']);
	$("#calendar").datepicker({
		showOtherMonths: true,
		onChangeMonthYear: function(year, month, inst) {
			calendarMonth = month
			calendarYear = year
		},
//		onChangeMonthYear: function () { resetCalendar() },
//		onSelect: function () { resetCalendar() },
		onAfterUpdate: function () { resetCalendar() }
	})

	//resetCalendar()

	$('<div id="indicator"></div>').html('&nbsp;').appendTo('#calendar')
		.css({position: 'absolute', 'z-index': -1, width: 44, height: 23, top: $('#calendar .ui-datepicker-current-day').position().top+84, left: $('#calendar .ui-datepicker-current-day').position().left+30})
		
	initFormContact()
	initFormComment()
	
	$('#disc-1').css({opacity: 0}).addClass('odd')
	setTimeout('switchDisc()', 9500)
})

var cDisc = 0

function switchDisc() {

	if (cDisc < 1) { cDisc = cDisc+1 } else { cDisc = 0 }
	
	$('#disc .disc').animate({opacity: 0}, function () {
		$(this).hide()
		$('#disc-'+cDisc).show().css({opacity: 0}).animate({opacity: 1})
	})

	setTimeout('switchDisc()', 9500)
}

function initFormComment() {
	
	$('#form-comment').each(function () {
		$(this).load('/komentarz?'+$(this).attr('rel'), function () {
			ajaxifyFormComment(this)
			$('.buttons input', this).val('').addClass('btn-send')
			$('#puchar').css({bottom: '-30px'})
			$('#viewport-bottom').css({bottom: '-11px'})
			//initImages()
		})
	})
	
}

function ajaxifyFormComment(e) {
	if (e) {
		$('form', e).ajaxForm({ target: '#form-comment', success: function () {
				ajaxifyFormComment(e)
				$('.buttons input', this).val('').addClass('btn-send')
				//initImages()
			},
			beforeSubmit: function () {
				$('.buttons input', e).attr('disabled', 'disabled')
				$('form input, form textarea', e).attr('disabled', 'disabled')
			}
		})
	}
}

function initFormContact() {
	
	$('#form-contact').each(function () {
		$(this).load('/kontakt', function () {
			ajaxifyFormContact(this)
			$('.buttons input', this).val('').addClass('btn-send')
			//initImages()
		})
	})
	
}

function ajaxifyFormContact(e) {
	if (e) {
		$('form', e).ajaxForm({ target: '#form-contact', success: function () {
				ajaxifyFormContact(e)
				$('.buttons input', this).val('').addClass('btn-send')
				//initImages()
			},
			beforeSubmit: function () {
				$('.buttons input', e).attr('disabled', 'disabled')
				$('form input, form textarea', e).attr('disabled', 'disabled')
			}
		})
	}
}

