target_calendar_php = baseUrl + "/index/calendar-php";
target_calendarselectday = baseUrl + "/index/calendarselectday";
d = new Date();
curr_month = d.getMonth() - (-1);
curr_year = d.getFullYear();
var da = function () {
    if ($("#selectMonth :selected").val()!='')
        var datearray = $("#selectMonth :selected").val().split('-');
    else
        var datearray = [curr_year,curr_month];
    return datearray;
}

$(document).ready(function() {
    $("#selectMonth")
        .change(function () {
            var datearray = da();
            $.get(target_calendar_php,{'place':'main','month':datearray[1],'year':datearray[0]},function(data){
                $("#monthActualBig").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':datearray[1]-1,'year':datearray[0]},function(data){
                $("#monthPrev").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':datearray[1],'year':datearray[0]},function(data){
                $("#monthActual").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':datearray[1]-(-1),'year':datearray[0]},function(data){
                $("#monthNext").html(data);
                bindBehaviors();
            });
        });
    $("#arrowDown, #monthNext")
        .click(function () {
            if ($("#selectMonth option:selected").val() != $("#selectMonth option:last-child").val()) {
                var datearray = da();
                if ($("#selectMonth :selected").val()=='') {
                    $("#selectMonth :selected").removeAttr("selected");
                    $("#selectMonth option[value='"+curr_year+"-"+curr_month+"']").attr({selected:'selected'});
                }
                $.get(target_calendar_php,{'place':'main','month':datearray[1]-(-1),'year':datearray[0]},function(data){
                    $("#monthActualBig").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1],'year':datearray[0]},function(data){
                    $("#monthPrev").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1]-(-1),'year':datearray[0]},function(data){
                    $("#monthActual").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1]-(-2),'year':datearray[0]},function(data){
                    $("#monthNext").html(data);
                    bindBehaviors();
                });
                $("#selectMonth :selected").removeAttr("selected").next().attr({selected:'selected'});
            }
        });
    $("#arrowUp, #monthPrev")
        .click(function () {
            if ($("#selectMonth option:selected").val() != $("#selectMonth option:nth-child(2)").val()) {
                var datearray = da();
                if ($("#selectMonth :selected").val()=='') {
                    $("#selectMonth :selected").removeAttr("selected");
                    $("#selectMonth option[value='"+curr_year+"-"+curr_month+"']").attr({selected:'selected'});
                }
                $.get(target_calendar_php,{'place':'main','month':datearray[1]-1,'year':datearray[0]},function(data){
                    $("#monthActualBig").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1]-2,'year':datearray[0]},function(data){
                    $("#monthPrev").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1]-1,'year':datearray[0]},function(data){
                    $("#monthActual").html(data);
                    bindBehaviors();
                });
                $.get(target_calendar_php,{'place':'left','month':datearray[1],'year':datearray[0]},function(data){
                    $("#monthNext").html(data);
                    bindBehaviors();
                });
                $("#selectMonth :selected").removeAttr("selected").prev().attr({selected:'selected'});
            }
        });
    $(".naptar_tartalom")
        .click(function () {
            if ($(this).find("div:last").text() != '') {
                var datearray = da();
                $("#program_over_layer").css("display","block");
                $(".program_over_header").text($(".naptar_fejlec").text() + ' ' + $(this).find("div:first").text() + '.');
                $.get(target_calendarselectday,{'day':$(this).find("div:first").text(),'month':datearray[1],'year':datearray[0]},function(data){
                    $(".program_over_content").html(data);
                });
            }
        });
    $(".program_over_bezar")
        .click(function () {
            $("#program_over_layer").css("display","none");
        });
    $("#findToday")
        .click(function (event) {
            event.preventDefault();
            $.get(target_calendar_php,{'place':'main','month':curr_month,'year':curr_year},function(data){
                $("#monthActualBig").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':curr_month-1,'year':curr_year},function(data){
                $("#monthPrev").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':curr_month,'year':curr_year},function(data){
                $("#monthActual").html(data);
                bindBehaviors();
            });
            $.get(target_calendar_php,{'place':'left','month':curr_month-(-1),'year':curr_year},function(data){
                $("#monthNext").html(data);
                bindBehaviors();
            });
            $("#selectMonth :selected").removeAttr("selected");
            $("#selectMonth option[value='"+curr_year+"-"+curr_month+"']").attr({selected:'selected'});
        });
    /* ajanlatok */
    $("#date").datepicker();
    $("#date_until").datepicker();
    pattern_date = new RegExp("(\d){4}-(\d){2}-(\d){2}");
    $("#submitbutton").click( function () {
        msg = "";
        if ($("#text").val()=='') {
            msg += "Nincs kitöltve az Esemény mező!\n";
        }
        if (!$("#date").val().match(/(\d){4}-(\d){2}-(\d){2}/)) {
            msg += "Hibás a kezdődátum formátuma! (éééé-hh-nn)\n";
        }
        if ($("#date_until").val()!='' && !$("#date_until").val().match(/(\d){4}-(\d){2}-(\d){2}/)) {
            msg += "Hibás a végdátum formátuma! (éééé-hh-nn)\n";
        }
        if ($("#time").val()!='' && !$("#time").val().match(/(\d){2}:(\d){2}/)) {
            msg += "Hibás a kezdőidőpont formátuma! (óó:pp)\n";
        }
        if ($("#time_until").val()!='' && !$("#time_until").val().match(/(\d){2}:(\d){2}/)) {
            msg += "Hibás a végidőpont formátuma! (óó:pp)\n";
        }
        if ($("#place").val()=='') {
            msg += "Nincs kitöltve a Helyszín mező!\n";
        }
        if ($("[name=captcha]").val()=='') {
            msg += "Írd be a képen szereplő kódot az alatta levő üres mezőbe!\n";
        }
        if (msg!="") {
            alert (msg);
            return false;
        }
    });
    /* */
});

function bindBehaviors() {
    $(".naptar_tartalom")
        .click(function () {
            if ($(this).find("div:last").text() != '') {
                var datearray = da();
                $("#program_over_layer").css("display","block");
                $(".program_over_header").text($(".naptar_fejlec").text() + ' ' + $(this).find("div:first").text() + '.');
                $.get(target_calendarselectday,{'day':$(this).find("div:first").text(),'month':datearray[1],'year':datearray[0]},function(data){
                    $(".program_over_content").html(data);
                });
            }
        });
    $(".program_over_bezar")
        .click(function () {
            $("#program_over_layer").css("display","none");
        });
};


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expiredays, path, domain, secure) {
	if (expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
	}
    document.cookie= name + "=" + escape(value) +
        ((expiredays) ? "; expires=" + exdate.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
