/* use to load functions after loading page - jquery util*/
$(document).ready(function() {
	
	//ustawienie tej samej wysokości dla elementu .content po lewej i prawej stronie
	setContentHeight();
	
	//ustawienie odpowiedniej wysokości dla tła przed elementem textarea
	$('.beforeTextarea').height($('.beforeTextarea + textarea').height() + 6);
	
	$("#nobelSport").not('.currentPage').mouseover(function(){
		$("#nobelSport img").attr("src", "images/index/nobel_sport_active.jpg");
	});
	$("#nobelSport").not('.currentPage').mouseout(function(){
		$("#nobelSport img").attr("src", "images/index/nobel_sport.jpg");
	});
	
	$("#foto").not('.currentPage').mouseover(function(){
		$("#foto img").attr("src", "images/index/foto_active.jpg");
	});
	
	$("#foto").not('.currentPage').mouseout(function(){
		$("#foto img").attr("src", "images/index/foto.jpg");
	});
	
	$("#informator").not('.currentPage').mouseover(function(){
		$("#informator img").attr("src", "images/index/informator_active.jpg");
	});
	
	$("#informator").not('.currentPage').mouseout(function(){
		$("#informator img").attr("src", "images/index/informator.jpg");
	});
	
	$("#formularzZgloszeniowy").not('.currentPage').mouseover(function(){
		$("#formularzZgloszeniowy img").attr("src", "images/index/formularz_zgloszeniowy_active.jpg");
	});
	
	$("#formularzZgloszeniowy").not('.currentPage').mouseout(function(){
		$("#formularzZgloszeniowy img").attr("src", "images/index/formularz_zgloszeniowy.jpg");
	});
	
	$("#programNobelSport").not('.currentPage').mouseover(function(){
		$("#programNobelSport img").attr("src", "images/index/program_nobel_sport_active.jpg");
	});
	
	$("#programNobelSport").not('.currentPage').mouseout(function(){
		$("#programNobelSport img").attr("src", "images/index/program_nobel_sport.jpg");
	});
	
	$("#kontakt").not('.currentPage').mouseover(function(){
		$("#kontakt img").attr("src", "images/index/kontakt_active.jpg");
	});
	
	$("#kontakt").not('.currentPage').mouseout(function(){
		$("#kontakt img").attr("src", "images/index/kontakt.jpg");
	});
	
	/* FORMULARZE */
	$(".inputCheckbox").click(function(){
		$(this).toggleClass('checked');
	});	
	
	
	
	$('div.input input.input').focus(function(){
		if($(this).val() == $(this).attr('default') && !$(this).hasClass('error')){
			$(this).val('');
		}
		$(this).removeClass('error');
	});
	
	$('div.input input.input').blur(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('default'));
		}
	});
	
	$('div.input textarea.input').focus(function(){
		if($(this).text() == $(this).attr('default') && !$(this).hasClass('error')){
			$(this).text('');
		}
		$(this).removeClass('error');
	});
	
	$('div.input textarea.input').blur(function(){
		if($(this).text() == ''){
			$(this).text($(this).attr('default'));
		}
	});
	
	$('input#submitForm, input#submitContactForm').click(function(event){
		$('input.input').each(function(){
			if($(this).val() == $(this).attr('default')){
				$(this).val('');					
			}
		});
		
		$('textarea.input').each(function(){
			if($(this).text() == $(this).attr('default')){
				$(this).text('');
			}
		});
		
	});
	

	/* KONNIEC FORMULARZE */
	
	/* KALENDARZ */
	$.fullCalendar.monthNames = ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'];
	$.fullCalendar.dayAbbrevs = ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'];
	
	var hideEvents = new Array();
	
	/*GALERIA*/
	
