function styleDatepicker(id){
	$('#boxes #' + id + ' table.ui-datepicker-calendar tr').each(function(){
		$(this).prepend('<td class="first"></td>').append('<td class="last"></td>');
	});
	$('#container .calendar .ui-datepicker-header .ui-icon-circle-triangle-w').removeClass('ui-icon-circle-triangle-w').addClass('ui-icon-triangle-1-w');
	$('#container .calendar .ui-datepicker-header .ui-icon-circle-triangle-e').removeClass('ui-icon-circle-triangle-e').addClass('ui-icon-triangle-1-e');
	$('#' + id + ' .ui-datepicker-header .ui-datepicker-next').prepend('<span class="month">' + $('#' + id + ' .ui-datepicker-header .ui-datepicker-next span.ui-icon').text() + '</span>');
	$('#' + id + ' .ui-datepicker-header .ui-datepicker-prev').append('<span class="month">' + $('#' + id + ' .ui-datepicker-header .ui-datepicker-prev span.ui-icon').text() + '</span>');
}

var cycleDefaults = {
	timeout: 6000,
	random: 1,
	speed: 1500
}

var fancyboxDefaults = {
	centerOnScroll: true,
	overlayColor: '#000'
};


var calendar = {
	JSON: {},
	getJSON: function(year,month) {
		var addr = $('.footerCalendar').find('span:first').attr('rel');
		addr = addr.replace("tmpMonth", (month+1));
		addr = addr.replace('tmpYear', year);
		$.getJSON(addr, function(data) {
			calendar.JSON = $.extend(true, calendar.JSON, data);
			$('.calendar').datepicker('refresh');
			listActivities(year,(month+1));
		});
	},
	getMonth: function(year,month) {
		if (typeof this.JSON['m' + year + '-' + (month+1)] === 'undefined') {
			this.JSON['m' + year + '-' + (month+1)] = {};
			this.getJSON(year,month);
		}
	},
	getDay: function(date) {
		var yearMonth = 'm' + date.getFullYear() + '-' + (date.getMonth()+1),
		day = date.getDate();

		if (typeof this.JSON[yearMonth] === 'undefined') {
			return false;
		}
		else {
			var ret = false;
			for(activity in this.JSON[yearMonth]){
				var tmpDate = new Date(this.JSON[yearMonth][activity].date*1000);
				if(tmpDate.getDate() == day){
					ret = true;
				}
			}
			return ret;
		}
	}
};

jQuery.datepicker.oldUpdateDatepicker = jQuery.datepicker._updateDatepicker;
jQuery.datepicker._updateDatepicker = function(inst) {

	this.oldUpdateDatepicker(inst);
	calendar.getMonth(inst.selectedYear, inst.selectedMonth);
	if($(inst).attr('id') == 'activityCalendar'){
		listActivities(inst.selectedYear, (inst.selectedMonth+1));
	}
	styleDatepicker(inst.dpDiv.parent().attr('id'));
	initScroller();
}

function listActivities(year,month) {
	if($('.activityList').length > 0){
		var now = new Date();
		var yearMonth = 'm' + year + '-' + month;
		$('.activityList').html('');
		if(typeof(calendar.JSON[yearMonth]) === 'undefined')
			calendar.getMonth(year,(month-1));
		else {
			for(activity in calendar.JSON[yearMonth]){
				var tmpDate = new Date(calendar.JSON[yearMonth][activity].date*1000);
				$('.activityList').append('<li>'
					+ '<span class="square '+ (tmpDate > now ? 'red' : 'blue') + '"></span><span class="date">'+ tmpDate.getDate() + '.' + (tmpDate.getMonth()+1) + '.' + tmpDate.getFullYear() +'</span>'
					+ '<span class="title">' + calendar.JSON[yearMonth][activity].title + '</span></li>');
			};
			ajaxFancyboxInit();
		}
	}
}

function ajaxFancyboxInit(){
	$('.memberList li a.member').fancybox({
		type: 'ajax',
		ajax: {
			dataFilter: function(data) {
				return $(data).find('.memberContainer').first();
			}
		},
		overlayColor: '#000',
		autoDimensions: false,
		height: 1,
		width: 600,
		onComplete: function(){
			$.fancybox.resize();
		}
	});
}

function setDefaultDate(){
	var defaultDate = null;
	if(document.URL.indexOf('month=') != -1 && document.URL.indexOf('year=') != -1){
		var month = document.URL.substring(document.URL.indexOf('month=') + 6, document.URL.length);
		var year = document.URL.substring((document.URL.indexOf('year=') + 5), document.URL.indexOf('year=') + 9);
		defaultDate = "15." + month + '.' + year;
	};
	return defaultDate;
}

