//html5
(function () { if (!/*@cc_on!@*/0) return; var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','); for (var i = 0; i < e.length; i++) { document.createElement(e[i]) } })()

//accordion
function initMenu() {
    //jQuery('.accordion li:first').addClass('active');
    //jQuery('.accordion ul:first').show();
    jQuery('.accordion li a').click(
	function () {
	    var checkElement = jQuery(this).next();
	    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
	        return false;
	    }
	    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
	        jQuery('.accordion ul:visible').slideUp('normal');
	        jQuery('.accordion ul:visible').parent('li').removeClass('active');
	        checkElement.slideDown('normal');
	        checkElement.parent('li').addClass('active');
	        return false;
	    }
	}
	);
}

function findPos(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curtop];
    }
}

function getHightLight(next) {
    if ($('#searchtext').val().length >= 3) {
        $('#article').removeHighlight().highlight($('#searchtext').val());
        $(document).scrollTop($(document).scrollTop() - 80);
    }
    else {
        $('#article').removeHighlight();
    }
}

function HightLightStep(step) {
    if ($('.highlight.first').length) {
        var i = 0;
        $('.highlight').each(function (index) {
            if (i >= 0) {
                if ($(this).hasClass('first')) {

                    var nextstep = i - 1;
                    if (step == "next") {
                        nextstep = i + 1;
                    }
                    $(this).removeClass('first');
                    if ($('.highlight:eq(' + nextstep + ')').length) {
                        $('.highlight:eq(' + nextstep + ')').addClass('first');
                    }
                    else {
                        if (step == "next") {
                            $('.highlight:first').addClass('first');
                        }
                        else {
                            $('.highlight:last').addClass('first');
                        }
                    }
                    i = -10;
                    $(document).scrollTop($("span.highlight.first").offset().top);
                    $(document).scrollTop($(document).scrollTop() - 80);
                }
                i++;
            }
        });
    }
}

function formatDate(date) {
    var mili = date.substring(6, date.length);
    var mili2 = mili.substring(0, mili.length - 2);
    var ms = parseInt(mili2);
    var d = new Date(ms);
    var t = new Date();
    if (t.getDate() == d.getDate() && t.getMonth() == d.getMonth() && t.getFullYear() == d.getFullYear())
        return "ma, " + (d.getHours() > 9 ? d.getHours() : "0" + d.getHours()) + ":" + (d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes());
    else if (t.getDate() == d.getDate() + 1 && t.getMonth() == d.getMonth() && t.getFullYear() == d.getFullYear())
        return "tegnap, " + (d.getHours() > 9 ? d.getHours() : "0" + d.getHours()) + ":" + (d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes());
    else
        return d.getFullYear() + "." + ((1 + d.getMonth()) > 9 ? (1 + d.getMonth()) : "0" + (1 + d.getMonth())) + "."
            + (d.getDate() > 9 ? d.getDate() : "0" + d.getDate()) + "., " + (d.getHours() > 9 ? d.getHours() : "0" + d.getHours()) + ":" + (d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes());
}

