function initTabs() {
    $('div.tabs li a').click(function(e){
        tabs_id = $(this).parents('div.tabs').attr('id');
        act_tab = $(this).attr('href');
        act_tab_obj = $(act_tab);

        /* all other tabs except active */
        $('div[id^="'+ tabs_id + '-"]' ).not(act_tab_obj).hide();
        /* active tab */
        act_tab_obj.show();

        $(this).parent().siblings().children().removeClass('active');
        $(this).addClass('active');

        e.preventDefault();
    })
}

var slider_nav_cycle_interval;
function initSliderNavCycle(timeout){
    slider_nav_cycle_interval= setInterval(function(){
        act_tab = $('#slider .sliderNav a[class*="active"]').next();
        if (act_tab.length == 0) {
            act_tab = $($('#slider .sliderNav a').siblings()[0]);
        }

        act_tab_obj = $(act_tab.attr('id'));

        $('#slider-content').children('.slide').not(act_tab_obj).hide();
        act_tab_obj.show();

        $('#slider .sliderNav a').siblings().removeClass('active');
        act_tab.addClass('active');

    }, timeout)
}


function initSliderNav(){
    timeout = 5000;
    $('#slider .sliderNav a')
        .mouseenter(function(e){
            act_tab = $(this).attr('id');
            act_tab_obj = $(act_tab);

            $('#slider-content').children('.slide').not(act_tab_obj).hide();
            act_tab_obj.show();

            $(this).siblings().removeClass('active');
            $(this).addClass('active');

            clearInterval(slider_nav_cycle_interval);

            e.preventDefault();
        })
        .mouseleave(function(e){
            initSliderNavCycle(timeout);
        })

    $('#slider .slide')
        .mouseenter(function(e){
            clearInterval(slider_nav_cycle_interval);
        })
        .mouseleave(function(){
            initSliderNavCycle(timeout);
        })

    initSliderNavCycle(timeout);
}

function initArtistSongListMore() {
    if ( $('#artist_songs_list_more').length>0 ) {
        data = ({});
        $('#artist_songs_list_more').click(function(e){
            artist_songs_list_more_url = $(this).attr('data-source');
            $.ajax({
                type: 'GET',
                url: artist_songs_list_more_url,
                data: data,
                success: function(response) {
                    if (response.success) {
                        $('#artist_songs_list').html(response.html);
                        if ($.browser.msie) {
                            if ($.browser.version == '7.0') {
                                $('#artist_songs_list').parent().html($('#artist_songs_list').parent().html());
                            }
                        }
                        $('#artist_songs_list_more').hide();
                    }
                }
            });

            e.preventDefault();
        })
    }
}

function initArtistBiographyMore() {
    if ( $('#biography_more_link').length>0 ) {
        $('#biography_more_link').click(function(e){
            toggleArtistBiographyMore();
            e.preventDefault();
        })

        $('#biography_more_link_hide').click(function(e){
            toggleArtistBiographyMore();
            e.preventDefault();
        })
    }
}

function toggleArtistBiographyMore() {
    $('#biography_more').toggle();
    $('#biography_more_link').toggle();
    $('#biography_more_link_hide').toggle();
}

/* UserBox */

if (typeof UserBox == "undefined") UserBox = {};

UserBox.init = function() {
    UserBox.init_login_box();
    //UserBox.init_register_link();
};

UserBox.init_login_box = function() {
    $('#popup-login').hide();

    $('#popup-login-link').click(function(e){
        $('#popup-login').show();
        e.preventDefault();
    })

    $('#popup-login-close-link').click(function(e){
        $('#popup-login').hide();
        e.preventDefault();
    })

    $('#popup-login a.button-log').click(function(e) {
        $('#popup-login form').submit();
        e.preventDefault();
    });

    $('#popup-login #id_username').keyup(function(e){
        if ( e.keyCode === $.ui.keyCode.ENTER) {
            $('#popup-login form').submit();
        }
    });

    $('#popup-login #id_password').keyup(function(e){
        if ( e.keyCode === $.ui.keyCode.ENTER) {
            $('#popup-login form').submit();
        }
    });

};

UserBox.init_register_link = function() {
    $('#register-link').click(function(e){
        $( "#reg-dialog" ).dialog({
            modal: true,
            buttons: {
                'zavrieť': function() {
                    $( this ).dialog( "close" );
                }
            }
        });
        e.preventDefault();
    })
};

