var t;
var c;
$(document).ready(function() {
    $('#other_captcha_image').click(function() {
        $('#captcha_img').attr('src', '/captcha.php?chid=' + (new Date()).getTime());
        return false;
    });
    c = 0;
    init_calendarbutton();
    init_switcher();
    $.datepicker.setDefaults($.datepicker.regional["hu"]);
    $(".date").datepicker();
    $("#login-button-id").click(function(){
        dialog('login', 'Bejelentkezés / Felhasználó');
    });
    $(".login-button").click(function(){
        dialog('login', 'Bejelentkezés / Felhasználó');
    });
    if ($("#ms-count").val() != "0") doTimer();
    $("#msa-play").click(function(){
        if ($("#ms-playon").val() == "1") {
            $("#ms-playon").val("0");
            $(this).children("img").attr("src", "/images/general/button-play.png");
        } else {
            $("#ms-playon").val("1");
            $(this).children("img").attr("src", "/images/general/button-pause.png");
        }
        doTimer();
    });
    $(".msa").click(function(){
        $("#ms-playon").val("0");
        $("#msa-play").children("img").attr("src", "/images/general/button-play.png");
        var i = $(this).attr("id").split("-");
        //$(".Menustrip-roll").hide();
        //$("#ms-"+i[1]).show("clip", 500);
        $("#ms-main").css("background-image", $("#ms-"+i[1]).css("background-image"));
        $("#ms-main-a").html($("#ms-"+i[1]+" a").html());
        $("#ms-main-a").attr("href", $("#ms-"+i[1]+" a").attr("href"));
        $("#ms-main-div").html($("#ms-"+i[1]+" div").html());
        $("#ms-main-div").attr("onclick", "document.location='"+$("#ms-"+i[1]+" a").attr("href")+"';");
        $(".msi").removeClass("actual");
        $("#msi-"+i[1]).addClass("actual");
        doTimer();
    });
    //$("#wrapper0").css("display", "block");
    init_menu();
    $("#ms-wrapper").css("display", "block");
});
function timedCount() {
    //$(".Menustrip-roll").hide();
    //$("#ms-"+c).show("clip", 500);
    $("#ms-main").css("background-image", $("#ms-"+c).css("background-image"));
    $("#ms-main-a").html($("#ms-"+c+" a").html());
    $("#ms-main-a").attr("href", $("#ms-"+c+" a").attr("href"));
    $("#ms-main-div").html($("#ms-"+c+" div").html());
    $("#ms-main-div").attr("onclick", "document.location='"+$("#ms-"+c+" a").attr("href")+"';");
    $("#ms-main").show("blind", 1000);
    $(".msi").removeClass("actual");
    $("#msi-"+c).addClass("actual");
    c = c + 1;
    if (c >= parseInt($("#ms-count").val())) c = 0;
    t = setTimeout("timedCount()", 6000);
}
function doTimer() {
    if ($("#ms-playon").val() == "1") timedCount();
    else clearTimeout(t);
}

function init_switcher(){
    $(".switch").each(function(i){
        if($(this).hasClass("open")) {
            $(this).parent().children(".switcher").show();
        } else {
            $(this).parent().children(".switcher").hide();
        }
    });
    $(".switch").click(function(){
        if($(this).hasClass("close")) {
            $(this).removeClass("close");
            $(this).addClass("open");
            $(this).parent().children(".switcher").show("blind");
            setCookie("switcher["+$(this).parent().attr("id")+"]", "open", 1);
        } else {
            $(this).removeClass("open");
            $(this).addClass("close");
            $(this).parent().children(".switcher").hide("blind");
            setCookie("switcher["+$(this).parent().attr("id")+"]", "close", 1);
        }
    });
}
function init_menu(){
    $(".menuitem").each(function(i){
        $(this).parent().children("div").css("left", ($(this).position().left)+"px");
    });
    $(".menuitemspan").hover(function () {
        $(this).children("div").show();
        if ($(this).children("div").css("display")=="block") $(this).children(".menuitem").addClass("active");
    }, function () {
        $(this).children("div").hide();
        if ($(this).children("div").css("display")=="none") $(this).children(".menuitem").removeClass("active");
    });
}
function init_calendarbutton() {
    $(".tt").hover(function () {
        $(this).children("div").css("display", "block");
    }, function () {
        $(this).children("div").css("display", "none");
    });
    $('#calendar_prev').unbind('click');
    $('#calendar_prev').click(function() {
        //$('#ajax-loader2').css('top', $('#Rendezvenyek').position().top+46+'px');
        $('#ajax-loader2').css('display', 'block');
        $.ajax({
            url: '/ajax_index.php',
            type: 'POST',
            data: {
                modul: 'Rendezvenyek',
                date: $(this).attr('name'),
                ajax: true
            },
            success: function(response) {
                $('#esemenynaptar').html($('#esemenynaptar', response).html());
                $('#ajax-loader2').css('display', 'none');
                init_calendarbutton();
            }
        });
    });
    $('#calendar_next').unbind('click');
    $('#calendar_next').click(function() {
        //$('#ajax-loader2').css('top', $('#Rendezvenyek').position().top+46+'px');
        $('#ajax-loader2').css('display', 'block');
        $.ajax({
            url: '/ajax_index.php',
            type: 'POST',
            data: {
                modul: 'Rendezvenyek',
                date: $(this).attr('name'),
                ajax: true
            },
            success: function(response) {
                $('#esemenynaptar').html($('#esemenynaptar', response).html());
                $('#ajax-loader2').css('display', 'none');
                init_calendarbutton();
            }
        });
    });
}
function dialog(idname, title) {
    $("#"+idname).dialog({
        title: title,
        width: 505,
        minHeight: 50,
        modal: true,
        resizable: false,
        draggable: true,
        autoShow: false
    });
    $("#"+idname).dialog("open");
}
function setCookie(n,v,d){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+d);
    document.cookie=n+"="+escape(v)+((d==0)?"":";expires="+exdate.toGMTString())+";path=/";
}
function getCookie(n){
    if(document.cookie.length>0){
        var cs=document.cookie.indexOf(n+"=");
        if(cs!=-1){
            cs=cs+n.length+1;
            var ce=document.cookie.indexOf(";",cs);
            if(ce==-1)ce=document.cookie.length;
            return unescape(document.cookie.substring(cs,ce));
        }
    }
    return "";
}
function setBG(bc, bi) {
    $("body").css("background-color", bc);
    $("body").css("background-image", bi);
    setCookie('bgcolor', bc);
    setCookie('bgimage', bi);
}
function pager(actual) {
    document.getElementById('pageractual').value = actual;
    document.getElementById('pagerform').submit();
}
function pager2(actual) {
    document.getElementById('p').value = actual;
    document.getElementById('pagerform').submit();
}
function pager2f(actual) {
    $('#pagerform').attr("action", "/forraskereso/?p="+actual);
    $('#pagerform').submit();
}

