function fix_menu(){
    $('ul.navigation > li').mouseover(function(){
            $(this).addClass("on");
    });
    $('ul.navigation > li:not(.always)').mouseout(function(){
            $(this).removeClass("on");
    });
}

function tools_anim_subtools(tool){
    var subtool = $(tool).next()
    while (subtool.attr('class')=='btt_funkcjonalnosci'){
        if (subtool.css('display') == 'none'){
            subtool.show('fast');
        } else {
            subtool.hide('fast');
        }
        subtool = subtool.next();
    }
}

function tools_hide_others(tool){
    $('div.box_narzedzia').children().each(function(){
        if ($(this).attr('class') == 'btt_funkcjonalnosci'){
            if ($(this).css('display') != 'none'){
                $(this).hide('fast');
            }
        }
    });
}

function tools_anim(){
    $('div.box_narzedzia').children().each(function(i){
        if ($(this).attr('class') == 'btt_funkcjonalnosci'){
            $(this).hide();
        }else{
        	if ($(this).attr('class') != 'booking') {
	            $(this).click(function(){
	                tools_hide_others(this);
	                tools_anim_subtools(this);
	                return false;
	            });
        	}
        }
    });
}

function change_content(current, classname){
    var buttons_class = "." + classname + '_buttons';                                
    $(buttons_class).each(function(){
        var content = '#' + classname + '_content_' + $(this).attr('rel');
        if ($(this).attr('rel') == current){
            $(content).show();
        } else {
            $(content).hide();
        }
    });
}


function change_button_contents(classname){
    var buttons_class = "." + classname + '_buttons';
    $(buttons_class).each(function(i){
        var content = '#' + classname + '_content_' + $(this).attr('rel');
        if (i==0){
            $(content).show();
        } else {
            $(content).hide();
        }
        $(this).click(function(){
            change_content($(this).attr('rel'), classname)
        });
    });
}

function set_change_bloczek_glowny_sekcji(current, id){
    $(id).each(function(i){
        if (i==current){
           $(this).show();
           this.current_visible = true;
        } else {
            $(this).hide();
            this.current_visible = false;
        }
    });
}

function loop_bloczek_glowny_sekcji(id){
    var current = 0;
    $(id).each(function(i){
        var next = $(this).next().length;
        if ((this.current_visible == true) && (next > 0)) {
            current = i + 1;
        }
    });
    set_change_bloczek_glowny_sekcji(current, id)
}

timerSekcjaId = 0;
function change_bloczek_glowny_sekcji(){
    var current = 0;
    $('.bloczek_glowny_sekcji').each(function(i){
        if (i==current){
            this.current_visible = true; 
        } else {
            this.current_visible = false;
        }
    });
    timerSekcjaId = setInterval("loop_bloczek_glowny_sekcji('.bloczek_glowny_sekcji');", 5000);
    $('.bloczek_glowny_sekcji').each(function(){
        $(this).mouseover(function(){
            clearInterval(timerSekcjaId);
        });
        $(this).mouseout(function(){
            clearInterval(timerSekcjaId);
            timerSekcjaId = setInterval("loop_bloczek_glowny_sekcji('.bloczek_glowny_sekcji');", 5000);
        });
    });
}

timerHitMiesiacaId = 0;
function change_hit_miesiaca(){
    var current = 0;
    $('.hit_miesiaca_collection').each(function(i){
        if (i==current){
            this.current_visible = true; 
        } else {
            this.current_visible = false;
            $(this).hide();
        }
    });
    timerHitMiesiacaId = setInterval("loop_bloczek_glowny_sekcji('.hit_miesiaca_collection');", 5000);
    $('.hit_miesiaca_collection').each(function(){
        $(this).mouseover(function(){
            clearInterval(timerHitMiesiacaId);
        });
        $(this).mouseout(function(){
            clearInterval(timerHitMiesiacaId);
            timerHitMiesiacaId = setInterval("loop_bloczek_glowny_sekcji('.hit_miesiaca_collection');", 5000);
        });
    });
}