function initSongDetailChords() {
    if ( $('#song-detail-chords').length>0 ) {
        var used_chords = $.parseJSON($('#song-detail-chords').attr('data-used-chords'));
        $('#song-detail-chords .akord-tooltip').mouseover(function(e){
            var position = $(this).position();
            var left = position.left + 15;
            var top = position.top + 15;

            $("#chord_tooltip").html(used_chords[$(this).text().toUpperCase()])
                               .css("left", left + 'px')
                               .css("top", top + 'px')
                               .show();
        }).mouseout(function(e){
            $("#chord_tooltip").html('').hide();
        })
    }
}

function initEventAccordion() {
    if ( $('#event-accordion').length>0 ) {
        $('#event-accordion').accordion({ autoHeight: false });
    }

    if ( $("#event-accordion h3").not('.ended').length > 0) {
        $($("#event-accordion h3").not('.ended')[0]).click();
    }
}

/* ContactForm */

if (typeof ContactForm == "undefined") ContactForm = {};

ContactForm.init = function() {
    if ( $('#contact_form').length>0 ) {
        $('#contact_form a').click(function(e){
            if ( $('#id_message').val().length>0 ) {
                url = $('#contact_form').attr('data-url');
                data = $('#contact_form').serialize();
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    success: function(response) {
                        if (response.success) {
                            $('#contact_form').hide();
                            $('#contact_form').parent().html(response.html);
                        }
                    }
                });
            }
            e.preventDefault();
        })
    }
};

/* MonthCalendar */

if (typeof MonthCalendar == "undefined") MonthCalendar = {};

MonthCalendar.init = function() {
    if ( $('#calendar').length>0 ) {
        MonthCalendar.init_popup();
        MonthCalendar.init_nav();
    }
};

MonthCalendar.init_popup = function() {
        events_data = $.parseJSON($("#calendar > .content").attr("data-events"));
        //$(window).bind('load', function(){
        i=0;
        $("#calendar table").find("a").each(function () {
            var object = $(this);
            object.mouseover(function() {
                if ( events_data[object.text()] !== undefined ) {
                    $("#pop-up-calendar ul").html(events_data[object.text()]);
                    $("#pop-up-calendar .section > a").attr('href',$(this).attr('href'));
                }
                else {
                    $("#pop-up-calendar ul").html('');
                }

                var position = object.position();
                var left = position.left - 109;
                var top = position.top - ($("#pop-up-calendar").height());

                $("#pop-up-calendar").css('left', left + 'px');
                $("#pop-up-calendar").css('top', top + 'px');

                $("#pop-up-calendar").css("display","none");
                $("#pop-up-calendar").css("visibility","visible");

                if ( events_data[object.text()] !== undefined ) {
                    $("#pop-up-calendar").stop(true,true).delay(500).fadeIn(500);
                }
            });
        //})

        $("#pop-up-calendar").hover(function() {
            $(this).css("visibility","visible");
            $(this).css("display","block");
        }, function() {
            $(this).delay(500).css("visibility","hidden");
        });

        $("#calendar").hover (function () {} , function() {
            $("#pop-up-calendar").stop(true,true).delay(500).css("visibility","hidden");
        });

        $("#calendar table td.out").mouseover(function() {
            $("#pop-up-calendar").css("display","none");
            $("#pop-up-calendar").css("visibility","visible");
        });
    });
};

MonthCalendar.init_nav = function() {
    $("#calendar .navigation a").click(function(e){
        data = {};
        data['month'] = $(this).attr('data-month');
        data['year'] = $(this).attr('data-year');
        request_url = $(this).parent().attr('data-request-url');

        $.ajax({
            type: 'GET',
            url: request_url,
            data: data,
            success: function(response) {
                if (response.success) {
                    $('#calendar').html(response.html);
                    MonthCalendar.init();
                }
            }
        });

        e.preventDefault();
    });
};

/* Search */

if (typeof Search == "undefined") Search = {};

Search.obj_id = "#search-results";
Search.button_obj_id = "#button-search";
Search.form_obj_id = "#search-form";

Search.init = function() {
    Search.init_input();
    Search.init_results();
    Search.init_paging();
};

Search.init_input = function() {
    if ( $(Search.button_obj_id).length>0 ) {
        $(Search.button_obj_id).click(function(e){
            $(Search.form_obj_id).submit();
            e.preventDefault();
        })
    }
};