//	$("#smallImages img").click(function(){
//		/*
//		 * STARA WERSJA GALERII 
//		 * $("#currentImage img").attr('src', $(this).attr('id'));
//		setContentHeight();
//		*/
//	});
	
	$("#smallImages a").lightBox({
		txtImage: "Obraz",
		txtOf: "z",
		overlayOpacity: 0.3
	});
	
	$('.inGalery a.prev').click(function(){
		currImage = $('div#currentImage img').attr('src');
		
		$('#smallImages img').each(function(){
			if($(this).attr('id') == currImage){
				newImage = $(this).prev('img').attr('id');
				if(newImage){
					$("#currentImage img").attr('src', newImage);
				}
			}
		});
	});
	
	$('.inGalery a.next').click(function(){
		currImage = $('#currentImage img').attr('src');
		
		$('#smallImages img').each(function(){
			if($(this).attr('id') == currImage){
				newImage = $(this).next('img').attr('id');
				if(newImage){
					$("#currentImage img").attr('src', newImage);
				}
			}
		});
		
	});
	
	$("#galleryPreview").data('page', 0);
	
	$("#galleryPreview a.next").click(function(){
		iEvent = $("#galleryPreview").attr('name');
		iRequestedPage = $("#galleryPreview").data('page') + 1;
		$.ajax({
			url: 'index/index/reload/id/' + iEvent + '/page/' + iRequestedPage,
			success: function(data, textStatus) {
				if(data){
					$("#smallImages").html(data);
					$("#galleryPreview").data('page', + iRequestedPage);
                                        $("#galleryPreview .pager").children('a').removeClass('current');
                                        $("#galleryPreview .pager").children('a:contains(' + (iRequestedPage + 1)+ ')').addClass('current')
				}
			}
		});
	});
	
	$("#galleryPreview a.prev").click(function(){
		iEvent = $("#galleryPreview").attr('name');
		iRequestedPage = $("#galleryPreview").data('page') - 1;
		if(iRequestedPage >= 0){
			$.ajax({
				url: 'index/index/reload/id/' + iEvent + '/page/' + iRequestedPage,
				success: function(data, textStatus) {
					if(data){
						$("#smallImages").html(data);
						$("#galleryPreview").data('page', + iRequestedPage);
                                                $("#galleryPreview .pager").children('a').removeClass('current');
                                                $("#galleryPreview .pager").children('a:contains(' + (iRequestedPage + 1)+ ')').addClass('current')
					}
				}
			});
		}
	});

        $("#galleryPreview .pager").children('a').click(function(){
            iEvent = $("#galleryPreview").attr('name');
            iRequestedPage = parseInt($(this).html()) - 1;
            var self = $(this);
            if(iRequestedPage >= 0){
                $.ajax({
                    url: 'index/index/reload/id/' + iEvent + '/page/' + iRequestedPage,
                    success: function(data, textStatus) {
                        if(data){
                            $("#smallImages").html(data);
                            $("#galleryPreview").data('page', + iRequestedPage);
                            self.parent().children('a').removeClass('current');
                            self.addClass('current');
                        }
                    }
                });
            }
	});

        $('#scroller #scrollerTop').click(function(event){
            event.preventDefault();
            $('#mycustomscroll').scrollTop($('#mycustomscroll').scrollTop() - 18);
        });

        $('#scroller #scrollerBottom').click(function(event){
            event.preventDefault();
            $('#mycustomscroll').scrollTop($('#mycustomscroll').scrollTop() + 18);
        })
	
	$('#sponsorsTable').dataTable({
		"iDisplayLength": 30,
		"bLengthChange": false,
		"bInfo": false,
		"oLanguage": {
			"sProcessing": "Proszę czekać...",
			"sSearch": "",
			"sLengthMenu": "Wyświetl _MENU_ wierszy na stronie",
			"sZeroRecords": "Nic nie znaleziono",
			"sInfo": "Widoczne wiersze od _START_ do _END_ z _TOTAL_ wszystkich",
			"sInfoEmtpy": "Brak wierszy do wyświetlenia",
			"sInfoFiltered": "(wyodrębnione z _MAX_ wszystkich wierszy)",
			"sZeroRecords": "",
			"oPaginate": {
				"sFirst":    "Pierwsza strona",
				"sPrevious": "Poprzednia strona",
				"sNext":     "Następna strona",
				"sLast":     "Ostatnia strona"
			}
		}
	});
	
});

function calendar(options){
	
	$("#events").fullCalendar({
		draggable: false,
		fixedWeeks: false,
		year: options.displayYear,
		month: parseInt(options.displayMonth) - 1,
		buttons: {
		   			today:     false,
		   			prevYear:  false,
		   			prevMonth: '&lt;&lt;',
		   			nextMonth: '&gt;&gt;',
		   			nextYear:  false
		   			},
		weekStart: 1,
		showTime: false,
		
		events: function(start, end, callback) {
		   $.getJSON("index/json/month",
		      {
		         start: dateYmd(start),
		         end: dateYmd(end)
		      },
		      
		      function(result) {
		    	 calevents = result;
		    	 callback(calevents);
		    	 
		    	 hideEvents = calevents;
		         
		         $('.eventPopup').removeClass('eventPopup');
		         $('.currentEventDate').removeClass('currentEventDate');
		         
		    	 dateIter = new Date(start);		         
		         $('td.day').each(function(){
		    		 for(key in hideEvents){
		    			 if(dateComp(hideEvents[key].start, dateIter)){
		    				 $(this).addClass('eventPopup');
		    				 break;
		    			 }
		    		 }
		    		 $(this).attr('id', dateYmd(dateIter));
		    		 
		    		 if(dateYmd(dateIter) == options.currentEventDate){
		    			 $(this).addClass('currentEventDate'); 			 
		    		 }
		    		 dateIter.setDate(dateIter.getDate() + 1);	 
		    		 
		    	 });
		         
		         setContentHeight();
		      });
		}
	
	});
	
}

function leadingZero(n){
	return ((n < 10) ? "0": "") + n; 
}

function dateYmd(date){
	temp = new Date(date)
	return temp.getFullYear() + '-' + leadingZero(parseInt(temp.getMonth()) + 1) + '-' + leadingZero(parseInt(temp.getDate()));
}

function dateComp(date1, date2){
	return (date1.getDate() == date2.getDate() 
			&& date1.getMonth() == date2.getMonth() 
			&& date1.getFullYear() == date2.getFullYear()) ? true : false;
}

function eventLinks(aEvents){
	eventContent = '';
	for(key in aEvents){
		eventContent += '<a href="index/index/event/id/' + aEvents[key].id + '/">' + aEvents[key].title + '</a>';
	}
	
	return eventContent;
}

function showPopup(aEvents, elem){
	$('#popup').hide();
	if(aEvents.length > 0) {
		$('#popup #popupContent').html(eventLinks(aEvents));
		
		position = $(elem).position();
		newTop = parseInt(position.top);
		newLeft = parseInt(position.left);
		$("#popup").css("top", newTop + 20 + "px");
		$("#popup").css("left", newLeft - 45 + "px");
		$('#popup').show();
	}
}

function setCurrentDayEvent(currentEventDate){
	$('#' + currentEventDate).addClass('currentEvent');
}


function setContentHeight(){
	maxHeight = 0;

	$('.content').each(function(){
		if($(this).height() > maxHeight){
			maxHeight = $(this).height();
		}
	});
	
	$('.content').each(function(){
		$(this).height(maxHeight);
	});
}

