var smFragmentPath = '/templates/cdos/sm/';

$(document).ready(function() {
    // jizdne a slevy
    $("div.jizdenka a").attr("href", "#");
    $("#tab1").removeClass("close");
    $("#tab1").addClass("open");
    $("input[name='odpr'][value='inland1']").attr("checked", "checked");
    $("#selFare").html($("#selinland1").html());
    $("div.jizdenka form").show();

    // ovladaci prvek pro vyber data - datepicker
    setupDatepicker();

    // ovladaci prvek pro zmenu casu
    setupTimeControl();

    // zalozky
    $('#tabcontents').hide();
var tabnumber=1;
if (document.getElementById('mt4') && document.getElementById('mt4').className=="over")tabnumber=4;
    $('#tabcontent').html($('#tab-'+tabnumber).html());

    // zalozky
    $('.contentboxgreygrad .boxextra .tabmenu a').click(function() {
        $('.contentboxgreygrad .boxextra .tabmenu a').removeClass("over");
        var idtab = $(this).attr('href');
        $('#tabcontent').html($(idtab).html());
        $(this).addClass("over");
		return false;
    });

    // jizdne a slevy
    $("div.jizdenka a").click(function() {
        var oCurTab = $("div.jizdenka a.open");
        var aSelTab = $("#" + this.id);
        if (oCurTab.attr("id") == this.id) return false;
        oCurTab.removeClass("open");
        oCurTab.addClass("close");
        aSelTab.removeClass("close");
        aSelTab.addClass("open");
        switch (this.id) {
            case "tab1":
                $("#radioBoxIntl").hide();
                $("#radioBoxInland").show();
                $("input[name='odpr'][value='inland1']").attr("checked", "checked");
                radioFareOnClick("inland1");
                break;
            case "tab2":
                $("#radioBoxInland").hide();
                $("#radioBoxIntl").show();
                $("input[name='odpr'][value='intl1']").attr("checked", "checked");
                radioFareOnClick("intl1");
                break;
        }
        return false;
    });

    // jizdne a slevy
    $("input[name='odpr']").click(function() {
        radioFareOnClick(this.value);
    });

    // jizdne a slevy
    $("#btnShowOffer").click(function() {
        window.location.href = $('#selFare').val();
    });

    // prihlasovani
    $("a.login").click(function() {
        if (typeof fillAZAKSourceApp != "undefined") {
            $('#loginbox').load(smFragmentPath + 'loginbox.htm div.subbox', fillAZAKSourceApp);
        } else {
            $('#loginbox').load(smFragmentPath + 'loginbox.htm div.subbox');
        }
        $('#loginbox').show();
    });


    // smart menu
    $("#smartmenuclose div.box a").click(function() {
        return smartMenuItemOnClick(this);
    });

    // smart menu - hlavni stranka
    $("div.smartmenuhome div.box div.icons a").click(function() {
        switch (this.id) {
            case "ic-1":
                $('#smFormBox').load(smFragmentPath + 'fhp1.htm #smFormhp', {}, function() {
                    setupIcons($('#ic-1')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
            case "ic-2":
                $('#smFormBox').load(smFragmentPath + 'fhp2.htm #smFormhp', {}, function() {
                    setupIcons($('#ic-2')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
            case "ic-3":
                $('#smFormBox').load(smFragmentPath + 'fhp3.htm #smFormhp', {}, function() {
                    setupIcons($('#ic-3')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
            case "ic-4":
                $('#smFormBox').load(smFragmentPath + 'fhp4.php #smFormhp', {}, function() {
                    setupIcons($('#ic-4')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
            case "ic-5":
                $('#smFormBox').load(smFragmentPath + 'fhp5.htm #smFormhp', {}, function() {
                    setupIcons($('#ic-5')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
            case "ic-6":
                $('#smFormBox').load(smFragmentPath + 'fhp6.htm #smFormhp', {}, function() {
                    setupIcons($('#ic-6')[0]);
                    setupDatepicker();
                    setupTimeControl();
                });
                break;
        }
        return false;
    });
});

// jizdne a slevy
function radioFareOnClick(val) {
    $("#selFare").html($("#sel" + val).html());
}

// prihlasovani
function closeLoginBox() {
    $('#loginbox').hide();
    return false;
}

// smart menu
function smartMenuItemOnClick(oItem) {
    switch (oItem.id) {
        case "ic-1":
        case "icf-1":
            $('#sm1').load(smFragmentPath + 'f1.htm #smForm', {}, function() {
               setupIcons2($('#icf-1')[0]);
               setupDatepicker();
                setupTimeControl();
            });
            break;
        case "ic-2":
        case "icf-2":
            $('#sm1').load(smFragmentPath + 'f2.htm #smForm', {}, function() {
               setupIcons2($('#icf-2')[0]);
                setupDatepicker();
                setupTimeControl();
            });
            break;
        case "ic-3":
        case "icf-3":
            $('#sm1').load(smFragmentPath + 'f3.htm #smForm', {}, function() {
               setupIcons2($('#icf-3')[0]);
                setupDatepicker();
                setupTimeControl();
            });
            break;
        case "ic-4":
        case "icf-4":
            $('#sm1').load(smFragmentPath + 'f4.php #smForm', {}, function() {
               setupIcons2($('#icf-4')[0]);
                setupDatepicker();
                setupTimeControl();
            });
            break;
        case "ic-5":
        case "icf-5":
            $('#sm1').load(smFragmentPath + 'f5.htm #smForm', {}, function() {
                setupIcons2($('#icf-5')[0]);
                setupDatepicker();
                setupTimeControl();
            });
            break;
        case "ic-6":
        case "icf-6":
            $('#sm1').load(smFragmentPath + 'f6.htm #smForm', {}, function() {
                setupIcons2($('#icf-6')[0]);                
                setupDatepicker();
                setupTimeControl();
            });
            break;
    }
    $('#sm1').show();
    return false;
}

// smart menu
function closeSmartForm() {
    $('#sm1').hide();
    return false;
}

function setupDatepicker() {
	try {
		var now = new Date();
		$("#datepicker").attr('value', zeroFill(now.getDate(), 2) + '.' + zeroFill(now.getMonth() + 1, 2) + '.' + now.getFullYear());
		$("#datepicker").datepicker({ showOn: 'button', buttonImage: '/images/cdos/cal.gif', buttonImageOnly: true });
	}
	catch (e) { }
}

function setupTimeControl() {
	try {
		var now = new Date();
		$("#thetime").attr('value', zeroFill(now.getHours(), 2) + ':' + zeroFill(now.getMinutes(), 2));
		$("#time-up").click(function() {
			updateTime(1);
		});
		$("#time-down").click(function() {
			updateTime(-1);
		});
	}
	catch (e) { }
}

function updateTime(hourStep) {
	var timeVal = $("#thetime").attr('value');
	var timeSep = timeVal.split(':');
	var h;
	if (!isNaN(timeSep[0])) {
		h = Number(timeSep[0]);
		h += hourStep;
		if (h > 23)
			h = 0;
		else if (h < 0)
			h = 23;
	}
	else {
		h = 12;
	}
	$("#thetime").attr('value', zeroFill(h, 2) + ':00');
}

//1 zmeni na 01
function zeroFill(number, width)
{
	width -= number.toString().length;
	if ( width > 0 ) {
		return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
	}
	return number;
}

// nastavení stisknuté ikony ve SmartMenu na HP
function setupIcons(icon)
{
                $('#ic-1')[0].className = "sm-spojeni";
                $('#ic-2')[0].className = "sm-razeni";
                $('#ic-3')[0].className = "sm-cena";
                $('#ic-4')[0].className = "sm-poloha";
                $('#ic-5')[0].className = "sm-jizdenka";
                $('#ic-6')[0].className = "sm-sluzby";
                icon.className = icon.className + "over";
}

// nastavení stisknuté ikony ve SmartMenu mimo HP
function setupIcons2(icon)
{
                $('#icf-1')[0].className = "sm-spojeni";
                $('#icf-2')[0].className = "sm-razeni";
                $('#icf-3')[0].className = "sm-cena";
                $('#icf-4')[0].className = "sm-poloha";
                $('#icf-5')[0].className = "sm-jizdenka";
                $('#icf-6')[0].className = "sm-sluzby";
                icon.className = icon.className + "over";
}