jQuery(document).ready(function () {
    initMenu();

    $('input').placeholder();
    $('textarea').placeholder();

    jQuery.each(jQuery.browser, function (i, val) {
        jQuery('body').addClass(i);
    });
    if (!jQuery.browser.webkit) jQuery('body').addClass('non-webkit');

    //hide outline ie7
    /*$("a").each(function() {
    $(this).attr("hideFocus", "true").css("outline", "none");
    });*/

    jQuery('a.calc_toggle').toggle(function () {
        jQuery('div.hide').slideDown();
        jQuery(this).html('Bezár');
    }, function () {
        jQuery('div.hide').slideUp();
        jQuery(this).html('Tovább kalkulátorok &raquo;');
    });

    jQuery('#nav_content span.dd-arrow').toggle(function () {
        jQuery(this).addClass('active');
        jQuery('div.fl .cat-dd').slideDown();
    }, function () {
        jQuery(this).removeClass('active');
        jQuery('div.fl .cat-dd').slideUp();
    });

    jQuery('#nav_content span.dd-button').toggle(function () {
        jQuery(this).addClass('active');
        jQuery('div.fr .cat-dd').slideDown();
    }, function () {
        jQuery(this).removeClass('active');
        jQuery('div.fr .cat-dd').slideUp();
    });

    jQuery("a.open-q").toggle(function () {
        $("#question-edit-form").show();
    }, function () {
        $("#question-edit-form").hide();
    });

    jQuery('a.dropdown').toggle(function () {
        jQuery(this).addClass('active');
        jQuery('div.dropdown').slideDown();
        jQuery('#adv_searchbox #as_button1').hide();
        jQuery('#adv_searchbox input.huge').css('width', '576px');
    }, function () {
        jQuery(this).removeClass('active');
        jQuery('#adv_searchbox input.huge').css('width', '475px');
        jQuery('#adv_searchbox #as_button1').show();
        jQuery('div.dropdown').slideUp();
    });
    jQuery('a.dropdown2').toggle(function () {
        if (typeof as_fm !== 'undefined') as_fm = 6;
        jQuery(this).addClass('active');
        jQuery('#as_columns').hide();
        jQuery('#as_columns a').remove();
        if (jQuery('a.dropdown').hasClass('active')) {
            jQuery('a.dropdown').trigger('click');
        }
        jQuery('a.dropdown3, a.dropdown').hide();
        jQuery('input[name="as_fromdate"]').val('');
        jQuery('input[name="as_todate"]').val('');
        jQuery('div.dropdown2').slideDown();
        jQuery('#adv_searchbox #as_button1').hide();
        jQuery('#adv_searchbox input.huge').css('width', '576px');
    }, function () {
        if (typeof as_fm !== 'undefined') as_fm = null;
        jQuery(this).removeClass('active');
        jQuery('a.dropdown3, a.dropdown').show();
        jQuery('#adv_searchbox input.huge').css('width', '475px');
        jQuery('#adv_searchbox #as_button1').show();
        jQuery('div.dropdown2').slideUp();
    });
    jQuery('a.dropdown3').toggle(function () {
        jQuery(this).addClass('active');
        jQuery('#adv_searchbox span.dd').slideDown();
    }, function () {
        jQuery(this).removeClass('active');
        jQuery('#adv_searchbox span.dd').slideUp();
    });

    jQuery('a.more_topic').toggle(function () {
        jQuery(this).addClass('active');
        jQuery('#highlights span.dd').slideDown();
    }, function () {
        jQuery(this).removeClass('active');
        jQuery('#highlights span.dd').slideUp();
    });

    jQuery('.law_list li').toggle(function () {
        jQuery(this).addClass('active');
        jQuery(this).children('ul').slideDown();
    }, function () {
        jQuery(this).removeClass('active');
        jQuery(this).children('ul').slideUp();
    });

    jQuery('div.button div.fr a').click(function () {
        jQuery('div.button div.fr a.hide').fadeIn();
        return false;
    });

    jQuery('.hide_desc').click(function () {
        jQuery('div.desc').fadeOut();
        return false;
    });

    jQuery('.rule p img').click(function () {
        jQuery('div.desc').fadeIn();
        return false;
    });

    jQuery('#to_top').click(function () {
        jQuery('html,body').animate({ scrollTop: 0 }, 400);
        return false;
    });

    jQuery('.searchbox h5 span').toggle(function () {
        jQuery(this).parent('h5').addClass('active');
        jQuery('.searchbox').children('form').slideUp();
    }, function () {
        jQuery(this).parent('h5').removeClass('active');
        jQuery('.searchbox').children('form').slideDown();
    });

    jQuery('.hvgCalculator button').click(function () {
        jQuery('div.hvgResult').fadeIn();
        return false;
    });

    jQuery('#header nav').find('a').each(function () {
        if (location.href.indexOf(this.href) != -1) {
            $(this).addClass('active');
        }
    });
    var timer1;
    jQuery('#copyright span').hover(function () {
        if (timer1) {
            clearTimeout(timer1);
            timer1 = null
        }
        timer1 = setTimeout(function () { jQuery('body').addClass('credits'); }, 2000)
    }, function () {
        if (timer1) {
            clearTimeout(timer1);
            timer1 = null
        }
        jQuery('body').removeClass('credits');
    });

    jQuery('#header input[name="q"]')
			.autocomplete({
			    source: function (request, response) {
			        $.ajax({
			            url: "/skins/default/AjaxHandlers/searchautocomplete.ashx",
			            dataType: 'json',
			            data: { term: request.term },
			            success: function (data, textStatus, jqXHR) {
			                as_ac_status = jqXHR.status;
			                response(data, textStatus, jqXHR);
			            }
			        });
			    },
			    select: function (event, ui) {
			        if (as_ac_status == 200) {
			            this.value = ui.item.value + ' ';
			        }
			        else if (as_ac_status == 201) {
			            var st = this.value.split(' ');
			            st.pop();
			            st.push(ui.item.value);
			            this.value = st.join(" ");
			        }
			        return false;
			    }
			});
});