function sekcja_click(el,identity){
   eval('var s1=sekcja_'+identity);
   eval('var s2=sekcja_'+identity+'_on');
   $(el).parent().parent().children('li').children('span').children('img').each(function(i){
        $(this).attr('src', s1[i])
    });
    $(el).children('img').attr('src', s2[$('#sekcja_'+ identity +' div.zakladki ul li').index($(el).parent())])
    $('.content_'+identity).hide();
    $('#' + $(el).attr('rel')).show();
}

function set_change_sekcje(current, id, identity){
    $(id).each(function(i){
        if (i==current){
            //$(this).click();
            sekcja_click(this, identity);
            this.current_visible = true;
        } else {
            this.current_visible = false;
        }
    });
}

function loop_change_sekcje(id, identity){
    var current = 0;
    $(id).each(function(i){
        if ((this.current_visible == true) && ((i+1) < $(id).length)){
            current = i + 1;
        }
    });
    set_change_sekcje(current, id, identity);
}

var timerSekcjeId;
function change_sekcje(identity, sek){
    var id =  '#sekcja_' + identity + ' div.zakladki ul span';
    $(id).each(function(i){
        if (i==0){
            this.current_visible = true;
        } else {
            this.current_visible = false;
        }
    });
    $('#sekcja_' + identity).everyTime(sek, 'sekcje_' + identity, function(){
       loop_change_sekcje(id, identity);
    });
    //timerSekcjeId = setInterval("loop_change_sekcje('" + id +"', '"+ identity + "');", 5000);
};

function set_change_temat_miesiaca(current){
    $('.box_temat_miesiaca div.zakladki ul span').each(function(i){
        if (i==current){
            $(this).click();
            this.current_visible = true;
        } else {
            this.current_visible = false;
        }
    });
}

function loop_change_temat_miesiaca(){
    var current = 0;
    $('.box_temat_miesiaca div.zakladki ul span').each(function(i){
        if ((this.current_visible == true) && ((i+1) < $('.box_temat_miesiaca div.zakladki ul span').length)){
            current = i + 1;
        }
    });
    set_change_temat_miesiaca(current);
}

timerTematMiesiacaId = 0;
function change_temat_miesiaca(){
    $('.box_temat_miesiaca div.zakladki ul span').each(function(i){
        if (i==0){
            this.current_visible = true;
        } else {
            this.current_visible = false;
        }
    });
    clearInterval(timerTematMiesiacaId);
    timerTematMiesiacaId = setInterval("loop_change_temat_miesiaca();", 5000);
};

function block_div_change(contents, buttons, k){
    $(buttons).each(function(i){
        if (i == k){
            $(this).parent().attr('class', $(this).parent().attr('class') + ' active');
        } else {
            $(this).parent().attr('class', $(this).parent().attr('class').replace('active',''));
        }
    });
    $(contents).each(function(i){
        if (i == k){
            $(this).show();
        } else {
            $(this).hide();
        }
    });
    return false;
}

function BlockDivChange(contents, buttons){
    $(buttons).each(function(i){
        $(this).click(function(){
            block_div_change(contents, buttons, i);
            return false;
        });
    });
}

function pretty_scroll(){
    $('.scroll-pane').each(function(){
            var hide_after_scroll_add = false;                            
            if ($(this).parent().parent().parent().css('display') == 'none'){
                $(this).parent().parent().parent().show();
                hide_after_scroll_add = true;
            }
            $(this).jScrollPane({dragMinHeight: 30, dragMaxHeight: 30, animateInterval: 10, animateStep:1});
            if (hide_after_scroll_add){
                $(this).parent().parent().parent().parent().hide();
            }
        });
}

$(document.body).ready(function(){
        fix_menu();
        tools_anim();
        pretty_scroll();
        change_bloczek_glowny_sekcji();
})