function showCalendar() {
    $("#chci").datepicker('show');
}
function showTime() {
   $('#time').focus();
}

function bindTimepicker(){
    $('#time').datetime({
        language    : 'cs',
        format      : 'HH. - mm.',
        showScale    :    true,
        minuteStep : 15,
        showButtons : true
    });
}
function bindDatepicker(){
    $('#chci').attr('class', '');

    $("#chci").datepicker({
        showOn: 'button',
        buttonImage: '/img/chciimg.gif',
        buttonImageOnly: true,
        minDate: 0,
        maxDate: '+30D',
        firstDay: 1,
        dateFormat: 'dd.mm.yy',
        dayNames: ['Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota'],
        dayNamesMin: ['Ne', 'Po', 'Út', 'St', 'Čt', 'Pá', 'So'],
        monthNames: ['Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec'],
        monthNamesShort: ['Led','Úno','Bře','Dub','Kvě','Čer','Čec','Srp','Zář','Říj','Lis','Pro'],
        nextText: 'Další',
        prevText: 'Předchozí',
        buttonText: 'Kalendář',
        showButtonPanel:true,
        closeText:'Zavřít'

    });
    if($('#contactNow').val() == 'true'){
        $('#chci').attr('class', 'hasDatepicker disabled');
    }

}

function formatDate(date, format) {
    format = format + "";
    var result = "";
    var i_format = 0;
    var c = "";
    var token = "";
    var y = date.getYear() + "";
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var E = date.getDay();
    var H = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
    var value = new Object();
    if (y.length < 4) {
        y = "" + (y - 0 + 1900);
    }
    value["y"] = "" + y;
    value["yyyy"] = y;
    value["yy"] = y.substring(2, 4);
    value["M"] = M;
    value["MM"] = LZ(M);
    value["d"] = d;
    value["dd"] = LZ(d);
    value["H"] = H;
    value["HH"] = LZ(H);
    if (H == 0) {
        value["h"] = 12;
    } else if (H > 12) {
        value["h"] = H - 12;
    } else {
        value["h"] = H;
    }
    value["hh"] = LZ(value["h"]);
    if (H > 11) {
        value["K"] = H - 12;
    } else {
        value["K"] = H;
    }
    value["k"] = H + 1;
    value["KK"] = LZ(value["K"]);
    value["kk"] = LZ(value["k"]);
    if (H > 11) {
        value["a"] = "PM";
    } else {
        value["a"] = "AM";
    }
    value["m"] = m;
    value["mm"] = LZ(m);
    value["s"] = s;
    value["ss"] = LZ(s);
    while (i_format < format.length) {
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (value[token] != null) {
            result = result + value[token];
        } else {
            result = result + token;
        }
    }
    return result;
}

function LZ(x) {
    return(x < 0 || x > 9 ? "" : "0") + x
}

function setContactNow(){
    $('#chci').hide('medium', function(){$('#chci').val(formatDate(new Date(), 'dd.MM.yyyy'));});
    $('#chci').show('medium');
    var date = new Date();
    var hr = date.getHours();
    $('#time').hide('medium', function(){$('#time').val(parseInt(hr)+'. - '+(parseInt(hr)+1)+'. hod');});
    $('#time').show('medium');
    $('#vdobeabs').hide('slow', function(){
        $('#vdobeabs').html('<strong style=\"color:red\">Ozveme se do 30 min. po odeslání formuláře</strong>');
    });
    $("#chci").datepicker('hide');
    $('#vdobeabs').show('medium');
    $('#contactNow').val('true');

    $('#time').removeAttr('class');
    $('#time').unbind();
    $('#time').attr('class', 'disabled');
    $('#chci').attr('class', 'hasDatepicker disabled');
    $('#timepickerIco').hide('slow');
}

function unsetContactNow(){
    $('#time').attr('class', 'hasDateTime');
    $('#chci').attr('class', 'hasDatepicker');
    $('#contactNow').val('');
    $('#contactNowDatepicker').attr('checked', false);
    $('#vdobeabs').hide('slow', function(){
        $('#vdobeabs').text('Příklad vyplnění: 8.- 9. hod');
    });
    $('#vdobeabs').show('medium');
    $('#timepickerIco').show('medium');
	$('#chci').val('');
	$('#time').val('');

    bindTimepicker();
}

$(document).ready(function() {
    bindDatepicker();
    $('#ui-datepicker-div').css('display', 'none');
    bindTimepicker();
    if($('#contactNow').val() != 'true'){
        bindTimepicker();
    }


    $.aop.after({target: $.datepicker, method: '_gotoToday'},
            function(regex, newString) {
                if($('#contactNow').val() == ''){
                    setContactNow();
                }
                else{
                    unsetContactNow();
                }
            }
    );

    $.aop.after({target: $.datepicker, method: '_showDatepicker'},
            function(regex, newString) {
                if($('#contactNow').val() == 'true'){
                    $('#contactNowDatepicker').attr('checked', true);
                }
                else{
                    $('#contactNowDatepicker').removeAttr('checked');
                }
            }
    );

    $.aop.after({target: $.datepicker, method: '_selectDay'},
            function(regex, newString) {
                if($('#contactNow').val() == 'true'){
                    unsetContactNow();
                }
                $("#chci").datepicker('hide');
            }
    );
});