//adv search
var as_ac_status = 200;
function as_loadform(fullform) {
    $(document).ready(function () {
        $('.as_tags').hide();
        $('.as_cbl').toChecklist({
            addScrollBar: true,
            addSearchBox: false,
            cssChecklist: 'checklist',
            cssChecklistHighlighted: 'checklistHighlighted',
            //cssEven: 'even',
            //cssOdd: 'odd',
            cssChecked: 'checked'
        });
        var dates = $('input[name="as_fromdate"], input[name="as_todate"]').datepicker({
            changeMonth: true,
            changeYear: true,
            minDate: new Date(1900, 1 - 1, 1),
            maxDate: '+1y',
            dateFormat: 'yy.mm.dd',
            onSelect: function (selectedDate) {
                var option = this.name == "as_fromdate" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
                dates.not(this).datepicker("option", option, date);
            }
        });
        $('input[name="as_term"]')
            .bind('autocompleteopen', function (event, ui) {
                $(this).data('is_open', true);
            })
            .bind('autocompleteclose', function (event, ui) {
                $(this).data('is_open', false);
            })
			.bind("keydown", function (event) {
			    if (event.keyCode === $.ui.keyCode.TAB &&
						$(this).data("autocomplete").menu.active) {
			        event.preventDefault();
			    }
			    if (event.keyCode === $.ui.keyCode.ENTER && $(this).data('is_open') !== true) {
			        as_doSearch();
			    }
			})
			.autocomplete({
			    source: function (request, response) {
			        $.ajax({
			            url: "/skins/default/AjaxHandlers/searchautocomplete.ashx",
			            dataType: 'json',
			            data: { term: request.term },
			            success: function (data, textStatus, jqXHR) {
			                as_ac_status = jqXHR.status;
			                response(data, textStatus, jqXHR);
			            }
			        });
			    },
			    focus: function () {
			        return false;
			    }
                , select: function (event, ui) {
                    if (as_ac_status == 200) {
                        this.value = ui.item.value + ' ';
                    }
                    else if (as_ac_status == 201) {
                        var st = this.value.split(' ');
                        st.pop();
                        st.push(ui.item.value);
                        this.value = st.join(" ") + ' ';
                    }
                    return false;
                }
			});


        $('#as_columns a').live('click', function (e) {
            var found = 0;
            $(this).parent().find('a').each(function () {
                found = found + 1;
            });
            if (found <= 1) $(this).parent().hide();

            $(this).remove();
            e.preventDefault();
        });

        if (fullform) {
            $(document).keydown(function (e) {
                switch (e.which) {
                    case 37: if (as_pg_prev) as_goToPage(as_p - 1); break;
                    case 39: if (as_pg_next) as_goToPage(as_p + 1); break;
                    default: break;
                }
            });
            $('#as_collist a').click(function (e) {
                as_addCol($(this).data('colid'));
                $('a.dropdown3').trigger('click');
                e.preventDefault();
            });

            if (as_m == null) as_updateTab(1);
            else if (as_m == 2) as_updateTab(2);
            else if (as_m == 6) as_updateTab(3);
            else if (as_m == 7) as_updateTab(4);
            else if (as_m == 5) as_updateTab(5);
            else if (as_m == 3) as_updateTab(6);
            else if (as_m == 0) as_updateTab(7);
            else if (as_m == 15) as_updateTab(8);

            $('input[name="as_term"]').val(as_q);
            $('input[name="as_fromdate"]').datepicker("setDate", as_df);
            $('input[name="as_todate"]').datepicker("setDate", as_dt);

            $('input[name="as_jy"]').val(as_jy);
            $('input[name="as_jn"]').val(as_jn);
            if (as_jp.length > 0)
                $('select[name="kibocsatok"]').val(as_jp[0]);
            for (i in as_jt) {
                $('#as_cbl_jt input:checkbox[value=' + as_jt[i] + '],' +
                    'input#as_spec_saldo[value=' + as_jt[i] + '],' + 
                    'input#as_spec_ave[value=' + as_jt[i] + ']').prop("checked", true);
            }
            if (as_o.length > 0) {
                if (as_o.indexOf("c") == -1 && as_o.indexOf("s") != -1) $('#as_o_t').prop('checked', false);
                if (as_o.indexOf("s") == -1 && as_o.indexOf("c") != -1) $('#as_o_s').prop('checked', false);
                if (as_o.indexOf("n") != -1) $('#as_o_n').prop('checked', true);
                if (as_o.indexOf("m") != -1) $('#as_o_hn').prop('checked', true);
                if (as_o.indexOf("n") == -1 && as_o.indexOf("m") == -1) $('#as_o_h').prop('checked', true);
            }

            for (i in as_c) {
                $('#as_columns').show().append('<a data-colid="' + as_c[i] + '" href="javascript:void(0);">' + as_getColNameByID(as_c[i]) + '</a>');
            }

            if (as_m == 6 || as_m == 3) {
                $('div.dropdown2').show();
                $('a.dropdown2').trigger('click');
            }
            else if (as_df != '' || as_dt != '') {
                $('div.dropdown').show();
                $('a.dropdown').trigger('click');
            }
        }
    });
}