Search.init_results = function() {
    if ( $(Search.obj_id).length>0 ) {
        $(Search.obj_id + ' .nav-panel a').click(function(e){
            id = $(this).attr('href').substring(1);
            $(Search.obj_id + ' .results-tab').not('#results-tab-' + id).hide();
            $(Search.obj_id + ' #results-tab-' + id).show();
            $(Search.obj_id + ' .nav-panel a').removeClass('active');
            $(this).addClass('active');
            e.preventDefault();
        })
    }
};

Search.init_paging =  function() {
    if ( $(Search.obj_id).length>0 ) {
        $(Search.obj_id + ' .button-page, ' + Search.obj_id + ' .button-jump').click(function(e){
            url = $(Search.obj_id).attr('data-url');

            data = {};
            data['results_tab'] = $(this).parents('.results-tab').attr('data-results-tab');
            data['term'] = $(Search.obj_id).attr('data-term');
            if($(this).attr('href') == '.') {
                data['page'] = '1';
            } else {
                data['page'] = $(this).attr('href').substring(9);
            }

            result_tab_obj = $(this).parents('.results-tab');

            $.ajax({
                type: 'GET',
                url: url,
                data: data,
                success: function(response) {
                    if (response.success) {
                        result_tab_obj.html(response.html);
                        Search.init_paging();
                    }
                }
            });


            e.preventDefault();
        })
    }
};

/* EventFilterForm */

if (typeof EventFilterForm == "undefined") EventFilterForm = {};

EventFilterForm.form_obj_id = "#event-filter-form";
EventFilterForm.button_obj_id = "#event-filter-form-submit";

EventFilterForm.init = function() {
    EventFilterForm.init_country_city_fields();
    EventFilterForm.init_submit();
};


EventFilterForm.init_submit = function() {
    if ( $(EventFilterForm.button_obj_id).length>0 ) {
        $(EventFilterForm.button_obj_id).click(function(e) {
            $(EventFilterForm.form_obj_id + ' form').submit();
            e.preventDefault();
        })
    }
}

EventFilterForm.init_country_city_fields = function() {
    if ( $(EventFilterForm.form_obj_id).length>0 ) {
        $('#id_country').change(function(){
            EventFilterForm.on_country_city_fields();
        });

        EventFilterForm.on_country_city_fields();
    }
};

EventFilterForm.on_country_city_fields = function() {
    city = $('#id_city').val();

    $('#id_city option').remove();
    $('#id_city').parent().parent().css('visibility','hidden');

    country_city_set = $.parseJSON($('#id_country').attr('data-country-city-set'));
    if (country_city_set) {
        cities = country_city_set[$('#id_country').val()];
    }
    else {
        cities = null;
    }

    if ( cities ) {
        if ( cities.length > 0) {
            $('#id_city').parent().parent().css('visibility','');

            $('#id_city').append("<option value=''>Všetky</option>");

            $.each(cities,function(item, val){
                if (city == val[0]) {
                    selected = ' selected="selected"';
                }
                else {
                    selected = '';
                }

                $('#id_city').append("<option value='" + val[0] + "'" + selected + ">" + val[1] + "</option>");
            })
        }
    }
    else {
        $('#id_city').parent().parent().css('visibility','hidden');
    }
};

/* CatalogSearch */

if (typeof CatalogSearch == "undefined") CatalogSearch = {};

CatalogSearch.form_obj_id = "#catalog-search-form";
CatalogSearch.button_obj_id = "#catalog-search-button";

CatalogSearch.init = function() {
    if ( $(CatalogSearch.button_obj_id).length>0 ) {
        $(CatalogSearch.button_obj_id).click(function(e) {
            $(CatalogSearch.form_obj_id).submit();
            e.preventDefault();
        })
    }
};

/* FloatBox */

if (typeof FloatBox == "undefined") FloatBox = {};

FloatBox.init = function() {
    if ( $('#floating-box').length>0 ) {
        $('#floating-box').addFloating({ targetTop: 10, snap: true });
    }
};


function initBase() {
    initTabs();
    initSliderNav();
    initArtistSongListMore();
    initArtistBiographyMore();
    initSongDetailChords();
    initEventAccordion();
    UserBox.init();
    ContactForm.init();
    MonthCalendar.init();
    Search.init();
    EventFilterForm.init();
    CatalogSearch.init();
    FloatBox.init();
}

$(initBase);

