// globális változók, ezekben tároljuk a vásárokat
var busyDaysDate = [];
var busyDaysDateLabel = [];
var busyDaysDesc = [];
var busyDaysImg = [];
var idxForMonth = [];
var actDate = new Date();

// ez végzi az AJAX hívást, ami lekéri az adott hónap eseményeit
function fetchBusyDays(year, month)
{
    busyDaysDate = [];
    busyDaysDateLabel = [];
    busyDaysDesc = [];
    busyDaysImg = [];
    idxForMonth = [];

    var start_date = '';
 
    // if a month and year were supplied, build a start_date in yyyy-mm-dd format
    if (year != undefined && month != undefined) {
        start_date = '01' +'-';
        start_date += month +'-';
        start_date += year;
    }
 
    $.ajax({
        async: false,
        url : 'index.php',
        dataType: 'json',
        data : {
            eID : 'getCalendarEvents',
            calendar_vars : ({
                'action' : 'monthChanged',
                'date' 	 : start_date,
                'lang'	 : $('#hiddenlang').val()
            })
        },
        success : function(data) {
            var counter = 0;
            $.each(data, function(index, value) {
                var tempSDate = new Date(value.sdate.replace(/-/g,"/"));
                var sDate = "";
                if (value.edate == "1970-01-01" || value.sdate == value.edate) {
                    sDate = (tempSDate.getDate() < 10) ? "0"+tempSDate.getDate() : tempSDate.getDate();
                    sDate += "."+(tempSDate.getMonth()+1)+"."+tempSDate.getFullYear();
                    
                    busyDaysDate.push(value.sdate.replace(/-/g,"/"));
                    busyDaysDateLabel.push(sDate);
                    busyDaysDesc.push(value.desc);
                    busyDaysImg.push(value.img);
                    idxForMonth.push(counter++);
                } else {
                    var tempEDate = new Date(value.edate.replace(/-/g,"/"));
                    idxForMonth.push(counter);
                    sDate = (tempSDate.getDate() < 10) ? "0"+tempSDate.getDate() : tempSDate.getDate();
                    sDate += "."+(tempSDate.getMonth()+1)+".";
                    var eDate = (tempEDate.getDate() < 10) ? "0"+tempEDate.getDate() : tempEDate.getDate();
                    eDate += "."+(tempEDate.getMonth()+1)+"."+tempEDate.getFullYear();
                    while (tempSDate <= tempEDate) {
                        var theDate = tempSDate.getFullYear()+"/"+(tempSDate.getMonth()+1)+"/";
                        theDate += (tempSDate.getDate() < 10) ? "0"+tempSDate.getDate() : tempSDate.getDate();
                        busyDaysDate.push(theDate);
                        busyDaysDateLabel.push(sDate+" - "+eDate);
                        busyDaysDesc.push(value.desc);
                        busyDaysImg.push(value.img);
                        
                        tempSDate.setDate(tempSDate.getDate()+1);
                        counter++;
                    }
                }
            });
        }
    });
    selectDate('01/12/1000');
}

// minden egyes nap megjelenítésénél lefut, itt állítjuk be a napok formázását
function highlightDays(date)
{
    for (var i = 0; i < busyDaysDate.length; i++) {
        if (new Date(busyDaysDate[i]).getDate() == date.getDate()) {
            return [true, 'has-event']; // [0] = true | false if this day is selectable, [1] = class to add, [2] = tooltip to display
        }
    }
	
    if (date > actDate) {
        return [true, 'nextDays', 'no event'];
    }
	
    return [true, ''];
}

// egy adott napot kiválasztva hajtódik végre
function selectDate(date)
{
    var tempArray = date.split("/");
    convDate = new Date(tempArray[2]+"/"+tempArray[1]+"/"+tempArray[0]);
    theDate = convDate.getFullYear()+"/"+(convDate.getMonth()+1)+"/";
    theDate += (convDate.getDate() < 10) ? "0"+convDate.getDate() : convDate.getDate();
	
    $('#calendar_texts').html("");
    if ($.inArray(theDate, busyDaysDate) != -1) { // egy adott nap listázása
        $.each(busyDaysDate, function(index, value) {
            if (theDate == value) {
                
                var eventHTML = '<div class="eventText"><b>'+busyDaysDateLabel[index]+":</b> "+busyDaysDesc[index]+'</div><img src="'+busyDaysImg[index]+'" />';
                $('#calendar_texts').html($('#calendar_texts').html()+eventHTML);
            }
        });
    } else { // az egész hónap listázása
        $.each(idxForMonth, function(index, value) {
            var eventHTML = '<div class="eventText"><b>'+
                                busyDaysDateLabel[value]+":</b> "+
                                busyDaysDesc[value]+'</div>'+
                                '<img src="'+busyDaysImg[value]+'" />';
            $('#calendar_texts').html($('#calendar_texts').html()+eventHTML);
        });
    }
}

// A második szintű menü pozíciójának beállítása
function fixMenu()
{
    if ($('#headerWrap') !== null) {
        var max_width = $('#headerWrap').offset().left + $('#headerWrap').width();
        var m1_offset = $('#mainMenu a.cur').offset();
        if (m1_offset == null) {
            m1_offset = $('#mainMenu a.act').offset();
        }
        var m2_width = $('#menuLevel2').width();

        if (m1_offset != null) {
            if (m1_offset.left + m2_width > max_width) {
                $('#menuLevel2').offset({left: (m1_offset.left-m2_width+$('#mainMenu a.cur').parent().width())});
            } else {
                $('#menuLevel2').offset({left: m1_offset.left});
            }
        }
    }
}

// A kép előtt látható image
function fixImageText() {
    if ($("#imageText") !== null) {
        if ($("#imageText").html() == "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->") {
            $("#imageText").remove();
        } else {
            $("#imageText").html('<div id="imageTextTop"></div>'+$("#imageText").html()+'<div id="imageTextBottom"></div>');
        }
    }
}

function openEvents() {
    if ($("#calendar_texts").height() == 182) {
        $("#calendar_texts").height("auto");
        $("#calendar_content_btn2 a").removeClass("opener");
        $("#calendar_content_btn2 a").addClass("closer");
    } else {
        $("#calendar_texts").height(182);
        $("#calendar_content_btn2 a").removeClass("closer");
        $("#calendar_content_btn2 a").addClass("opener");
    }
    
}

function fixCalendarMonth() {
    if ($('.ui-datepicker-month') !== null) {
        $('.ui-datepicker-month').live('click',function(){
            selectDate('01/12/1000');
        });
    }
}

$(document).ready(function() {
    fixMenu();
    fixImageText();
    fixCalendarMonth();
    fetchBusyDays();
    selectDate('01/12/1000');
    $( "#calendar" ).datepicker({
        prevText: '<',
        nextText: '>',
        firstDay: 1,
        dayNamesMin: ['SO', 'MO', 'DI', 'MI', 'DO', 'FR', 'SA'],
        beforeShowDay: highlightDays,
        onChangeMonthYear: fetchBusyDays,
        onSelect: selectDate
    });
});