function as_goToPage(page) {
    as_p = page;
    as_doSearch();
}
function as_setFilter(page) {
    switch (page) {
        case 1:
        default: as_m = null; break; //Összes
        case 2: as_m = 2; break; //Kérdés/válasz
        case 3: as_m = 6; break; //Jogszabály
        case 4: as_m = 7; break; //Esemény
        case 5: as_m = 5; break; //Kalkulátor
        case 6: as_m = 3; break; //Segédlet
        case 7: as_m = 0; break; //Cikk
        case 8: as_m = 15; break; //Jogi kommentár
    }
    as_fm = -1;
    as_doSearch();
}
function as_addCol(id) {
    var found = false;
    $('#as_columns a').each(function () {
        var d = $(this).data('colid');
        if (d != undefined && d == id) found = true; ;
    });
    if (!found) {
        $('#as_columns').show().append('<a data-colid="' + id + '" href="javascript:void(0);">' + as_getColNameByID(id) + '</a>');
    }
}
function as_getColNameByID(id) {
    var result = '';
    $('#as_collist a').each(function () {
        var d = $(this).data('colid');
        if (d != undefined && d == id) {
            result = $(this).data('colname');
            return false;
        }
    });
    return result;
}
function as_doSearch() {
    var url = '/kereses/?';
    var params = {}

    var fq = $('input[name="as_term"]').val();
    if (fq != null && fq != '') params.q = fq;

    if (typeof as_p !== 'undefined' &&
        (typeof as_q === 'undefined' || fq == null || fq == as_q || (fq != '' && as_q == ''))) {
        if (as_p != null && as_p != 1)
            params.p = as_p;
    }
    if (typeof as_fm !== 'undefined' && as_fm != -1) as_m = as_fm;
    if (as_m != null) params.m = as_m;

    if (as_m == 6 || as_m == 3) {
        var fjy = $('input[name="as_jy"]').val();
        if (fjy != null && fjy != '') params.jy = fjy;

        var fjn = $('input[name="as_jn"]').val();
        if (fjn != null && fjn != '') params.jn = fjn;

        var fjp = $('select[name="kibocsatok"]').val();
        if (fjp != null && fjp != '')
            params.jp = fjp;

        var fjt = '';
        $('#as_cbl_jt input:checkbox:checked, #as_spec_saldo:checked, #as_spec_ave:checked').each(function () {
            fjt += $(this).val() + ",";
        });
        if (fjt != '') params.jt = rtrim(fjt, ',');

        var fo = '';
        if ($('#as_o_t').is(':checked') && !$('#as_o_s').is(':checked')) fo += 'c';
        if ($('#as_o_s').is(':checked') && !$('#as_o_t').is(':checked')) fo += 's';
        if ($('#as_o_n').is(':checked')) fo += 'n';
        if ($('#as_o_h').is(':checked')) fo += '';
        if ($('#as_o_hn').is(':checked')) fo += 'm';
        if (fo != '') params.o = fo;
    }
    else {
        var fdf = $('input[name="as_fromdate"]').val();
        if (fdf != null && fdf != '') params.df = fdf;

        var fdt = $('input[name="as_todate"]').val();
        if (fdt != null && fdt != '') params.dt = fdt;

        var fc = '';
        $('#as_columns a').each(function () {
            fc += $(this).data('colid') + ",";
        });
        if (fc != '') params.c = rtrim(fc, ',');
    }

    window.location = url + jQuery.param(params);
}
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function as_updateTab(activeTab) {
    $('.tabs a').removeClass('active');
    $('.tabs .t' + activeTab).addClass('active');
}
function lp_onTimeStateChanged() {
    var selectedTimeState = $("#timeStateSelect option:selected").val();

    var pathParts = location.pathname.split('/');
    var redirectPath = pathParts[0] + "/" + pathParts[1] + "/" + pathParts[2];

    redirectPath += "/" + selectedTimeState;

    window.location = redirectPath;
}
function lp_onToggleMetaData() {
    if ($("#legislation-metadata").is(':visible')) {
        $("#legislation-metadata").hide();
        $("#meta-data-toggle").text("Jogszabály részletes adatainak mutatása");
    }
    else {
        $("#legislation-metadata").show();
        $("#meta-data-toggle").text("Csak a jogszabály szövegék mutatása");
    }
}
function lapagerLaw(page, linktype, webid) {
    var tmpUrl = "/ajax/linkedlaw/" + webid + "/" + linktype + "/" + page;
    var ctrl = "#linkedlaw_" + linktype;
    $.ajax({
        type: "GET",
        url: tmpUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "HTML",
        error: function (msg) {
            $(ctrl + " #links").replaceWith('<div class="ajaxError">Hiba történt a kommunikáció folyamán.<div>');
        },
        success: function (data) {
            $(ctrl).fadeOut('fast').replaceWith($(data).fadeIn('slow'));
        }
    });

    return false;
}
function cv_onTimeStateChanged() {
    var selectedTimeState = $("#timeStateSelect option:selected").val();

    var pathParts = location.pathname.split('/');
    var redirectPath = pathParts[0] + "/" + pathParts[1] + "/" + pathParts[2];

    if (document.getElementById("timeStateSelect").selectedIndex != 0) {
        redirectPath += "/" + selectedTimeState;
    }

    window.location = redirectPath;
}

// # link fix
$(document).ready(function () {
    $(window).ready(function () {
        $(window).bind('scroll', function () {
            if (location.hash.length > 1) {
                $(window).unbind("scroll");
                $(document).scrollTop($(document).scrollTop() - 80);
            }
        });
    });
    $(window).bind('hashchange', function () {
        $(document).scrollTop($(document).scrollTop() - 80);
    });
});
