function TIMEPICKER(ajaxURL){
    var oThis = this;
    var retElement = null
    
    this.ajaxURL  = ajaxURL;
    this.interval = true;
    this.staticTable = false;
    
    this.parentElement = $('#timePickerTable');
    
    $('.time-picker').click(function(e){
        oThis.retElement = $(e.target)
        oThis.showTable()
    })
}

TIMEPICKER.prototype.init = function(){
    var oThis = this;

    if(!this.staticTable){//ilyenkor nem kattinthatoak a datumok
        $('.closeTimePicker', this.parentElement).click(function(){oThis.hideTable()})
        
        $('.dayPicker:not(".disabledDate, .disableHour, .reserved")', $(this.parentElement)).click(function(){
            $(oThis.retElement).val($(this).attr('title'))
            oThis.onSelect(this);
            oThis.hideTable();
        })
    }
    
    
    $('#nextWeekButton').click(function(){
        var nextIndex = parseInt($('#currentWeekIndex').val());
        
        if(oThis.interval && nextIndex==3) return;

        $.get(
            oThis.ajaxURL+'&ajax='+(nextIndex+1),
            function(data){
                $('#dateTableContainer').html(data);
                oThis.parentElement = $('#timePickerTable');
                oThis.init();
                oThis.showTable();
            }
        )
    })
    
    $('#prevWeekButton').click(function(){
        var prevIndex = parseInt($('#currentWeekIndex').val());
        
        if(prevIndex==0) return;
        
        $.get(
            oThis.ajaxURL+'&ajax='+(prevIndex-1),
            function(data){
                $('#dateTableContainer').html(data);
                oThis.parentElement = $('#timePickerTable');
                oThis.init();
                oThis.showTable();
            }
        )
    })
    
}
TIMEPICKER.prototype.showTable = function(){
    if($(this.retElement).get(0).tagName!="INPUT") return;
    
    var oThis = this;
    
    var ofs = $(oThis.retElement).offset()

    $(this.parentElement).css({
        left: ofs.left + $(oThis.retElement).width() + 5 + "px",
        top:  (ofs.top) + "px"
    }).show()
    
}

TIMEPICKER.prototype.hideTable = function(){
    $(this.parentElement).hide()
}

TIMEPICKER.prototype.onSelect = function(td){
    $('#foglalas_datum').val($(td).children('.timepicker-date').val())
    $('#foglalas_ido').val($(td).children('.timepicker-id').val())
    $('#foglalas_weekday').val($(td).children('.timepicker-weekday').val())
}