function initScroller(){
	var pageContentHtml = $('.pageContent').html();
	if($('.pageContent').size() > 0 && $('.pageContent').find('.sympal_slot_wrapper').size() == 0 && $('.pageContent').find('.galleryPhotos').size() == 0){
		if($('.scrollbar').size() == 0 )
			$('.pageContent').html('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div><div class="viewport"><div class="overview">' + pageContentHtml + '</div></div>');
		$('.pageContent').tinyscrollbar({
			sizethumb: 100
		});

	}
	var galleryPhotosHtml = $('.galleryPhotos').html();
	if($('.galleryPhotos').size() > 0){
		if($('.scrollbar').size() == 0 )
			$('.galleryPhotos').html('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div><div class="viewport"><div class="overview">' + galleryPhotosHtml + '</div></div>');
		$('.galleryPhotos').tinyscrollbar({
			sizethumb: 100
		});

	}
}

var gallery = {
	t: '',
	galleryPhotos: [],
	enlargedContainer: '.galleryDetails .enlarged',
	next: {},
	prev: {},
	slideshow: 'on',
	timeout: 5500,
	init: function(){
		$('ul.galleryPhotos li a').each(function(){
			gallery.galleryPhotos.push(this);
		});
		$('ul.galleryPhotos li a').click(function(e){
			e.preventDefault();
			var clicked = $(this);
			gallery.setNextAndPrev(clicked);
			gallery.enlarge(clicked);
			clearTimeout(gallery.t);
			if(gallery.slideshow == 'on')
				gallery.t = setTimeout("(gallery.next).click()", gallery.timeout);
		});
		$('a#prev').click(function(e){
			e.preventDefault();
			(gallery.prev).click();
		});
		$('a#next').click(function(e){
			e.preventDefault();
			(gallery.next).click();
		});
		$('#slideshowControll').click(function(e){
			e.preventDefault();
			if(gallery.slideshow == 'on'){
				clearTimeout(gallery.t);
				gallery.slideshow = 'off';
				$(this).html('wznów pokaz');
			}
			else {
				gallery.slideshow = 'on';
				$(this).html('zatrzymaj pokaz');
				(gallery.next).click();
			}
		});
		$(gallery.galleryPhotos[0]).click();
	},
	setNextAndPrev: function(img){
		for(var i=0;i<gallery.galleryPhotos.length; i++){
			if($(gallery.galleryPhotos[i]).attr('href') == img.attr('href')){
				if(typeof(gallery.galleryPhotos[i+1]) != 'undefined')
					gallery.next = $(gallery.galleryPhotos[i+1]);
				else
					gallery.next = $(gallery.galleryPhotos[0]);
				if(typeof(gallery.galleryPhotos[i-1]) != 'undefined')
					gallery.prev = $(gallery.galleryPhotos[i-1]);
				else
					gallery.prev = $(gallery.galleryPhotos[gallery.galleryPhotos.length-1]);
			}
		};
	},
	enlarge: function(img){
		var enlargedContainer = $(this.enlargedContainer);
		enlargedContainer.find('.enlargedContent').fadeOut('fast', function(){
			var image = enlargedContainer.find('.enlargedContent img').first();
			image.attr('src', $(img).attr('href'));
			enlargedContainer.addClass('loading');
			image.load(function(){
				enlargedContainer.removeClass('loading');
				enlargedContainer.find('.enlargedContent').html(image).fadeIn()
			});
		});
	}
}

$(function(){

	initScroller();

	var defaultDate = setDefaultDate();
	var datepickerSettings = {
			navigationAsDateFormat: true,
			nextText: 'M',
			prevText: 'M',
			beforeShowDay: highlightActivities,
			dateFormat: 'dd.mm.yy',
			defaultDate: defaultDate,
			onSelect: function(dateText, inst){
				if($(this).hasClass('footerCalendar'))
					window.location.href = $(this).find('span:first').attr('class') + '?year=' + inst.selectedYear + '&month=' + (inst.selectedMonth+1);
			},
			onChangeMonthYear: function(year, month, inst){
				$('.calendar').datepicker('setDate', '15.' + month + '.' + year)
			}
	};
	$('a[href$=".jpg"],a[href$=".png"],a[href$=".gif"]').not($('ul.galleryPhotos li a')).fancybox(fancyboxDefaults);
	if($('#cyclePhotos').size() > 0)
		$('#cyclePhotos').cycle(cycleDefaults);
	$('.calendar').datepicker(datepickerSettings);
	$.datepicker.regional['pl'];

	function highlightActivities(date){
		var today = new Date();
		if(calendar.getDay(date))
			if(today < date)
				return [true, 'ui-state-active'];
			else
				return [true, 'ui-state-active past'];
		return [false, ''];
	}

	gallery.init();

	ajaxFancyboxInit();
});

